1 from unittest import TestCase
2 from bluechips.tests import *
3 from bluechips.model.types import Currency
4 from decimal import Decimal
6 class TestCurrency(TestCase):
7 def test_initInt(self):
9 Make sure the constructor for Currency works
11 self.assert_(Currency(1) is Currency(1),
12 "Currency objects not interned")
13 def test_initString(self):
15 Make sure the constructor for Currency works with strings
17 self.assertEqual(Currency("0.01"), Currency(1))
18 self.assert_(Currency("0.01") is Currency(1),
19 "string and int constructors return different values")
21 def test_string(self):
23 Test converting a Currency to a string
25 self.assertEqual(str(Currency(1)), "$0.01")
26 self.assertEqual(str(Currency(100)), "$1.00")
27 self.assertEqual(str(Currency(101)), "$1.01")
28 self.assertEqual(str(Currency(-1)), "-$0.01")
29 self.assertEqual(str(Currency(-100)), "-$1.00")
30 self.assertEqual(str(Currency(-101)), "-$1.01")
32 def test_stringNoDollar(self):
34 Test that Currency values can be retrieved without the dollar sign
36 self.assertEqual(Currency(1).__str_no_dollar__(), "0.01")
37 self.assertEqual(Currency(100).__str_no_dollar__(), "1.00")
38 self.assertEqual(Currency(101).__str_no_dollar__(), "1.01")
39 self.assertEqual(Currency(-1).__str_no_dollar__(), "-0.01")
40 self.assertEqual(Currency(-100).__str_no_dollar__(), "-1.00")
41 self.assertEqual(Currency(-101).__str_no_dollar__(), "-1.01")
43 def test_additionMath(self):
45 Confirm that addition works over currency types and ints
47 self.assertEqual(Currency(2) + 2, Currency(4))
48 self.assertEqual(2 + Currency(2), Currency(4))
49 self.assertEqual(Currency(2) + Currency(2), Currency(4))
51 def test_additionType(self):
53 Adding Currencies or a Currency and an int should yield a
56 self.assertEqual(type(Currency(2) + 2), Currency)
57 self.assertEqual(type(2 + Currency(2)), Currency)
58 self.assertEqual(type(Currency(2) + Currency(2)), Currency)
60 def test_multMath(self):
62 This test tests the same 3 things as ``test_addition``, but
65 self.assertEqual(Currency(100) * Decimal("0.25"), Currency(25))
66 self.assertEqual(Decimal("0.25") * Currency(100), Currency(25))
67 self.assertEqual(Currency(10) * Currency(10), Currency(100))
69 def test_multType(self):
71 The result of multiplying a Currency with something else
74 self.assertEqual(type(Currency(100) * Decimal("0.25")), Currency)
75 self.assertEqual(type(Decimal("0.25") * Currency(100)), Currency)
76 self.assertEqual(type(Currency(100) * Currency(100)), Currency)