reports: Add RelatedPostings.group_by_account() classmethod.

This commit is contained in:
Brett Smith 2020-06-15 11:20:30 -04:00
parent ccc3a829da
commit 8333ed8876
2 changed files with 24 additions and 0 deletions

View file

@ -274,6 +274,12 @@ class RelatedPostings(Sequence[data.Posting]):
for value, posts in mapping.items():
yield value, cls(posts, _can_own=True)
@classmethod
def group_by_account(cls: Type[RelatedType],
postings: Iterable[data.Posting],
) -> Iterator[Tuple[data.Account, RelatedType]]:
return cls._group_by(postings, operator.attrgetter('account'))
@classmethod
def group_by_meta(cls: Type[RelatedType],
postings: Iterable[data.Posting],

View file

@ -398,3 +398,21 @@ def test_group_by_first_meta_link(link_swap_posts):
actual = actual_all.get(key, '')
assert len(actual) == 2
assert all(post.account == expect_account for post in actual)
def test_group_by_account():
entries = [
testutil.Transaction(postings=[
('Income:Donations', -10),
('Assets:Cash', 10),
]),
testutil.Transaction(postings=[
('Income:Donations', -20),
('Assets:Cash', 20),
]),
]
postings = data.Posting.from_entries(entries)
actual = dict(core.RelatedPostings.group_by_account(postings))
assert len(actual) == 2
for key, related in actual.items():
assert len(related) == 2
assert all(post.account == key for post in related)