From 9fef177d2dc628e5a3f65fd67ff7eb4844ebeb87 Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Wed, 29 Apr 2020 10:12:08 -0400 Subject: [PATCH] reports: Add RelatedPostings.all_meta_links() method. --- conservancy_beancount/reports/core.py | 9 +++++++ tests/test_reports_related_postings.py | 37 ++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/conservancy_beancount/reports/core.py b/conservancy_beancount/reports/core.py index 147c726..be4b65d 100644 --- a/conservancy_beancount/reports/core.py +++ b/conservancy_beancount/reports/core.py @@ -140,6 +140,15 @@ class RelatedPostings(Sequence[data.Posting]): def add(self, post: data.Posting) -> None: self._postings.append(post) + def all_meta_links(self, key: MetaKey) -> Set[str]: + retval: Set[str] = set() + for post in self: + try: + retval.update(post.meta.get_links(key)) + except TypeError: + pass + return retval + def clear(self) -> None: self._postings.clear() diff --git a/tests/test_reports_related_postings.py b/tests/test_reports_related_postings.py index 718ba04..fbb484a 100644 --- a/tests/test_reports_related_postings.py +++ b/tests/test_reports_related_postings.py @@ -196,6 +196,43 @@ def test_meta_values_many_types(): related.add(testutil.Posting('Income:Donations', -index, key=value)) assert related.meta_values('key') == expected +@pytest.mark.parametrize('count', range(3)) +def test_all_meta_links_zero(count): + postings = ( + testutil.Posting('Income:Donations', -n, testkey=str(n)) + for n in range(count) + ) + related = core.RelatedPostings( + post._replace(meta=data.Metadata(post.meta)) + for post in postings + ) + assert related.all_meta_links('approval') == set() + +def test_all_meta_links_singletons(): + postings = ( + testutil.Posting('Income:Donations', -10, statement=value) + for value in itertools.chain( + testutil.NON_LINK_METADATA_STRINGS, + testutil.LINK_METADATA_STRINGS, + testutil.NON_STRING_METADATA_VALUES, + )) + related = core.RelatedPostings( + post._replace(meta=data.Metadata(post.meta)) + for post in postings + ) + assert related.all_meta_links('statement') == testutil.LINK_METADATA_STRINGS + +def test_all_meta_links_multiples(): + postings = ( + testutil.Posting('Income:Donations', -10, approval=' '.join(value)) + for value in itertools.permutations(testutil.LINK_METADATA_STRINGS, 2) + ) + related = core.RelatedPostings( + post._replace(meta=data.Metadata(post.meta)) + for post in postings + ) + assert related.all_meta_links('approval') == testutil.LINK_METADATA_STRINGS + def test_group_by_meta_zero(): assert len(core.RelatedPostings.group_by_meta([], 'metacurrency')) == 0