From a59d19c0581702321ebb6f89ab20f158cdc9d7bc Mon Sep 17 00:00:00 2001 From: Scott Torborg Date: Sat, 7 Nov 2009 15:53:24 -1000 Subject: [PATCH] more test coverage --- bluechips/lib/permissions.py | 4 +- bluechips/model/subitem.py | 11 ++- bluechips/tests/model/test_currency.py | 89 ++++--------------- .../tests/model/test_currency_validator.py | 30 +++++++ bluechips/tests/model/test_expenditure.py | 9 +- bluechips/tests/model/test_split.py | 19 ++++ 6 files changed, 77 insertions(+), 85 deletions(-) create mode 100644 bluechips/tests/model/test_currency_validator.py create mode 100644 bluechips/tests/model/test_split.py diff --git a/bluechips/lib/permissions.py b/bluechips/lib/permissions.py index 63c9c5c..4a30fdc 100644 --- a/bluechips/lib/permissions.py +++ b/bluechips/lib/permissions.py @@ -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): diff --git a/bluechips/model/subitem.py b/bluechips/model/subitem.py index 81cadfa..2bbbf9b 100644 --- a/bluechips/model/subitem.py +++ b/bluechips/model/subitem.py @@ -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 '' % (self.expense, - self.user, - self.amount) + return ('' % + (self.expense, self.user, self.amount)) # pragma: nocover __all__ = ['Subitem'] diff --git a/bluechips/tests/model/test_currency.py b/bluechips/tests/model/test_currency.py index a689778..4b8bdb4 100644 --- a/bluechips/tests/model/test_currency.py +++ b/bluechips/tests/model/test_currency.py @@ -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 index 0000000..bae8ca9 --- /dev/null +++ b/bluechips/tests/model/test_currency_validator.py @@ -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 diff --git a/bluechips/tests/model/test_expenditure.py b/bluechips/tests/model/test_expenditure.py index 3af4af6..1e38ef4 100644 --- a/bluechips/tests/model/test_expenditure.py +++ b/bluechips/tests/model/test_expenditure.py @@ -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) == - '') + '') diff --git a/bluechips/tests/model/test_split.py b/bluechips/tests/model/test_split.py new file mode 100644 index 0000000..86c8a9b --- /dev/null +++ b/bluechips/tests/model/test_split.py @@ -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) == '' % + (self.sp.expenditure, self.sp.user, self.sp.share)) -- 2.45.2