]> asedeno.scripts.mit.edu Git - bluechips.git/commitdiff
added lots more expenditure tests
authorScott Torborg <scott@crookedmedia.com>
Sun, 8 Nov 2009 21:15:11 +0000 (11:15 -1000)
committerScott Torborg <scott@crookedmedia.com>
Sun, 8 Nov 2009 21:15:11 +0000 (11:15 -1000)
bluechips/tests/model/test_expenditure.py

index 1e38ef471e43c63264f47a2193c37b790fd8b2a0..b0446fd5063192ec8b234661d8a8c518be7a6577 100644 (file)
@@ -1,3 +1,5 @@
+from decimal import Decimal
+
 from unittest import TestCase
 from bluechips import model
 from bluechips.model import meta
@@ -8,6 +10,9 @@ class TestExpenditure(TestCase):
         self.u = model.User(u'chaz', u'Charles Root', False)
         self.e = model.Expenditure(self.u, Currency('444.88'),
                                    u'chaz buys lunch')
+        meta.Session.add(self.u)
+        meta.Session.add(self.e)
+        meta.Session.commit()
 
     def test_constructor(self):
         assert self.e.spender == self.u
@@ -17,3 +22,57 @@ class TestExpenditure(TestCase):
     def test_repr(self):
         assert (repr(self.e) == 
                 '<Expenditure: spender: Charles Root spent: $444.88>')
+
+    def test_even_split(self):
+        self.e.even_split()
+        meta.Session.commit()
+        for sp in self.e.splits:
+            assert sp.share == Currency('111.22')
+
+    def test_split_change_to_zero(self):
+        self.e.even_split()
+        meta.Session.commit()
+        split_dict = {}
+        split_dict[self.u] = Decimal(1)
+        self.e.split(split_dict)
+
+    def _two_way_split_test(self, amount, min, max):
+        e2 = model.Expenditure(self.u, amount,
+                              u'testing splits')
+        u2 = model.User(u'bo', u'Bo Jangles', False)
+        meta.Session.add(u2)
+        meta.Session.add(e2)
+        meta.Session.commit()
+        split_dict = {}
+        split_dict[self.u] = Decimal(1)
+        split_dict[u2] = Decimal(1)
+        e2.split(split_dict)
+        assert min <= e2.share(u2) <= max
+        meta.Session.delete(e2)
+        meta.Session.delete(u2)
+        meta.Session.commit()
+
+    def test_split_rounds_down(self):
+        self._two_way_split_test(Currency('40.01'),
+                                 Currency('20.00'),
+                                 Currency('20.01'))
+
+    def test_split_rounds_up(self):
+        self._two_way_split_test(Currency('39.99'),
+                                 Currency('19.99'),
+                                 Currency('20.00'))
+
+    def test_split_small(self):
+        self._two_way_split_test(Currency('0.01'),
+                                 Currency('0.00'),
+                                 Currency('0.01'))
+
+    def test_split_small_negative(self):
+        self._two_way_split_test(Currency('-0.01'),
+                                 Currency('-0.01'),
+                                 Currency('-0.00'))
+
+    def tearDown(self):
+        meta.Session.delete(self.e)
+        meta.Session.delete(self.u)
+        meta.Session.commit()