Changeset - e7720b8fb866
[Not reviewed]
0 3 0
Brett Smith - 4 years ago 2020-03-16 14:15:31
brettcsmith@brettcsmith.org
tests: Add check_post_meta.

This makes it simple to conveniently check all posting metadata in tests.
3 files changed with 34 insertions and 16 deletions:
0 comments (0 inline, 0 general)
tests/test_meta_expense_allocation.py
Show inline comments
...
 
@@ -48,3 +48,3 @@ def test_valid_values_on_postings(src_value, set_value):
 
    assert not errors
 
    assert txn.postings[-1].meta.get(TEST_KEY) == set_value
 
    testutil.check_post_meta(txn, None, {TEST_KEY: set_value})
 

	
...
 
@@ -59,2 +59,3 @@ def test_invalid_values_on_postings(src_value):
 
    assert errors
 
    testutil.check_post_meta(txn, None, {TEST_KEY: src_value})
 

	
...
 
@@ -69,3 +70,3 @@ def test_valid_values_on_transactions(src_value, set_value):
 
    assert not errors
 
    assert txn.postings[-1].meta.get(TEST_KEY) == set_value
 
    testutil.check_post_meta(txn, None, {TEST_KEY: set_value})
 

	
...
 
@@ -80,2 +81,3 @@ def test_invalid_values_on_transactions(src_value):
 
    assert errors
 
    testutil.check_post_meta(txn, None, None)
 

	
...
 
@@ -89,5 +91,6 @@ def test_invalid_values_on_transactions(src_value):
 
def test_non_expense_accounts_skipped(account):
 
    meta = {TEST_KEY: 'program'}
 
    txn = testutil.Transaction(postings=[
 
        (account, -25),
 
        ('Expenses:General', 25, {TEST_KEY: 'program'}),
 
        ('Expenses:General', 25, meta.copy()),
 
    ])
...
 
@@ -96,2 +99,3 @@ def test_non_expense_accounts_skipped(account):
 
    assert not errors
 
    testutil.check_post_meta(txn, None, meta)
 

	
...
 
@@ -112,10 +116,10 @@ def test_default_values(account, set_value):
 
    assert not errors
 
    assert txn.postings[-1].meta[TEST_KEY] == set_value
 
    testutil.check_post_meta(txn, None, {TEST_KEY: set_value})
 

	
 
@pytest.mark.parametrize('date,set_value', [
 
    (testutil.EXTREME_FUTURE_DATE, False),
 
    (testutil.FUTURE_DATE, True),
 
    (testutil.FY_START_DATE, True),
 
    (testutil.FY_MID_DATE, True),
 
    (testutil.PAST_DATE, False),
 
    (testutil.EXTREME_FUTURE_DATE, None),
 
    (testutil.FUTURE_DATE, 'program'),
 
    (testutil.FY_START_DATE, 'program'),
 
    (testutil.FY_MID_DATE, 'program'),
 
    (testutil.PAST_DATE, None),
 
])
...
 
@@ -129,3 +133,3 @@ def test_default_value_set_in_date_range(date, set_value):
 
    assert not errors
 
    got_value = (txn.postings[-1].meta or {}).get(TEST_KEY)
 
    assert bool(got_value) == bool(set_value)
 
    expect_meta = None if set_value is None else {TEST_KEY: set_value}
 
    testutil.check_post_meta(txn, None, expect_meta)
tests/test_meta_tax_implication.py
Show inline comments
...
 
@@ -60,3 +60,3 @@ def test_valid_values_on_postings(src_value, set_value):
 
    assert not errors
 
    assert txn.postings[-1].meta.get(TEST_KEY) == set_value
 
    testutil.check_post_meta(txn, None, {TEST_KEY: set_value})
 

	
...
 
@@ -71,2 +71,3 @@ def test_invalid_values_on_postings(src_value):
 
    assert errors
 
    testutil.check_post_meta(txn, None, {TEST_KEY: src_value})
 

	
...
 
@@ -81,3 +82,3 @@ def test_valid_values_on_transactions(src_value, set_value):
 
    assert not errors
 
    assert txn.postings[-1].meta.get(TEST_KEY) == set_value
 
    testutil.check_post_meta(txn, None, {TEST_KEY: set_value})
 

	
...
 
@@ -92,2 +93,3 @@ def test_invalid_values_on_transactions(src_value):
 
    assert errors
 
    testutil.check_post_meta(txn, None, None)
 

	
...
 
@@ -99,5 +101,6 @@ def test_invalid_values_on_transactions(src_value):
 
def test_non_asset_accounts_skipped(account):
 
    meta = {TEST_KEY: 'USA-Corporation'}
 
    txn = testutil.Transaction(postings=[
 
        (account, 25),
 
        ('Assets:Cash', -25, {TEST_KEY: 'USA-Corporation'}),
 
        ('Assets:Cash', -25, meta.copy()),
 
    ])
...
 
@@ -106,2 +109,3 @@ def test_non_asset_accounts_skipped(account):
 
    assert not errors
 
    testutil.check_post_meta(txn, None, meta)
 

	
...
 
@@ -115,3 +119,3 @@ def test_asset_credits_skipped():
 
    assert not errors
 
    assert not txn.postings[-1].meta
 
    testutil.check_post_meta(txn, None, None)
 

	
...
 
@@ -124,3 +128,3 @@ def test_asset_credits_skipped():
 
])
 
def test_default_value_set_in_date_range(date, need_value):
 
def test_validation_only_in_date_range(date, need_value):
 
    txn = testutil.Transaction(date=date, postings=[
...
 
@@ -132 +136,2 @@ def test_default_value_set_in_date_range(date, need_value):
 
    assert bool(errors) == bool(need_value)
 
    testutil.check_post_meta(txn, None, None)
tests/testutil.py
Show inline comments
...
 
@@ -29,2 +29,11 @@ PAST_DATE = datetime.date(2000, 1, 1)
 

	
 
def check_post_meta(txn, *expected_meta, default=None):
 
    assert len(txn.postings) == len(expected_meta)
 
    for post, expected in zip(txn.postings, expected_meta):
 
        if not expected:
 
            assert not post.meta
 
        else:
 
            assert all(post.meta.get(key, default) == value
 
                       for key, value in expected.items())
 

	
 
def parse_date(s, fmt='%Y-%m-%d'):
0 comments (0 inline, 0 general)