Files
@ 23e1ed7cee08
Branch filter:
Location: NPO-Accounting/conservancy_beancount/tests/testutil.py - annotation
23e1ed7cee08
9.4 KiB
text/x-python
Revert "meta_receipt: Don't check chargebacks. RT#10978."
This reverts commit 6aadb740e619d19352370a992b46e54268f48033.
This is supposed to be done in meta_approval, not meta_receipt.
This reverts commit 6aadb740e619d19352370a992b46e54268f48033.
This is supposed to be done in meta_approval, not meta_receipt.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | 786291902231 786291902231 786291902231 786291902231 786291902231 786291902231 786291902231 786291902231 786291902231 786291902231 786291902231 786291902231 786291902231 786291902231 786291902231 786291902231 786291902231 d5a6141f6db6 786291902231 786291902231 786291902231 786291902231 786291902231 0d370c445b9e 786291902231 4874a107e834 4874a107e834 16c47c64b23d 16c47c64b23d 786291902231 786291902231 16c47c64b23d 8d3816a8fd87 786291902231 e7720b8fb866 e7720b8fb866 e7720b8fb866 e7720b8fb866 e7720b8fb866 e7720b8fb866 f1c115de49a8 f1c115de49a8 f1c115de49a8 f1c115de49a8 e7720b8fb866 d9420ac2b62f 93feb2f4a3b3 93feb2f4a3b3 93feb2f4a3b3 93feb2f4a3b3 93feb2f4a3b3 93feb2f4a3b3 d9420ac2b62f d9420ac2b62f 93feb2f4a3b3 d9420ac2b62f d9420ac2b62f 99dbd1ac9596 99dbd1ac9596 99dbd1ac9596 99dbd1ac9596 99dbd1ac9596 786291902231 786291902231 786291902231 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 8d3816a8fd87 9b63d898af70 9b63d898af70 9b63d898af70 d66ba8773f5e d66ba8773f5e d66ba8773f5e 786291902231 786291902231 01c3b975d811 d66ba8773f5e d66ba8773f5e 01c3b975d811 547ae657808f 01c3b975d811 786291902231 d66ba8773f5e 786291902231 786291902231 786291902231 786291902231 786291902231 786291902231 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 2909c405e620 2909c405e620 2909c405e620 2909c405e620 2909c405e620 2909c405e620 2909c405e620 2909c405e620 2909c405e620 2909c405e620 2909c405e620 2909c405e620 9b63d898af70 9b63d898af70 9b63d898af70 9b63d898af70 9b63d898af70 9b63d898af70 9b63d898af70 4eaba1ebf645 4eaba1ebf645 4eaba1ebf645 4eaba1ebf645 4eaba1ebf645 4eaba1ebf645 5aa30e5456d0 5aa30e5456d0 5aa30e5456d0 5aa30e5456d0 5aa30e5456d0 5aa30e5456d0 5aa30e5456d0 5aa30e5456d0 5aa30e5456d0 5aa30e5456d0 5aa30e5456d0 5aa30e5456d0 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 701ccdc19250 0d370c445b9e 0d370c445b9e 30d371278aeb 30d371278aeb 4874a107e834 4874a107e834 4874a107e834 30d371278aeb 8d3816a8fd87 4874a107e834 4874a107e834 4874a107e834 4874a107e834 4874a107e834 0d370c445b9e 30d371278aeb 30d371278aeb 30d371278aeb 0d370c445b9e 0d370c445b9e f09f029fc4cb 4874a107e834 4874a107e834 4874a107e834 4874a107e834 4874a107e834 4874a107e834 f09f029fc4cb d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 a9eab2d4ea5f a9eab2d4ea5f d5a6141f6db6 a9eab2d4ea5f d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 f09f029fc4cb d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 f09f029fc4cb d5a6141f6db6 f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb f09f029fc4cb d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 91a436abd521 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 d5a6141f6db6 | """Mock Beancount objects for testing"""
# Copyright © 2020 Brett Smith
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import datetime
import itertools
import beancount.core.amount as bc_amount
import beancount.core.data as bc_data
from decimal import Decimal
from pathlib import Path
from conservancy_beancount import rtutil
EXTREME_FUTURE_DATE = datetime.date(datetime.MAXYEAR, 12, 30)
FUTURE_DATE = datetime.date.today() + datetime.timedelta(days=365 * 99)
FY_START_DATE = datetime.date(2020, 3, 1)
FY_MID_DATE = datetime.date(2020, 9, 1)
PAST_DATE = datetime.date(2000, 1, 1)
TESTS_DIR = Path(__file__).parent
def check_post_meta(txn, *expected_meta, default=None):
assert len(txn.postings) == len(expected_meta)
for post, expected in zip(txn.postings, expected_meta):
if not expected:
assert not post.meta
else:
actual = None if post.meta is None else {
key: post.meta.get(key, default) for key in expected
}
assert actual == expected
def combine_values(*value_seqs):
stop = 0
for seq in value_seqs:
try:
stop = max(stop, len(seq))
except TypeError:
pass
return itertools.islice(
zip(*(itertools.cycle(seq) for seq in value_seqs)),
stop,
)
def date_seq(date=FY_MID_DATE, step=1):
while True:
yield date
date += datetime.timedelta(days=step)
def parse_date(s, fmt='%Y-%m-%d'):
return datetime.datetime.strptime(s, fmt).date()
def test_path(s):
if s is None:
return s
s = Path(s)
if not s.is_absolute():
s = TESTS_DIR / s
return s
def Amount(number, currency='USD'):
return bc_amount.Amount(Decimal(number), currency)
def Cost(number, currency='USD', date=FY_MID_DATE, label=None):
return bc_data.Cost(Decimal(number), currency, date, label)
def Posting(account, number,
currency='USD', cost=None, price=None, flag=None,
type_=bc_data.Posting, **meta):
if cost is not None:
cost = Cost(*cost)
if not meta:
meta = None
return type_(
account,
Amount(number, currency),
cost,
price,
flag,
meta,
)
def Transaction(date=FY_MID_DATE, flag='*', payee=None,
narration='', tags=None, links=None, postings=(),
**meta):
if isinstance(date, str):
date = parse_date(date)
meta.setdefault('filename', '<test>')
meta.setdefault('lineno', 0)
real_postings = []
for post in postings:
try:
post.account
except AttributeError:
if isinstance(post[-1], dict):
args = post[:-1]
kwargs = post[-1]
else:
args = post
kwargs = {}
post = Posting(*args, **kwargs)
real_postings.append(post)
return bc_data.Transaction(
meta,
date,
flag,
payee,
narration,
set(tags or ''),
set(links or ''),
real_postings,
)
LINK_METADATA_STRINGS = {
'Invoices/304321.pdf',
'rt:123/456',
'rt://ticket/234',
}
NON_LINK_METADATA_STRINGS = {
'',
' ',
' ',
}
NON_STRING_METADATA_VALUES = [
Decimal(5),
FY_MID_DATE,
Amount(50),
Amount(500, None),
]
OPENING_EQUITY_ACCOUNTS = itertools.cycle([
'Equity:Funds:Unrestricted',
'Equity:Funds:Restricted',
'Equity:OpeningBalance',
])
def balance_map(source=None, **kwargs):
# The source and/or kwargs should map currency name strings to
# things you can pass to Decimal (a decimal string, an int, etc.)
# This returns a dict that maps currency name strings to Amount instances.
retval = {}
if source is not None:
retval.update((currency, Amount(number, currency))
for currency, number in source)
if kwargs:
retval.update(balance_map(kwargs.items()))
return retval
def OpeningBalance(acct=None, **txn_meta):
if acct is None:
acct = next(OPENING_EQUITY_ACCOUNTS)
return Transaction(**txn_meta, postings=[
('Assets:Receivable:Accounts', 100),
('Assets:Receivable:Loans', 200),
('Liabilities:Payable:Accounts', -15),
('Liabilities:Payable:Vacation', -25),
(acct, -260),
])
class TestConfig:
def __init__(self, *,
payment_threshold=0,
repo_path=None,
rt_client=None,
):
self._payment_threshold = Decimal(payment_threshold)
self.repo_path = test_path(repo_path)
self._rt_client = rt_client
if rt_client is None:
self._rt_wrapper = None
else:
self._rt_wrapper = rtutil.RT(rt_client)
def payment_threshold(self):
return self._payment_threshold
def repository_path(self):
return self.repo_path
def rt_client(self):
return self._rt_client
def rt_wrapper(self):
return self._rt_wrapper
class _TicketBuilder:
MESSAGE_ATTACHMENTS = [
('(Unnamed)', 'multipart/alternative', '0b'),
('(Unnamed)', 'text/plain', '1.2k'),
('(Unnamed)', 'text/html', '1.4k'),
]
MISC_ATTACHMENTS = [
('Forwarded Message.eml', 'message/rfc822', '3.1k'),
('photo.jpg', 'image/jpeg', '65.2k'),
('ConservancyInvoice-301.pdf', 'application/pdf', '326k'),
('Company_invoice-2020030405_as-sent.pdf', 'application/pdf', '50k'),
('statement.txt', 'text/plain', '652b'),
('screenshot.png', 'image/png', '1.9m'),
]
def __init__(self):
self.id_seq = itertools.count(1)
self.misc_attchs = itertools.cycle(self.MISC_ATTACHMENTS)
def new_attch(self, attch):
return (str(next(self.id_seq)), *attch)
def new_msg_with_attachments(self, attachments_count=1):
for attch in self.MESSAGE_ATTACHMENTS:
yield self.new_attch(attch)
for _ in range(attachments_count):
yield self.new_attch(next(self.misc_attchs))
def new_messages(self, messages_count, attachments_count=None):
for n in range(messages_count):
if attachments_count is None:
att_count = messages_count - n
else:
att_count = attachments_count
yield from self.new_msg_with_attachments(att_count)
class RTClient:
_builder = _TicketBuilder()
DEFAULT_URL = 'https://example.org/defaultrt/REST/1.0/'
TICKET_DATA = {
'1': list(_builder.new_messages(1, 3)),
'2': list(_builder.new_messages(2, 1)),
'3': list(_builder.new_messages(3, 0)),
}
del _builder
def __init__(self,
url=DEFAULT_URL,
default_login=None,
default_password=None,
proxy=None,
default_queue='General',
skip_login=False,
verify_cert=True,
http_auth=None,
):
self.url = url
if http_auth is None:
self.user = default_login
self.password = default_password
self.auth_method = 'login'
self.login_result = skip_login or None
else:
self.user = http_auth.username
self.password = http_auth.password
self.auth_method = type(http_auth).__name__
self.login_result = True
self.last_login = None
def login(self, login=None, password=None):
if login is None and password is None:
login = self.user
password = self.password
self.login_result = bool(login and password and not password.startswith('bad'))
self.last_login = (login, password, self.login_result)
return self.login_result
def get_attachments(self, ticket_id):
try:
return list(self.TICKET_DATA[str(ticket_id)])
except KeyError:
return None
def get_attachment(self, ticket_id, attachment_id):
try:
att_seq = iter(self.TICKET_DATA[str(ticket_id)])
except KeyError:
return None
att_id = str(attachment_id)
multipart_id = None
for attch in att_seq:
if attch[0] == att_id:
break
elif attch[2].startswith('multipart/'):
multipart_id = attch[0]
else:
return None
tx_id = multipart_id or att_id
if attch[1] == '(Unnamed)':
filename = ''
else:
filename = attch[1]
return {
'id': att_id,
'ContentType': attch[2],
'Filename': filename,
'Transaction': tx_id,
}
def get_ticket(self, ticket_id):
ticket_id_s = str(ticket_id)
if ticket_id_s not in self.TICKET_DATA:
return None
return {
'id': 'ticket/{}'.format(ticket_id_s),
'numerical_id': ticket_id_s,
}
|