]> asedeno.scripts.mit.edu Git - bluechips.git/blob - bluechips/tests/model/test_expenditure.py
Added UI for working with tags
[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         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)
39
40     def _two_way_split_test(self, amount, min, max):
41         e2 = model.Expenditure(self.u, amount,
42                               u'testing splits')
43         u2 = model.User(u'bo', u'Bo Jangles', False)
44         meta.Session.add(u2)
45         meta.Session.add(e2)
46         meta.Session.commit()
47         split_dict = {}
48         split_dict[self.u] = Decimal(1)
49         split_dict[u2] = Decimal(1)
50         e2.split(split_dict)
51         assert min <= e2.share(u2) <= max
52         meta.Session.delete(e2)
53         meta.Session.delete(u2)
54         meta.Session.commit()
55
56     def test_split_rounds_down(self):
57         self._two_way_split_test(Currency('40.01'),
58                                  Currency('20.00'),
59                                  Currency('20.01'))
60
61     def test_split_rounds_up(self):
62         self._two_way_split_test(Currency('39.99'),
63                                  Currency('19.99'),
64                                  Currency('20.00'))
65
66     def test_split_small(self):
67         self._two_way_split_test(Currency('0.01'),
68                                  Currency('0.00'),
69                                  Currency('0.01'))
70
71     def test_split_small_negative(self):
72         self._two_way_split_test(Currency('-0.01'),
73                                  Currency('-0.01'),
74                                  Currency('-0.00'))
75
76     def test_split_irrational_rounding(self):
77         e2 = model.Expenditure(self.u, Decimal('2375.00'),
78                                u'rounding test')
79         u2 = model.User(u'rat', u'Irrational Rat', False)
80         meta.Session.add(u2)
81         meta.Session.add(e2)
82         meta.Session.commit()
83         split_dict = {}
84         split_dict[u2] = Decimal('750.00')
85         split_dict[self.u] = Decimal('4000.00')
86         e2.split(split_dict)
87         assert e2.share(u2) == Decimal('375.00')
88         meta.Session.delete(e2)
89         meta.Session.delete(u2)
90         meta.Session.commit()
91
92     def tearDown(self):
93         meta.Session.delete(self.e)
94         meta.Session.delete(self.u)
95         meta.Session.commit()