plugin: Ensure run() can deal with all possible directives.

This commit is contained in:
Brett Smith 2020-03-31 14:30:49 -04:00
parent 87f35fe27f
commit 5566672cd6
2 changed files with 37 additions and 2 deletions

View file

@ -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]:

View file

@ -14,10 +14,16 @@
# 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 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