File diff 4de5df9035f6 → f508df06c162
tests/test_reports_rewrite.py
Show inline comments
...
 
@@ -12,13 +12,13 @@ import pytest
 
from decimal import Decimal
 

	
 
import yaml
 

	
 
from . import testutil
 

	
 
from conservancy_beancount import data
 
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)
...
 
@@ -128,13 +128,13 @@ def test_parse_good_condition(subject, operator, operand):
 
    '.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(ValueError):
 
    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=[
...
 
@@ -192,13 +192,13 @@ def test_parse_good_set(subject, operator, operand):
 
    '.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(ValueError):
 
    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'],
...
 
@@ -268,32 +268,27 @@ def test_valid_rewrite_rule(source):
 

	
 
@pytest.mark.parametrize('source', [
 
    # Incomplete rules
 
    {},
 
    {'if': ['.account in Equity']},
 
    {'a': ['.account = Income:Other'], 'b': ['.account = Expenses:Other']},
 
    # Condition/assignment mixup
 
    {'if': ['.account = Equity:Other'], 'then': ['equity-type = other']},
 
    {'if': ['.account == Equity:Other'], 'then': ['equity-type != 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']},
 
    # Date assignment
 
    {'if': ['.date == 2020-01-01'], 'then': ['.date = 2020-02-02']},
 
    # 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(ValueError):
 
    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'],
...
 
@@ -330,27 +325,27 @@ 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(ValueError):
 
    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']],
 
    ['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(ValueError):
 
    with pytest.raises(errors.RewriteRuleLoadError):
 
        rewrite.RewriteRuleset.from_yaml(yaml_doc)