2020-04-08 15:55:00 +00:00
|
|
|
"""Test data.balance_of function"""
|
|
|
|
# Copyright © 2020 Brett Smith
|
2021-01-08 21:57:43 +00:00
|
|
|
# License: AGPLv3-or-later WITH Beancount-Plugin-Additional-Permission-1.0
|
2020-04-08 15:55:00 +00:00
|
|
|
#
|
2021-01-08 21:57:43 +00:00
|
|
|
# Full copyright and licensing details can be found at toplevel file
|
|
|
|
# LICENSE.txt in the repository.
|
2020-04-08 15:55:00 +00:00
|
|
|
|
|
|
|
from decimal import Decimal
|
2020-04-08 18:16:57 +00:00
|
|
|
from operator import methodcaller
|
2020-04-08 15:55:00 +00:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from . import testutil
|
|
|
|
|
|
|
|
from conservancy_beancount import data
|
|
|
|
|
2020-04-08 18:16:57 +00:00
|
|
|
is_cash_eq = data.Account.is_cash_equivalent
|
2020-04-09 18:13:07 +00:00
|
|
|
USD = testutil.Amount
|
2020-04-08 18:16:57 +00:00
|
|
|
|
2020-04-08 15:55:00 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def payable_payment_txn():
|
|
|
|
return testutil.Transaction(postings=[
|
|
|
|
('Liabilities:Payable:Accounts', 50),
|
|
|
|
('Assets:Checking', -50),
|
|
|
|
('Expenses:BankingFees', 5),
|
|
|
|
('Assets:Checking', -5),
|
|
|
|
])
|
|
|
|
|
2020-04-09 18:13:07 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def fx_donation_txn():
|
|
|
|
return testutil.Transaction(postings=[
|
|
|
|
('Income:Donations', -500, 'EUR', ('.9', 'USD')),
|
|
|
|
('Assets:Checking', 445),
|
|
|
|
('Expenses:BankingFees', 5),
|
|
|
|
])
|
|
|
|
|
2020-04-08 18:16:57 +00:00
|
|
|
def balance_under(txn, *accts):
|
|
|
|
pred = methodcaller('is_under', *accts)
|
|
|
|
return data.balance_of(txn, pred)
|
|
|
|
|
2020-04-08 15:55:00 +00:00
|
|
|
def test_balance_of_simple_txn():
|
|
|
|
txn = testutil.Transaction(postings=[
|
|
|
|
('Assets:Cash', 50),
|
|
|
|
('Income:Donations', -50),
|
|
|
|
])
|
2020-04-09 18:13:07 +00:00
|
|
|
assert balance_under(txn, 'Assets') == USD(50)
|
|
|
|
assert balance_under(txn, 'Income') == USD(-50)
|
2020-04-08 15:55:00 +00:00
|
|
|
|
|
|
|
def test_zero_balance_of(payable_payment_txn):
|
2020-04-09 18:13:07 +00:00
|
|
|
expected = testutil.Amount(0, '')
|
|
|
|
assert balance_under(payable_payment_txn, 'Equity') == expected
|
|
|
|
assert balance_under(payable_payment_txn, 'Assets:Cash') == expected
|
|
|
|
assert balance_under(payable_payment_txn, 'Liabilities:CreditCard') == expected
|
2020-04-08 15:55:00 +00:00
|
|
|
|
2020-04-08 18:16:57 +00:00
|
|
|
def test_nonzero_balance_of(payable_payment_txn):
|
2020-04-09 18:13:07 +00:00
|
|
|
assert balance_under(payable_payment_txn, 'Assets', 'Expenses') == USD(-50)
|
|
|
|
assert balance_under(payable_payment_txn, 'Assets', 'Liabilities') == USD(-5)
|
2020-04-08 15:55:00 +00:00
|
|
|
|
2020-04-08 18:16:57 +00:00
|
|
|
def test_multiarg_balance_of():
|
|
|
|
txn = testutil.Transaction(postings=[
|
|
|
|
('Liabilities:CreditCard', 650),
|
|
|
|
('Expenses:BankingFees', 5),
|
|
|
|
('Assets:Checking', -655),
|
|
|
|
])
|
2020-04-09 18:13:07 +00:00
|
|
|
assert data.balance_of(txn, is_cash_eq, data.Account.is_credit_card) == USD(-5)
|
2020-04-08 15:55:00 +00:00
|
|
|
|
2020-04-08 18:16:57 +00:00
|
|
|
def test_balance_of_multipost_txn(payable_payment_txn):
|
2020-04-09 18:13:07 +00:00
|
|
|
assert data.balance_of(payable_payment_txn, is_cash_eq) == USD(-55)
|
|
|
|
|
|
|
|
def test_balance_of_multicurrency_txn(fx_donation_txn):
|
|
|
|
assert balance_under(fx_donation_txn, 'Income') == USD(-450)
|
|
|
|
assert balance_under(fx_donation_txn, 'Income', 'Assets') == USD(-5)
|
|
|
|
assert balance_under(fx_donation_txn, 'Income', 'Expenses') == USD(-445)
|