Changeset - d41bc5e9b62f
[Not reviewed]
0 2 0
Brett Smith - 4 years ago 2020-04-22 16:02:06
brettcsmith@brettcsmith.org
reports: Add RelatedPostings.clear() method.
2 files changed with 10 insertions and 0 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/reports/core.py
Show inline comments
...
 
@@ -117,24 +117,27 @@ class RelatedPostings(Sequence[data.Posting]):
 
    ) -> Union[data.Posting, Sequence[data.Posting]]:
 
        if isinstance(index, slice):
 
            raise NotImplementedError("RelatedPostings[slice]")
 
        else:
 
            return self._postings[index]
 

	
 
    def __len__(self) -> int:
 
        return len(self._postings)
 

	
 
    def add(self, post: data.Posting) -> None:
 
        self._postings.append(post)
 

	
 
    def clear(self) -> None:
 
        self._postings.clear()
 

	
 
    def iter_with_balance(self) -> Iterable[Tuple[data.Posting, Balance]]:
 
        balance = MutableBalance()
 
        for post in self:
 
            balance.add_amount(post.units)
 
            yield post, balance
 

	
 
    def balance(self) -> Balance:
 
        for _, balance in self.iter_with_balance():
 
            pass
 
        try:
 
            return balance
 
        except NameError:
tests/test_reports_related_postings.py
Show inline comments
...
 
@@ -69,24 +69,31 @@ def test_balance_empty():
 

	
 
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 test_clear():
 
    related = core.RelatedPostings()
 
    related.add(testutil.Posting('Income:Donations', -10))
 
    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 = []
 
    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(),
0 comments (0 inline, 0 general)