Files
@ 8d584734ec0c
Branch filter:
Location: NPO-Accounting/conservancy_beancount/tests/test_reports_related_postings.py - annotation
8d584734ec0c
3.6 KiB
text/x-python
data: Add Posting.from_entries() method.
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 | 219cd4bc37a4 219cd4bc37a4 219cd4bc37a4 219cd4bc37a4 219cd4bc37a4 219cd4bc37a4 219cd4bc37a4 219cd4bc37a4 219cd4bc37a4 219cd4bc37a4 219cd4bc37a4 219cd4bc37a4 219cd4bc37a4 219cd4bc37a4 219cd4bc37a4 219cd4bc37a4 b28646aa12e1 219cd4bc37a4 219cd4bc37a4 219cd4bc37a4 5aa30e5456d0 5aa30e5456d0 219cd4bc37a4 219cd4bc37a4 219cd4bc37a4 219cd4bc37a4 219cd4bc37a4 219cd4bc37a4 219cd4bc37a4 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 219cd4bc37a4 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 219cd4bc37a4 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 219cd4bc37a4 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 219cd4bc37a4 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 219cd4bc37a4 b28646aa12e1 219cd4bc37a4 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 219cd4bc37a4 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 219cd4bc37a4 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 b28646aa12e1 | """test_reports_related_postings - Unit tests for RelatedPostings"""
# 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 datetime
import itertools
from decimal import Decimal
import pytest
from . import testutil
from conservancy_beancount import data
from conservancy_beancount.reports import core
def date_seq(date=testutil.FY_MID_DATE, step=1):
while True:
yield date
date = date + datetime.timedelta(days=step)
def accruals_and_payments(acct, src_acct, dst_acct, start_date, *amounts):
dates = date_seq(start_date)
for amt, currency in amounts:
yield testutil.Transaction(date=next(dates), postings=[
(acct, amt, currency),
(dst_acct if amt < 0 else src_acct, -amt, currency),
])
@pytest.fixture
def credit_card_cycle():
return list(accruals_and_payments(
'Liabilities:CreditCard',
'Assets:Checking',
'Expenses:Other',
datetime.date(2020, 4, 1),
(-110, 'USD'),
(110, 'USD'),
(-120, 'USD'),
(120, 'USD'),
))
@pytest.fixture
def two_accruals_three_payments():
return list(accruals_and_payments(
'Assets:Receivable:Accounts',
'Income:Donations',
'Assets:Checking',
datetime.date(2020, 4, 10),
(440, 'USD'),
(-230, 'USD'),
(550, 'EUR'),
(-210, 'USD'),
(-550, 'EUR'),
))
def test_balance_empty():
balance = core.RelatedPostings().balance()
assert not balance
assert balance.is_zero()
def test_balance_credit_card(credit_card_cycle):
related = core.RelatedPostings()
assert related.balance() == testutil.balance_map()
expected = Decimal()
for txn in credit_card_cycle:
post = txn.postings[0]
expected += post.units.number
related.add(post)
assert related.balance() == testutil.balance_map(USD=expected)
assert expected == 0
def check_iter_with_balance(entries):
expect_posts = [txn.postings[0] for txn in entries]
expect_balances = []
balance_tally = collections.defaultdict(Decimal)
related = core.RelatedPostings()
for post in expect_posts:
number, currency = post.units
balance_tally[currency] += number
expect_balances.append(testutil.balance_map(balance_tally.items()))
related.add(post)
for (post, balance), exp_post, exp_balance in zip(
related.iter_with_balance(),
expect_posts,
expect_balances,
):
assert post is exp_post
assert balance == exp_balance
assert post is expect_posts[-1]
assert related.balance() == expect_balances[-1]
def test_iter_with_balance_empty():
assert not list(core.RelatedPostings().iter_with_balance())
def test_iter_with_balance_credit_card(credit_card_cycle):
check_iter_with_balance(credit_card_cycle)
def test_iter_with_balance_two_acccruals(two_accruals_three_payments):
check_iter_with_balance(two_accruals_three_payments)
|