Files @ 58b02b6f33c2
Branch filter:

Location: NPO-Accounting/conservancy_beancount/tests/test_reports_balance.py - annotation

Brett Smith
accrual: Move more functionality into AccrualPostings.
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
d8df34ebaf6f
d8df34ebaf6f
5aa30e5456d0
5aa30e5456d0
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
5aa30e5456d0
68acb86e7e07
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
396173b55d1b
1b8137529472
1b8137529472
1b8137529472
1b8137529472
1b8137529472
1b8137529472
1b8137529472
1b8137529472
1b8137529472
1b8137529472
1b8137529472
1b8137529472
1b8137529472
1b8137529472
1b8137529472
1b8137529472
3780c31c5901
3780c31c5901
3780c31c5901
3780c31c5901
3780c31c5901
3780c31c5901
3780c31c5901
3780c31c5901
3780c31c5901
3780c31c5901
3780c31c5901
3780c31c5901
3780c31c5901
3780c31c5901
3780c31c5901
3780c31c5901
3780c31c5901
2c44cc8f5027
68acb86e7e07
68acb86e7e07
68acb86e7e07
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
d8df34ebaf6f
d8df34ebaf6f
d8df34ebaf6f
d8df34ebaf6f
d8df34ebaf6f
d8df34ebaf6f
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
2c44cc8f5027
"""test_reports_balance - Unit tests for reports.core.Balance"""
# 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

from decimal import Decimal

import pytest

from . import testutil

import babel.numbers

from conservancy_beancount.reports import core

DEFAULT_STRINGS = [
    ({}, "Zero balance"),
    ({'JPY': 0, 'BRL': 0}, "Zero balance"),
    ({'USD': '20.00'}, "20.00 USD"),
    ({'EUR': '50.00', 'GBP': '80.00'}, "80.00 GBP, 50.00 EUR"),
    ({'JPY': '-5500.00', 'BRL': '-8500.00'}, "-8,500.00 BRL, -5,500 JPY"),
]

def test_empty_balance():
    balance = core.Balance()
    assert not balance
    assert len(balance) == 0
    assert balance.is_zero()
    with pytest.raises(KeyError):
        balance['USD']

@pytest.mark.parametrize('currencies', [
    'USD',
    'EUR GBP',
    'JPY INR BRL',
])
def test_zero_balance(currencies):
    keys = currencies.split()
    balance = core.Balance(testutil.balance_map((key, 0) for key in keys))
    assert balance
    assert len(balance) == len(keys)
    assert balance.is_zero()
    assert all(balance[key].number == 0 for key in keys)
    assert all(balance[key].currency == key for key in keys)

@pytest.mark.parametrize('currencies', [
    'USD',
    'EUR GBP',
    'JPY INR BRL',
])
def test_nonzero_balance(currencies):
    amounts = testutil.balance_map(zip(currencies.split(), itertools.count(110, 100)))
    balance = core.Balance(amounts.items())
    assert balance
    assert len(balance) == len(amounts)
    assert not balance.is_zero()
    assert all(balance[key] == amt for key, amt in amounts.items())

def test_mixed_balance():
    amounts = testutil.balance_map(USD=0, EUR=120)
    balance = core.Balance(amounts.items())
    assert balance
    assert len(balance) == 2
    assert not balance.is_zero()
    assert all(balance[key] == amt for key, amt in amounts.items())

@pytest.mark.parametrize('balance_map_kwargs,expected', [
    ({}, True),
    ({'USD': 0}, True),
    ({'USD': 0, 'EUR': 0}, True),
    ({'USD': -10, 'EUR': 0}, False),
    ({'EUR': -10}, False),
    ({'USD': -10, 'EUR': -20}, False),
    ({'USD': 10, 'EUR': -20}, False),
    ({'JPY': 10}, False),
    ({'JPY': 10, 'BRL': 0}, False),
    ({'JPY': 10, 'BRL': 20}, False),
])
def test_eq_zero(balance_map_kwargs, expected):
    amounts = testutil.balance_map(**balance_map_kwargs)
    balance = core.Balance(amounts.items())
    assert balance.eq_zero() == expected
    assert balance.is_zero() == expected

@pytest.mark.parametrize('balance_map_kwargs,expected', [
    ({}, True),
    ({'USD': 0}, True),
    ({'USD': 0, 'EUR': 0}, True),
    ({'EUR': -10}, False),
    ({'USD': 10, 'EUR': -20}, False),
    ({'USD': -10, 'EUR': -20}, False),
    ({'JPY': 10}, True),
    ({'JPY': 10, 'BRL': 0}, True),
    ({'JPY': 10, 'BRL': 20}, True),
])
def test_ge_zero(balance_map_kwargs, expected):
    amounts = testutil.balance_map(**balance_map_kwargs)
    balance = core.Balance(amounts.items())
    assert balance.ge_zero() == expected

@pytest.mark.parametrize('balance_map_kwargs,expected', [
    ({}, True),
    ({'USD': 0}, True),
    ({'USD': 0, 'EUR': 0}, True),
    ({'EUR': -10}, True),
    ({'USD': 10, 'EUR': -20}, False),
    ({'USD': -10, 'EUR': -20}, True),
    ({'JPY': 10}, False),
    ({'JPY': 10, 'BRL': 0}, False),
    ({'JPY': 10, 'BRL': 20}, False),
])
def test_le_zero(balance_map_kwargs, expected):
    amounts = testutil.balance_map(**balance_map_kwargs)
    balance = core.Balance(amounts.items())
    assert balance.le_zero() == expected

@pytest.mark.parametrize('balance_map_kwargs', [
    {},
    {'USD': 0},
    {'EUR': 10},
    {'JPY': 20, 'BRL': 30},
    {'EUR': -15},
    {'JPY': -25, 'BRL': -35},
    {'JPY': 40, 'USD': 0, 'EUR': -50},
])
def test_neg(balance_map_kwargs):
    amounts = testutil.balance_map(**balance_map_kwargs)
    actual = -core.Balance(amounts.items())
    assert set(actual) == set(balance_map_kwargs)
    for key in balance_map_kwargs:
        assert actual[key] == -amounts[key]

@pytest.mark.parametrize('kwargs1,kwargs2,expected', [
    ({}, {}, True),
    ({}, {'USD': 0}, True),
    ({}, {'EUR': 1}, False),
    ({'USD': 1}, {'EUR': 1}, False),
    ({'USD': 1}, {'USD': '1.0'}, True),
    ({'USD': 1}, {'USD': '1.0', 'EUR': '2.0'}, False),
    ({'USD': 1, 'BRL': '2.0'}, {'USD': '1.0', 'EUR': '2.0'}, False),
    ({'USD': 1, 'EUR': 2, 'BRL': '3.0'}, {'USD': '1.0', 'EUR': '2.0'}, False),
    ({'USD': 1, 'EUR': 2}, {'USD': '1.0', 'EUR': '2.0'}, True),
])
def test_eq(kwargs1, kwargs2, expected):
    bal1 = core.Balance(testutil.balance_map(**kwargs1))
    bal2 = core.Balance(testutil.balance_map(**kwargs2))
    actual = bal1 == bal2
    assert actual == expected

@pytest.mark.parametrize('balance_map_kwargs,expected', DEFAULT_STRINGS)
def test_str(balance_map_kwargs, expected):
    amounts = testutil.balance_map(**balance_map_kwargs)
    assert str(core.Balance(amounts.items())) == expected

@pytest.mark.parametrize('bal_kwargs,expected', DEFAULT_STRINGS)
def test_format_defaults(bal_kwargs, expected):
    amounts = testutil.balance_map(**bal_kwargs)
    assert core.Balance(amounts).format() == expected

@pytest.mark.parametrize('fmt,expected', [
    ('¤##0.0', '¥5000, -€1500.00'),
    ('#,#00.0¤¤', '5,000JPY, -1,500.00EUR'),
    ('¤+##0.0;¤-##0.0', '¥+5000, €-1500.00'),
    ('#,#00.0 ¤¤;(#,#00.0 ¤¤)', '5,000 JPY, (1,500.00 EUR)'),
])
def test_format_fmt(fmt, expected):
    amounts = testutil.balance_map(JPY=5000, EUR=-1500)
    balance = core.Balance(amounts)
    assert balance.format(fmt) == expected

@pytest.mark.parametrize('sep', [
    '; ',
    '—',
    '\0',
])
def test_format_sep(sep):
    bal_kwargs, expected = DEFAULT_STRINGS[-1]
    expected = expected.replace(', ', sep)
    amounts = testutil.balance_map(**bal_kwargs)
    balance = core.Balance(amounts)
    assert balance.format(sep=sep) == expected

def test_format_none():
    amounts = testutil.balance_map(BRL=65000)
    balance = core.Balance(amounts)
    expected = babel.numbers.format_currency(65000, 'BRL')
    assert balance.format(None) == expected

@pytest.mark.parametrize('empty', [
    "N/A",
    "Zero",
    "ø",
])
def test_format_empty(empty):
    balance = core.Balance()
    assert balance.format(empty=empty) == empty