77a22b2ff182614c5b2afe1a195e237cdac70d40
[utils.git] / src / budget_sync / test / test_money.py
1 import unittest
2 from budget_sync.money import Money
3
4
5 class TestMoney(unittest.TestCase):
6 def test_str(self):
7 self.assertEqual("123.45", str(Money(cents=12345)))
8 self.assertEqual("123", str(Money(cents=12300)))
9 self.assertEqual("123.40", str(Money(cents=12340)))
10 self.assertEqual("120", str(Money(cents=12000)))
11 self.assertEqual("-123.45", str(Money(cents=-12345)))
12 self.assertEqual("-123", str(Money(cents=-12300)))
13 self.assertEqual("-123.40", str(Money(cents=-12340)))
14 self.assertEqual("-120", str(Money(cents=-12000)))
15 self.assertEqual("0", str(Money(cents=0)))
16
17 def test_from_str(self):
18 with self.assertRaisesRegex(TypeError, "^Can't use Money\\.from_str to convert from non-str value$"):
19 Money.from_str(123)
20 with self.assertRaisesRegex(ValueError, "^invalid Money string: missing digits$"):
21 Money.from_str("")
22 with self.assertRaisesRegex(ValueError, "^invalid Money string: missing digits$"):
23 Money.from_str(".")
24 with self.assertRaisesRegex(ValueError, "^invalid Money string: missing digits$"):
25 Money.from_str("-")
26 with self.assertRaisesRegex(ValueError, "^invalid Money string: missing digits$"):
27 Money.from_str("-.")
28 with self.assertRaisesRegex(ValueError, "^invalid Money string: characters after sign and before first `\\.` must be ascii digits$"):
29 Money.from_str("+asdjkhfk")
30 with self.assertRaisesRegex(ValueError, "^invalid Money string: too many digits after `\\.`$"):
31 Money.from_str("12.345")
32 with self.assertRaisesRegex(ValueError, "^invalid Money string: too many `\\.` characters$"):
33 Money.from_str("12.3.4")
34 self.assertEqual(Money(cents=0), Money.from_str("0"))
35 self.assertEqual(Money(cents=0), Money.from_str("+0"))
36 self.assertEqual(Money(cents=0), Money.from_str("-0"))
37 self.assertEqual(Money(cents=-1000), Money.from_str("-010"))
38 self.assertEqual(Money(cents=1000), Money.from_str("+010"))
39 self.assertEqual(Money(cents=1000), Money.from_str("010"))
40 self.assertEqual(Money(cents=1000), Money.from_str("10"))
41 self.assertEqual(Money(cents=1234), Money.from_str("12.34"))
42 self.assertEqual(Money(cents=-1234), Money.from_str("-12.34"))
43 self.assertEqual(Money(cents=-1234), Money.from_str("-12.34"))
44 self.assertEqual(Money(cents=1230), Money.from_str("12.3"))
45 self.assertEqual(Money(cents=1200), Money.from_str("12."))
46 self.assertEqual(Money(cents=1200), Money.from_str("12"))
47 self.assertEqual(Money(cents=0), Money.from_str(".0"))
48 self.assertEqual(Money(cents=10), Money.from_str(".1"))
49 self.assertEqual(Money(cents=12), Money.from_str(".12"))
50 self.assertEqual(Money(cents=-12), Money.from_str("-.12"))
51
52 # FIXME(programmerjake): add other methods
53
54
55 if __name__ == "__main__":
56 unittest.main()