meta_entity: Don't validate entries out of date range.

This commit is contained in:
Brett Smith 2020-05-19 10:30:50 -04:00
parent 47235f694c
commit b8d76ec5a0
5 changed files with 20 additions and 6 deletions

View file

@ -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)
)

View file

@ -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.

View file

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

View file

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

View file

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