summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorScott Gasch <[email protected]>2021-07-28 22:13:43 -0700
committerScott Gasch <[email protected]>2021-07-28 22:13:43 -0700
commit4faa994d32223c8d560d9dad0ca90a3f7eb10d6a (patch)
tree1200e5615d94247a120b98a4ddceb2cd6674e0af /tests
parentc79ecbf708a63a54a9c3e8d189b65d4794930082 (diff)
Money, Rate, CentCount and a bunch of bugfixes.
Diffstat (limited to 'tests')
-rwxr-xr-xtests/ansi_test.py19
-rwxr-xr-xtests/centcount_test.py102
-rwxr-xr-xtests/money_test.py103
-rwxr-xr-xtests/rate_test.py77
-rwxr-xr-xtests/string_utils_test.py6
5 files changed, 307 insertions, 0 deletions
diff --git a/tests/ansi_test.py b/tests/ansi_test.py
new file mode 100755
index 0000000..4c1f449
--- /dev/null
+++ b/tests/ansi_test.py
@@ -0,0 +1,19 @@
+#!/usr/bin/env python3
+
+import unittest
+
+import ansi
+import unittest_utils as uu
+
+
+class TestAnsi(unittest.TestCase):
+
+ def test_colorizer(self):
+ with ansi.Colorizer() as c:
+ print("testing...")
+ print("Section:")
+ print(" This is some detail.")
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/tests/centcount_test.py b/tests/centcount_test.py
new file mode 100755
index 0000000..3122b98
--- /dev/null
+++ b/tests/centcount_test.py
@@ -0,0 +1,102 @@
+#!/usr/bin/env python3
+
+import unittest
+
+from type.centcount import CentCount
+import unittest_utils as uu
+
+
+class TestCentCount(unittest.TestCase):
+
+ def test_basic_utility(self):
+ amount = CentCount(1.45)
+ another = CentCount.parse("USD 1.45")
+ self.assertEqual(amount, another)
+
+ def test_negation(self):
+ amount = CentCount(1.45)
+ amount = -amount
+ self.assertEqual(CentCount(-1.45), amount)
+
+ def test_addition_and_subtraction(self):
+ amount = CentCount(1.00)
+ another = CentCount(2.00)
+ total = amount + another
+ self.assertEqual(CentCount(3.00), total)
+ delta = another - amount
+ self.assertEqual(CentCount(1.00), delta)
+ neg = amount - another
+ self.assertEqual(CentCount(-1.00), neg)
+ neg += another
+ self.assertEqual(CentCount(1.00), neg)
+ neg += 1.00
+ self.assertEqual(CentCount(2.00), neg)
+ neg -= 1.00
+ self.assertEqual(CentCount(1.00), neg)
+ x = 1000 - amount
+ self.assertEqual(CentCount(9.0), x)
+
+ def test_multiplication(self):
+ amount = CentCount(3.00)
+ amount *= 3
+ self.assertEqual(CentCount(9.00), amount)
+ with self.assertRaises(TypeError):
+ another = CentCount(0.33)
+ amount *= another
+
+ def test_division(self):
+ amount = CentCount(10.00)
+ x = amount / 5.0
+ self.assertEqual(CentCount(2.00), x)
+ with self.assertRaises(TypeError):
+ another = CentCount(1.33)
+ amount /= another
+
+ def test_equality(self):
+ usa = CentCount(1.0, 'USD')
+ can = CentCount(1.0, 'CAD')
+ self.assertNotEqual(usa, can)
+ eh = CentCount(1.0, 'CAD')
+ self.assertEqual(can, eh)
+
+ def test_comparison(self):
+ one = CentCount(1.0)
+ two = CentCount(2.0)
+ three = CentCount(3.0)
+ neg_one = CentCount(-1)
+ self.assertLess(one, two)
+ self.assertLess(neg_one, one)
+ self.assertGreater(one, neg_one)
+ self.assertGreater(three, one)
+ looney = CentCount(1.0, 'CAD')
+ with self.assertRaises(TypeError):
+ print(looney < one)
+
+ def test_strict_mode(self):
+ one = CentCount(1.0, strict_mode=True)
+ two = CentCount(2.0, strict_mode=True)
+ with self.assertRaises(TypeError):
+ x = one + 2.4
+ self.assertEqual(CentCount(3.0), one + two)
+ with self.assertRaises(TypeError):
+ x = two - 1.9
+ self.assertEqual(CentCount(1.0), two - one)
+ with self.assertRaises(TypeError):
+ print(one == 1.0)
+ self.assertTrue(CentCount(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 = CentCount(10.0)
+ x = ten * 2 / 3
+ x.truncate_fractional_cents()
+ self.assertEqual(CentCount(6.66), x)
+
+
+if __name__ == '__main__':
+ unittest.main()
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()
diff --git a/tests/rate_test.py b/tests/rate_test.py
new file mode 100755
index 0000000..621539b
--- /dev/null
+++ b/tests/rate_test.py
@@ -0,0 +1,77 @@
+#!/usr/bin/env python3
+
+import unittest
+
+from type.rate import Rate
+from type.money import Money
+
+import unittest_utils as uu
+
+
+class TestRate(unittest.TestCase):
+ def test_basic_utility(self):
+ my_stock_returns = Rate(percent_change=-20.0)
+ my_portfolio = 1000.0
+ self.assertAlmostEqual(
+ 800.0,
+ my_stock_returns.apply_to(my_portfolio)
+ )
+
+ my_bond_returns = Rate(percentage=104.5)
+ my_money = Money(500.0)
+ self.assertAlmostEqual(
+ Money(522.5),
+ my_bond_returns.apply_to(my_money)
+ )
+
+ my_multiplier = Rate(multiplier=1.72)
+ my_nose_length = 3.2
+ self.assertAlmostEqual(
+ 5.504,
+ my_multiplier.apply_to(my_nose_length)
+ )
+
+ def test_conversions(self):
+ x = Rate(104.55)
+ s = x.__repr__()
+ y = Rate(s)
+ self.assertAlmostEqual(x, y)
+ f = float(x)
+ z = Rate(f)
+ self.assertAlmostEqual(x, z)
+
+ def test_divide(self):
+ x = Rate(20.0)
+ x /= 2
+ self.assertAlmostEqual(10.0, x)
+ x = Rate(-20.0)
+ x /= 2
+ self.assertAlmostEqual(-10.0, x)
+
+ def test_add(self):
+ x = Rate(5.0)
+ y = Rate(10.0)
+ z = x + y
+ self.assertAlmostEqual(15.0, z)
+ x = Rate(-5.0)
+ x += y
+ self.assertAlmostEqual(5.0, x)
+
+ def test_sub(self):
+ x = Rate(5.0)
+ y = Rate(10.0)
+ z = x - y
+ self.assertAlmostEqual(-5.0, z)
+ z = y - x
+ self.assertAlmostEqual(5.0, z)
+
+ def test_repr(self):
+ x = Rate(percent_change=-50.0)
+ s = x.__repr__(relative=True)
+ self.assertEqual("-50.000%", s)
+ s = x.__repr__()
+ self.assertEqual("+50.000%", s)
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/tests/string_utils_test.py b/tests/string_utils_test.py
index 0472daa..cc57036 100755
--- a/tests/string_utils_test.py
+++ b/tests/string_utils_test.py
@@ -180,6 +180,12 @@ class TestStringUtils(unittest.TestCase):
self.assertFalse(su.is_snake_case('thisIsATest'))
self.assertTrue(su.is_snake_case('this_is_a_test'))
+ def test_sprintf_context(self):
+ with su.SprintfStdout() as buf:
+ print("This is a test.")
+ print("This is another one.")
+ self.assertEqual('This is a test.\nThis is another one.\n', buf())
+
if __name__ == '__main__':
bootstrap.initialize(unittest.main)()