From c68666f3532ac17668c3aa9820dbb0f870454441 Mon Sep 17 00:00:00 2001 From: Jason Antman Date: Sun, 18 Feb 2018 16:26:09 -0500 Subject: [PATCH] issue #105 - fix unit tests --- biweeklybudget/biweeklypayperiod.py | 10 +-- .../tests/unit/models/test_transaction.py | 51 +++++++++---- .../tests/unit/models/test_utils.py | 8 ++- .../tests/unit/test_biweeklypayperiod.py | 71 ++++++++++++++----- 4 files changed, 101 insertions(+), 39 deletions(-) diff --git a/biweeklybudget/biweeklypayperiod.py b/biweeklybudget/biweeklypayperiod.py index fb795b8c..616302b4 100644 --- a/biweeklybudget/biweeklypayperiod.py +++ b/biweeklybudget/biweeklypayperiod.py @@ -43,9 +43,7 @@ from decimal import Decimal from biweeklybudget import settings -from biweeklybudget.models import ( - Transaction, ScheduledTransaction, Budget, BudgetTransaction -) +from biweeklybudget.models import Transaction, ScheduledTransaction, Budget from biweeklybudget.utils import dtnow @@ -450,7 +448,7 @@ def _make_budget_sums(self): # t['type'] == 'Transaction' res[t['budget_id']]['trans_total'] += t['amount'] if t['budgeted_amount'] is None: - res[t['planned_budget_id']]['allocated'] += t['amount'] + res[t['budget_id']]['allocated'] += t['amount'] res[t['budget_id']]['spent'] += t['amount'] else: res[t['planned_budget_id']]['allocated'] += t['budgeted_amount'] @@ -616,7 +614,7 @@ def _dict_for_trans(self, t): 'budget_id': t.budget_transactions[0].budget_id, 'budget_name': t.budget_transactions[0].budget.name, 'planned_budget_id': t.planned_budget_id, - 'planned_budget_name': t.planned_budget.name + 'planned_budget_name': None } if t.reconcile is None: res['reconcile_id'] = None @@ -626,6 +624,8 @@ def _dict_for_trans(self, t): res['budgeted_amount'] = None else: res['budgeted_amount'] = t.budgeted_amount + if t.planned_budget is not None: + res['planned_budget_name'] = t.planned_budget.name return res def _dict_for_sched_trans(self, t): diff --git a/biweeklybudget/tests/unit/models/test_transaction.py b/biweeklybudget/tests/unit/models/test_transaction.py index 59d7b013..eda1775e 100644 --- a/biweeklybudget/tests/unit/models/test_transaction.py +++ b/biweeklybudget/tests/unit/models/test_transaction.py @@ -101,7 +101,8 @@ def test_sum_error(self): b1: Decimal('100.00'), b2: Decimal('10.23') }) - assert 'sum of requested amounts is' in str(exc) + assert 'sum of requested amounts is 110.23 but actual amount of this ' \ + 'Transaction is 123.45' in str(exc) assert mock_sess.mock_calls == [] def test_none(self): @@ -128,12 +129,10 @@ def test_none_but_existing(self): transaction=t, amount=Decimal('90.00'), budget=b2 ) assert t.budget_transactions == [bt1, bt2] - t.set_budget_amounts({}) - assert t.budget_transactions == [] - assert mock_sess.mock_calls == [ - call.delete(bt1), - call.delete(bt2) - ] + with pytest.raises(RuntimeError) as exc: + t.set_budget_amounts({}) + assert 'sum of requested amounts is 0' in str(exc) + assert mock_sess.mock_calls == [] def test_add_one(self): mock_sess = Mock() @@ -143,14 +142,14 @@ def test_add_one(self): t = Transaction( actual_amount=Decimal('10.00') ) - bt1 = BudgetTransaction( - transaction=t, amount=Decimal('10.00'), budget=b1 - ) assert t.budget_transactions == [] t.set_budget_amounts({b1: Decimal('10.00')}) - assert t.budget_transactions == [bt1] + assert len(t.budget_transactions) == 1 + assert t.budget_transactions[0].transaction == t + assert t.budget_transactions[0].budget == b1 + assert t.budget_transactions[0].amount == Decimal('10.00') assert mock_sess.mock_calls == [ - call.add(bt1) + call.add(t.budget_transactions[0]) ] def test_add_three(self): @@ -182,6 +181,9 @@ def test_add_three(self): assert t.budget_transactions[2].budget == b3 assert t.budget_transactions[2].amount == Decimal('40.00') assert mock_sess.mock_calls == [ + call.add(t.budget_transactions[0]), + call.add(t.budget_transactions[1]), + call.add(t.budget_transactions[2]) ] def test_sync(self): @@ -205,8 +207,27 @@ def test_sync(self): b1: Decimal('40.00'), b3: Decimal('60.00') }) - assert len(t.budget_transactions) + # NOTE: Since we don't have actual SQLAlchemy backing this, the + # deleted BudgetTransaction is never removed from the list... + assert len(t.budget_transactions) == 3 + assert isinstance(t.budget_transactions[0], BudgetTransaction) + assert t.budget_transactions[0].budget == b1 + assert t.budget_transactions[0].transaction == t + assert t.budget_transactions[0].amount == Decimal('40.00') + assert t.budget_transactions[0] == bt1 + # BEGIN to-be-deleted BudgetTransaction + assert t.budget_transactions[1].budget == b2 + assert t.budget_transactions[1].transaction == t + assert t.budget_transactions[1].amount == Decimal('90.00') + assert isinstance(t.budget_transactions[1], BudgetTransaction) + assert t.budget_transactions[1] == bt2 + # END to-be-deleted BudgetTransaction + assert t.budget_transactions[2].budget == b3 + assert t.budget_transactions[2].transaction == t + assert t.budget_transactions[2].amount == Decimal('60.00') + assert isinstance(t.budget_transactions[2], BudgetTransaction) assert mock_sess.mock_calls == [ - call.delete(bt1), - call.delete(bt2) + call.add(t.budget_transactions[0]), + call.delete(bt2), + call.add(t.budget_transactions[2]) ] diff --git a/biweeklybudget/tests/unit/models/test_utils.py b/biweeklybudget/tests/unit/models/test_utils.py index ad23e72f..9aca68f2 100644 --- a/biweeklybudget/tests/unit/models/test_utils.py +++ b/biweeklybudget/tests/unit/models/test_utils.py @@ -97,7 +97,6 @@ def test_simple(self): budgeted_amount=Decimal('123.45'), description=desc, account=acct, - budget=budg1, notes='foo' ), call( @@ -106,10 +105,15 @@ def test_simple(self): budgeted_amount=Decimal('-123.45'), description=desc, account=acct, - budget=standing, notes='foo' ) ] + assert t1.mock_calls == [ + call.set_budget_amounts({budg1: Decimal('123.45')}) + ] + assert t2.mock_calls == [ + call.set_budget_amounts({standing: Decimal('-123.45')}) + ] assert mock_tr.mock_calls == [ call(transaction=t1, note=desc), call(transaction=t2, note=desc) diff --git a/biweeklybudget/tests/unit/test_biweeklypayperiod.py b/biweeklybudget/tests/unit/test_biweeklypayperiod.py index 727dea23..b331dab7 100644 --- a/biweeklybudget/tests/unit/test_biweeklypayperiod.py +++ b/biweeklybudget/tests/unit/test_biweeklypayperiod.py @@ -47,6 +47,7 @@ from biweeklybudget.models.transaction import Transaction from biweeklybudget.models.scheduled_transaction import ScheduledTransaction from biweeklybudget.models.budget_model import Budget +from biweeklybudget.models.budget_transaction import BudgetTransaction from biweeklybudget.tests.unit_helpers import binexp_to_dict from biweeklybudget.utils import dtnow @@ -592,19 +593,22 @@ def test_scheduled_income(self): 'type': 'Transaction', 'amount': Decimal('22.22'), 'budgeted_amount': Decimal('20.20'), - 'budget_id': 1 + 'budget_id': 1, + 'planned_budget_id': 1 }, { 'type': 'Transaction', 'amount': Decimal('33.33'), 'budgeted_amount': Decimal('33.33'), - 'budget_id': 2 + 'budget_id': 2, + 'planned_budget_id': 2 }, { 'type': 'ScheduledTransaction', 'amount': Decimal('-1234.56'), 'budgeted_amount': Decimal('-1234.56'), - 'budget_id': 4 + 'budget_id': 4, + 'planned_budget_id': 4 } ] } @@ -694,19 +698,22 @@ def test_actual_income(self): 'type': 'Transaction', 'amount': Decimal('22.22'), 'budgeted_amount': Decimal('20.20'), - 'budget_id': 1 + 'budget_id': 1, + 'planned_budget_id': 1 }, { 'type': 'Transaction', 'amount': Decimal('33.33'), 'budgeted_amount': Decimal('33.33'), - 'budget_id': 2 + 'budget_id': 2, + 'planned_budget_id': 2 }, { 'type': 'Transaction', 'amount': Decimal('-1234.56'), 'budgeted_amount': Decimal('-1234.56'), - 'budget_id': 4 + 'budget_id': 4, + 'planned_budget_id': 4 } ] } @@ -953,7 +960,7 @@ def test_simple(self): m_budget = Mock(name='bar') type(m_budget).name = 'bar' m = Mock( - spec_set=Transaction, + spec=Transaction, id=123, date=date(year=2017, month=7, day=15), scheduled_trans_id=567, @@ -962,8 +969,16 @@ def test_simple(self): budgeted_amount=Decimal('120.00'), account_id=2, account=m_account, - budget_id=3, - budget=m_budget + planned_budget_id=3, + planned_budget=m_budget, + budget_transactions=[ + Mock( + spec_set=BudgetTransaction, + budget_id=3, + budget=m_budget, + amount=Decimal('123.45') + ) + ] ) type(m).reconcile = None assert self.cls._dict_for_trans(m) == { @@ -979,7 +994,9 @@ def test_simple(self): 'account_name': 'foo', 'budget_id': 3, 'budget_name': 'bar', - 'reconcile_id': None + 'reconcile_id': None, + 'planned_budget_id': 3, + 'planned_budget_name': 'bar' } def test_reconciled(self): @@ -988,7 +1005,7 @@ def test_reconciled(self): m_budget = Mock(name='bar') type(m_budget).name = 'bar' m = Mock( - spec_set=Transaction, + spec=Transaction, id=123, date=date(year=2017, month=7, day=15), scheduled_trans_id=567, @@ -997,8 +1014,16 @@ def test_reconciled(self): budgeted_amount=Decimal('120.00'), account_id=2, account=m_account, - budget_id=3, - budget=m_budget + planned_budget_id=3, + planned_budget=m_budget, + budget_transactions=[ + Mock( + spec_set=BudgetTransaction, + budget_id=3, + budget=m_budget, + amount=Decimal('123.45') + ) + ] ) type(m).reconcile = Mock(id=2) assert self.cls._dict_for_trans(m) == { @@ -1014,7 +1039,9 @@ def test_reconciled(self): 'account_name': 'foo', 'budget_id': 3, 'budget_name': 'bar', - 'reconcile_id': 2 + 'reconcile_id': 2, + 'planned_budget_id': 3, + 'planned_budget_name': 'bar' } def test_budgeted_amount_none(self): @@ -1023,7 +1050,7 @@ def test_budgeted_amount_none(self): m_budget = Mock(name='bar') type(m_budget).name = 'bar' m = Mock( - spec_set=Transaction, + spec=Transaction, id=123, date=date(year=2017, month=7, day=15), scheduled_trans_id=567, @@ -1032,8 +1059,16 @@ def test_budgeted_amount_none(self): budgeted_amount=None, account_id=2, account=m_account, - budget_id=3, - budget=m_budget + planned_budget_id=None, + planned_budget=None, + budget_transactions=[ + Mock( + spec_set=BudgetTransaction, + budget_id=3, + budget=m_budget, + amount=Decimal('123.45') + ) + ] ) type(m).reconcile = None assert self.cls._dict_for_trans(m) == { @@ -1049,6 +1084,8 @@ def test_budgeted_amount_none(self): 'account_name': 'foo', 'budget_id': 3, 'budget_name': 'bar', + 'planned_budget_id': None, + 'planned_budget_name': None, 'reconcile_id': None }