conservancy_beancount/tests/test_data_account_meta.py
2020-07-15 10:27:05 -04:00

113 lines
4.1 KiB
Python

"""Test AccountMeta class"""
# 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 itertools
import pytest
from datetime import date as Date
from beancount.core.data import Open, Close, Booking
from conservancy_beancount import data
def test_attributes():
open_date = Date(2019, 6, 1)
name = 'Assets:Bank:Checking'
currencies = ['USD', 'EUR']
booking = Booking.STRICT
actual = data.AccountMeta(Open({}, open_date, name, list(currencies), booking))
assert actual.open_date == open_date
assert actual.account == name
assert isinstance(actual.account, data.Account)
assert actual.currencies == currencies
assert actual.booking == booking
def test_mapping():
src_meta = {'filename': 'maptest', 'lineno': 10}
actual = data.AccountMeta(Open(
src_meta.copy(), Date(2019, 6, 1), 'Income:Donations', None, None,
))
assert len(actual) == 2
assert set(actual) == set(src_meta) # Test __iter__
for key, expected in src_meta.items():
assert actual[key] == expected
def test_close_attributes_without_closing():
actual = data.AccountMeta(Open(
{}, Date(2019, 6, 1), 'Assets:Cash', None, None,
))
assert actual.close_date is None
assert actual.close_meta is None
def test_close_at_init():
src_meta = {'filename': 'initclose', 'lineno': 50}
close_date = Date(2020, 6, 1)
name = 'Assets:Bank:MoneyMarket'
actual = data.AccountMeta(
Open({}, Date(2019, 6, 1), name, None, None),
Close(src_meta.copy(), close_date, name),
)
assert actual.close_date == close_date
assert actual.close_meta == src_meta
def test_add_closing():
src_meta = {'filename': 'laterclose', 'lineno': 65}
close_date = Date(2020, 1, 1)
name = 'Assets:Bank:EUR'
actual = data.AccountMeta(Open({}, Date(2019, 6, 1), name, None, None))
assert actual.close_date is None
assert actual.close_meta is None
actual.add_closing(Close(src_meta.copy(), close_date, name))
assert actual.close_date == close_date
assert actual.close_meta == src_meta
def test_add_closing_already_inited():
name = 'Assets:Bank:Savings'
actual = data.AccountMeta(
Open({}, Date(2019, 6, 1), name, None, None),
Close({}, Date(2019, 7, 1), name),
)
with pytest.raises(ValueError):
actual.add_closing(Close({}, Date(2019, 8, 1), name))
def test_add_closing_called_twice():
name = 'Assets:Bank:FX'
actual = data.AccountMeta(Open({}, Date(2019, 6, 1), name, None, None))
actual.add_closing(Close({}, Date(2019, 7, 1), name))
with pytest.raises(ValueError):
actual.add_closing(Close({}, Date(2019, 8, 1), name))
@pytest.mark.parametrize('close_date,close_name', [
(Date(2020, 6, 1), 'Income:Grants'), # Account name doesn't match
(Date(2010, 6, 1), 'Income:Donations'), # Close predates Open
])
def test_bad_closing_at_init(close_date, close_name):
with pytest.raises(ValueError):
data.AccountMeta(
Open({}, Date(2019, 6, 1), 'Income:Donations', None, None),
Close({}, close_date, close_name),
)
@pytest.mark.parametrize('close_date,close_name', [
(Date(2020, 6, 1), 'Income:Grants'), # Account name doesn't match
(Date(2010, 6, 1), 'Income:Donations'), # Close predates Open
])
def test_add_closing_wrong_account(close_date, close_name):
actual = data.AccountMeta(
Open({}, Date(2019, 6, 1), 'Income:Donations', None, None),
)
with pytest.raises(ValueError):
actual.add_closing(Close({}, close_date, close_name))