2020-03-19 19:49:56 +00:00
|
|
|
"""Test main plugin"""
|
|
|
|
# Copyright © 2020 Brett Smith
|
2021-01-08 21:57:43 +00:00
|
|
|
# License: AGPLv3-or-later WITH Beancount-Plugin-Additional-Permission-1.0
|
2020-03-19 19:49:56 +00:00
|
|
|
#
|
2021-01-08 21:57:43 +00:00
|
|
|
# Full copyright and licensing details can be found at toplevel file
|
|
|
|
# LICENSE.txt in the repository.
|
2020-03-19 19:49:56 +00:00
|
|
|
|
2020-03-31 18:30:49 +00:00
|
|
|
import datetime
|
|
|
|
|
|
|
|
from decimal import Decimal
|
|
|
|
|
2020-03-19 19:49:56 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from . import testutil
|
|
|
|
|
2020-03-31 18:30:49 +00:00
|
|
|
import beancount.core.data as bc_data
|
|
|
|
|
2020-03-19 21:23:27 +00:00
|
|
|
from conservancy_beancount import beancount_types, errors as errormod, plugin
|
2020-03-19 19:49:56 +00:00
|
|
|
|
|
|
|
HOOK_REGISTRY = plugin.HookRegistry()
|
|
|
|
|
2020-03-19 21:23:27 +00:00
|
|
|
class NonError(errormod.Error):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-03-19 19:49:56 +00:00
|
|
|
class TransactionHook:
|
|
|
|
DIRECTIVE = beancount_types.Transaction
|
|
|
|
HOOK_GROUPS = frozenset()
|
|
|
|
|
2020-03-19 21:23:27 +00:00
|
|
|
def __init__(self, config):
|
|
|
|
self.config = config
|
|
|
|
|
2020-03-19 19:49:56 +00:00
|
|
|
def run(self, txn):
|
|
|
|
assert False, "something called base class run method"
|
|
|
|
|
|
|
|
|
2020-03-19 21:23:27 +00:00
|
|
|
@HOOK_REGISTRY.add_hook
|
|
|
|
class ConfigurationError(TransactionHook):
|
|
|
|
HOOK_GROUPS = frozenset(['unconfigured'])
|
|
|
|
|
|
|
|
def __init__(self, config):
|
|
|
|
raise errormod.ConfigurationError("testing error")
|
|
|
|
|
|
|
|
|
2020-03-19 19:49:56 +00:00
|
|
|
@HOOK_REGISTRY.add_hook
|
|
|
|
class TransactionError(TransactionHook):
|
|
|
|
HOOK_GROUPS = frozenset(['configured'])
|
|
|
|
|
|
|
|
def run(self, txn):
|
2020-03-19 21:23:27 +00:00
|
|
|
return [NonError('txn:{}'.format(id(txn)), txn)]
|
2020-03-19 19:49:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
@HOOK_REGISTRY.add_hook
|
|
|
|
class PostingError(TransactionHook):
|
|
|
|
HOOK_GROUPS = frozenset(['configured', 'posting'])
|
|
|
|
|
|
|
|
def run(self, txn):
|
2020-03-19 21:23:27 +00:00
|
|
|
return [NonError('post:{}'.format(id(post)), txn)
|
|
|
|
for post in txn.postings]
|
2020-03-19 19:49:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def config_map():
|
|
|
|
return {}
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def easy_entries():
|
|
|
|
return [
|
|
|
|
testutil.Transaction(postings=[
|
|
|
|
('Income:Donations', -25),
|
|
|
|
('Assets:Cash', 25),
|
|
|
|
]),
|
|
|
|
testutil.Transaction(postings=[
|
|
|
|
('Expenses:General', 10),
|
|
|
|
('Liabilites:CreditCard', -10),
|
|
|
|
]),
|
|
|
|
]
|
|
|
|
|
|
|
|
def map_errors(errors):
|
|
|
|
retval = {}
|
2020-03-19 21:23:27 +00:00
|
|
|
for error in errors:
|
|
|
|
key, _, errid = error.message.partition(':')
|
2020-03-19 19:49:56 +00:00
|
|
|
retval.setdefault(key, set()).add(errid)
|
|
|
|
return retval
|
|
|
|
|
2020-03-19 20:00:22 +00:00
|
|
|
@pytest.mark.parametrize('group_str,expected', [
|
|
|
|
(None, [TransactionError, PostingError]),
|
|
|
|
('', [TransactionError, PostingError]),
|
|
|
|
('all', [TransactionError, PostingError]),
|
|
|
|
('Transaction', [TransactionError, PostingError]),
|
|
|
|
('-posting', [TransactionError]),
|
|
|
|
('-configured posting', [PostingError]),
|
|
|
|
('configured -posting', [TransactionError]),
|
|
|
|
])
|
|
|
|
def test_registry_group_by_directive(group_str, expected):
|
|
|
|
args = () if group_str is None else (group_str,)
|
2020-03-19 20:42:13 +00:00
|
|
|
actual = {hook for _, hook in HOOK_REGISTRY.group_by_directive(*args)}
|
2020-03-19 20:00:22 +00:00
|
|
|
assert actual.issuperset(expected)
|
2020-03-19 20:11:29 +00:00
|
|
|
if len(expected) == 1:
|
|
|
|
assert not (TransactionError in actual and PostingError in actual)
|
2020-03-19 19:49:56 +00:00
|
|
|
|
|
|
|
def test_registry_unknown_group_name():
|
|
|
|
with pytest.raises(ValueError):
|
2020-03-19 20:42:13 +00:00
|
|
|
next(HOOK_REGISTRY.group_by_directive('UnKnownTestGroup'))
|
2020-03-19 19:49:56 +00:00
|
|
|
|
2020-04-07 17:31:09 +00:00
|
|
|
def test_registry_load_included_hooks():
|
|
|
|
registry = plugin.HookRegistry()
|
|
|
|
assert not list(registry.group_by_directive())
|
|
|
|
registry.load_included_hooks()
|
|
|
|
actual = {hook.__name__ for key, hook in registry.group_by_directive() if key == 'Transaction'}
|
|
|
|
assert len(actual) >= 5
|
|
|
|
assert 'MetaProject' in actual
|
|
|
|
assert 'MetaRTLinks' in actual
|
|
|
|
|
2020-03-19 19:49:56 +00:00
|
|
|
def test_run_with_multiple_hooks(easy_entries, config_map):
|
|
|
|
out_entries, errors = plugin.run(easy_entries, config_map, '', HOOK_REGISTRY)
|
|
|
|
assert len(out_entries) == 2
|
|
|
|
errmap = map_errors(errors)
|
|
|
|
assert len(errmap.get('txn', '')) == 2
|
|
|
|
assert len(errmap.get('post', '')) == 4
|
|
|
|
|
|
|
|
def test_run_with_one_hook(easy_entries, config_map):
|
|
|
|
out_entries, errors = plugin.run(easy_entries, config_map, 'posting', HOOK_REGISTRY)
|
|
|
|
assert len(out_entries) == 2
|
|
|
|
errmap = map_errors(errors)
|
|
|
|
assert len(errmap.get('txn', '')) == 0
|
|
|
|
assert len(errmap.get('post', '')) == 4
|
2020-03-31 18:30:49 +00:00
|
|
|
|
|
|
|
def test_run_on_all_directives(config_map):
|
|
|
|
meta = {
|
|
|
|
'filename': __file__,
|
|
|
|
'lineno': 125,
|
|
|
|
}
|
|
|
|
date = datetime.date(2020, 3, 1)
|
|
|
|
acct = 'Assets:Cash'
|
|
|
|
usd = 'USD'
|
|
|
|
entries = [
|
|
|
|
bc_data.Open(meta, date, acct, [usd], None),
|
|
|
|
bc_data.Close(meta, date.replace(year=date.year + 1), acct),
|
|
|
|
bc_data.Commodity(meta, date, usd),
|
|
|
|
bc_data.Pad(meta, date, acct, 'Income:Other'),
|
|
|
|
bc_data.Balance(meta, date, acct, 0, None, None),
|
|
|
|
bc_data.Transaction(meta, date, None, None, 'found cash', {}, {}, []),
|
|
|
|
bc_data.Note(meta, date, acct, 'test note'),
|
|
|
|
bc_data.Event(meta, date, 'test event', 'Test Event 1'),
|
|
|
|
bc_data.Query(meta, date, 'test query', ''),
|
|
|
|
bc_data.Price(meta, date, 'EUR', (Decimal('1.10508'), usd)),
|
|
|
|
bc_data.Document(meta, date, acct, '/TestDocument.txt', None, None),
|
|
|
|
bc_data.Custom(meta, date, 'test custom', ['test value']),
|
|
|
|
]
|
|
|
|
out_entries, errors = plugin.run(entries, config_map, '-all', HOOK_REGISTRY)
|
|
|
|
assert out_entries is entries
|
|
|
|
assert not errors
|