@@ -63,6 +63,27 @@ def two_accruals_three_payments():
(-550, 'EUR'),
))
@pytest.fixture
def link_swap_posts():
retval = []
meta = {
'rt-id': 'rt:12 rt:16',
'_post_type': data.Posting,
'_meta_type': data.Metadata,
}
for n in range(1, 3):
n = Decimal(n)
retval.append(testutil.Posting(
'Assets:Receivable:Accounts', n * 10, metanum=n, **meta,
meta['rt-id'] = 'rt:16 rt:12'
'Liabilities:Payable:Accounts', n * -10, metanum=n, **meta,
return retval
def test_initialize_with_list(credit_card_cycle):
related = core.RelatedPostings(credit_card_cycle[0].postings)
assert len(related) == 2
@@ -313,3 +334,35 @@ def test_group_by_meta_many_single_posts(two_accruals_three_payments):
actual = dict(core.RelatedPostings.group_by_meta(postings, 'metanumber'))
assert set(actual) == {post.units.number for post in postings}
assert len(actual) == len(postings)
def test_group_by_first_meta_link_zero():
assert not list(core.RelatedPostings.group_by_first_meta_link([], 'foo'))
def test_group_by_first_meta_link_no_key(link_swap_posts):
actual = dict(core.RelatedPostings.group_by_first_meta_link(
iter(link_swap_posts), 'Nonexistent',
assert len(actual) == 1
assert list(actual[None]) == link_swap_posts
def test_group_by_first_meta_link_bad_type(link_swap_posts):
assert all(post.meta.get('metanum') for post in link_swap_posts), \
"did not find metadata required by test"
iter(link_swap_posts), 'metanum',
def test_group_by_first_meta_link(link_swap_posts):
actual_all = dict(core.RelatedPostings.group_by_first_meta_link(
iter(link_swap_posts), 'rt-id',
assert len(actual_all) == 2
for key, expect_account in [
('rt:12', 'Assets:Receivable:Accounts'),
('rt:16', 'Liabilities:Payable:Accounts'),
]:
actual = actual_all.get(key, '')
assert len(actual) == 2
assert all(post.account == expect_account for post in actual)