conservancy_beancount/tests/test_reports_period_postings.py

63 lines
2.3 KiB
Python
Raw Normal View History

"""test_reports_period_postings - Unit tests for PeriodPostings"""
# 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 datetime
import operator
from decimal import Decimal
import pytest
from . import testutil
from conservancy_beancount import data
from conservancy_beancount.reports import core
def check_balance_attrs(balance_postings, start_usd, stop_usd):
if start_usd:
expected = {'USD': testutil.Amount(start_usd)}
assert balance_postings.start_bal == expected
assert balance_postings.begin_bal == expected
else:
assert balance_postings.start_bal.is_zero()
assert balance_postings.begin_bal.is_zero()
expected = {'USD': testutil.Amount(stop_usd)}
assert balance_postings.stop_bal == expected
assert balance_postings.end_bal == expected
expected = {'USD': testutil.Amount(stop_usd - start_usd)}
assert balance_postings.period_bal == expected
@pytest.mark.parametrize('start_date,expect_start_bal', [
(datetime.date(2019, 2, 1), 0),
(datetime.date(2019, 4, 1), 30),
(datetime.date(2019, 6, 1), 120),
])
def test_balance_postings_attrs(start_date, expect_start_bal):
entries = [testutil.Transaction(date=datetime.date(2019, n, 15), postings=[
('Income:Donations', -n * 10),
('Assets:Cash', n * 10),
]) for n in range(3, 7)]
cls = core.PeriodPostings.with_start_date(start_date)
actual = dict(cls.group_by_account(data.Posting.from_entries(entries)))
assert len(actual) == 2
check_balance_attrs(actual['Assets:Cash'], expect_start_bal, 180)
check_balance_attrs(actual['Income:Donations'], -expect_start_bal, -180)
@pytest.mark.parametrize('start_date,expect_count', [
(datetime.date(2019, 2, 1), 4),
(datetime.date(2019, 4, 1), 3),
(datetime.date(2019, 6, 1), 1),
])
def test_balance_postings_filter(start_date, expect_count):
entries = [testutil.Transaction(date=datetime.date(2019, n, 15), postings=[
('Income:Donations', -n * 10),
('Assets:Cash', n * 10),
]) for n in range(3, 7)]
cls = core.PeriodPostings.with_start_date(start_date)
for _, related in cls.group_by_account(data.Posting.from_entries(entries)):
assert len(related) == expect_count