Skip to content

Commit

Permalink
issue #105 - fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jantman committed Feb 18, 2018
1 parent 2eda35e commit c68666f
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 39 deletions.
10 changes: 5 additions & 5 deletions biweeklybudget/biweeklypayperiod.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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']
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down
51 changes: 36 additions & 15 deletions biweeklybudget/tests/unit/models/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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()
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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])
]
8 changes: 6 additions & 2 deletions biweeklybudget/tests/unit/models/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ def test_simple(self):
budgeted_amount=Decimal('123.45'),
description=desc,
account=acct,
budget=budg1,
notes='foo'
),
call(
Expand All @@ -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)
Expand Down
71 changes: 54 additions & 17 deletions biweeklybudget/tests/unit/test_biweeklypayperiod.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
}
]
}
Expand Down Expand Up @@ -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
}
]
}
Expand Down Expand Up @@ -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,
Expand All @@ -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) == {
Expand All @@ -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):
Expand All @@ -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,
Expand All @@ -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) == {
Expand All @@ -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):
Expand All @@ -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,
Expand All @@ -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) == {
Expand All @@ -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
}

Expand Down

0 comments on commit c68666f

Please sign in to comment.