filters: Add audit_date() function.
This commit is contained in:
parent
944c19da8d
commit
8250f0a8ef
2 changed files with 32 additions and 0 deletions
|
@ -14,6 +14,7 @@
|
|||
# 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
|
||||
import re
|
||||
|
||||
from beancount.core import data as bc_data
|
||||
|
@ -39,6 +40,13 @@ from .beancount_types import (
|
|||
Postings = Iterable[data.Posting]
|
||||
Regexp = Union[str, Pattern]
|
||||
|
||||
def audit_date(entries: Entries) -> Optional[datetime.date]:
|
||||
for entry in entries:
|
||||
if (isinstance(entry, bc_data.Custom)
|
||||
and entry.type == 'conservancy_beancount_audit'): # type:ignore[attr-defined]
|
||||
return entry.date
|
||||
return None
|
||||
|
||||
def filter_meta_equal(postings: Postings, key: MetaKey, value: MetaValue) -> Postings:
|
||||
for post in postings:
|
||||
try:
|
||||
|
|
|
@ -22,6 +22,8 @@ from . import testutil
|
|||
|
||||
from datetime import date
|
||||
|
||||
from beancount.core import data as bc_data
|
||||
|
||||
from conservancy_beancount import data
|
||||
from conservancy_beancount import filters
|
||||
|
||||
|
@ -159,3 +161,25 @@ def test_remove_opening_balance_txn(opening_txn):
|
|||
for entry in entries
|
||||
for post in getattr(entry, 'postings', ())
|
||||
)
|
||||
|
||||
@pytest.mark.parametrize('entry', [
|
||||
bc_data.Custom({}, testutil.FY_START_DATE, 'conservancy_beancount_audit', []),
|
||||
None,
|
||||
])
|
||||
def test_audit_date(entry):
|
||||
dates = testutil.date_seq()
|
||||
entries = [
|
||||
bc_data.Open({}, next(dates), 'Income:Donations', ['USD'], None),
|
||||
bc_data.Open({}, next(dates), 'Assets:Cash', ['USD'], None),
|
||||
testutil.Transaction(postings=[
|
||||
('Income:Donations', -10),
|
||||
('Assets:Cash', 10),
|
||||
]),
|
||||
]
|
||||
if entry is not None:
|
||||
entries.append(entry)
|
||||
actual = filters.audit_date(entries)
|
||||
if entry is None:
|
||||
assert actual is None
|
||||
else:
|
||||
assert actual == entry.date
|
||||
|
|
Loading…
Reference in a new issue