reports: Add RelatedPostings.all_meta_links() method.
This commit is contained in:
parent
46ac91e86e
commit
9fef177d2d
2 changed files with 46 additions and 0 deletions
|
@ -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()
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue