reports: Let RelatedPostings be initialized with an Iterable[Posting].

This commit is contained in:
Brett Smith 2020-04-27 15:51:30 -04:00
parent 5e061da940
commit d01df054ab
2 changed files with 23 additions and 3 deletions

View file

@ -98,8 +98,8 @@ class RelatedPostings(Sequence[data.Posting]):
for an example. for an example.
""" """
def __init__(self) -> None: def __init__(self, source: Iterable[data.Posting]=()) -> None:
self._postings: List[data.Posting] = [] self._postings: List[data.Posting] = list(source)
@classmethod @classmethod
def group_by_meta(cls, def group_by_meta(cls,

View file

@ -63,6 +63,18 @@ def two_accruals_three_payments():
(-550, 'EUR'), (-550, 'EUR'),
)) ))
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
def test_balance_empty(): def test_balance_empty():
balance = core.RelatedPostings().balance() balance = core.RelatedPostings().balance()
assert not balance assert not balance
@ -79,13 +91,21 @@ def test_balance_credit_card(credit_card_cycle):
assert related.balance() == testutil.balance_map(USD=expected) assert related.balance() == testutil.balance_map(USD=expected)
assert expected == 0 assert expected == 0
def test_clear(): def test_clear_after_add():
related = core.RelatedPostings() related = core.RelatedPostings()
related.add(testutil.Posting('Income:Donations', -10)) related.add(testutil.Posting('Income:Donations', -10))
assert related.balance() assert related.balance()
related.clear() related.clear()
assert not related.balance() assert not related.balance()
def test_clear_after_initialization():
related = core.RelatedPostings([
testutil.Posting('Income:Donations', -12),
])
assert related.balance()
related.clear()
assert not related.balance()
def check_iter_with_balance(entries): def check_iter_with_balance(entries):
expect_posts = [txn.postings[0] for txn in entries] expect_posts = [txn.postings[0] for txn in entries]
expect_balances = [] expect_balances = []