from decimal import Decimal
class TestCurrency(TestCase):
- def test_initialization(self):
+ 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),
- "Currency string conversion breaks")
+ "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):
"""
def test_additionType(self):
"""
- Check that adding Currencies or a Currency and an int yields a
+ 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_multiplication(self):
+ def test_multMath(self):
"""
This test tests the same 3 things as ``test_addition``, but
for multiplication
"""
- assert Currency(100) * Decimal(0.25) is Currency(25), \
- "Currency * Decimal is Currency"
- assert Decimal(0.25) * Currency(100) is Currency(25), \
- "Decimal * Currency is Currency"
+ 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)