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):