conservancy_beancount/tests/test_reports_period_postings.py

72 lines
2.8 KiB
Python
Raw Normal View History

"""test_reports_period_postings - Unit tests for PeriodPostings"""
# Copyright © 2020 Brett Smith
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# 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 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