summaryrefslogtreecommitdiff
path: root/tests/centcount_test.py
blob: 3122b984c4459e046068ee7adbbb9cffcea819dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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()