]> asedeno.scripts.mit.edu Git - bluechips.git/commitdiff
more test coverage
authorScott Torborg <scott@crookedmedia.com>
Sun, 8 Nov 2009 01:53:24 +0000 (15:53 -1000)
committerScott Torborg <scott@crookedmedia.com>
Sun, 8 Nov 2009 01:53:24 +0000 (15:53 -1000)
bluechips/lib/permissions.py
bluechips/model/subitem.py
bluechips/tests/model/test_currency.py
bluechips/tests/model/test_currency_validator.py [new file with mode: 0644]
bluechips/tests/model/test_expenditure.py
bluechips/tests/model/test_split.py [new file with mode: 0644]

index 63c9c5c0241a495990bdd9ca4abe6aeba4391b36..4a30fdc3e0c66b59ab27bc7b866aa1b6eb276ca4 100644 (file)
@@ -12,12 +12,12 @@ from bluechips.model import meta
 class BlueChipUser(RequestPermission):
     def check(self, app, environ, start_response):
         if 'REMOTE_USER' not in environ:
-            raise NotAuthenticatedError('Not Authenticated')
+            raise NotAuthenticatedError('Not Authenticated') # pragma: nocover
         environ['user'] = meta.Session.query(model.User).\
             filter_by(username=unicode(environ['REMOTE_USER'])).\
             first()
         if environ['user'] == None:
-            raise NotAuthorizedError('You are not allowed access.')
+            raise NotAuthorizedError('You are not allowed access.') # pragma: nocover
         return app(environ, start_response)
 
 class DummyAuthenticate(AddDictToEnviron):
index 81cadfaf02ab5a729cd975d00546408de2316958..2bbbf9b997f0779349251a4c2d7fe311f680a60c 100644 (file)
@@ -2,13 +2,12 @@ from types import Currency
 
 class Subitem(object):
     def __init__(self, expenditure=None, user=None, amount=Currency(0)):
-        self.expenditure = expenditure
-        self.user = user
-        self.share = share
+        self.expenditure = expenditure # pragma: nocover
+        self.user = user # pragma: nocover
+        self.share = share # pragma: nocover
         
     def __repr__(self):
-        return '<Subitem: expense: %s user: %s cost: %s>' % (self.expense,
-                                                             self.user,
-                                                             self.amount)
+        return ('<Subitem: expense: %s user: %s cost: %s>' %
+                (self.expense, self.user, self.amount)) # pragma: nocover
 
 __all__ = ['Subitem']
index a689778eea64c7c9f32d3d45bf73c31f6dd01b78..4b8bdb419474f1a3cdbc7a319f76fcbd97a7fc09 100644 (file)
@@ -1,76 +1,19 @@
 from unittest import TestCase
-from bluechips.tests import *
-from bluechips.model.types import Currency
-from decimal import Decimal
+from bluechips.model import types
 
 class TestCurrency(TestCase):
-    def test_initInt(self):
-        """
-        Make sure the constructor for Currency works
-        """
-        self.assert_(Currency(1) is Currency(1), 
-                     "Currency objects not interned")
-    def test_initString(self):
-        """
-        Make sure the constructor for Currency works with strings
-        """
-        self.assertEqual(Currency("0.01"), Currency(1))
-        self.assert_(Currency("0.01") is Currency(1),
-                     "string and int constructors return different values")
-    
-    def test_string(self):
-        """
-        Test converting a Currency to a string
-        """
-        self.assertEqual(str(Currency(1)), "$0.01")
-        self.assertEqual(str(Currency(100)), "$1.00")
-        self.assertEqual(str(Currency(101)), "$1.01")
-        self.assertEqual(str(Currency(-1)), "-$0.01")
-        self.assertEqual(str(Currency(-100)), "-$1.00")
-        self.assertEqual(str(Currency(-101)), "-$1.01")
-    
-    def test_stringNoDollar(self):
-        """
-        Test that Currency values can be retrieved without the dollar sign
-        """
-        self.assertEqual(Currency(1).__str_no_dollar__(), "0.01")
-        self.assertEqual(Currency(100).__str_no_dollar__(), "1.00")
-        self.assertEqual(Currency(101).__str_no_dollar__(), "1.01")
-        self.assertEqual(Currency(-1).__str_no_dollar__(), "-0.01")
-        self.assertEqual(Currency(-100).__str_no_dollar__(), "-1.00")
-        self.assertEqual(Currency(-101).__str_no_dollar__(), "-1.01")
-    
-    def test_additionMath(self):
-        """
-        Confirm that addition works over currency types and ints
-        """
-        self.assertEqual(Currency(2) + 2, Currency(4))
-        self.assertEqual(2 + Currency(2), Currency(4))
-        self.assertEqual(Currency(2) + Currency(2), Currency(4))
-    
-    def test_additionType(self):
-        """
-        Adding Currencies or a Currency and an int should yield a
-        Currency
-        """
-        self.assertEqual(type(Currency(2) + 2), Currency)
-        self.assertEqual(type(2 + Currency(2)), Currency)
-        self.assertEqual(type(Currency(2) + Currency(2)), Currency)
-        
-    def test_multMath(self):
-        """
-        This test tests the same 3 things as ``test_addition``, but
-        for multiplication
-        """
-        self.assertEqual(Currency(100) * Decimal("0.25"), Currency(25))
-        self.assertEqual(Decimal("0.25") * Currency(100), Currency(25))
-        self.assertEqual(Currency(10) * Currency(10), Currency(100))
-    
-    def test_multType(self):
-        """
-        The result of multiplying a Currency with something else
-        should be a currency
-        """
-        self.assertEqual(type(Currency(100) * Decimal("0.25")), Currency)
-        self.assertEqual(type(Decimal("0.25") * Currency(100)), Currency)
-        self.assertEqual(type(Currency(100) * Currency(100)), Currency)
+    def setUp(self):
+        self.c = types.Currency('12.34')
+
+    def test_currency_float(self):
+        assert float(self.c) == 1234.
+
+    def test_currency_int(self):
+        val = int(self.c)
+        assert val == 1234
+        assert type(val) == int
+
+    def test_currency_long(self):
+        val = long(self.c)
+        assert val == 1234
+        assert type(val) == long
diff --git a/bluechips/tests/model/test_currency_validator.py b/bluechips/tests/model/test_currency_validator.py
new file mode 100644 (file)
index 0000000..bae8ca9
--- /dev/null
@@ -0,0 +1,30 @@
+from unittest import TestCase
+from formencode import Invalid
+
+from bluechips.model import types
+
+class TestCurrencyValidator(TestCase):
+    def setUp(self):
+        self.v = types.CurrencyValidator()
+
+    def test_currency_validator_good(self):
+        assert (self.v.to_python('12.34') ==
+                types.Currency('12.34'))
+
+    def test_currency_validator_nonzero(self):
+        try:
+            self.v.to_python('0')
+        except Invalid:
+            pass
+
+    def test_currency_validator_precision(self):
+        try:
+            self.v.to_python('12.345')
+        except Invalid:
+            pass
+
+    def test_currency_validator_amount(self):
+        try:
+            self.v.to_python('foo')
+        except Invalid:
+            pass
index 3af4af62e0c84db2042810c8a557749f27ffe69c..1e38ef471e43c63264f47a2193c37b790fd8b2a0 100644 (file)
@@ -1,18 +1,19 @@
 from unittest import TestCase
 from bluechips import model
+from bluechips.model import meta
 from bluechips.model.types import Currency
 
 class TestExpenditure(TestCase):
     def setUp(self):
-        self.u = model.User('chaz', u'Charles Root', False)
-        self.e = model.Expenditure(self.u, Currency('445.27'),
+        self.u = model.User(u'chaz', u'Charles Root', False)
+        self.e = model.Expenditure(self.u, Currency('444.88'),
                                    u'chaz buys lunch')
 
     def test_constructor(self):
         assert self.e.spender == self.u
-        assert self.e.amount == Currency('445.27')
+        assert self.e.amount == Currency('444.88')
         assert self.e.description == u'chaz buys lunch'
 
     def test_repr(self):
         assert (repr(self.e) == 
-                '<Expenditure: spender: Charles Root spent: $445.27>')
+                '<Expenditure: spender: Charles Root spent: $444.88>')
diff --git a/bluechips/tests/model/test_split.py b/bluechips/tests/model/test_split.py
new file mode 100644 (file)
index 0000000..86c8a9b
--- /dev/null
@@ -0,0 +1,19 @@
+from unittest import TestCase
+from bluechips import model
+from bluechips.model.types import Currency
+
+class TestSplit(TestCase):
+    def setUp(self):
+        self.u = model.User('chaz', u'Charles Root', False)
+        self.e = model.Expenditure(self.u, Currency('12.34'),
+                                   u'A test expenditure')
+        self.sp = model.Split(self.e, self.u, Currency('5.55'))
+
+    def test_constructor(self):
+        assert self.sp.expenditure == self.e
+        assert self.sp.user == self.u
+        assert self.sp.share == Currency('5.55')
+
+    def test_repr(self):
+        assert (repr(self.sp) == '<Split: expense: %s user: %s share: %s>' %
+                (self.sp.expenditure, self.sp.user, self.sp.share))