Files @ 28e59e7a3b6e
Branch filter:

Location: NPO-Accounting/conservancy_beancount/tests/test_data_balance_of.py

Brett Smith
data: Add balance_of() function.
"""Test data.balance_of function"""
# 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/>.

from decimal import Decimal

import pytest

from . import testutil

from conservancy_beancount import data

@pytest.fixture
def payable_payment_txn():
    return testutil.Transaction(postings=[
        ('Liabilities:Payable:Accounts', 50),
        ('Assets:Checking', -50),
        ('Expenses:BankingFees', 5),
        ('Assets:Checking', -5),
    ])

@pytest.fixture
def none_posting_txn():
    return testutil.Transaction(postings=[
        ('Income:Donations', -30),
        ('Expenses:BankingFees', 3),
        ('Assets:Checking', None),
    ])

@pytest.fixture
def multipost_one_none_txn():
    return testutil.Transaction(postings=[
        ('Liabilities:Payable:Accounts', 50),
        ('Assets:Checking', -50),
        ('Expenses:BankingFees', 5),
        ('Assets:Checking', None),
    ])

def test_balance_of_simple_txn():
    txn = testutil.Transaction(postings=[
        ('Assets:Cash', 50),
        ('Income:Donations', -50),
    ])
    assert data.balance_of(txn, 'Assets') == 50
    assert data.balance_of(txn, 'Income') == -50

def test_zero_balance_of(payable_payment_txn):
    assert data.balance_of(payable_payment_txn, 'Equity') == 0
    assert data.balance_of(payable_payment_txn, 'Assets:Cash') == 0
    assert data.balance_of(payable_payment_txn, 'Liabilities:CreditCard') == 0

def test_balance_of_multipost_txn(payable_payment_txn):
    assert data.balance_of(payable_payment_txn, 'Assets') == -55

def test_multiarg_balance_of(payable_payment_txn):
    assert data.balance_of(payable_payment_txn, 'Assets', 'Expenses') == -50
    assert data.balance_of(payable_payment_txn, 'Assets', 'Liabilities') == -5

def test_balance_of_uses_whole_account_names(payable_payment_txn):
    assert data.balance_of(payable_payment_txn, 'Assets:Check') == 0

def test_balance_of_none_posting(none_posting_txn):
    assert data.balance_of(none_posting_txn, 'Assets') is None

def test_balance_of_none_posting_with_default(none_posting_txn):
    expected = Decimal('Infinity')
    assert data.balance_of(none_posting_txn, 'Assets', default=expected) == expected

def test_balance_of_other_side_of_none_posting(none_posting_txn):
    assert data.balance_of(none_posting_txn, 'Income') == -30
    assert data.balance_of(none_posting_txn, 'Expenses') == 3

def test_balance_of_multi_postings_one_none(multipost_one_none_txn):
    assert data.balance_of(multipost_one_none_txn, 'Assets') is None

def test_balance_of_multi_postings_one_none(multipost_one_none_txn):
    expected = Decimal('Infinity')
    assert data.balance_of(multipost_one_none_txn, 'Assets', default=expected) == expected