diff --git a/conservancy_beancount/reports/core.py b/conservancy_beancount/reports/core.py index 2ef93c8..82bc36d 100644 --- a/conservancy_beancount/reports/core.py +++ b/conservancy_beancount/reports/core.py @@ -98,8 +98,8 @@ class RelatedPostings(Sequence[data.Posting]): for an example. """ - def __init__(self) -> None: - self._postings: List[data.Posting] = [] + def __init__(self, source: Iterable[data.Posting]=()) -> None: + self._postings: List[data.Posting] = list(source) @classmethod def group_by_meta(cls, diff --git a/tests/test_reports_related_postings.py b/tests/test_reports_related_postings.py index ba0066d..718ba04 100644 --- a/tests/test_reports_related_postings.py +++ b/tests/test_reports_related_postings.py @@ -63,6 +63,18 @@ def two_accruals_three_payments(): (-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(): balance = core.RelatedPostings().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 expected == 0 -def test_clear(): +def test_clear_after_add(): related = core.RelatedPostings() related.add(testutil.Posting('Income:Donations', -10)) assert related.balance() related.clear() 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): expect_posts = [txn.postings[0] for txn in entries] expect_balances = []