Files
@ 5793a55dbcc6
Branch filter:
Location: NPO-Accounting/conservancy_beancount/tests/test_reports_rewrite.py
5793a55dbcc6
12.5 KiB
text/x-python
reconciler: Fix reconciler lumping unmatched books items together (#20737)
Issue was that the subset matching was passing through the grouped transactions
rather than the original transactions when it failed to match.
Issue was that the subset matching was passing through the grouped transactions
rather than the original transactions when it failed to match.
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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | """test_reports_rewrite - Unit tests for report rewrite functionality"""
# Copyright © 2020 Brett Smith
# License: AGPLv3-or-later WITH Beancount-Plugin-Additional-Permission-1.0
#
# Full copyright and licensing details can be found at toplevel file
# LICENSE.txt in the repository.
import datetime
import pytest
from decimal import Decimal
import yaml
from . import testutil
from conservancy_beancount import data, errors
from conservancy_beancount.reports import rewrite
CMP_OPS = frozenset('< <= == != >= >'.split())
@pytest.mark.parametrize('name', ['Equity:Other', 'Expenses:Other', 'Income:Other'])
@pytest.mark.parametrize('operator', CMP_OPS)
def test_account_condition(name, operator):
operand = 'Expenses:Other'
txn = testutil.Transaction(postings=[(name, -5)])
post, = data.Posting.from_txn(txn)
tester = rewrite.AccountTest(operator, operand)
assert tester(post) == eval(f'name {operator} operand')
@pytest.mark.parametrize('name,expected', [
('Expenses:Postage', True),
('Expenses:Tax', True),
('Expenses:Tax:Sales', True),
('Expenses:Tax:VAT', True),
('Expenses:Taxes', False),
('Expenses:Other', False),
('Liabilities:Tax', False),
])
def test_account_in_condition(name, expected):
txn = testutil.Transaction(postings=[(name, 5)])
post, = data.Posting.from_txn(txn)
tester = rewrite.AccountTest('in', 'Expenses:Tax Expenses:Postage')
assert tester(post) == expected
@pytest.mark.parametrize('n', range(3, 12, 3))
@pytest.mark.parametrize('operator', CMP_OPS)
def test_date_condition(n, operator):
date = datetime.date(2020, n, n)
txn = testutil.Transaction(date=date, postings=[
('Income:Other', -5),
])
post, = data.Posting.from_txn(txn)
tester = rewrite.DateTest(operator, '2020-06-06')
assert tester(post) == eval(f'n {operator} 6')
@pytest.mark.parametrize('value', ['test', 'testvalue', 'testzed'])
@pytest.mark.parametrize('operator', CMP_OPS)
def test_metadata_condition(value, operator):
key = 'testkey'
operand = 'testvalue'
txn = testutil.Transaction(postings=[
('Income:Other', -5, {key: value}),
])
post, = data.Posting.from_txn(txn)
tester = rewrite.MetadataTest(key, operator, operand)
assert tester(post) == eval(f'value {operator} operand')
@pytest.mark.parametrize('value,expected', [
('Root', False),
('Root:Branch', True),
('Root:Branch:Leaf', True),
('Branch', False),
('RootBranch:Leaf', False),
(None, False),
])
def test_metadata_in_condition(value, expected):
key = 'testkey'
meta = {} if value is None else {key: value}
txn = testutil.Transaction(postings=[
('Income:Other', -5, meta),
])
post, = data.Posting.from_txn(txn)
tester = rewrite.MetadataTest(key, 'in', 'Root:Branch')
assert tester(post) == expected
@pytest.mark.parametrize('value', ['4.5', '4.75', '5'])
@pytest.mark.parametrize('operator', CMP_OPS)
def test_number_condition(value, operator):
operand = '4.75'
txn = testutil.Transaction(postings=[
('Expenses:Other', value),
])
post, = data.Posting.from_txn(txn)
tester = rewrite.NumberTest(operator, operand)
assert tester(post) == eval(f'value {operator} operand')
@pytest.mark.parametrize('subject,operand', [
('.account', 'Income:Other'),
('.date', '1990-05-10'),
('.number', '5.79'),
('testkey', 'testvalue'),
])
@pytest.mark.parametrize('operator', CMP_OPS)
def test_parse_good_condition(subject, operator, operand):
actual = rewrite.TestRegistry.parse(f'{subject}{operator}{operand}')
if subject == '.account':
assert isinstance(actual, rewrite.AccountTest)
assert actual.operand == operand
elif subject == '.date':
assert isinstance(actual, rewrite.DateTest)
assert actual.operand == datetime.date(1990, 5, 10)
elif subject == '.number':
assert isinstance(actual, rewrite.NumberTest)
assert actual.operand == Decimal(operand)
else:
assert isinstance(actual, rewrite.MetadataTest)
assert actual.key == 'testkey'
assert actual.operand == 'testvalue'
@pytest.mark.parametrize('cond_s', [
'.account = Income:Other', # Bad operator
'.account===Equity:Other', # Bad operand (`=Equity:Other` is not valid)
'.account in foo', # Bad operand
'.date == 1990-90-5', # Bad operand
'.date in 1990-9-9', # Bad operator
'.number > 0xff', # Bad operand
'.number in 16', # Bad operator
'units.number == 5', # Bad subject (syntax)
'.units == 5', # Bad subject (unknown)
])
def test_parse_bad_condition(cond_s):
with pytest.raises(errors.RewriteRuleConditionError):
rewrite.TestRegistry.parse(cond_s)
@pytest.mark.parametrize('value', ['Equity:Other', 'Income:Other'])
def test_account_set(value):
value = data.Account(value)
txn = testutil.Transaction(postings=[
('Expenses:Other', 5),
])
post, = data.Posting.from_txn(txn)
setter = rewrite.AccountSet('=', value)
assert setter(post) == ('account', value)
@pytest.mark.parametrize('key', ['aa', 'bb'])
def test_metadata_set(key):
txn_meta = {'filename': 'metadata_set', 'lineno': 100}
post_meta = {'aa': 'one', 'bb': 'two'}
meta = {'aa': 'one', 'bb': 'two'}
txn = testutil.Transaction(**txn_meta, postings=[
('Expenses:Other', 5, post_meta),
])
post, = data.Posting.from_txn(txn)
setter = rewrite.MetadataSet(key, '=', 'newvalue')
assert setter(post) == (key, 'newvalue')
@pytest.mark.parametrize('value', ['0.25', '-.5', '1.9'])
@pytest.mark.parametrize('currency', ['USD', 'EUR', 'INR'])
def test_number_set(value, currency):
txn = testutil.Transaction(postings=[
('Expenses:Other', 5, currency),
])
post, = data.Posting.from_txn(txn)
setter = rewrite.NumberSet('*=', value)
assert setter(post) == ('units', testutil.Amount(Decimal(value) * 5, currency))
@pytest.mark.parametrize('subject,operator,operand', [
('.account', '=', 'Income:Other'),
('.number', '*=', '.5'),
('.number', '*=', '-1'),
('testkey', '=', 'testvalue'),
])
def test_parse_good_set(subject, operator, operand):
actual = rewrite.SetRegistry.parse(f'{subject}{operator}{operand}')
if subject == '.account':
assert isinstance(actual, rewrite.AccountSet)
assert actual.value == operand
elif subject == '.number':
assert isinstance(actual, rewrite.NumberSet)
assert actual.value == Decimal(operand)
else:
assert isinstance(actual, rewrite.MetadataSet)
assert actual.key == subject
assert actual.value == operand
@pytest.mark.parametrize('set_s', [
'.account==Equity:Other', # Bad operand (`=Equity:Other` is not valid)
'.account*=2', # Bad operator
'.date = 2020-02-20', # Bad subject
'.number*=0xff', # Bad operand
'.number=5', # Bad operator
'testkey += foo', # Bad operator
'testkey *= 3', # Bad operator
])
def test_parse_bad_set(set_s):
with pytest.raises(errors.RewriteRuleActionError):
rewrite.SetRegistry.parse(set_s)
def test_good_rewrite_rule():
rule = rewrite.RewriteRule({
'if': ['.account in Income'],
'add': ['income-type = Other'],
})
txn = testutil.Transaction(postings=[
('Assets:Cash', 10),
('Income:Other', -10),
])
cash_post, inc_post = data.Posting.from_txn(txn)
assert not rule.match(cash_post)
assert rule.match(inc_post)
new_post, = rule.rewrite(inc_post)
assert new_post.account == 'Income:Other'
assert new_post.units == testutil.Amount(-10)
assert new_post.meta.pop('income-type', None) == 'Other'
assert new_post.meta
assert new_post.meta.date == txn.date
def test_complicated_rewrite_rule():
account = 'Income:Donations'
income_key = 'income-type'
income_type = 'Refund'
rule = rewrite.RewriteRule({
'if': ['.account == Expenses:Refunds'],
'project': [
f'.account = {account}',
'.number *= .8',
f'{income_key} = {income_type}',
],
'general': [
f'.account = {account}',
'.number *= .2',
f'{income_key} = {income_type}',
'project = Conservancy',
],
})
txn = testutil.Transaction(postings=[
('Assets:Cash', -12),
('Expenses:Refunds', 12, {'project': 'Bravo'}),
])
cash_post, src_post = data.Posting.from_txn(txn)
assert not rule.match(cash_post)
assert rule.match(src_post)
proj_post, gen_post = rule.rewrite(src_post)
assert proj_post.account == 'Income:Donations'
assert proj_post.units == testutil.Amount('9.60')
assert proj_post.meta[income_key] == income_type
assert proj_post.meta['project'] == 'Bravo'
assert gen_post.account == 'Income:Donations'
assert gen_post.units == testutil.Amount('2.40')
assert gen_post.meta[income_key] == income_type
assert gen_post.meta['project'] == 'Conservancy'
@pytest.mark.parametrize('source', [
# Account assignments
{'if': ['.account in Income Expenses'], 'then': ['.account = Equity']},
{'if': ['.account == Assets:PettyCash'], 'then': ['.account = Assets:Cash']},
{'if': ['.account == Liabilities:CreditCard'], 'then': ['.account = Liabilities:Visa']},
# Number splits
{'if': ['.date >= 2020-01-01'], 'a': ['.number *= 2'], 'b': ['.number *= -1']},
{'if': ['.date >= 2020-01-02'], 'a': ['.number *= .85'], 'b': ['.number *= .15']},
# Metadata assignment
{'if': ['a==1'], 'then': ['b=2', 'c=3']},
])
def test_valid_rewrite_rule(source):
assert rewrite.RewriteRule(source)
@pytest.mark.parametrize('source', [
# Incomplete rules
{},
{'if': ['.account in Equity']},
{'a': ['.account = Income:Other'], 'b': ['.account = Expenses:Other']},
# Cross-category account assignment
{'if': ['.date >= 2020-01-01'], 'then': ['.account = Assets:Cash']},
{'if': ['.account in Equity'], 'then': ['.account = Assets:Cash']},
# Number reallocation != 1
{'if': ['.date >= 2020-01-01'], 'then': ['.number *= .5']},
{'if': ['.date >= 2020-01-01'], 'a': ['k1=v1'], 'b': ['k2=v2']},
# Redundant assignments
{'if': ['.account in Income'],
'then': ['.account = Income:Other', '.account = Income:Other']},
{'if': ['.number > 0'],
'a': ['.number *= .5', '.number *= .5'],
'b': ['.number *= .5']},
])
def test_invalid_rewrite_rule(source):
with pytest.raises(errors.RewriteRuleValidationError):
rewrite.RewriteRule(source)
def test_rewrite_ruleset():
account = 'Income:CurrencyConversion'
ruleset = rewrite.RewriteRuleset(rewrite.RewriteRule(src) for src in [
{'if': ['.account == Expenses:CurrencyConversion'],
'rename': [f'.account = {account}']},
{'if': ['project == alpha', '.account != Assets:Cash'],
'cap': ['project = Alpha']},
])
txn = testutil.Transaction(project='alpha', postings=[
('Assets:Cash', -20),
('Expenses:CurrencyConversion', 18),
('Expenses:CurrencyConversion', 1, {'project': 'Conservancy'}),
('Expenses:BankingFees', 1),
])
posts = ruleset.rewrite(data.Posting.from_txn(txn))
post = next(posts)
assert post.account == 'Assets:Cash'
assert post.meta['project'] == 'alpha'
post = next(posts)
assert post.account == account
# Project not capitalized because the first rule took priority
assert post.meta['project'] == 'alpha'
post = next(posts)
assert post.account == account
assert post.meta['project'] == 'Conservancy'
post = next(posts)
assert post.account == 'Expenses:BankingFees'
assert post.meta['project'] == 'Alpha'
def test_ruleset_from_yaml_path():
yaml_path = testutil.test_path('userconfig/Rewrites01.yml')
assert rewrite.RewriteRuleset.from_yaml(yaml_path)
def test_ruleset_from_yaml_str():
with testutil.test_path('userconfig/Rewrites01.yml').open() as yaml_file:
yaml_s = yaml_file.read()
assert rewrite.RewriteRuleset.from_yaml(yaml_s)
def test_bad_ruleset_yaml_path():
yaml_path = testutil.test_path('repository/Projects/project-data.yml')
with pytest.raises(errors.RewriteRuleLoadError):
rewrite.RewriteRuleset.from_yaml(yaml_path)
@pytest.mark.parametrize('source', [
# Wrong root objects
1,
2.3,
True,
None,
{},
'string',
['a'],
[['b']],
# Rules have wrong type
[{'if': '.account in Equity', 'add': ['testkey = value']}],
[{'if': ['.account in Equity'], 'add': 'testkey = value'}],
])
def test_bad_ruleset_yaml_str(source):
yaml_doc = yaml.safe_dump(source)
with pytest.raises(errors.RewriteRuleLoadError):
rewrite.RewriteRuleset.from_yaml(yaml_doc)
|