Files
@ c7fbf5b5d586
Branch filter:
Location: NPO-Accounting/conservancy_beancount/tests/test_meta_paypal_id.py
c7fbf5b5d586
6.2 KiB
text/x-python
config: s/XDG_CACHE_DIR/XDG_CACHE_HOME/g
Per the spec.
Per the spec.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | """Test validation of paypal-id metadata"""
# Copyright © 2020 Brett Smith
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import itertools
import pytest
from . import testutil
from conservancy_beancount.plugin import meta_paypal_id
VALID_TXN_IDS = {
# Basically normal ids.
'A1234567890BCDEFG',
'12345HIJKLNMO6780',
# All numeric
'05678901234567890',
# All alphabetic
'CDEFHSNOECRUHOECR',
}
VALID_INVOICE_IDS = {
'INV2-ABCD-1234-EFGH-6789',
# All numeric
'INV2-1010-2020-3030-4567',
# All alphabetic
'INV2-ABCD-EFGH-IJKL-MNOP',
}
VALID_VALUES = VALID_TXN_IDS | VALID_INVOICE_IDS
INVALID_VALUES = {
# Empty
'',
' ',
# Punctuation and whitespace
'Z12345-67890QRSTU',
'Y12345.67890QRSTU',
'X12345_67890QRSTU',
'W12345 67890QRSTU',
'INV2-ABCD.1234-EFGH-7890',
'INV2-ABCD-1234_EFGH-7890',
'INV2-ABCD-1234-EFGH 7890',
# Too short
'Q1234567890RSTUV',
'INV2-ABCD-1234-EFGH-789',
# Too long
'V123456789012345WX',
'INV2-ABCD-1234-EFGH-78900',
'INV2-ABCD-1234-EFGH-7890-IJKL',
# Bad cadence
'INV2-ABCD-1234-EFG-H7890',
'INV2ABCD-123-EFG-456-789',
'INV2ABCDEFGHIJKLMNOPQRST',
}
ACCOUNTS = itertools.cycle([
'Assets:PayPal',
'Assets:Receivable:Accounts',
])
TEST_KEY = 'paypal-id'
INVALID_MSG = f"{{}} has invalid {TEST_KEY}: {{}}".format
BAD_TYPE_MSG = f"{{}} has wrong type of {TEST_KEY}: expected str but is a {{}}".format
@pytest.fixture(scope='module')
def hook():
config = testutil.TestConfig()
return meta_paypal_id.MetaPayPalID(config)
def paypal_account_for_id(paypal_id):
if paypal_id.startswith('INV'):
return 'Assets:Receivable:Accounts'
else:
return 'Assets:PayPal'
@pytest.mark.parametrize('src_value', VALID_VALUES)
def test_valid_values_on_postings(hook, src_value):
txn = testutil.Transaction(postings=[
('Income:Donations', -25),
(paypal_account_for_id(src_value), 25, {TEST_KEY: src_value}),
])
assert not list(hook.run(txn))
@pytest.mark.parametrize('src_value', INVALID_VALUES)
def test_invalid_values_on_postings(hook, src_value):
acct = paypal_account_for_id(src_value)
txn = testutil.Transaction(postings=[
('Income:Donations', -25),
(acct, 25, {TEST_KEY: src_value}),
])
actual = {error.message for error in hook.run(txn)}
assert actual == {INVALID_MSG(acct, src_value)}
@pytest.mark.parametrize('src_value,acct', testutil.combine_values(
testutil.NON_STRING_METADATA_VALUES,
ACCOUNTS,
))
def test_bad_type_values_on_postings(hook, src_value, acct):
txn = testutil.Transaction(postings=[
('Income:Donations', -25),
(acct, 25, {TEST_KEY: src_value}),
])
actual = {error.message for error in hook.run(txn)}
assert actual == {BAD_TYPE_MSG(acct, type(src_value).__name__)}
@pytest.mark.parametrize('src_value', VALID_VALUES)
def test_valid_values_on_transactions(hook, src_value):
txn = testutil.Transaction(**{TEST_KEY: src_value}, postings=[
('Income:Donations', -25),
(paypal_account_for_id(src_value), 25),
])
assert not list(hook.run(txn))
@pytest.mark.parametrize('src_value', INVALID_VALUES)
def test_invalid_values_on_transactions(hook, src_value):
acct = paypal_account_for_id(src_value)
txn = testutil.Transaction(**{TEST_KEY: src_value}, postings=[
('Income:Donations', -25),
(acct, 25),
])
actual = {error.message for error in hook.run(txn)}
assert actual == {INVALID_MSG(acct, src_value)}
@pytest.mark.parametrize('src_value,acct', testutil.combine_values(
testutil.NON_STRING_METADATA_VALUES,
ACCOUNTS,
))
def test_bad_type_values_on_transactions(hook, src_value, acct):
txn = testutil.Transaction(**{TEST_KEY: src_value}, postings=[
('Income:Donations', -25),
(acct, 25),
])
actual = {error.message for error in hook.run(txn)}
assert actual == {BAD_TYPE_MSG(acct, type(src_value).__name__)}
@pytest.mark.parametrize('src_value', VALID_INVOICE_IDS)
def test_invoice_ids_not_accepted_for_non_accruals(hook, src_value):
acct = 'Assets:PayPal'
txn = testutil.Transaction(**{TEST_KEY: src_value}, postings=[
('Income:Donations', -25),
(acct, 25),
])
actual = {error.message for error in hook.run(txn)}
assert actual == {INVALID_MSG(acct, src_value)}
@pytest.mark.parametrize('src_value', VALID_TXN_IDS)
def test_transaction_ids_not_accepted_for_accruals(hook, src_value):
acct = 'Assets:Receivable:Accounts'
txn = testutil.Transaction(**{TEST_KEY: src_value}, postings=[
('Income:Donations', -25),
(acct, 25),
])
actual = {error.message for error in hook.run(txn)}
assert actual == {INVALID_MSG(acct, src_value)}
def test_required_for_assets_paypal(hook):
acct = 'Assets:PayPal'
txn = testutil.Transaction(postings=[
('Income:Donations', -35),
(acct, 35),
])
actual = {error.message for error in hook.run(txn)}
assert actual == {f"{acct} missing {TEST_KEY}"}
@pytest.mark.parametrize('txn_id,inv_id', testutil.combine_values(
VALID_TXN_IDS,
VALID_INVOICE_IDS,
))
def test_invoice_payment_transaction_ok(hook, txn_id, inv_id):
txn = testutil.Transaction(**{TEST_KEY: txn_id}, postings=[
('Assets:Receivable:Accounts', -100, {TEST_KEY: inv_id}),
('Assets:PayPal', 97),
('Expenses:BankingFees', 3),
])
assert not list(hook.run(txn))
def test_not_required_on_opening(hook):
txn = testutil.Transaction(postings=[
('Assets:PayPal', 1000),
(next(testutil.OPENING_EQUITY_ACCOUNTS), -1000),
])
assert not list(hook.run(txn))
|