2020-03-05 22:41:58 +00:00
|
|
|
"""Test main plugin run loop"""
|
|
|
|
# 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 pytest
|
|
|
|
|
|
|
|
from . import testutil
|
|
|
|
|
|
|
|
from conservancy_beancount import plugin
|
|
|
|
|
|
|
|
CONFIG_MAP = {}
|
2020-03-06 14:22:55 +00:00
|
|
|
HOOK_REGISTRY = plugin.HookRegistry()
|
2020-03-05 22:41:58 +00:00
|
|
|
|
2020-03-06 14:22:55 +00:00
|
|
|
@HOOK_REGISTRY.add_hook
|
2020-03-05 22:41:58 +00:00
|
|
|
class TransactionCounter:
|
2020-03-06 00:31:07 +00:00
|
|
|
HOOK_GROUPS = frozenset(['Transaction', 'counter'])
|
2020-03-05 22:41:58 +00:00
|
|
|
|
2020-03-05 22:48:59 +00:00
|
|
|
def run(self, txn):
|
2020-03-06 00:31:07 +00:00
|
|
|
return ['txn:{}'.format(id(txn))]
|
2020-03-05 22:41:58 +00:00
|
|
|
|
|
|
|
|
2020-03-06 14:22:55 +00:00
|
|
|
@HOOK_REGISTRY.add_hook
|
2020-03-05 22:41:58 +00:00
|
|
|
class PostingCounter(TransactionCounter):
|
2020-03-06 00:31:07 +00:00
|
|
|
HOOK_GROUPS = frozenset(['Posting', 'counter'])
|
2020-03-05 22:41:58 +00:00
|
|
|
|
2020-03-05 22:48:59 +00:00
|
|
|
def run(self, txn, post):
|
2020-03-06 00:31:07 +00:00
|
|
|
return ['post:{}'.format(id(post))]
|
|
|
|
|
2020-03-05 22:41:58 +00:00
|
|
|
|
2020-03-06 00:31:07 +00:00
|
|
|
def map_errors(errors):
|
|
|
|
retval = {}
|
|
|
|
for errkey in errors:
|
|
|
|
key, _, errid = errkey.partition(':')
|
|
|
|
retval.setdefault(key, set()).add(errid)
|
|
|
|
return retval
|
2020-03-05 22:41:58 +00:00
|
|
|
|
|
|
|
def test_with_multiple_hooks():
|
|
|
|
in_entries = [
|
|
|
|
testutil.Transaction(postings=[
|
|
|
|
('Income:Donations', -25),
|
|
|
|
('Assets:Cash', 25),
|
|
|
|
]),
|
|
|
|
testutil.Transaction(postings=[
|
|
|
|
('Expenses:General', 10),
|
|
|
|
('Liabilites:CreditCard', -10),
|
|
|
|
]),
|
|
|
|
]
|
2020-03-06 14:22:55 +00:00
|
|
|
out_entries, errors = plugin.run(in_entries, CONFIG_MAP, '', HOOK_REGISTRY)
|
2020-03-05 22:41:58 +00:00
|
|
|
assert len(out_entries) == 2
|
2020-03-06 00:31:07 +00:00
|
|
|
errmap = map_errors(errors)
|
|
|
|
assert len(errmap.get('txn', '')) == 2
|
|
|
|
assert len(errmap.get('post', '')) == 4
|
2020-03-05 22:41:58 +00:00
|
|
|
|
|
|
|
def test_with_posting_hooks_only():
|
|
|
|
in_entries = [
|
|
|
|
testutil.Transaction(postings=[
|
|
|
|
('Income:Donations', -25),
|
|
|
|
('Assets:Cash', 25),
|
|
|
|
]),
|
|
|
|
testutil.Transaction(postings=[
|
|
|
|
('Expenses:General', 10),
|
|
|
|
('Liabilites:CreditCard', -10),
|
|
|
|
]),
|
|
|
|
]
|
2020-03-06 14:22:55 +00:00
|
|
|
out_entries, errors = plugin.run(in_entries, CONFIG_MAP, 'Posting', HOOK_REGISTRY)
|
2020-03-05 22:41:58 +00:00
|
|
|
assert len(out_entries) == 2
|
2020-03-06 00:31:07 +00:00
|
|
|
errmap = map_errors(errors)
|
|
|
|
assert len(errmap.get('txn', '')) == 0
|
|
|
|
assert len(errmap.get('post', '')) == 4
|