]> asedeno.scripts.mit.edu Git - bluechips.git/blob - bluechips/tests/model/test_currency.py
a689778eea64c7c9f32d3d45bf73c31f6dd01b78
[bluechips.git] / bluechips / tests / model / test_currency.py
1 from unittest import TestCase
2 from bluechips.tests import *
3 from bluechips.model.types import Currency
4 from decimal import Decimal
5
6 class TestCurrency(TestCase):
7     def test_initInt(self):
8         """
9         Make sure the constructor for Currency works
10         """
11         self.assert_(Currency(1) is Currency(1), 
12                      "Currency objects not interned")
13     def test_initString(self):
14         """
15         Make sure the constructor for Currency works with strings
16         """
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")
20     
21     def test_string(self):
22         """
23         Test converting a Currency to a string
24         """
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")
31     
32     def test_stringNoDollar(self):
33         """
34         Test that Currency values can be retrieved without the dollar sign
35         """
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")
42     
43     def test_additionMath(self):
44         """
45         Confirm that addition works over currency types and ints
46         """
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))
50     
51     def test_additionType(self):
52         """
53         Adding Currencies or a Currency and an int should yield a
54         Currency
55         """
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)
59         
60     def test_multMath(self):
61         """
62         This test tests the same 3 things as ``test_addition``, but
63         for multiplication
64         """
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))
68     
69     def test_multType(self):
70         """
71         The result of multiplying a Currency with something else
72         should be a currency
73         """
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)