2020-04-12 13:47:41 +00:00
|
|
|
"""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/>.
|
|
|
|
|
2020-04-12 19:18:19 +00:00
|
|
|
import collections
|
2020-04-12 13:47:41 +00:00
|
|
|
import datetime
|
|
|
|
import itertools
|
|
|
|
|
2020-04-12 15:00:41 +00:00
|
|
|
from decimal import Decimal
|
|
|
|
|
2020-04-12 13:47:41 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from . import testutil
|
|
|
|
|
|
|
|
from conservancy_beancount import data
|
|
|
|
from conservancy_beancount.reports import core
|
|
|
|
|
2020-04-12 19:18:19 +00:00
|
|
|
def accruals_and_payments(acct, src_acct, dst_acct, start_date, *amounts):
|
2020-04-22 13:17:58 +00:00
|
|
|
dates = testutil.date_seq(start_date)
|
2020-04-12 19:18:19 +00:00
|
|
|
for amt, currency in amounts:
|
2020-04-24 17:37:35 +00:00
|
|
|
post_meta = {'metanumber': amt, 'metacurrency': currency}
|
2020-04-12 19:18:19 +00:00
|
|
|
yield testutil.Transaction(date=next(dates), postings=[
|
2020-04-24 17:37:35 +00:00
|
|
|
(acct, amt, currency, post_meta),
|
|
|
|
(dst_acct if amt < 0 else src_acct, -amt, currency, post_meta),
|
2020-04-12 19:18:19 +00:00
|
|
|
])
|
2020-04-12 13:47:41 +00:00
|
|
|
|
2020-04-12 19:18:19 +00:00
|
|
|
@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'),
|
|
|
|
))
|
2020-04-12 13:47:41 +00:00
|
|
|
|
2020-04-12 19:18:19 +00:00
|
|
|
@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'),
|
|
|
|
))
|
2020-04-12 13:47:41 +00:00
|
|
|
|
2020-04-27 19:51:30 +00:00
|
|
|
def test_initialize_with_list(credit_card_cycle):
|
|
|
|
related = core.RelatedPostings(credit_card_cycle[0].postings)
|
|
|
|
assert len(related) == 2
|
|
|
|
|
|
|
|
def test_initialize_with_iterable(two_accruals_three_payments):
|
|
|
|
related = core.RelatedPostings(
|
|
|
|
post for txn in two_accruals_three_payments
|
|
|
|
for post in txn.postings
|
|
|
|
if post.account == 'Assets:Receivable:Accounts'
|
|
|
|
)
|
|
|
|
assert len(related) == 5
|
|
|
|
|
2020-04-12 19:18:19 +00:00
|
|
|
def test_balance_empty():
|
|
|
|
balance = core.RelatedPostings().balance()
|
|
|
|
assert not balance
|
|
|
|
assert balance.is_zero()
|
2020-04-12 13:47:41 +00:00
|
|
|
|
2020-05-30 21:31:21 +00:00
|
|
|
@pytest.mark.parametrize('index,expected', enumerate([
|
|
|
|
-110,
|
|
|
|
0,
|
|
|
|
-120,
|
|
|
|
0,
|
|
|
|
]))
|
|
|
|
def test_balance_credit_card(credit_card_cycle, index, expected):
|
|
|
|
related = core.RelatedPostings(
|
|
|
|
txn.postings[0] for txn in credit_card_cycle[:index + 1]
|
|
|
|
)
|
2020-06-03 22:54:12 +00:00
|
|
|
assert related.balance() == {'USD': testutil.Amount(expected, 'USD')}
|
2020-04-27 19:51:30 +00:00
|
|
|
|
2020-04-12 19:18:19 +00:00
|
|
|
def check_iter_with_balance(entries):
|
|
|
|
expect_posts = [txn.postings[0] for txn in entries]
|
|
|
|
expect_balances = []
|
|
|
|
balance_tally = collections.defaultdict(Decimal)
|
|
|
|
for post in expect_posts:
|
|
|
|
number, currency = post.units
|
|
|
|
balance_tally[currency] += number
|
2020-06-03 22:54:12 +00:00
|
|
|
expect_balances.append({code: testutil.Amount(number, code)
|
|
|
|
for code, number in balance_tally.items()})
|
2020-05-30 21:31:21 +00:00
|
|
|
related = core.RelatedPostings(expect_posts)
|
2020-04-12 19:18:19 +00:00
|
|
|
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)
|
2020-04-22 15:59:12 +00:00
|
|
|
|
2020-05-28 02:29:45 +00:00
|
|
|
def test_balance_at_cost_mixed():
|
|
|
|
txn = testutil.Transaction(postings=[
|
|
|
|
('Expenses:Other', '22'),
|
|
|
|
('Expenses:Other', '30', 'EUR', ('1.1',)),
|
|
|
|
('Expenses:Other', '40', 'EUR'),
|
|
|
|
('Expenses:Other', '50', 'USD', ('1.1', 'EUR')),
|
|
|
|
])
|
|
|
|
related = core.RelatedPostings(data.Posting.from_txn(txn))
|
|
|
|
balance = related.balance_at_cost()
|
|
|
|
amounts = set(balance.values())
|
|
|
|
assert amounts == {testutil.Amount(55, 'USD'), testutil.Amount(95, 'EUR')}
|
|
|
|
|
|
|
|
def test_balance_at_single_currency_cost():
|
|
|
|
txn = testutil.Transaction(postings=[
|
|
|
|
('Expenses:Other', '22'),
|
|
|
|
('Expenses:Other', '30', 'EUR', ('1.1',)),
|
|
|
|
('Expenses:Other', '40', 'GBP', ('1.1',)),
|
|
|
|
])
|
|
|
|
related = core.RelatedPostings(data.Posting.from_txn(txn))
|
|
|
|
balance = related.balance_at_cost()
|
|
|
|
amounts = set(balance.values())
|
|
|
|
assert amounts == {testutil.Amount(99)}
|
|
|
|
|
|
|
|
def test_balance_at_cost_zeroed_out():
|
|
|
|
txn = testutil.Transaction(postings=[
|
|
|
|
('Income:Other', '-22'),
|
|
|
|
('Assets:Receivable:Accounts', '20', 'EUR', ('1.1',)),
|
|
|
|
])
|
|
|
|
related = core.RelatedPostings(data.Posting.from_txn(txn))
|
|
|
|
balance = related.balance_at_cost()
|
|
|
|
assert balance.is_zero()
|
|
|
|
|
|
|
|
def test_balance_at_cost_singleton():
|
|
|
|
txn = testutil.Transaction(postings=[
|
|
|
|
('Assets:Receivable:Accounts', '20', 'EUR', ('1.1',)),
|
|
|
|
])
|
|
|
|
related = core.RelatedPostings(data.Posting.from_txn(txn))
|
|
|
|
balance = related.balance_at_cost()
|
|
|
|
amounts = set(balance.values())
|
|
|
|
assert amounts == {testutil.Amount(22)}
|
|
|
|
|
|
|
|
def test_balance_at_cost_singleton_without_cost():
|
|
|
|
txn = testutil.Transaction(postings=[
|
|
|
|
('Assets:Receivable:Accounts', '20'),
|
|
|
|
])
|
|
|
|
related = core.RelatedPostings(data.Posting.from_txn(txn))
|
|
|
|
balance = related.balance_at_cost()
|
|
|
|
amounts = set(balance.values())
|
|
|
|
assert amounts == {testutil.Amount(20)}
|
|
|
|
|
|
|
|
def test_balance_at_cost_empty():
|
|
|
|
related = core.RelatedPostings()
|
|
|
|
balance = related.balance_at_cost()
|
|
|
|
assert balance.is_zero()
|
|
|
|
|
2020-04-22 15:59:12 +00:00
|
|
|
def test_meta_values_empty():
|
|
|
|
related = core.RelatedPostings()
|
|
|
|
assert related.meta_values('key') == set()
|
|
|
|
|
|
|
|
def test_meta_values_no_match():
|
2020-05-30 21:31:21 +00:00
|
|
|
related = core.RelatedPostings([
|
|
|
|
testutil.Posting('Income:Donations', -1, metakey='metavalue'),
|
|
|
|
])
|
2020-04-22 15:59:12 +00:00
|
|
|
assert related.meta_values('key') == {None}
|
|
|
|
|
|
|
|
def test_meta_values_no_match_default_given():
|
2020-05-30 21:31:21 +00:00
|
|
|
related = core.RelatedPostings([
|
|
|
|
testutil.Posting('Income:Donations', -1, metakey='metavalue'),
|
|
|
|
])
|
2020-04-22 15:59:12 +00:00
|
|
|
assert related.meta_values('key', '') == {''}
|
|
|
|
|
|
|
|
def test_meta_values_one_match():
|
2020-05-30 21:31:21 +00:00
|
|
|
related = core.RelatedPostings([
|
|
|
|
testutil.Posting('Income:Donations', -1, key='metavalue'),
|
|
|
|
])
|
2020-04-22 15:59:12 +00:00
|
|
|
assert related.meta_values('key') == {'metavalue'}
|
|
|
|
|
|
|
|
def test_meta_values_some_match():
|
2020-05-30 21:31:21 +00:00
|
|
|
related = core.RelatedPostings([
|
|
|
|
testutil.Posting('Income:Donations', -1, key='1'),
|
|
|
|
testutil.Posting('Income:Donations', -2, metakey='2'),
|
|
|
|
])
|
2020-04-22 15:59:12 +00:00
|
|
|
assert related.meta_values('key') == {'1', None}
|
|
|
|
|
|
|
|
def test_meta_values_some_match_default_given():
|
2020-05-30 21:31:21 +00:00
|
|
|
related = core.RelatedPostings([
|
|
|
|
testutil.Posting('Income:Donations', -1, key='1'),
|
|
|
|
testutil.Posting('Income:Donations', -2, metakey='2'),
|
|
|
|
])
|
2020-04-22 15:59:12 +00:00
|
|
|
assert related.meta_values('key', '') == {'1', ''}
|
|
|
|
|
|
|
|
def test_meta_values_all_match():
|
2020-05-30 21:31:21 +00:00
|
|
|
related = core.RelatedPostings([
|
|
|
|
testutil.Posting('Income:Donations', -1, key='1'),
|
|
|
|
testutil.Posting('Income:Donations', -2, key='2'),
|
|
|
|
])
|
2020-04-22 15:59:12 +00:00
|
|
|
assert related.meta_values('key') == {'1', '2'}
|
|
|
|
|
|
|
|
def test_meta_values_all_match_one_value():
|
2020-05-30 21:31:21 +00:00
|
|
|
related = core.RelatedPostings([
|
|
|
|
testutil.Posting('Income:Donations', -1, key='1'),
|
|
|
|
testutil.Posting('Income:Donations', -2, key='1'),
|
|
|
|
])
|
2020-04-22 15:59:12 +00:00
|
|
|
assert related.meta_values('key') == {'1'}
|
|
|
|
|
|
|
|
def test_meta_values_all_match_default_given():
|
2020-05-30 21:31:21 +00:00
|
|
|
related = core.RelatedPostings([
|
|
|
|
testutil.Posting('Income:Donations', -1, key='1'),
|
|
|
|
testutil.Posting('Income:Donations', -2, key='2'),
|
|
|
|
])
|
2020-04-22 15:59:12 +00:00
|
|
|
assert related.meta_values('key', '') == {'1', '2'}
|
|
|
|
|
|
|
|
def test_meta_values_many_types():
|
|
|
|
expected = {
|
|
|
|
datetime.date(2020, 4, 1),
|
|
|
|
Decimal(42),
|
|
|
|
testutil.Amount(5),
|
|
|
|
'rt:42',
|
|
|
|
}
|
2020-05-30 21:31:21 +00:00
|
|
|
related = core.RelatedPostings(
|
|
|
|
testutil.Posting('Income:Donations', -index, key=value)
|
|
|
|
for index, value in enumerate(expected)
|
|
|
|
)
|
2020-04-22 15:59:12 +00:00
|
|
|
assert related.meta_values('key') == expected
|
2020-04-24 17:37:35 +00:00
|
|
|
|
2020-04-29 14:12:08 +00:00
|
|
|
@pytest.mark.parametrize('count', range(3))
|
|
|
|
def test_all_meta_links_zero(count):
|
|
|
|
postings = (
|
|
|
|
testutil.Posting('Income:Donations', -n, testkey=str(n))
|
|
|
|
for n in range(count)
|
|
|
|
)
|
|
|
|
related = core.RelatedPostings(
|
|
|
|
post._replace(meta=data.Metadata(post.meta))
|
|
|
|
for post in postings
|
|
|
|
)
|
2020-06-11 13:28:11 +00:00
|
|
|
assert next(related.all_meta_links('approval'), None) is None
|
2020-04-29 14:12:08 +00:00
|
|
|
|
|
|
|
def test_all_meta_links_singletons():
|
|
|
|
postings = (
|
|
|
|
testutil.Posting('Income:Donations', -10, statement=value)
|
|
|
|
for value in itertools.chain(
|
|
|
|
testutil.NON_LINK_METADATA_STRINGS,
|
|
|
|
testutil.LINK_METADATA_STRINGS,
|
|
|
|
testutil.NON_STRING_METADATA_VALUES,
|
|
|
|
))
|
|
|
|
related = core.RelatedPostings(
|
|
|
|
post._replace(meta=data.Metadata(post.meta))
|
|
|
|
for post in postings
|
|
|
|
)
|
2020-06-11 13:28:11 +00:00
|
|
|
assert set(related.all_meta_links('statement')) == testutil.LINK_METADATA_STRINGS
|
2020-04-29 14:12:08 +00:00
|
|
|
|
|
|
|
def test_all_meta_links_multiples():
|
|
|
|
postings = (
|
|
|
|
testutil.Posting('Income:Donations', -10, approval=' '.join(value))
|
|
|
|
for value in itertools.permutations(testutil.LINK_METADATA_STRINGS, 2)
|
|
|
|
)
|
|
|
|
related = core.RelatedPostings(
|
|
|
|
post._replace(meta=data.Metadata(post.meta))
|
|
|
|
for post in postings
|
|
|
|
)
|
2020-06-11 13:28:11 +00:00
|
|
|
assert set(related.all_meta_links('approval')) == testutil.LINK_METADATA_STRINGS
|
|
|
|
|
|
|
|
def test_all_meta_links_preserves_order():
|
|
|
|
postings = (
|
|
|
|
testutil.Posting('Income:Donations', -10, approval=c)
|
|
|
|
for c in '121323'
|
|
|
|
)
|
|
|
|
related = core.RelatedPostings(
|
|
|
|
post._replace(meta=data.Metadata(post.meta))
|
|
|
|
for post in postings
|
|
|
|
)
|
|
|
|
assert list(related.all_meta_links('approval')) == list('123')
|
2020-04-29 14:12:08 +00:00
|
|
|
|
2020-04-24 17:37:35 +00:00
|
|
|
def test_group_by_meta_zero():
|
2020-05-30 21:31:21 +00:00
|
|
|
assert not list(core.RelatedPostings.group_by_meta([], 'metacurrency'))
|
2020-04-24 17:37:35 +00:00
|
|
|
|
|
|
|
def test_group_by_meta_one(credit_card_cycle):
|
|
|
|
posting = next(post for post in data.Posting.from_entries(credit_card_cycle)
|
|
|
|
if post.account.is_credit_card())
|
|
|
|
actual = core.RelatedPostings.group_by_meta([posting], 'metacurrency')
|
2020-05-30 21:31:21 +00:00
|
|
|
assert set(key for key, _ in actual) == {'USD'}
|
2020-04-24 17:37:35 +00:00
|
|
|
|
|
|
|
def test_group_by_meta_many(two_accruals_three_payments):
|
|
|
|
postings = [post for post in data.Posting.from_entries(two_accruals_three_payments)
|
|
|
|
if post.account == 'Assets:Receivable:Accounts']
|
2020-05-30 21:31:21 +00:00
|
|
|
actual = dict(core.RelatedPostings.group_by_meta(postings, 'metacurrency'))
|
2020-04-24 17:37:35 +00:00
|
|
|
assert set(actual) == {'USD', 'EUR'}
|
|
|
|
for key, group in actual.items():
|
|
|
|
assert 2 <= len(group) <= 3
|
|
|
|
assert group.balance().is_zero()
|
|
|
|
|
|
|
|
def test_group_by_meta_many_single_posts(two_accruals_three_payments):
|
|
|
|
postings = [post for post in data.Posting.from_entries(two_accruals_three_payments)
|
|
|
|
if post.account == 'Assets:Receivable:Accounts']
|
2020-05-30 21:31:21 +00:00
|
|
|
actual = dict(core.RelatedPostings.group_by_meta(postings, 'metanumber'))
|
2020-04-24 17:37:35 +00:00
|
|
|
assert set(actual) == {post.units.number for post in postings}
|
|
|
|
assert len(actual) == len(postings)
|