From 8bc17dbf4a9ad1966e3184784d558d1b8a473500 Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Thu, 10 Sep 2020 16:06:43 -0400 Subject: [PATCH] meta_entity: Set metadata when entity comes from payee. RT#12525 This makes it easier to write bean-queries, since you don't have to check two places for the "real" entity. --- conservancy_beancount/plugin/meta_entity.py | 2 ++ tests/test_meta_entity.py | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/conservancy_beancount/plugin/meta_entity.py b/conservancy_beancount/plugin/meta_entity.py index 1e4ea02..a105b0b 100644 --- a/conservancy_beancount/plugin/meta_entity.py +++ b/conservancy_beancount/plugin/meta_entity.py @@ -72,6 +72,8 @@ class MetaEntity(core.TransactionHook): txn_entity, txn_entity_ok = self._check_entity(txn.meta, txn.payee) if txn_entity_ok is False: yield errormod.InvalidMetadataError(txn, self.METADATA_KEY, txn_entity) + if txn_entity is txn.payee: + txn.meta[self.METADATA_KEY] = txn.payee for post in data.Posting.from_txn(txn): if not post.account.is_under( 'Assets:Receivable', diff --git a/tests/test_meta_entity.py b/tests/test_meta_entity.py index cf491b0..dd5c712 100644 --- a/tests/test_meta_entity.py +++ b/tests/test_meta_entity.py @@ -118,11 +118,13 @@ def test_invalid_values_on_postings(hook, src_value): @pytest.mark.parametrize('src_value', VALID_VALUES) def test_valid_values_on_transactions(hook, src_value): - txn = testutil.Transaction(**{TEST_KEY: src_value}, postings=[ + txn = testutil.Transaction(payee='Payee', **{TEST_KEY: src_value}, postings=[ ('Assets:Cash', -25), ('Expenses:General', 25), ]) assert not any(hook.run(txn)) + # Make sure payee doesn't overwrite metadata. See payee test below. + assert txn.meta[TEST_KEY] == src_value @pytest.mark.parametrize('src_value', ANONYMOUS_VALUES) def test_anonymous_values_on_transactions(hook, src_value): @@ -151,6 +153,9 @@ def test_valid_values_on_payee(hook, src_value): ('Expenses:General', 25), ]) assert not any(hook.run(txn)) + # In this case, we want the hook to set metadata to make it easier to + # write bean-queries. + assert txn.meta[TEST_KEY] == src_value @pytest.mark.parametrize('src_value', ANONYMOUS_VALUES) def test_anonymous_values_on_payee(hook, src_value):