Changeset - 51db04dc200c
[Not reviewed]
0 1 0
Brett Smith - 4 years ago 2020-05-15 20:46:20
brettcsmith@brettcsmith.org
test_meta_receipt: Prepare for multiple fallback meta keys.
1 file changed with 66 insertions and 65 deletions:
0 comments (0 inline, 0 general)
tests/test_meta_receipt.py
Show inline comments
...
 
@@ -40,10 +40,11 @@ class AccountForTesting(typing.NamedTuple):
 
    required_types: PostType
 
    fallback_meta: typing.Optional[str]
 
    fallback_meta: typing.Sequence[str] = ()
 

	
 
    def missing_message(self, include_fallback=True):
 
        if self.fallback_meta is None or not include_fallback:
 
            rest = ""
 
        else:
 
            rest = f"/{self.fallback_meta}"
 
        return f"{self.name} missing {TEST_KEY}{rest}"
 
        return "{} missing {}{}{}".format(
 
            self.name,
 
            TEST_KEY,
 
            '/' if self.fallback_meta else '',
 
            '/'.join(self.fallback_meta),
 
        )
 

	
...
 
@@ -60,18 +61,21 @@ class AccountForTesting(typing.NamedTuple):
 
ACCOUNTS = [AccountForTesting._make(t) for t in [
 
    ('Assets:Bank:CheckCard', PostType.CREDIT, 'check'),
 
    ('Assets:Bank:CheckCard', PostType.DEBIT, 'check-id'),
 
    ('Assets:Cash', PostType.BOTH, None),
 
    ('Assets:Checking', PostType.CREDIT, 'check'),
 
    ('Assets:Checking', PostType.DEBIT, 'check-id'),
 
    ('Assets:Savings', PostType.BOTH, None),
 
    ('Liabilities:CreditCard', PostType.CREDIT, None),
 
    ('Liabilities:CreditCard', PostType.DEBIT, 'invoice'),
 
    ('Assets:Bank:CheckCard', PostType.CREDIT, ('check',)),
 
    ('Assets:Bank:CheckCard', PostType.DEBIT, ('check-id',)),
 
    ('Assets:Cash', PostType.BOTH, ()),
 
    ('Assets:Checking', PostType.CREDIT, ('check',)),
 
    ('Assets:Checking', PostType.DEBIT, ('check-id',)),
 
    ('Assets:Savings', PostType.BOTH, ()),
 
    ('Liabilities:CreditCard', PostType.CREDIT, ()),
 
    ('Liabilities:CreditCard', PostType.DEBIT, ('invoice',)),
 
]]
 

	
 
ACCOUNTS_WITH_LINK_FALLBACK = [acct for acct in ACCOUNTS
 
                               if acct.fallback_meta and acct.fallback_meta != 'check-id']
 
ACCOUNTS_WITH_CHECK_ID_FALLBACK = [acct for acct in ACCOUNTS
 
                                   if acct.fallback_meta == 'check-id']
 
ACCOUNTS_WITHOUT_FALLBACKS = [acct for acct in ACCOUNTS if not acct.fallback_meta]
 
KNOWN_FALLBACKS = {acct.fallback_meta for acct in ACCOUNTS if acct.fallback_meta}
 
ACCOUNTS_WITH_LINK_FALLBACK = [
 
    (acct, fallback_key)
 
    for acct in ACCOUNTS
 
    for fallback_key in acct.fallback_meta
 
    if fallback_key != 'check-id'
 
]
 
ACCOUNTS_WITH_CHECK_ID_FALLBACK = [
 
    acct for acct in ACCOUNTS if 'check-id' in acct.fallback_meta
 
]
 

	
...
 
@@ -183,3 +187,3 @@ def test_bad_type_receipt_on_txn(hook, test_acct, other_acct, value):
 

	
 
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
 
@pytest.mark.parametrize('test_pair,other_acct,value', testutil.combine_values(
 
    ACCOUNTS_WITH_LINK_FALLBACK,
...
 
@@ -188,7 +192,7 @@ def test_bad_type_receipt_on_txn(hook, test_acct, other_acct, value):
 
))
 
def test_valid_fallback_on_post(hook, test_acct, other_acct, value):
 
    check(hook, test_acct, other_acct, None,
 
          post_meta={test_acct.fallback_meta: value})
 
def test_valid_fallback_on_post(hook, test_pair, other_acct, value):
 
    test_acct, meta_key = test_pair
 
    check(hook, test_acct, other_acct, None, post_meta={meta_key: value})
 

	
 
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
 
@pytest.mark.parametrize('test_pair,other_acct,value', testutil.combine_values(
 
    ACCOUNTS_WITH_LINK_FALLBACK,
...
 
@@ -197,7 +201,8 @@ def test_valid_fallback_on_post(hook, test_acct, other_acct, value):
 
))
 
def test_invalid_fallback_on_post(hook, test_acct, other_acct, value):
 
def test_invalid_fallback_on_post(hook, test_pair, other_acct, value):
 
    test_acct, meta_key = test_pair
 
    check(hook, test_acct, other_acct, {test_acct.missing_message()},
 
          post_meta={test_acct.fallback_meta: value})
 
          post_meta={meta_key: value})
 

	
 
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
 
@pytest.mark.parametrize('test_pair,other_acct,value', testutil.combine_values(
 
    ACCOUNTS_WITH_LINK_FALLBACK,
...
 
@@ -206,11 +211,11 @@ def test_invalid_fallback_on_post(hook, test_acct, other_acct, value):
 
))
 
def test_bad_type_fallback_on_post(hook, test_acct, other_acct, value):
 
def test_bad_type_fallback_on_post(hook, test_pair, other_acct, value):
 
    test_acct, meta_key = test_pair
 
    expected = {
 
        test_acct.missing_message(),
 
        test_acct.wrong_type_message(value, test_acct.fallback_meta),
 
        test_acct.wrong_type_message(value, meta_key),
 
    }
 
    check(hook, test_acct, other_acct, expected,
 
          post_meta={test_acct.fallback_meta: value})
 
    check(hook, test_acct, other_acct, expected, post_meta={meta_key: value})
 

	
 
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
 
@pytest.mark.parametrize('test_pair,other_acct,value', testutil.combine_values(
 
    ACCOUNTS_WITH_LINK_FALLBACK,
...
 
@@ -219,7 +224,7 @@ def test_bad_type_fallback_on_post(hook, test_acct, other_acct, value):
 
))
 
def test_valid_fallback_on_txn(hook, test_acct, other_acct, value):
 
    check(hook, test_acct, other_acct, None,
 
          txn_meta={test_acct.fallback_meta: value})
 
def test_valid_fallback_on_txn(hook, test_pair, other_acct, value):
 
    test_acct, meta_key = test_pair
 
    check(hook, test_acct, other_acct, None, txn_meta={meta_key: value})
 

	
 
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
 
@pytest.mark.parametrize('test_pair,other_acct,value', testutil.combine_values(
 
    ACCOUNTS_WITH_LINK_FALLBACK,
...
 
@@ -228,7 +233,8 @@ def test_valid_fallback_on_txn(hook, test_acct, other_acct, value):
 
))
 
def test_invalid_fallback_on_txn(hook, test_acct, other_acct, value):
 
def test_invalid_fallback_on_txn(hook, test_pair, other_acct, value):
 
    test_acct, meta_key = test_pair
 
    check(hook, test_acct, other_acct, {test_acct.missing_message()},
 
          txn_meta={test_acct.fallback_meta: value})
 
          txn_meta={meta_key: value})
 

	
 
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
 
@pytest.mark.parametrize('test_pair,other_acct,value', testutil.combine_values(
 
    ACCOUNTS_WITH_LINK_FALLBACK,
...
 
@@ -237,9 +243,9 @@ def test_invalid_fallback_on_txn(hook, test_acct, other_acct, value):
 
))
 
def test_bad_type_fallback_on_txn(hook, test_acct, other_acct, value):
 
def test_bad_type_fallback_on_txn(hook, test_pair, other_acct, value):
 
    test_acct, meta_key = test_pair
 
    expected = {
 
        test_acct.missing_message(),
 
        test_acct.wrong_type_message(value, test_acct.fallback_meta),
 
        test_acct.wrong_type_message(value, meta_key),
 
    }
 
    check(hook, test_acct, other_acct, expected,
 
          txn_meta={test_acct.fallback_meta: value})
 
    check(hook, test_acct, other_acct, expected, txn_meta={meta_key: value})
 

	
...
 
@@ -251,4 +257,3 @@ def test_bad_type_fallback_on_txn(hook, test_acct, other_acct, value):
 
def test_valid_check_id_on_post(hook, test_acct, other_acct, value):
 
    check(hook, test_acct, other_acct, None,
 
          post_meta={test_acct.fallback_meta: value})
 
    check(hook, test_acct, other_acct, None, post_meta={'check-id': value})
 

	
...
 
@@ -262,6 +267,5 @@ def test_invalid_check_id_on_post(hook, test_acct, other_acct, value):
 
        test_acct.missing_message(),
 
        f"{test_acct.name} has invalid {test_acct.fallback_meta}: {value}",
 
        f"{test_acct.name} has invalid check-id: {value}",
 
    }
 
    check(hook, test_acct, other_acct, expected,
 
          post_meta={test_acct.fallback_meta: value})
 
    check(hook, test_acct, other_acct, expected, post_meta={'check-id': value})
 

	
...
 
@@ -277,6 +281,5 @@ def test_bad_type_check_id_on_post(hook, test_acct, other_acct, value):
 
        test_acct.missing_message(),
 
        test_acct.wrong_type_message(value, test_acct.fallback_meta),
 
        test_acct.wrong_type_message(value, 'check-id'),
 
    }
 
    check(hook, test_acct, other_acct, expected,
 
          post_meta={test_acct.fallback_meta: value})
 
    check(hook, test_acct, other_acct, expected, post_meta={'check-id': value})
 

	
...
 
@@ -288,4 +291,3 @@ def test_bad_type_check_id_on_post(hook, test_acct, other_acct, value):
 
def test_valid_check_id_on_txn(hook, test_acct, other_acct, value):
 
    check(hook, test_acct, other_acct, None,
 
          txn_meta={test_acct.fallback_meta: value})
 
    check(hook, test_acct, other_acct, None, txn_meta={'check-id': value})
 

	
...
 
@@ -299,6 +301,5 @@ def test_invalid_check_id_on_txn(hook, test_acct, other_acct, value):
 
        test_acct.missing_message(),
 
        f"{test_acct.name} has invalid {test_acct.fallback_meta}: {value}",
 
        f"{test_acct.name} has invalid check-id: {value}",
 
    }
 
    check(hook, test_acct, other_acct, expected,
 
          txn_meta={test_acct.fallback_meta: value})
 
    check(hook, test_acct, other_acct, expected, txn_meta={'check-id': value})
 

	
...
 
@@ -314,11 +315,10 @@ def test_bad_type_check_id_on_txn(hook, test_acct, other_acct, value):
 
        test_acct.missing_message(),
 
        test_acct.wrong_type_message(value, test_acct.fallback_meta),
 
        test_acct.wrong_type_message(value, 'check-id'),
 
    }
 
    check(hook, test_acct, other_acct, expected,
 
          txn_meta={test_acct.fallback_meta: value})
 
    check(hook, test_acct, other_acct, expected, txn_meta={'check-id': value})
 

	
 
@pytest.mark.parametrize('test_acct,other_acct,key,value', testutil.combine_values(
 
    ACCOUNTS_WITHOUT_FALLBACKS,
 
    [acct for acct in ACCOUNTS if not acct.fallback_meta],
 
    NOT_REQUIRED_ACCOUNTS,
 
    KNOWN_FALLBACKS,
 
    {key for acct in ACCOUNTS for key in acct.fallback_meta},
 
    testutil.LINK_METADATA_STRINGS,
...
 
@@ -329,3 +329,3 @@ def test_fallback_not_accepted_on_other_accounts(hook, test_acct, other_acct, ke
 

	
 
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
 
@pytest.mark.parametrize('test_pair,other_acct,value', testutil.combine_values(
 
    ACCOUNTS_WITH_LINK_FALLBACK,
...
 
@@ -334,3 +334,3 @@ def test_fallback_not_accepted_on_other_accounts(hook, test_acct, other_acct, ke
 
))
 
def test_fallback_on_zero_amount_postings(hook, test_acct, other_acct, value):
 
def test_fallback_on_zero_amount_postings(hook, test_pair, other_acct, value):
 
    # Unfortunately it does happen that we get donations that go 100% to
...
 
@@ -338,2 +338,3 @@ def test_fallback_on_zero_amount_postings(hook, test_acct, other_acct, value):
 
    # Assets account.
 
    test_acct, meta_key = test_pair
 
    txn = testutil.Transaction(postings=[
...
 
@@ -341,3 +342,3 @@ def test_fallback_on_zero_amount_postings(hook, test_acct, other_acct, value):
 
        ('Expenses:BankingFees', '.1'),
 
        (test_acct.name, 0, {test_acct.fallback_meta: value}),
 
        (test_acct.name, 0, {meta_key: value}),
 
    ])
0 comments (0 inline, 0 general)