Files @ 5a8da108b983
Branch filter:

Location: NPO-Accounting/conservancy_beancount/tests/test_plugin_txn_date.py

bsturmfels
statement_reconciler: Add initial Chase bank CSV statement matching

We currently don't have many examples to work with, so haven't done any
significant testing of the matching accuracy between statement and books.
"""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,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=[
        ('Assets:Cash', 5),
        ('Income:Donations', -5),
    ])
    assert not list(HOOK.run(txn))

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