X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=blobdiff_plain;f=bluechips%2Ftests%2Fmodel%2Ftest_expenditure.py;h=2bf8037cbc57dc1402998be412c55f30a26dcff4;hb=82ad8466cea54eb777e71bb1a74798461461598b;hp=1e38ef471e43c63264f47a2193c37b790fd8b2a0;hpb=a59d19c0581702321ebb6f89ab20f158cdc9d7bc;p=bluechips.git diff --git a/bluechips/tests/model/test_expenditure.py b/bluechips/tests/model/test_expenditure.py index 1e38ef4..2bf8037 100644 --- a/bluechips/tests/model/test_expenditure.py +++ b/bluechips/tests/model/test_expenditure.py @@ -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,74 @@ class TestExpenditure(TestCase): def test_repr(self): assert (repr(self.e) == '') + + 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() + users = meta.Session.query(model.User).all() + split_dict = dict((user, Decimal('0')) for user in users) + 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 test_split_irrational_rounding(self): + e2 = model.Expenditure(self.u, Decimal('2375.00'), + u'rounding test') + u2 = model.User(u'rat', u'Irrational Rat', False) + meta.Session.add(u2) + meta.Session.add(e2) + meta.Session.commit() + split_dict = {} + split_dict[u2] = Decimal('750.00') + split_dict[self.u] = Decimal('4000.00') + e2.split(split_dict) + assert e2.share(u2) == Decimal('375.00') + meta.Session.delete(e2) + meta.Session.delete(u2) + meta.Session.commit() + + def tearDown(self): + meta.Session.delete(self.e) + meta.Session.delete(self.u) + meta.Session.commit()