1 from decimal import Decimal
3 from unittest import TestCase
4 from bluechips import model
5 from bluechips.model import meta
6 from bluechips.model.types import Currency
8 class TestExpenditure(TestCase):
10 self.u = model.User(u'chaz', u'Charles Root', False)
11 self.e = model.Expenditure(self.u, Currency('444.88'),
13 meta.Session.add(self.u)
14 meta.Session.add(self.e)
17 def test_constructor(self):
18 assert self.e.spender == self.u
19 assert self.e.amount == Currency('444.88')
20 assert self.e.description == u'chaz buys lunch'
23 assert (repr(self.e) ==
24 '<Expenditure: spender: Charles Root spent: $444.88>')
26 def test_even_split(self):
29 for sp in self.e.splits:
30 assert sp.share == Currency('111.22')
32 def test_split_change_to_zero(self):
35 users = meta.Session.query(model.User).all()
36 split_dict = dict((user, Decimal('0')) for user in users)
37 split_dict[self.u] = Decimal(1)
38 self.e.split(split_dict)
40 def _two_way_split_test(self, amount, min, max):
41 e2 = model.Expenditure(self.u, amount,
43 u2 = model.User(u'bo', u'Bo Jangles', False)
48 split_dict[self.u] = Decimal(1)
49 split_dict[u2] = Decimal(1)
51 assert min <= e2.share(u2) <= max
52 meta.Session.delete(e2)
53 meta.Session.delete(u2)
56 def test_split_rounds_down(self):
57 self._two_way_split_test(Currency('40.01'),
61 def test_split_rounds_up(self):
62 self._two_way_split_test(Currency('39.99'),
66 def test_split_small(self):
67 self._two_way_split_test(Currency('0.01'),
71 def test_split_small_negative(self):
72 self._two_way_split_test(Currency('-0.01'),
77 meta.Session.delete(self.e)
78 meta.Session.delete(self.u)