From b8d76ec5a0a32d2d8568c1309366b2b139d88de5 Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Tue, 19 May 2020 10:30:50 -0400 Subject: [PATCH] meta_entity: Don't validate entries out of date range. --- conservancy_beancount/plugin/core.py | 4 ++-- conservancy_beancount/plugin/meta_approval.py | 4 ++-- conservancy_beancount/plugin/meta_entity.py | 2 +- conservancy_beancount/plugin/meta_project.py | 2 +- tests/test_meta_entity.py | 14 ++++++++++++++ 5 files changed, 20 insertions(+), 6 deletions(-) diff --git a/conservancy_beancount/plugin/core.py b/conservancy_beancount/plugin/core.py index 57db6a5..7733368 100644 --- a/conservancy_beancount/plugin/core.py +++ b/conservancy_beancount/plugin/core.py @@ -178,7 +178,7 @@ class TransactionHook(Hook[Transaction]): DIRECTIVE = Transaction TXN_DATE_RANGE: _GenericRange = _GenericRange(DEFAULT_START_DATE, DEFAULT_STOP_DATE) - def _run_on_txn(self, txn: Transaction) -> bool: + def _run_on_txn(self, txn: Transaction, skip_flags: str='!') -> bool: """Check whether we should run on a given transaction This method implements our usual checks for whether or not a hook @@ -186,7 +186,7 @@ class TransactionHook(Hook[Transaction]): their own implementations. See _PostingHook below for an example. """ return ( - txn.flag != '!' + txn.flag not in skip_flags and txn.date in self.TXN_DATE_RANGE and not data.is_opening_balance_txn(txn) ) diff --git a/conservancy_beancount/plugin/meta_approval.py b/conservancy_beancount/plugin/meta_approval.py index be3a24b..3158e5b 100644 --- a/conservancy_beancount/plugin/meta_approval.py +++ b/conservancy_beancount/plugin/meta_approval.py @@ -30,9 +30,9 @@ class MetaApproval(core._RequireLinksPostingMetadataHook): def __init__(self, config: configmod.Config) -> None: self.payment_threshold = -config.payment_threshold() - def _run_on_txn(self, txn: Transaction) -> bool: + def _run_on_txn(self, txn: Transaction, skip_flags: str='!') -> bool: return ( - super()._run_on_txn(txn) + super()._run_on_txn(txn, skip_flags) # approval is required when funds leave a cash equivalent asset, # UNLESS that transaction is a transfer to another asset, # or paying off a credit card. diff --git a/conservancy_beancount/plugin/meta_entity.py b/conservancy_beancount/plugin/meta_entity.py index 4370170..10827e2 100644 --- a/conservancy_beancount/plugin/meta_entity.py +++ b/conservancy_beancount/plugin/meta_entity.py @@ -67,7 +67,7 @@ class MetaEntity(core.TransactionHook): return entity, self.ENTITY_RE.match(entity) is not None def run(self, txn: Transaction) -> errormod.Iter: - if data.is_opening_balance_txn(txn): + if not self._run_on_txn(txn, ''): return txn_entity, txn_entity_ok = self._check_entity(txn.meta, txn.payee) if txn_entity_ok is False: diff --git a/conservancy_beancount/plugin/meta_project.py b/conservancy_beancount/plugin/meta_project.py index 0014706..982b893 100644 --- a/conservancy_beancount/plugin/meta_project.py +++ b/conservancy_beancount/plugin/meta_project.py @@ -102,7 +102,7 @@ class MetaProject(core._NormalizePostingMetadataHook): else: raise errormod.InvalidMetadataError(txn, self.METADATA_KEY, None, post) - def _run_on_txn(self, txn: Transaction) -> bool: + def _run_on_txn(self, txn: Transaction, skip_flags: str='') -> bool: return txn.date in self.TXN_DATE_RANGE def run(self, txn: Transaction) -> errormod.Iter: diff --git a/tests/test_meta_entity.py b/tests/test_meta_entity.py index 5cd4c6d..e077a57 100644 --- a/tests/test_meta_entity.py +++ b/tests/test_meta_entity.py @@ -212,3 +212,17 @@ def test_which_accounts_required_on(hook, account, required): def test_not_required_on_opening(hook): txn = testutil.OpeningBalance() assert not list(hook.run(txn)) + +@pytest.mark.parametrize('date,need_value', [ + (testutil.EXTREME_FUTURE_DATE, False), + (testutil.FUTURE_DATE, True), + (testutil.FY_START_DATE, True), + (testutil.FY_MID_DATE, True), + (testutil.PAST_DATE, False), +]) +def test_required_by_date(hook, date, need_value): + txn = testutil.Transaction(date=date, postings=[ + ('Income:Donations', -10), + ('Assets:Checking', 10), + ]) + assert any(hook.run(txn)) == need_value