diff --git a/tests/test_data_posting_meta.py b/tests/test_data_posting_meta.py index 1edfc66203e7723438096009b450bc8db4e1a777..0fb50026488064e06996cdcf25bf87296a93ffe2 100644 --- a/tests/test_data_posting_meta.py +++ b/tests/test_data_posting_meta.py @@ -21,13 +21,21 @@ from . import testutil from conservancy_beancount import data @pytest.fixture -def simple_txn(index=None, key=None): +def simple_txn(): return testutil.Transaction(note='txn note', postings=[ ('Assets:Cash', 5), ('Income:Donations', -5, {'note': 'donation love', 'extra': 'Extra'}), ]) SIMPLE_TXN_METAKEYS = frozenset(['filename', 'lineno', 'note']) +@pytest.fixture +def payee_txn(): + return testutil.Transaction(payee='SampleCo', postings=[ + ('Assets:Receivable:Accounts', -100), + ('Assets:Checking', 95), + ('Expenses:BankingFees', 5, {'entity': 'MyBank'}), + ]) + def test_getitem_transaction(simple_txn): assert data.PostingMeta(simple_txn, 0)['note'] == 'txn note' @@ -88,6 +96,22 @@ def test_get_links_from_post_override(simple_txn): meta = data.PostingMeta(simple_txn, 1) assert list(meta.get_links('note')) == ['donation', 'love'] +def test_payee_used_as_entity(payee_txn): + actual = [data.PostingMeta(payee_txn, n, p)['entity'] + for n, p in enumerate(payee_txn.postings)] + assert actual == ['SampleCo', 'SampleCo', 'MyBank'] + +def test_entity_metadata_has_precedence_over_payee(payee_txn): + payee_txn.meta['entity'] = 'ExampleCo' + actual = [data.PostingMeta(payee_txn, n, p)['entity'] + for n, p in enumerate(payee_txn.postings)] + assert actual == ['ExampleCo', 'ExampleCo', 'MyBank'] + +def test_keyerror_when_no_entity_or_payee(simple_txn): + meta = data.PostingMeta(simple_txn, 1) + with pytest.raises(KeyError): + meta['entity'] + # The .get() tests are arguably testing the stdlib, but they're short and # they confirm that we're using the stdlib as we intend. def test_get_with_meta_value(simple_txn):