]> asedeno.scripts.mit.edu Git - bluechips.git/blob - bluechips/tests/model/test_expenditure.py
added lots more expenditure tests
[bluechips.git] / bluechips / tests / model / test_expenditure.py
1 from decimal import Decimal
2
3 from unittest import TestCase
4 from bluechips import model
5 from bluechips.model import meta
6 from bluechips.model.types import Currency
7
8 class TestExpenditure(TestCase):
9     def setUp(self):
10         self.u = model.User(u'chaz', u'Charles Root', False)
11         self.e = model.Expenditure(self.u, Currency('444.88'),
12                                    u'chaz buys lunch')
13         meta.Session.add(self.u)
14         meta.Session.add(self.e)
15         meta.Session.commit()
16
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'
21
22     def test_repr(self):
23         assert (repr(self.e) == 
24                 '<Expenditure: spender: Charles Root spent: $444.88>')
25
26     def test_even_split(self):
27         self.e.even_split()
28         meta.Session.commit()
29         for sp in self.e.splits:
30             assert sp.share == Currency('111.22')
31
32     def test_split_change_to_zero(self):
33         self.e.even_split()
34         meta.Session.commit()
35         split_dict = {}
36         split_dict[self.u] = Decimal(1)
37         self.e.split(split_dict)
38
39     def _two_way_split_test(self, amount, min, max):
40         e2 = model.Expenditure(self.u, amount,
41                               u'testing splits')
42         u2 = model.User(u'bo', u'Bo Jangles', False)
43         meta.Session.add(u2)
44         meta.Session.add(e2)
45         meta.Session.commit()
46         split_dict = {}
47         split_dict[self.u] = Decimal(1)
48         split_dict[u2] = Decimal(1)
49         e2.split(split_dict)
50         assert min <= e2.share(u2) <= max
51         meta.Session.delete(e2)
52         meta.Session.delete(u2)
53         meta.Session.commit()
54
55     def test_split_rounds_down(self):
56         self._two_way_split_test(Currency('40.01'),
57                                  Currency('20.00'),
58                                  Currency('20.01'))
59
60     def test_split_rounds_up(self):
61         self._two_way_split_test(Currency('39.99'),
62                                  Currency('19.99'),
63                                  Currency('20.00'))
64
65     def test_split_small(self):
66         self._two_way_split_test(Currency('0.01'),
67                                  Currency('0.00'),
68                                  Currency('0.01'))
69
70     def test_split_small_negative(self):
71         self._two_way_split_test(Currency('-0.01'),
72                                  Currency('-0.01'),
73                                  Currency('-0.00'))
74
75     def tearDown(self):
76         meta.Session.delete(self.e)
77         meta.Session.delete(self.u)
78         meta.Session.commit()