104 lines
3.7 KiB
Python
104 lines
3.7 KiB
Python
"""Test AccountMeta class"""
|
|
# 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
|
|
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))
|