From 0581525c98241a78104097f75f80ed29e6623784 Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Sat, 6 Jun 2020 11:31:04 -0400 Subject: [PATCH] reports: Add Balance.__pos__() method. I did this while I was working on normalize_amount_func. It turns out it's not immediately needed, but it's still nice to have. --- conservancy_beancount/reports/core.py | 3 +++ tests/test_reports_balance.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/conservancy_beancount/reports/core.py b/conservancy_beancount/reports/core.py index 01e04cd..9c0ae79 100644 --- a/conservancy_beancount/reports/core.py +++ b/conservancy_beancount/reports/core.py @@ -140,6 +140,9 @@ class Balance(Mapping[str, data.Amount]): def __neg__(self: BalanceType) -> BalanceType: return type(self)(-amt for amt in self.values()) + def __pos__(self: BalanceType) -> BalanceType: + return self + def __getitem__(self, key: str) -> data.Amount: return self._currency_map[key] diff --git a/tests/test_reports_balance.py b/tests/test_reports_balance.py index 93a918f..3a424b4 100644 --- a/tests/test_reports_balance.py +++ b/tests/test_reports_balance.py @@ -168,6 +168,20 @@ def test_neg(mapping): for key, number in mapping.items(): assert actual[key] == testutil.Amount(-number, key) +@pytest.mark.parametrize('mapping', [ + {}, + {'USD': 0}, + {'EUR': 10}, + {'JPY': 20, 'BRL': 30}, + {'EUR': -15}, + {'JPY': -25, 'BRL': -35}, + {'JPY': 40, 'USD': 0, 'EUR': -50}, +]) +def test_pos(mapping): + amounts = frozenset(amounts_from_map(mapping)) + actual = +core.Balance(amounts) + assert set(actual.values()) == amounts + @pytest.mark.parametrize('map1,map2,expected', [ ({}, {}, True), ({}, {'USD': 0}, True),