diff --git a/conservancy_beancount/plugin/__init__.py b/conservancy_beancount/plugin/__init__.py index 20ded0b..41c7daa 100644 --- a/conservancy_beancount/plugin/__init__.py +++ b/conservancy_beancount/plugin/__init__.py @@ -104,7 +104,10 @@ def run( hook_registry: HookRegistry=HOOK_REGISTRY, ) -> Tuple[List[Directive], List[Error]]: errors: List[Error] = [] - hooks: Dict[HookName, List[Hook]] = {} + hooks: Dict[HookName, List[Hook]] = { + # mypy thinks NamedTuples don't have __name__ but they do at runtime. + t.__name__: [] for t in bc_data.ALL_DIRECTIVES # type:ignore[attr-defined] + } user_config = configmod.Config() for key, hook_type in hook_registry.group_by_directive(config): try: @@ -112,7 +115,7 @@ def run( except Error as error: errors.append(error) else: - hooks.setdefault(key, []).append(hook) + hooks[key].append(hook) for entry in entries: entry_type = type(entry).__name__ for hook in hooks[entry_type]: diff --git a/tests/test_plugin.py b/tests/test_plugin.py index f64d1b9..9ae6586 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -14,10 +14,16 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import datetime + +from decimal import Decimal + import pytest from . import testutil +import beancount.core.data as bc_data + from conservancy_beancount import beancount_types, errors as errormod, plugin HOOK_REGISTRY = plugin.HookRegistry() @@ -119,3 +125,29 @@ def test_run_with_one_hook(easy_entries, config_map): errmap = map_errors(errors) assert len(errmap.get('txn', '')) == 0 assert len(errmap.get('post', '')) == 4 + +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