From 4faa994d32223c8d560d9dad0ca90a3f7eb10d6a Mon Sep 17 00:00:00 2001 From: Scott Gasch Date: Wed, 28 Jul 2021 22:13:43 -0700 Subject: Money, Rate, CentCount and a bunch of bugfixes. --- tests/money_test.py | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100755 tests/money_test.py (limited to 'tests/money_test.py') diff --git a/tests/money_test.py b/tests/money_test.py new file mode 100755 index 0000000..57f4637 --- /dev/null +++ b/tests/money_test.py @@ -0,0 +1,103 @@ +#!/usr/bin/env python3 + +import unittest + +from type.money import Money +import unittest_utils as uu + + +class TestMoney(unittest.TestCase): + + def test_basic_utility(self): + amount = Money(1.45) + another = Money.parse("USD 1.45") + self.assertAlmostEqual(amount.amount, another.amount) + + def test_negation(self): + amount = Money(1.45) + amount = -amount + self.assertAlmostEqual(Money(-1.45).amount, amount.amount) + + def test_addition_and_subtraction(self): + amount = Money(1.00) + another = Money(2.00) + total = amount + another + self.assertEqual(Money(3.00), total) + delta = another - amount + self.assertEqual(Money(1.00), delta) + neg = amount - another + self.assertEqual(Money(-1.00), neg) + neg += another + self.assertEqual(Money(1.00), neg) + neg += 1.00 + self.assertEqual(Money(2.00), neg) + neg -= 1 + self.assertEqual(Money(1.00), neg) + x = 10 - amount + self.assertEqual(Money(9.0), x) + + def test_multiplication(self): + amount = Money(3.00) + amount *= 3 + self.assertEqual(Money(9.00), amount) + with self.assertRaises(TypeError): + another = Money(0.33) + amount *= another + + def test_division(self): + amount = Money(10.00) + x = amount / 5.0 + self.assertEqual(Money(2.00), x) + with self.assertRaises(TypeError): + another = Money(1.33) + amount /= another + + def test_equality(self): + usa = Money(1.0, 'USD') + can = Money(1.0, 'CAD') + self.assertNotEqual(usa, can) + eh = Money(1.0, 'CAD') + self.assertEqual(can, eh) + + def test_comparison(self): + one = Money(1.0) + two = Money(2.0) + three = Money(3.0) + neg_one = Money(-1) + self.assertLess(one, two) + self.assertLess(neg_one, one) + self.assertGreater(one, neg_one) + self.assertGreater(three, one) + looney = Money(1.0, 'CAD') + with self.assertRaises(TypeError): + print(looney < one) + + def test_strict_mode(self): + one = Money(1.0, strict_mode=True) + two = Money(2.0, strict_mode=True) + with self.assertRaises(TypeError): + x = one + 2.4 + self.assertEqual(Money(3.0), one + two) + with self.assertRaises(TypeError): + x = two - 1.9 + self.assertEqual(Money(1.0), two - one) + with self.assertRaises(TypeError): + print(one == 1.0) + self.assertTrue(Money(1.0) == one) + with self.assertRaises(TypeError): + print(one < 2.0) + self.assertTrue(one < two) + with self.assertRaises(TypeError): + print(two > 1.0) + self.assertTrue(two > one) + + def test_truncate_and_round(self): + ten = Money(10.0) + x = ten * 2 / 3 + self.assertEqual(6.66, x.truncate_fractional_cents()) + x = ten * 2 / 3 + self.assertEqual(6.67, x.round_fractional_cents()) + + +if __name__ == '__main__': + unittest.main() -- cgit v1.3