txn_date: Check more filenames.

This follows up on the recent change to books.Loader.
This commit is contained in:
Brett Smith 2021-02-26 17:02:59 -05:00
parent ca12496880
commit 9598b29ba7
3 changed files with 27 additions and 19 deletions

View file

@ -5,6 +5,7 @@
# Full copyright and licensing details can be found at toplevel file # Full copyright and licensing details can be found at toplevel file
# LICENSE.txt in the repository. # LICENSE.txt in the repository.
import os.path
import re import re
from ..beancount_types import ( from ..beancount_types import (
@ -24,11 +25,11 @@ class TransactionDate(core.TransactionHook):
"books dir setting is required to check transaction dates", "books dir setting is required to check transaction dates",
) )
books_pat = re.escape(str(books_path)) books_pat = re.escape(str(books_path))
self.filename_re = re.compile(rf'^{books_pat}/(\d{{4,}})\.beancount$') self.filename_re = re.compile(rf'^{books_pat}/(\d{{4,}})[{os.path.sep}_.]')
self.fy = config.fiscal_year_begin() self.fy = config.fiscal_year_begin()
def run(self, txn: Transaction) -> errormod.Iter: def run(self, txn: Transaction) -> errormod.Iter:
match = self.filename_re.fullmatch(txn.meta.get('filename', '')) match = self.filename_re.match(txn.meta.get('filename', ''))
if match is None: if match is None:
return return
file_fy = int(match.group(1)) file_fy = int(match.group(1))

View file

@ -5,7 +5,7 @@ from setuptools import setup
setup( setup(
name='conservancy_beancount', name='conservancy_beancount',
description="Plugin, library, and reports for reading Conservancy's books", description="Plugin, library, and reports for reading Conservancy's books",
version='1.18.3', version='1.18.4',
author='Software Freedom Conservancy', author='Software Freedom Conservancy',
author_email='info@sfconservancy.org', author_email='info@sfconservancy.org',
license='GNU AGPLv3+', license='GNU AGPLv3+',

View file

@ -5,6 +5,8 @@
# Full copyright and licensing details can be found at toplevel file # Full copyright and licensing details can be found at toplevel file
# LICENSE.txt in the repository. # LICENSE.txt in the repository.
import itertools
from datetime import date from datetime import date
import pytest import pytest
@ -18,30 +20,35 @@ from conservancy_beancount.plugin import txn_date as hookmod
BOOKS_PATH = testutil.test_path('books') BOOKS_PATH = testutil.test_path('books')
CONFIG = testutil.TestConfig(books_path=BOOKS_PATH) CONFIG = testutil.TestConfig(books_path=BOOKS_PATH)
HOOK = hookmod.TransactionDate(CONFIG) HOOK = hookmod.TransactionDate(CONFIG)
SUFFIXES = itertools.cycle([
@pytest.mark.parametrize('txn_date,fyear', [ '',
(date(2016, 1, 1), 2015), '_sidefile',
(date(2016, 2, 29), 2015), '/subdirfile',
(date(2016, 3, 1), 2016),
(date(2016, 12, 31), 2016),
(date(2017, 2, 28), 2016),
(date(2017, 3, 1), 2017),
]) ])
def test_good_txn(txn_date, fyear):
filename = str(BOOKS_PATH / f'{fyear}.beancount') @pytest.mark.parametrize('txn_date,fyear,suffix', [
(date(2016, 1, 1), 2015, next(SUFFIXES)),
(date(2016, 2, 29), 2015, next(SUFFIXES)),
(date(2016, 3, 1), 2016, next(SUFFIXES)),
(date(2016, 12, 31), 2016, next(SUFFIXES)),
(date(2017, 2, 28), 2016, next(SUFFIXES)),
(date(2017, 3, 1), 2017, next(SUFFIXES)),
])
def test_good_txn(txn_date, fyear, suffix):
filename = str(BOOKS_PATH / f'{fyear}{suffix}.beancount')
txn = testutil.Transaction(date=txn_date, filename=filename, postings=[ txn = testutil.Transaction(date=txn_date, filename=filename, postings=[
('Assets:Cash', 5), ('Assets:Cash', 5),
('Income:Donations', -5), ('Income:Donations', -5),
]) ])
assert not list(HOOK.run(txn)) assert not list(HOOK.run(txn))
@pytest.mark.parametrize('txn_date,fyear', [ @pytest.mark.parametrize('txn_date,fyear,suffix', [
(date(2018, 1, 1), 2017), (date(2018, 1, 1), 2017, next(SUFFIXES)),
(date(2018, 12, 31), 2018), (date(2018, 12, 31), 2018, next(SUFFIXES)),
(date(2019, 3, 1), 2019), (date(2019, 3, 1), 2019, next(SUFFIXES)),
]) ])
def test_bad_txn(txn_date, fyear): def test_bad_txn(txn_date, fyear, suffix):
filename = str(BOOKS_PATH / '2020.beancount') filename = str(BOOKS_PATH / f'2020{suffix}.beancount')
txn = testutil.Transaction(date=txn_date, filename=filename, postings=[ txn = testutil.Transaction(date=txn_date, filename=filename, postings=[
('Assets:Cash', 5), ('Assets:Cash', 5),
('Income:Donations', -5), ('Income:Donations', -5),