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.
This commit is contained in:
Brett Smith 2020-09-10 16:06:43 -04:00
parent e60078933d
commit 8bc17dbf4a
2 changed files with 8 additions and 1 deletions

View file

@ -72,6 +72,8 @@ class MetaEntity(core.TransactionHook):
txn_entity, txn_entity_ok = self._check_entity(txn.meta, txn.payee) txn_entity, txn_entity_ok = self._check_entity(txn.meta, txn.payee)
if txn_entity_ok is False: if txn_entity_ok is False:
yield errormod.InvalidMetadataError(txn, self.METADATA_KEY, txn_entity) 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): for post in data.Posting.from_txn(txn):
if not post.account.is_under( if not post.account.is_under(
'Assets:Receivable', 'Assets:Receivable',

View file

@ -118,11 +118,13 @@ def test_invalid_values_on_postings(hook, src_value):
@pytest.mark.parametrize('src_value', VALID_VALUES) @pytest.mark.parametrize('src_value', VALID_VALUES)
def test_valid_values_on_transactions(hook, src_value): 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), ('Assets:Cash', -25),
('Expenses:General', 25), ('Expenses:General', 25),
]) ])
assert not any(hook.run(txn)) 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) @pytest.mark.parametrize('src_value', ANONYMOUS_VALUES)
def test_anonymous_values_on_transactions(hook, src_value): 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), ('Expenses:General', 25),
]) ])
assert not any(hook.run(txn)) 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) @pytest.mark.parametrize('src_value', ANONYMOUS_VALUES)
def test_anonymous_values_on_payee(hook, src_value): def test_anonymous_values_on_payee(hook, src_value):