Changeset - 9598b29ba791
[Not reviewed]
0 3 0
Brett Smith - 4 years ago 2021-02-26 22:02:59
brettcsmith@brettcsmith.org
txn_date: Check more filenames.

This follows up on the recent change to books.Loader.
3 files changed with 26 insertions and 18 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/plugin/txn_date.py
Show inline comments
 
"""txn_date.py - Validate transactions are entered in the right file by date"""
 
# Copyright © 2020  Brett Smith
 
# License: AGPLv3-or-later WITH Beancount-Plugin-Additional-Permission-1.0
 
#
 
# Full copyright and licensing details can be found at toplevel file
 
# LICENSE.txt in the repository.
 

	
 
import os.path
 
import re
 

	
 
from ..beancount_types import (
 
    Transaction,
 
)
 

	
 
from . import core
 
from .. import config as configmod
 
from .. import errors as errormod
 
from .. import ranges
 

	
 
class TransactionDate(core.TransactionHook):
 
    def __init__(self, config: configmod.Config) -> None:
 
        books_path = config.books_path()
 
        if books_path is None:
 
            raise errormod.ConfigurationError(
 
                "books dir setting is required to check transaction dates",
 
            )
 
        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()
 

	
 
    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:
 
            return
 
        file_fy = int(match.group(1))
 
        txn_fy = self.fy.for_date(txn.date)
 
        if file_fy != txn_fy:
 
            yield errormod.Error(
 
                f"transaction dated in FY{txn_fy} entered in FY{file_fy} books",
 
                txn,
 
            )
setup.py
Show inline comments
 
#!/usr/bin/env python3
 

	
 
from setuptools import setup
 

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

	
 
    install_requires=[
 
        'babel>=2.6',  # Debian:python3-babel
 
        'beancount>=2.2',  # Debian:beancount
 
        'GitPython>=2.0',  # Debian:python3-git
 
        # 1.4.1 crashes when trying to save some documents.
 
        'odfpy>=1.4.0,!=1.4.1',  # Debian:python3-odf
 
        'pdfminer.six>=20200101',
 
        'PyYAML>=3.0',  # Debian:python3-yaml
 
        'regex',  # Debian:python3-regex
 
        'rt>=2.0',
 
    ],
 
    setup_requires=[
 
        'pytest-mypy',
 
        'pytest-runner',  # Debian:python3-pytest-runner
 
    ],
 
    tests_require=[
 
        'mypy>=0.770',  # Debian:python3-mypy
 
        'pytest',  # Debian:python3-pytest
 
    ],
 

	
 
    packages=[
 
        'conservancy_beancount',
 
        'conservancy_beancount.pdfforms',
 
        'conservancy_beancount.pdfforms.extract',
 
        'conservancy_beancount.plugin',
 
        'conservancy_beancount.reconcile',
 
        'conservancy_beancount.reports',
 
        'conservancy_beancount.tools',
 
    ],
 
    entry_points={
 
        'console_scripts': [
 
            'accrual-report = conservancy_beancount.reports.accrual:entry_point',
 
            'assemble-audit-reports = conservancy_beancount.tools.audit_report:entry_point',
 
            'balance-sheet-report = conservancy_beancount.reports.balance_sheet:entry_point',
 
            'budget-report = conservancy_beancount.reports.budget:entry_point',
 
            'bean-sort = conservancy_beancount.tools.sort_entries:entry_point',
 
            'extract-odf-links = conservancy_beancount.tools.extract_odf_links:entry_point',
 
            'fund-report = conservancy_beancount.reports.fund:entry_point',
 
            'ledger-report = conservancy_beancount.reports.ledger:entry_point',
 
            'opening-balances = conservancy_beancount.tools.opening_balances:entry_point',
 
            'pdfform-extract = conservancy_beancount.pdfforms.extract:entry_point',
 
            'pdfform-extract-irs990scheduleA = conservancy_beancount.pdfforms.extract.irs990scheduleA:entry_point',
 
            'pdfform-fill = conservancy_beancount.pdfforms.fill:entry_point',
 
            'query-report = conservancy_beancount.reports.query:entry_point',
tests/test_plugin_txn_date.py
Show inline comments
 
"""test_txn_date.py - Unit tests for transaction date validation"""
 
# Copyright © 2020  Brett Smith
 
# License: AGPLv3-or-later WITH Beancount-Plugin-Additional-Permission-1.0
 
#
 
# Full copyright and licensing details can be found at toplevel file
 
# LICENSE.txt in the repository.
 

	
 
import itertools
 

	
 
from datetime import date
 

	
 
import pytest
 

	
 
from . import testutil
 

	
 
from conservancy_beancount import config as configmod
 
from conservancy_beancount import errors as errormod
 
from conservancy_beancount.plugin import txn_date as hookmod
 

	
 
BOOKS_PATH = testutil.test_path('books')
 
CONFIG = testutil.TestConfig(books_path=BOOKS_PATH)
 
HOOK = hookmod.TransactionDate(CONFIG)
 
SUFFIXES = itertools.cycle([
 
    '',
 
    '_sidefile',
 
    '/subdirfile',
 
])
 

	
 
@pytest.mark.parametrize('txn_date,fyear', [
 
    (date(2016, 1, 1), 2015),
 
    (date(2016, 2, 29), 2015),
 
    (date(2016, 3, 1), 2016),
 
    (date(2016, 12, 31), 2016),
 
    (date(2017, 2, 28), 2016),
 
    (date(2017, 3, 1), 2017),
 
@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):
 
    filename = str(BOOKS_PATH / f'{fyear}.beancount')
 
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=[
 
        ('Assets:Cash', 5),
 
        ('Income:Donations', -5),
 
    ])
 
    assert not list(HOOK.run(txn))
 

	
 
@pytest.mark.parametrize('txn_date,fyear', [
 
    (date(2018, 1, 1), 2017),
 
    (date(2018, 12, 31), 2018),
 
    (date(2019, 3, 1), 2019),
 
@pytest.mark.parametrize('txn_date,fyear,suffix', [
 
    (date(2018, 1, 1), 2017, next(SUFFIXES)),
 
    (date(2018, 12, 31), 2018, next(SUFFIXES)),
 
    (date(2019, 3, 1), 2019, next(SUFFIXES)),
 
])
 
def test_bad_txn(txn_date, fyear):
 
    filename = str(BOOKS_PATH / '2020.beancount')
 
def test_bad_txn(txn_date, fyear, suffix):
 
    filename = str(BOOKS_PATH / f'2020{suffix}.beancount')
 
    txn = testutil.Transaction(date=txn_date, filename=filename, postings=[
 
        ('Assets:Cash', 5),
 
        ('Income:Donations', -5),
 
    ])
 
    errors = list(HOOK.run(txn))
 
    assert len(errors) == 1
 
    assert errors[0].message == f"transaction dated in FY{fyear} entered in FY2020 books"
 

	
 
@pytest.mark.parametrize('path_s', [
 
    'books/2020.beancount',
 
    'historical/2020.beancount',
 
    'definitions.beancount',
 
])
 
def test_outer_transactions_not_checked(path_s):
 
    txn_date = date(1900, 6, 15)
 
    filename = str(BOOKS_PATH / path_s)
 
    txn = testutil.Transaction(date=txn_date, filename=filename, postings=[
 
        ('Assets:Cash', 5),
 
        ('Income:Donations', -5),
 
    ])
 
    assert not list(HOOK.run(txn))
 

	
 
def test_error_without_books_path():
 
    config = configmod.Config()
 
    with pytest.raises(errormod.ConfigurationError):
 
        hookmod.TransactionDate(config)
0 comments (0 inline, 0 general)