Files
@ a5b3dc146351
Branch filter:
Location: NPO-Accounting/conservancy_beancount/tests/test_reports_accrual.py
a5b3dc146351
31.8 KiB
text/x-python
accrual: AccrualPostings.make_consistent() groups accruals by date.
This accommodates cases of contracts without separate invoices,
where a series of payments are scheduled over time.
The dance we used to do of group-by-invoice, then make consistent was
already suspect. It was originally motivated by the consistency checks that
are now gone. Use this opportunity to clean up and just make make_consistent
a classmethod.
This accommodates cases of contracts without separate invoices,
where a series of payments are scheduled over time.
The dance we used to do of group-by-invoice, then make consistent was
already suspect. It was originally motivated by the consistency checks that
are now gone. Use this opportunity to clean up and just make make_consistent
a classmethod.
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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 | """test_reports_accrual - Unit tests for accrual report"""
# 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 collections
import copy
import datetime
import io
import itertools
import operator
import re
import babel.numbers
import odf.opendocument
import odf.table
import odf.text
import pytest
from . import testutil
from decimal import Decimal
from typing import NamedTuple, Optional, Sequence
from beancount.core import data as bc_data
from beancount import loader as bc_loader
from conservancy_beancount import cliutil
from conservancy_beancount import data
from conservancy_beancount import rtutil
from conservancy_beancount.reports import accrual
_accruals_load = bc_loader.load_file(testutil.test_path('books/accruals.beancount'))
ACCRUAL_TXNS = [
entry for entry in _accruals_load[0]
if hasattr(entry, 'narration')
and entry.narration != 'Opening balances'
]
ACCRUALS_COUNT = sum(
1
for txn in ACCRUAL_TXNS
for post in txn.postings
if post.account.startswith(('Assets:Receivable:', 'Liabilities:Payable:'))
)
ACCOUNTS = [
'Assets:Receivable:Accounts',
'Assets:Receivable:Loans',
'Liabilities:Payable:Accounts',
'Liabilities:Payable:Vacation',
]
class AgingRow(NamedTuple):
date: datetime.date
entity: Sequence[str]
amount: Optional[Sequence[bc_data.Amount]]
at_cost: bc_data.Amount
rt_id: Sequence[str]
invoice: Sequence[str]
project: Sequence[str]
@classmethod
def make_simple(cls, date, entity, at_cost, invoice,
rt_id=None, orig_amount=None, project='Conservancy'):
if isinstance(date, str):
date = datetime.datetime.strptime(date, '%Y-%m-%d').date()
if not isinstance(at_cost, tuple):
at_cost = testutil.Amount(at_cost)
if rt_id is None and invoice.startswith('rt:'):
rt_id, _, _ = invoice.partition('/')
return cls(date, [entity], orig_amount, at_cost, [rt_id], [invoice], [project])
def check_row_match(self, sheet_row):
cells = testutil.ODSCell.from_row(sheet_row)
assert len(cells) >= len(self)
cells = iter(cells)
assert next(cells).value == self.date
assert next(cells).text == '\0'.join(self.entity)
assert next(cells).text == '\0'.join(
babel.numbers.format_currency(number, currency, format_type='accounting')
for number, currency in self.amount or ()
)
usd_cell = next(cells)
assert usd_cell.value_type == 'currency'
assert usd_cell.value == self.at_cost.number
assert next(cells).text == '\0'.join(self.project)
for index, cell in enumerate(cells):
links = cell.getElementsByType(odf.text.A)
assert len(links) == len(cell.childNodes)
assert index >= 1
AGING_AP = [
AgingRow.make_simple('2010-03-06', 'EarlyBird', -125, 'rt:44/440'),
AgingRow.make_simple('2010-03-30', 'EarlyBird', 75, 'rt:490/4900'),
AgingRow.make_simple('2010-04-25', 'Vendor', 200, 'FIXME'),
AgingRow.make_simple('2010-04-30', 'Vendor', 220, 'rt:310/3120'),
AgingRow.make_simple('2010-06-10', 'Lawyer', 280, 'rt:510/6100'),
AgingRow.make_simple('2010-06-18', 'EuroGov', 1100, 'rt:520/5200',
orig_amount=[testutil.Amount(1000, 'EUR')]),
AgingRow.make_simple('2010-06-20', 'StateGov', 50, 'Invoices/2010StateRegistration.pdf'),
]
AGING_AR = [
AgingRow.make_simple('2010-03-05', 'EarlyBird', -500, 'rt:40/400'),
AgingRow.make_simple('2010-05-15', 'MatchingProgram', 1500,
'rt://ticket/515/attachments/5150'),
AgingRow.make_simple('2010-06-15', 'GrantCo', 5500, 'rt:470/4700',
project='Development Grant'),
AgingRow.make_simple('2010-09-15', 'GrantCo', 6000, 'rt:470/4700',
project='Development Grant'),
]
class RTClient(testutil.RTClient):
TICKET_DATA = {
'40': [
('400', 'invoice feb.csv', 'text/csv', '40.4k'),
],
'44': [
('440', 'invoice feb.csv', 'text/csv', '40.4k'),
],
'310': [
('3100', 'VendorContract.pdf', 'application/pdf', '1.7m'),
('3120', 'VendorInvoiceB.pdf', 'application/pdf', '1.8m'),
],
'490': [],
'505': [],
'510': [
('4000', 'contract.pdf', 'application/pdf', '1.4m'),
('5100', 'invoice april.pdf', 'application/pdf', '1.5m'),
('5105', 'payment.png', 'image/png', '51.5k'),
('6100', 'invoice may.pdf', 'application/pdf', '1.6m'),
],
'515': [],
'520': [],
}
@pytest.fixture
def accrual_postings():
return data.Posting.from_entries(copy.deepcopy(ACCRUAL_TXNS))
def accruals_by_meta(postings, value, key='invoice', wrap_type=iter):
return wrap_type(
post for post in postings
if post.meta.get(key) == value
and post.account.is_under('Assets:Receivable', 'Liabilities:Payable')
)
def find_row_by_text(row_source, want_text):
for row in row_source:
try:
found_row = row.childNodes[0].text == want_text
except IndexError:
found_row = False
if found_row:
return row
return None
def check_aging_sheet(sheet, aging_rows, date, accrue_date):
if not aging_rows:
return
if isinstance(accrue_date, int):
accrue_date = date + datetime.timedelta(days=accrue_date)
rows = iter(sheet.getElementsByType(odf.table.TableRow))
for row in rows:
if "Aging Report" in row.text:
break
else:
assert None, "Header row not found"
assert f"Accrued by {accrue_date.isoformat()}" in row.text
assert f"Unpaid by {date.isoformat()}" in row.text
expect_rows = iter(aging_rows)
row0 = find_row_by_text(rows, aging_rows[0].date.isoformat())
next(expect_rows).check_row_match(row0)
for actual, expected in zip(rows, expect_rows):
expected.check_row_match(actual)
for row in rows:
if row.text.startswith("Total Aged "):
break
else:
assert None, "Totals rows not found"
actual_sum = Decimal(row.childNodes[-1].value)
for row in rows:
if row.text.startswith("Total Aged "):
actual_sum += Decimal(row.childNodes[-1].value)
else:
break
assert actual_sum == sum(
row.at_cost.number
for row in aging_rows
if row.date <= accrue_date
and row.at_cost.number > 0
)
def check_aging_ods(ods_file,
date=None,
recv_rows=AGING_AR,
pay_rows=AGING_AP,
):
if date is None:
date = datetime.date.today()
ods_file.seek(0)
ods = odf.opendocument.load(ods_file)
sheets = ods.spreadsheet.getElementsByType(odf.table.Table)
assert len(sheets) == 2
check_aging_sheet(sheets[0], recv_rows, date, -60)
check_aging_sheet(sheets[1], pay_rows, date, -30)
@pytest.mark.parametrize('search_terms,expect_count,check_func', [
([], ACCRUALS_COUNT, lambda post: post.account.is_under(
'Assets:Receivable:', 'Liabilities:Payable:',
)),
([('rt-id', '^rt:505$')], 2, lambda post: post.meta['entity'] == 'DonorA'),
([('invoice', r'^rt:\D+515/')], 1, lambda post: post.meta['entity'] == 'MatchingProgram'),
([('entity', '^Lawyer$')], 3, lambda post: post.meta['rt-id'] == 'rt:510'),
([('entity', '^Lawyer$'), ('contract', '^rt:510/')], 2,
lambda post: post.meta['invoice'].startswith('rt:510/')),
([('rt-id', '^rt:510$'), ('approval', '.')], 0, lambda post: False),
])
def test_filter_search(accrual_postings, search_terms, expect_count, check_func):
search_terms = [cliutil.SearchTerm._make(query) for query in search_terms]
actual = list(accrual.filter_search(accrual_postings, search_terms))
if expect_count < ACCRUALS_COUNT:
assert ACCRUALS_COUNT > len(actual) >= expect_count
else:
assert len(actual) == ACCRUALS_COUNT
for post in actual:
assert check_func(post)
@pytest.mark.parametrize('arg,expected', [
('aging', accrual.AgingReport),
('balance', accrual.BalanceReport),
('outgoing', accrual.OutgoingReport),
('age', accrual.AgingReport),
('bal', accrual.BalanceReport),
('out', accrual.OutgoingReport),
('outgoings', accrual.OutgoingReport),
])
def test_report_type_by_name(arg, expected):
assert accrual.ReportType.by_name(arg.lower()).value is expected
assert accrual.ReportType.by_name(arg.title()).value is expected
assert accrual.ReportType.by_name(arg.upper()).value is expected
@pytest.mark.parametrize('arg', [
'unknown',
'blance',
'outgong',
])
def test_report_type_by_unknown_name(arg):
# Raising ValueError helps argparse generate good messages.
with pytest.raises(ValueError):
accrual.ReportType.by_name(arg)
@pytest.mark.parametrize('acct_name', ACCOUNTS)
def test_accrual_postings_consistent_account(acct_name):
meta = {'invoice': '{acct_name} invoice.pdf'}
txn = testutil.Transaction(postings=[
(acct_name, 50, meta),
(acct_name, 25, meta),
])
related = accrual.AccrualPostings(data.Posting.from_txn(txn))
assert related.account == acct_name
def test_accrual_postings_entity():
txn = testutil.Transaction(postings=[
(ACCOUNTS[0], 25, {'entity': 'Accruee'}),
(ACCOUNTS[0], -15, {'entity': 'Payee15'}),
(ACCOUNTS[0], -10, {'entity': 'Payee10'}),
])
related = accrual.AccrualPostings(data.Posting.from_txn(txn))
assert related.entity == 'Accruee'
assert set(related.entities()) == {'Accruee', 'Payee10', 'Payee15'}
def test_accrual_postings_entities():
txn = testutil.Transaction(postings=[
(ACCOUNTS[0], 25, {'entity': 'Accruee'}),
(ACCOUNTS[0], -15, {'entity': 'Payee15'}),
(ACCOUNTS[0], -10, {'entity': 'Payee10'}),
])
related = accrual.AccrualPostings(data.Posting.from_txn(txn))
actual = related.entities()
assert next(actual, None) == 'Accruee'
assert set(actual) == {'Payee10', 'Payee15'}
def test_accrual_postings_entities_no_duplicates():
txn = testutil.Transaction(postings=[
(ACCOUNTS[0], 25, {'entity': 'Accruee'}),
(ACCOUNTS[0], -15, {'entity': 'Accruee'}),
(ACCOUNTS[0], -10, {'entity': 'Other'}),
])
related = accrual.AccrualPostings(data.Posting.from_txn(txn))
actual = related.entities()
assert next(actual, None) == 'Accruee'
assert next(actual, None) == 'Other'
assert next(actual, None) is None
def test_accrual_postings_inconsistent_account():
meta = {'invoice': 'invoice.pdf'}
txn = testutil.Transaction(postings=[
(acct_name, index, meta)
for index, acct_name in enumerate(ACCOUNTS)
])
related = accrual.AccrualPostings(data.Posting.from_txn(txn))
assert related.account is related.INCONSISTENT
def test_accrual_postings_rt_id():
txn = testutil.Transaction(postings=[
(ACCOUNTS[0], 10, {'rt-id': 'rt:90'}),
(ACCOUNTS[0], 10, {'rt-id': 'rt:90 rt:92'}),
(ACCOUNTS[0], 10, {'rt-id': 'rt:90 rt:94 rt:92'}),
])
related = accrual.AccrualPostings(data.Posting.from_txn(txn))
assert related.rt_id == 'rt:90'
def test_accrual_postings_rt_id_inconsistent():
txn = testutil.Transaction(postings=[
(ACCOUNTS[0], 10, {'rt-id': 'rt:96'}),
(ACCOUNTS[0], 10, {'rt-id': 'rt:98 rt:96'}),
])
related = accrual.AccrualPostings(data.Posting.from_txn(txn))
assert related.rt_id is related.INCONSISTENT
def test_accrual_postings_rt_id_none():
txn = testutil.Transaction(postings=[
(ACCOUNTS[0], 10),
])
related = accrual.AccrualPostings(data.Posting.from_txn(txn))
assert related.rt_id is None
@pytest.mark.parametrize('acct_name,invoice,day', testutil.combine_values(
ACCOUNTS,
['FIXME', '', None, *testutil.NON_STRING_METADATA_VALUES],
itertools.count(1),
))
def test_make_consistent_bad_invoice(acct_name, invoice, day):
if acct_name.startswith('Assets:'):
mult = 10
else:
mult = -10
txn = testutil.Transaction(date=datetime.date(2019, 1, day), postings=[
(acct_name, index * mult, {'invoice': invoice, 'entity': f'BadInvoice{day}'})
for index in range(1, 4)
])
consistent = dict(accrual.AccrualPostings.make_consistent(data.Posting.from_txn(txn)))
assert len(consistent) == 1
key = next(iter(consistent))
assert acct_name in key
if invoice:
assert str(invoice) in key
actual = consistent[key]
assert actual
assert len(actual) == 3
for act_post, exp_post in zip(actual, txn.postings):
assert act_post.units == exp_post.units
assert act_post.meta.get('invoice') == invoice
def test_make_consistent_across_accounts():
invoice = 'Invoices/CrossAccount.pdf'
txn = testutil.Transaction(date=datetime.date(2019, 2, 1), postings=[
(acct_name, 100, {'invoice': invoice, 'entity': 'CrossAccount'})
for acct_name in ACCOUNTS
])
consistent = dict(accrual.AccrualPostings.make_consistent(data.Posting.from_txn(txn)))
assert len(consistent) == len(ACCOUNTS)
for key, posts in consistent.items():
assert len(posts) == 1
assert posts.account in key
def test_make_consistent_both_invoice_and_account():
txn = testutil.Transaction(date=datetime.date(2019, 2, 2), postings=[
(acct_name, 150) for acct_name in ACCOUNTS
])
consistent = dict(accrual.AccrualPostings.make_consistent(data.Posting.from_txn(txn)))
assert len(consistent) == len(ACCOUNTS)
for key, posts in consistent.items():
assert len(posts) == 1
assert posts.account in key
@pytest.mark.parametrize('acct_name', ACCOUNTS)
def test_make_consistent_across_entity(acct_name):
amt_sign = operator.pos if acct_name.startswith('Assets') else operator.neg
txn = testutil.Transaction(postings=[
(acct_name, amt_sign(n), {'invoice': 'Inv/1.pdf', 'entity': f'Entity{n}'})
for n in range(1, 4)
])
consistent = dict(accrual.AccrualPostings.make_consistent(data.Posting.from_txn(txn)))
assert len(consistent) == 3
for key, posts in consistent.items():
assert len(posts) == 1
entities = posts.entities()
assert next(entities, None) == posts.entity
assert next(entities, None) is None
assert posts.entity in key
@pytest.mark.parametrize('acct_name', ACCOUNTS)
def test_make_consistent_entity_differs_accrual_payment(acct_name):
invoice = 'Invoices/DifferPay.pdf'
txn = testutil.Transaction(postings=[
# Depending on the account, the order of the accrual and payment might
# be swapped here, but that shouldn't matter.
(acct_name, 125, {'invoice': invoice, 'entity': 'Positive'}),
(acct_name, -125, {'invoice': invoice, 'entity': 'Negative'}),
])
related = list(data.Posting.from_txn(txn))
consistent = accrual.AccrualPostings.make_consistent(related)
_, actual = next(consistent)
assert len(actual) == len(related)
assert all(post in actual for post in related)
assert next(consistent, None) is None
def test_make_consistent_by_date_accruals_differ():
meta = {'rt-id': '1', 'invoice': 'rt:1/2', 'entity': 'MultiDate'}
entries = [testutil.Transaction(date=date, postings=[
('Assets:Receivable:Accounts', date.day * 100, meta),
]) for date in itertools.islice(testutil.date_seq(), 3)]
actual = [group for _, group in
accrual.AccrualPostings.make_consistent(data.Posting.from_entries(entries))]
assert len(actual) == 3
assert {post.units.number for group in actual for post in group} == {100, 200, 300}
def test_make_consistent_by_date_with_exact_payment():
meta = {'rt-id': '1', 'invoice': 'rt:1/3', 'entity': 'OnePayment'}
entries = [testutil.Transaction(date=date, postings=[(
'Assets:Receivable:Accounts',
35 * (1 if date.day % 2 else -1),
meta,
)]) for date in itertools.islice(testutil.date_seq(), 3)]
actual = [group for _, group in
accrual.AccrualPostings.make_consistent(data.Posting.from_entries(entries))]
assert len(actual) == 2
assert actual[0].is_zero()
assert len(actual[1]) == 1
assert actual[1][0].meta.date.day == 3
def test_make_consistent_by_date_with_underpayment():
meta = {'rt-id': '1', 'invoice': 'rt:1/4', 'entity': 'UnderPayment'}
entries = [testutil.Transaction(date=date, postings=[(
'Assets:Receivable:Accounts',
40 * (1 if date.day % 2 else -.5),
meta,
)]) for date in itertools.islice(testutil.date_seq(), 3)]
actual = [group for _, group in
accrual.AccrualPostings.make_consistent(data.Posting.from_entries(entries))]
assert len(actual) == 2
assert len(actual[0]) == 2
assert actual[0][0].units.number == 40
assert actual[0][1].units.number == -20
assert len(actual[1]) == 1
assert actual[1][0].meta.date.day == 3
def test_make_consistent_by_date_with_overpayment():
meta = {'rt-id': '1', 'invoice': 'rt:1/5', 'entity': 'OverPayment'}
entries = [testutil.Transaction(date=date, postings=[(
'Assets:Receivable:Accounts',
50 * (1 if date.day % 2 else -1.5),
meta,
)]) for date in itertools.islice(testutil.date_seq(), 3)]
actual = [group for _, group in
accrual.AccrualPostings.make_consistent(data.Posting.from_entries(entries))]
assert len(actual) == 2
assert actual[0].is_zero()
assert len(actual[1]) == 2
assert actual[1][0].meta.date.day == 2
assert actual[1][0].units.number == -25
assert actual[1][1].meta.date.day == 3
def test_make_consistent_by_date_with_late_payment():
meta = {'rt-id': '1', 'invoice': 'rt:1/6', 'entity': 'LatePayment'}
entries = [testutil.Transaction(date=date, postings=[(
'Assets:Receivable:Accounts',
60 * (-1 if date.day > 2 else 1),
meta,
)]) for date in itertools.islice(testutil.date_seq(), 3)]
actual = [group for _, group in
accrual.AccrualPostings.make_consistent(data.Posting.from_entries(entries))]
assert len(actual) == 2
assert len(actual[0]) == 2
assert actual[0][0].meta.date.day == 1
assert actual[0][1].meta.date.day == 3
assert len(actual[1]) == 1
assert actual[1][0].meta.date.day == 2
def test_make_consistent_by_date_with_split_payments():
meta = {'rt-id': '1', 'invoice': 'rt:1/7', 'entity': 'SplitPayments'}
entries = [testutil.Transaction(date=date, postings=[(
'Assets:Receivable:Accounts', amount, meta,
)]) for date, amount in zip(testutil.date_seq(), [70, 80, -50, -100])]
actual = [group for _, group in
accrual.AccrualPostings.make_consistent(data.Posting.from_entries(entries))]
assert len(actual) == 2
assert [post.units.number for post in actual[0]] == [70, -50, -20]
assert [post.units.number for post in actual[1]] == [80, -80]
@pytest.mark.parametrize('account,day', itertools.product(
ACCOUNTS,
[1, 10, 20, 30],
))
def test_make_consistent_with_three_one_split(account, day):
meta = {'rt-id': '1', 'invoice': 'rt:1/8', 'entity': '3Split'}
entries = [testutil.Transaction(date=datetime.date(2019, 5, dd), postings=[(
account, dd, meta,
)]) for dd in [5, 15, 25]]
meta['entity'] = '1Split'
entries.insert(day // 10, testutil.Transaction(
date=datetime.date(2019, 5, day),
postings=[(account, -45, meta)],
))
postings = data.Posting.from_entries(entries)
actual = dict(accrual.AccrualPostings.make_consistent(iter(postings)))
if account.startswith('Assets:'):
group_count = 3
post_count = 2
else:
group_count = 1
post_count = 4
assert len(actual) == group_count
for related in actual.values():
assert len(related) == post_count
assert sum(post.units.number for post in related) == 0
assert all(post.meta['invoice'] == meta['invoice'] for post in related)
assert {post.meta['entity'] for post in related} == {'1Split', '3Split'}
@pytest.mark.parametrize('account', ACCOUNTS)
def test_make_consistent_with_three_two_split(account):
meta = {'rt-id': '1', 'invoice': 'rt:1/9'}
entries = [testutil.Transaction(date=datetime.date(2019, 5, day), postings=[(
account, day * (1 if day % 10 else -1.5), meta,
)]) for day in range(5, 30, 5)]
postings = data.Posting.from_entries(entries)
actual = dict(accrual.AccrualPostings.make_consistent(iter(postings)))
if account.startswith('Assets:'):
group_count = 3
else:
group_count = 2
assert len(actual) == group_count
for related in actual.values():
assert len(related) >= 2
assert sum(post.units.number for post in related) == 0
assert all(post.meta['invoice'] == meta['invoice'] for post in related)
def check_output(output, expect_patterns):
output.seek(0)
testutil.check_lines_match(iter(output), expect_patterns)
def run_outgoing(rt_id, postings, rt_client=None):
if rt_client is None:
rt_client = RTClient()
rt_wrapper = rtutil.RT(rt_client)
if not isinstance(postings, accrual.AccrualPostings):
postings = accruals_by_meta(postings, rt_id, 'rt-id', wrap_type=accrual.AccrualPostings)
output = io.StringIO()
report = accrual.OutgoingReport(rt_wrapper, output)
report.run({rt_id: postings})
return output
@pytest.mark.parametrize('invoice,expected', [
('rt:505/5050', "Zero balance outstanding since 2010-05-05"),
('rt:510/5100', "Zero balance outstanding since 2010-05-10"),
('rt:510/6100', "-280.00 USD outstanding since 2010-06-10"),
('rt://ticket/515/attachments/5150', "1,500.00 USD outstanding since 2010-05-15",),
])
def test_balance_report(accrual_postings, invoice, expected, caplog):
related = accruals_by_meta(accrual_postings, invoice, wrap_type=accrual.AccrualPostings)
output = io.StringIO()
report = accrual.BalanceReport(output)
report.run({invoice: related})
assert not caplog.records
check_output(output, [invoice, expected])
def test_outgoing_report(accrual_postings, caplog):
rt_client = RTClient()
output = run_outgoing('rt:510', accrual_postings, rt_client)
rt_url = RTClient.DEFAULT_URL[:-9]
rt_id_url = rf'\b{re.escape(f"{rt_url}Ticket/Display.html?id=510")}\b'
contract_url = rf'\b{re.escape(f"{rt_url}Ticket/Attachment/4000/4000/contract.pdf")}\b'
assert not caplog.records
check_output(output, [
r'^PAYMENT FOR APPROVAL:$',
r'^REQUESTOR: Mx\. 510 <mx510@example\.org>$',
r'^PAYMENT TO: Hon\. Mx\. 510$',
r'^TOTAL TO PAY: \$280\.00$',
fr'^AGREEMENT: {contract_url}',
r'^BEANCOUNT ENTRIES:$',
# For each transaction, check for the date line, a metadata, and the
# Expenses posting.
r'^\s*2010-06-10\s',
fr'^\s+rt-id: "{rt_id_url}"$',
r'^\s+Expenses:Services:Legal\s+220\.00 USD$',
r'^\s*2010-06-10\s',
fr'^\s+contract: "{contract_url}"$',
r'^\s+Expenses:FilingFees\s+60\.00 USD$',
])
assert rt_client.edits == {'510': {
'CF_payment-amount': 'USD 280.00',
'CF_payment-method': 'USD ACH',
}}
assert 'payment-method:' not in output.getvalue()
def test_outgoing_report_custom_field_fallbacks(accrual_postings, caplog):
rt_client = RTClient(want_cfs=False)
output = run_outgoing('rt:510', accrual_postings, rt_client)
assert not caplog.records
check_output(output, [
r'^PAYMENT FOR APPROVAL:$',
r'^REQUESTOR: <mx510@example\.org>$',
r'^PAYMENT TO:\s*$',
])
def test_outgoing_report_fx_amounts(accrual_postings, caplog):
rt_client = RTClient()
output = run_outgoing('rt:520 rt:525', accrual_postings, rt_client)
assert not caplog.records
check_output(output, [
r'^PAYMENT FOR APPROVAL:$',
r'^REQUESTOR: Mx\. 520 <mx520@example\.org>$',
r'^TOTAL TO PAY: 1,000\.00 EUR \(\$1,100.00\)$',
])
assert rt_client.edits == {'520': {
'CF_payment-amount': 'EUR 1,000.00 ($1,100.00)',
'CF_payment-method': 'EUR International Wire',
}}
assert 'payment-method:' not in output.getvalue()
def test_outgoing_report_multi_invoice(accrual_postings, caplog):
rt_client = RTClient()
output = run_outgoing('rt:310', accrual_postings, rt_client)
log, = caplog.records
assert log.levelname == 'WARNING'
assert log.message.startswith('cannot set payment-method for rt:310: ')
check_output(output, [
r'^PAYMENT FOR APPROVAL:$',
r'^REQUESTOR: Mx\. 310 <mx310@example\.org>$',
r'^TOTAL TO PAY: \$420.00$',
])
assert rt_client.edits == {'310': {
'CF_payment-amount': 'USD 420.00',
}}
assert 'payment-method:' not in output.getvalue()
def test_outgoing_report_without_rt_id(accrual_postings, caplog):
invoice = 'rt://ticket/515/attachments/5150'
related = accruals_by_meta(
accrual_postings, invoice, wrap_type=accrual.AccrualPostings,
)
output = run_outgoing(None, related)
assert caplog.records
log = caplog.records[0]
assert log.message.startswith(
f"can't generate outgoings report for {invoice} because no RT ticket available:",
)
assert not output.getvalue()
def run_aging_report(postings, today=None):
if today is None:
today = datetime.date.today()
postings = (
post for post in postings
if post.account.is_under('Assets:Receivable', 'Liabilities:Payable')
)
groups = dict(accrual.AccrualPostings.make_consistent(postings))
output = io.BytesIO()
rt_wrapper = rtutil.RT(RTClient())
report = accrual.AgingReport(rt_wrapper, output, today)
report.run(groups)
return output
def test_aging_report(accrual_postings):
output = run_aging_report(accrual_postings)
check_aging_ods(output)
@pytest.mark.parametrize('date,recv_end,pay_end', [
# Both these dates are chosen for their off-by-one potential:
# the first is exactly 30 days after the 2010-06-10 payable;
# the second is exactly 60 days after the 2010-05-15 receivable.
(datetime.date(2010, 7, 10), 1, 5),
(datetime.date(2010, 7, 14), 2, 5),
])
def test_aging_report_date_cutoffs(accrual_postings, date, recv_end, pay_end):
expect_recv = AGING_AR[:recv_end]
expect_pay = AGING_AP[:pay_end]
output = run_aging_report(accrual_postings, date)
check_aging_ods(output, date, expect_recv, expect_pay)
def test_aging_report_entity_consistency(accrual_postings):
output = run_aging_report((
post for post in accrual_postings
if post.meta.get('rt-id') == 'rt:480'
and post.units.number < 0
))
check_aging_ods(output, None, [], [
AgingRow.make_simple('2010-04-15', 'MultiPartyA', 125, 'rt:480/4800'),
AgingRow.make_simple('2010-04-15', 'MultiPartyB', 125, 'rt:480/4800'),
])
def test_aging_report_does_not_include_too_recent_postings(accrual_postings):
# This date is after the Q3 posting, but too soon after for that to be
# included in the aging report.
date = datetime.date(2010, 10, 1)
output = run_aging_report((
post for post in accrual_postings
if post.meta.get('rt-id') == 'rt:470'
), date)
# Date+amount are both from the Q2 posting only.
check_aging_ods(output, date, [
AgingRow.make_simple('2010-06-15', 'GrantCo', 5500, 'rt:470/4700',
project='Development Grant'),
], [])
def run_main(arglist, config=None, out_type=io.StringIO):
if config is None:
config = testutil.TestConfig(
books_path=testutil.test_path('books/accruals.beancount'),
rt_client=RTClient(),
)
if out_type is io.BytesIO:
arglist.insert(0, '--output-file=-')
output = out_type()
errors = io.StringIO()
retcode = accrual.main(arglist, output, errors, config)
output.seek(0)
errors.seek(0)
return retcode, output, errors
def check_main_fails(arglist, config, error_flags):
retcode, output, errors = run_main(arglist, config)
assert retcode > 16
assert (retcode - 16) & error_flags
assert not output.getvalue()
return errors
@pytest.mark.parametrize('arglist', [
['--report-type=balance', 'entity=EarlyBird'],
['--report-type=outgoing', 'entity=EarlyBird'],
])
def test_output_excludes_payments(arglist):
retcode, output, errors = run_main(arglist)
assert not errors.getvalue()
assert retcode == 0
output.seek(0)
for line in output:
assert not re.match(r'\brt:4\d\b', line)
@pytest.mark.parametrize('arglist,expect_invoice', [
(['40'], 'rt:40/400'),
(['44/440'], 'rt:44/440'),
])
def test_output_payments_when_only_match(arglist, expect_invoice):
retcode, output, errors = run_main(arglist)
assert not errors.getvalue()
assert retcode == 0
check_output(output, [
rf'^{re.escape(expect_invoice)}:$',
r' outstanding since ',
])
@pytest.mark.parametrize('arglist,expect_amount', [
(['310'], 420),
(['310/3120'], 220),
(['-t', 'out', 'entity=Vendor'], 420),
])
def test_main_outgoing_report(arglist, expect_amount):
retcode, output, errors = run_main(arglist)
assert not errors.getvalue()
assert retcode == 0
rt_url = RTClient.DEFAULT_URL[:-9]
rt_id_url = re.escape(f'<{rt_url}Ticket/Display.html?id=310>')
contract_url = re.escape(f'<{rt_url}Ticket/Attachment/3120/3120/VendorContract.pdf>')
check_output(output, [
r'^REQUESTOR: Mx\. 310 <mx310@example\.org>$',
rf'^TOTAL TO PAY: \${expect_amount}\.00$',
r'^\s*2010-04-30\s',
r'^\s+Expenses:Travel\s+220 USD$',
])
@pytest.mark.parametrize('arglist', [
['-t', 'balance'],
['515/5150'],
])
def test_main_balance_report(arglist):
retcode, output, errors = run_main(arglist)
assert not errors.getvalue()
assert retcode == 0
check_output(output, [
r'\brt://ticket/515/attachments/5150:$',
r'^\s+1,500\.00 USD outstanding since 2010-05-15$',
])
def test_main_balance_report_because_no_rt_id():
invoice = 'Invoices/2010StateRegistration.pdf'
retcode, output, errors = run_main([invoice])
assert not errors.getvalue()
assert retcode == 0
check_output(output, [
rf'\b{re.escape(invoice)}:$',
r'^\s+-50\.00 USD outstanding since 2010-06-20$',
])
@pytest.mark.parametrize('arglist', [
[],
['entity=Lawyer'],
])
def test_main_aging_report(arglist):
if arglist:
recv_rows = [row for row in AGING_AR if 'Lawyer' in row.entity]
pay_rows = [row for row in AGING_AP if 'Lawyer' in row.entity]
else:
recv_rows = AGING_AR
pay_rows = AGING_AP
retcode, output, errors = run_main(arglist, out_type=io.BytesIO)
assert not errors.getvalue()
assert retcode == 0
check_aging_ods(output, None, recv_rows, pay_rows)
def test_main_no_books():
errors = check_main_fails([], testutil.TestConfig(), 1 | 8)
testutil.check_lines_match(iter(errors), [
r':[01]: +no books to load in configuration\b',
])
@pytest.mark.parametrize('arglist', [
['499'],
['505/99999'],
['-t', 'balance', 'entity=NonExistent'],
])
def test_main_no_matches(arglist, caplog):
check_main_fails(arglist, None, 8)
testutil.check_logs_match(caplog, [
('WARNING', 'no matching entries found to report'),
])
def test_main_no_rt(caplog):
config = testutil.TestConfig(
books_path=testutil.test_path('books/accruals.beancount'),
)
check_main_fails(['-t', 'out'], config, 4)
testutil.check_logs_match(caplog, [
('ERROR', 'unable to generate outgoing report: RT client is required'),
])
|