diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/python/numerics/t_amount.py | 505 |
1 files changed, 376 insertions, 129 deletions
diff --git a/tests/python/numerics/t_amount.py b/tests/python/numerics/t_amount.py index 856e13ee..f7eeee8f 100644 --- a/tests/python/numerics/t_amount.py +++ b/tests/python/numerics/t_amount.py @@ -1,27 +1,36 @@ # -*- coding: utf-8 -*- -import sys - import unittest import exceptions import operator -from ledger import amount +from ledger import * +from StringIO import * internalAmount = amount.exact -class AmountTestCase(unittest.TestCase): +class t_amountTestCase(unittest.TestCase): + testSession = None + + def assertValid(self, amt): + self.assertTrue(amt.valid()) + def setUp(self): + #self.testSession = session() + #set_session_context(self.testSession) + # Cause the display precision for dollars to be initialized to 2. x1 = amount("$1.00") self.assertTrue(x1) - amount.full_strings = True # makes error reports from UnitTests accurate - + + #amount.stream_fullstrings = True # make reports from UnitTests accurate + def tearDown(self): - amount.full_strings = False + pass + #amount.stream_fullstrings = False - def assertValid(self, amt): - self.assertTrue(amt.valid()) + #set_session_context() + #self.testSession = None def testParser(self): x0 = amount() @@ -32,29 +41,64 @@ class AmountTestCase(unittest.TestCase): x5 = amount(x4) x6 = amount(x4) x7 = amount(x4) - x8 = amount("$123.456") + x8 = amount("$123.45") x9 = amount(x8) x10 = amount(x8) x11 = amount(x8) x12 = amount("$100") - - self.assertEqual(3, x12.commodity().precision()) - + + self.assertEqual(2, x12.commodity.precision) + + #buf = "$100..." + #input = StringIO(buf) + #x13 = amount() + #x13.parse(input) + #self.assertEqual(x12, x13) + + x14 = amount() + self.assertRaises(exceptions.ArithmeticError, lambda: x14.parse("DM")) + + x15 = amount("$1.000.000,00") + + x16 = amount("$2000") + self.assertEqual("$2.000,00", x16.to_string()) + x16.parse("$2000,00") + self.assertEqual("$2.000,00", x16.to_string()) + + # Since European-ness is an additive quality, we must switch back + # to American-ness manually + x15.commodity.drop_flags(COMMODITY_STYLE_EUROPEAN) + + x17 = amount("$1,000,000.00") + + x18 = amount("$2000") + self.assertEqual("$2,000.00", x18.to_string()) + x18.parse("$2,000") + self.assertEqual("$2,000.00", x18.to_string()) + + self.assertEqual(x15, x17) + + x19 = amount("EUR 1000") + x20 = amount("EUR 1000") + + self.assertEqual("EUR 1000", x19.to_string()) + self.assertEqual("EUR 1000", x20.to_string()) + x1.parse("$100.0000", AMOUNT_PARSE_NO_MIGRATE) - self.assertEqual(3, x12.commodity().precision()) - self.assertEqual(x1.commodity(), x12.commodity()) + self.assertEqual(2, x12.commodity.precision) + self.assertEqual(x1.commodity, x12.commodity) self.assertEqual(x1, x12) - + x0.parse("$100.0000") - self.assertEqual(4, x12.commodity().precision()) - self.assertEqual(x0.commodity(), x12.commodity()) + self.assertEqual(4, x12.commodity.precision) + self.assertEqual(x0.commodity, x12.commodity) self.assertEqual(x0, x12) - + x2.parse("$100.00", AMOUNT_PARSE_NO_REDUCE) self.assertEqual(x2, x12) x3.parse("$100.00", AMOUNT_PARSE_NO_MIGRATE | AMOUNT_PARSE_NO_REDUCE) self.assertEqual(x3, x12) - + x4.parse("$100.00") self.assertEqual(x4, x12) x5.parse("$100.00", AMOUNT_PARSE_NO_MIGRATE) @@ -63,7 +107,7 @@ class AmountTestCase(unittest.TestCase): self.assertEqual(x6, x12) x7.parse("$100.00", AMOUNT_PARSE_NO_MIGRATE | AMOUNT_PARSE_NO_REDUCE) self.assertEqual(x7, x12) - + x8.parse("$100.00") self.assertEqual(x8, x12) x9.parse("$100.00", AMOUNT_PARSE_NO_MIGRATE) @@ -72,7 +116,7 @@ class AmountTestCase(unittest.TestCase): self.assertEqual(x10, x12) x11.parse("$100.00", AMOUNT_PARSE_NO_MIGRATE | AMOUNT_PARSE_NO_REDUCE) self.assertEqual(x11, x12) - + self.assertValid(x0) self.assertValid(x1) self.assertValid(x2) @@ -93,16 +137,21 @@ class AmountTestCase(unittest.TestCase): x3 = amount(123.456) x5 = amount("123456") x6 = amount("123.456") + x7 = amount("123456") + x8 = amount("123.456") x9 = amount(x3) x10 = amount(x6) + x11 = amount(x8) - self.assertRaises(exceptions.ArithmeticError, operator.eq, amount(0), x0) - self.assertRaises(exceptions.ArithmeticError, operator.eq, amount(), x0) - self.assertRaises(exceptions.ArithmeticError, operator.eq, amount("0"), x0) - self.assertRaises(exceptions.ArithmeticError, operator.eq, amount("0.0"), x0) + self.assertRaises(exceptions.ArithmeticError, lambda: amount(0) == x0) + self.assertRaises(exceptions.ArithmeticError, lambda: amount() == x0) + self.assertRaises(exceptions.ArithmeticError, lambda: amount("0") == x0) + self.assertRaises(exceptions.ArithmeticError, lambda: amount("0.0") == x0) self.assertEqual(x2, x1) self.assertEqual(x5, x1) + self.assertEqual(x7, x1) self.assertEqual(x6, x3) + self.assertEqual(x8, x3) self.assertEqual(x10, x3) self.assertEqual(x10, x9) @@ -112,19 +161,22 @@ class AmountTestCase(unittest.TestCase): self.assertValid(x3) self.assertValid(x5) self.assertValid(x6) + self.assertValid(x7) + self.assertValid(x8) self.assertValid(x9) self.assertValid(x10) + self.assertValid(x11) def testCommodityConstructors(self): - x1 = amount("$123.45") - x2 = amount("-$123.45") - x3 = amount("$-123.45") - x4 = amount("DM 123.45") - x5 = amount("-DM 123.45") - x6 = amount("DM -123.45") - x7 = amount("123.45 euro") - x8 = amount("-123.45 euro") - x9 = amount("123.45€") + x1 = amount("$123.45") + x2 = amount("-$123.45") + x3 = amount("$-123.45") + x4 = amount("DM 123.45") + x5 = amount("-DM 123.45") + x6 = amount("DM -123.45") + x7 = amount("123.45 euro") + x8 = amount("-123.45 euro") + x9 = amount("123.45€") x10 = amount("-123.45€") self.assertEqual(amount("$123.45"), x1) @@ -161,18 +213,22 @@ class AmountTestCase(unittest.TestCase): self.assertValid(x10) def testAssignment(self): - x0 = amount() - x1 = amount(123456) - x2 = amount(123456L) - x3 = amount(123.456) - x5 = amount("123456") - x6 = amount("123.456") - x9 = x3 + x0 = amount() + x1 = amount(123456) + x2 = amount(123456L) + x3 = amount(123.456) + x5 = amount("123456") + x6 = amount("123.456") + x7 = "123456" + x8 = "123.456" + x9 = amount(x3) x10 = amount(x6) self.assertEqual(x2, x1) self.assertEqual(x5, x1) + self.assertEqual(x7, x1) self.assertEqual(x6, x3) + self.assertEqual(x8, x3) self.assertEqual(x10, x3) self.assertEqual(x10, x9) @@ -181,12 +237,16 @@ class AmountTestCase(unittest.TestCase): x3 = amount(123.456) x5 = amount("123456") x6 = amount("123.456") + x7 = amount("123456") + x8 = amount("123.456") x9 = x3 x10 = amount(x6) self.assertEqual(x2, x1) self.assertEqual(x5, x1) + self.assertEqual(x7, x1) self.assertEqual(x6, x3) + self.assertEqual(x8, x3) self.assertEqual(x10, x3) self.assertEqual(x10, x9) @@ -201,6 +261,8 @@ class AmountTestCase(unittest.TestCase): self.assertValid(x3) self.assertValid(x5) self.assertValid(x6) + self.assertValid(x7) + self.assertValid(x8) self.assertValid(x9) self.assertValid(x10) @@ -255,18 +317,28 @@ class AmountTestCase(unittest.TestCase): x3 = amount(333333) x4 = amount(123456.0) x5 = amount("123456.0") + x6 = amount(123456.0) self.assertTrue(x1 == 123456) self.assertTrue(x1 != x2) self.assertTrue(x1 == (x2 - x3)) self.assertTrue(x1 == x4) self.assertTrue(x4 == x5) + self.assertTrue(x4 == x6) + + self.assertTrue(x1 == 123456) + self.assertTrue(123456 == x1) + self.assertTrue(x1 == 123456L) + self.assertTrue(123456L == x1) + self.assertTrue(x1 == 123456.0) + self.assertTrue(123456.0 == x1) self.assertValid(x1) self.assertValid(x2) self.assertValid(x3) self.assertValid(x4) self.assertValid(x5) + self.assertValid(x6) def testCommodityEquality(self): x0 = amount() @@ -282,10 +354,12 @@ class AmountTestCase(unittest.TestCase): x10 = amount("-123.45€") self.assertTrue(x0.is_null()) - self.assertRaises(exceptions.ArithmeticError, amount.is_zero, x0) - self.assertRaises(exceptions.ArithmeticError, amount.is_realzero, x0) - self.assertRaises(exceptions.ArithmeticError, amount.sign, x0) - self.assertRaises(exceptions.ArithmeticError, amount.compare, x0, 0) + self.assertRaises(exceptions.ArithmeticError, lambda: x0.is_zero()) + self.assertRaises(exceptions.ArithmeticError, lambda: x0.is_realzero()) + self.assertRaises(exceptions.ArithmeticError, lambda: x0.sign() == 0) + self.assertRaises(exceptions.ArithmeticError, lambda: x0.compare(x1) < 0) + self.assertRaises(exceptions.ArithmeticError, lambda: x0.compare(x2) > 0) + self.assertRaises(exceptions.ArithmeticError, lambda: x0.compare(x0) == 0) self.assertTrue(x1 != x2) self.assertTrue(x1 != x4) @@ -318,12 +392,12 @@ class AmountTestCase(unittest.TestCase): x5 = amount("-123.45") x6 = amount("123.45") - self.assertRaises(exceptions.ArithmeticError, operator.gt, x0, x1) - self.assertRaises(exceptions.ArithmeticError, operator.lt, x0, x2) - self.assertRaises(exceptions.ArithmeticError, operator.gt, x0, x3) - self.assertRaises(exceptions.ArithmeticError, operator.lt, x0, x4) - self.assertRaises(exceptions.ArithmeticError, operator.gt, x0, x5) - self.assertRaises(exceptions.ArithmeticError, operator.lt, x0, x6) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 > x1) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 < x2) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 > x3) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 < x4) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 > x5) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 < x6) self.assertTrue(x1 > x3) self.assertTrue(x3 <= x5) @@ -332,10 +406,10 @@ class AmountTestCase(unittest.TestCase): self.assertTrue(x3 < x4) self.assertTrue(x1 < 100) - self.assertTrue(x1 < 100L) - self.assertTrue(x1 < 100.0) self.assertTrue(100 > x1) + self.assertTrue(x1 < 100L) self.assertTrue(100L > x1) + self.assertTrue(x1 < 100.0) self.assertTrue(100.0 > x1) self.assertValid(x0) @@ -353,6 +427,7 @@ class AmountTestCase(unittest.TestCase): x4 = amount(internalAmount("$123.4544")) x5 = amount("$-123.45") x6 = amount("$123.45") + x7 = amount("DM 123.45") self.assertTrue(x1 > x3) self.assertTrue(x3 <= x5) @@ -361,6 +436,8 @@ class AmountTestCase(unittest.TestCase): self.assertFalse(x3 == x5) self.assertTrue(x3 < x1) self.assertTrue(x3 < x4) + self.assertFalse(x6 == x7) + self.assertRaises(exceptions.ArithmeticError, lambda: x6 < x7) self.assertValid(x1) self.assertValid(x2) @@ -370,6 +447,7 @@ class AmountTestCase(unittest.TestCase): self.assertValid(x6) def testIntegerAddition(self): + x0 = amount() x1 = amount(123) y1 = amount(456) @@ -386,6 +464,7 @@ class AmountTestCase(unittest.TestCase): self.assertEqual(amount("246913578246913578246913578"), x4 + x4) + self.assertValid(x0) self.assertValid(x1) self.assertValid(y1) self.assertValid(x4) @@ -430,13 +509,15 @@ class AmountTestCase(unittest.TestCase): self.assertEqual("$246.90", (x1 + x1).to_string()) self.assertEqual("$246.91", (x1 + x2).to_string()) - self.assertRaises(exceptions.ArithmeticError, operator.add, x1, x0) - self.assertRaises(exceptions.ArithmeticError, operator.add, x1, x3) - self.assertRaises(exceptions.ArithmeticError, operator.add, x1, x4) - self.assertRaises(exceptions.ArithmeticError, operator.add, x1, x5) - self.assertRaises(exceptions.ArithmeticError, operator.add, x1, x6) - self.assertRaises(exceptions.ArithmeticError, operator.add, x1, 123.45) - self.assertRaises(exceptions.ArithmeticError, operator.add, x1, 123) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 + x0) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 + x1) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 + x0) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 + x3) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 + x4) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 + x5) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 + x6) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 + 123.45) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 + 123) self.assertEqual(amount("DM 246.90"), x3 + x3) self.assertEqual(amount("246.90 euro"), x4 + x4) @@ -536,13 +617,15 @@ class AmountTestCase(unittest.TestCase): self.assertEqual("$0.00", (x1 - x1).to_string()) self.assertEqual("$-0.01", (x1 - x2).to_string()) - self.assertRaises(exceptions.ArithmeticError, operator.sub, x1, x0) - self.assertRaises(exceptions.ArithmeticError, operator.sub, x1, x3) - self.assertRaises(exceptions.ArithmeticError, operator.sub, x1, x4) - self.assertRaises(exceptions.ArithmeticError, operator.sub, x1, x5) - self.assertRaises(exceptions.ArithmeticError, operator.sub, x1, x6) - self.assertRaises(exceptions.ArithmeticError, operator.sub, x1, 123.45) - self.assertRaises(exceptions.ArithmeticError, operator.sub, x1, 123) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 - x0) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 - x1) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 - x0) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 - x3) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 - x4) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 - x5) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 - x6) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 - 123.45) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 - 123) self.assertEqual(amount("DM 0.00"), x3 - x3) self.assertEqual(amount("DM 23.45"), x3 - amount("DM 100.00")) @@ -619,7 +702,7 @@ class AmountTestCase(unittest.TestCase): x4 = amount("123456789123456789123456789") self.assertEqual(amount("15241578780673678546105778281054720515622620750190521"), - x4 * x4) + x4 * x4) self.assertValid(x1) self.assertValid(y1) @@ -654,13 +737,14 @@ class AmountTestCase(unittest.TestCase): x2 = amount("123456789123456789.123456789123456789") self.assertEqual(amount("15241578780673678546105778311537878.046486820281054720515622620750190521"), - x2 * x2) + x2 * x2) self.assertValid(x1) self.assertValid(y1) self.assertValid(x2) def testCommodityMultiplication(self): + x0 = amount() x1 = amount("$123.12") y1 = amount("$456.45") x2 = amount(internalAmount("$123.456789")) @@ -686,9 +770,12 @@ class AmountTestCase(unittest.TestCase): self.assertEqual("$15200.00", (x1 * x2).to_string()) self.assertEqual("$15199.99986168", (x2 * x1).to_string()) - self.assertRaises(exceptions.ArithmeticError, operator.mul, x1, x3) - self.assertRaises(exceptions.ArithmeticError, operator.mul, x1, x4) - self.assertRaises(exceptions.ArithmeticError, operator.mul, x1, x5) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 * x0) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 * x1) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 * x0) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 * x3) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 * x4) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 * x5) x1 *= amount("123.12") self.assertEqual(internalAmount("$15158.5344"), x1) @@ -712,14 +799,11 @@ class AmountTestCase(unittest.TestCase): self.assertValid(x5) self.assertValid(x7) - def divideByZero(self, amt): - return amt / 0 - def testIntegerDivision(self): x1 = amount(123) y1 = amount(456) - self.assertRaises(exceptions.ArithmeticError, self.divideByZero, x1) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 / 0) self.assertEqual(amount(0), amount(0) / x1) self.assertEqual(amount(0), 0 / x1) self.assertEqual(x1, x1 / 1) @@ -754,7 +838,7 @@ class AmountTestCase(unittest.TestCase): x1 = amount(123.123) y1 = amount(456.456) - self.assertRaises(exceptions.ArithmeticError, self.divideByZero, x1) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 / 0) self.assertEqual(amount("0.008121959"), amount(1.0) / x1) self.assertEqual(amount("0.008121959"), 1.0 / x1) self.assertEqual(x1, x1 / 1.0) @@ -780,8 +864,7 @@ class AmountTestCase(unittest.TestCase): y4 = amount("56.789") self.assertEqual(amount(1.0), x4 / x4) - self.assertEqual(amount("21739560323910.7554497273748437197344556164046"), - x4 / y4) + self.assertEqual(amount("21739560323910.7554497273748437197344556164046"), x4 / y4) self.assertValid(x1) self.assertValid(y1) @@ -789,6 +872,7 @@ class AmountTestCase(unittest.TestCase): self.assertValid(y4) def testCommodityDivision(self): + x0 = amount() x1 = amount("$123.12") y1 = amount("$456.45") x2 = amount(internalAmount("$123.456789")) @@ -796,7 +880,7 @@ class AmountTestCase(unittest.TestCase): x4 = amount("123.45 euro") x5 = amount("123.45€") - self.assertRaises(exceptions.ArithmeticError, operator.div, x1, 0) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 / 0) self.assertEqual(amount("$0.00"), 0 / x1) self.assertEqual(x1, x1 / 1) self.assertEqual(internalAmount("$0.00812216"), 1 / x1) @@ -814,9 +898,12 @@ class AmountTestCase(unittest.TestCase): self.assertEqual("$1.00", (x1 / x2).to_string()) self.assertEqual("$1.00273545321637426901", (x2 / x1).to_string()) - self.assertRaises(exceptions.ArithmeticError, operator.div, x1, x3) - self.assertRaises(exceptions.ArithmeticError, operator.div, x1, x4) - self.assertRaises(exceptions.ArithmeticError, operator.div, x1, x5) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 / x0) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 / x1) + self.assertRaises(exceptions.ArithmeticError, lambda: x0 / x0) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 / x3) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 / x4) + self.assertRaises(exceptions.ArithmeticError, lambda: x1 / x5) x1 /= amount("123.12") self.assertEqual(internalAmount("$1.00"), x1) @@ -846,14 +933,20 @@ class AmountTestCase(unittest.TestCase): self.assertValid(x7) def testNegation(self): + x0 = amount() x1 = amount(-123456) x3 = amount(-123.456) x5 = amount("-123456") x6 = amount("-123.456") + x7 = amount("-123456") + x8 = amount("-123.456") x9 = amount(- x3) + self.assertRaises(exceptions.ArithmeticError, lambda: x0.negate()) self.assertEqual(x5, x1) + self.assertEqual(x7, x1) self.assertEqual(x6, x3) + self.assertEqual(x8, x3) self.assertEqual(- x6, x9) self.assertEqual(x3.negate(), x9) @@ -865,6 +958,8 @@ class AmountTestCase(unittest.TestCase): self.assertValid(x3) self.assertValid(x5) self.assertValid(x6) + self.assertValid(x7) + self.assertValid(x8) self.assertValid(x9) self.assertValid(x10) @@ -926,9 +1021,9 @@ class AmountTestCase(unittest.TestCase): x1 = amount(-1234) x2 = amount(1234) - self.assertRaises(exceptions.ArithmeticError, amount.abs, x0) - self.assertEqual(amount(1234), abs(x1)) - self.assertEqual(amount(1234), abs(x2)) + self.assertRaises(exceptions.ArithmeticError, lambda: x0.abs()) + self.assertEqual(amount(1234), x1.abs()) + self.assertEqual(amount(1234), x2.abs()) self.assertValid(x0) self.assertValid(x1) @@ -938,22 +1033,52 @@ class AmountTestCase(unittest.TestCase): x1 = amount("$-1234.56") x2 = amount("$1234.56") - self.assertEqual(amount("$1234.56"), abs(x1)) - self.assertEqual(amount("$1234.56"), abs(x2)) + self.assertEqual(amount("$1234.56"), x1.abs()) + self.assertEqual(amount("$1234.56"), x2.abs()) self.assertValid(x1) self.assertValid(x2) def testFractionalRound(self): + x0 = amount() x1 = amount("1234.567890") - self.assertEqual(amount("1234.56789"), x1.round(6)) - self.assertEqual(amount("1234.56789"), x1.round(5)) - self.assertEqual(amount("1234.5679"), x1.round(4)) - self.assertEqual(amount("1234.568"), x1.round(3)) - self.assertEqual(amount("1234.57"), x1.round(2)) - self.assertEqual(amount("1234.6"), x1.round(1)) - self.assertEqual(amount("1235"), x1.round(0)) + self.assertRaises(exceptions.ArithmeticError, lambda: x0.precision) + self.assertRaises(exceptions.ArithmeticError, lambda: x0.round()) + self.assertRaises(exceptions.ArithmeticError, lambda: x0.round(2)) + self.assertRaises(exceptions.ArithmeticError, lambda: x0.unround()) + self.assertEqual(6, x1.precision) + + x1b = amount(x1.unround()) + + self.assertEqual(x1b.precision, x1b.unround().precision) + + y7 = amount(x1.round(7)) + y6 = amount(x1.round(6)) + y5 = amount(x1.round(5)) + y4 = amount(x1.round(4)) + y3 = amount(x1.round(3)) + y2 = amount(x1.round(2)) + y1 = amount(x1.round(1)) + y0 = amount(x1.round(0)) + + self.assertEqual(6, y7.precision) + self.assertEqual(6, y6.precision) + self.assertEqual(5, y5.precision) + self.assertEqual(4, y4.precision) + self.assertEqual(3, y3.precision) + self.assertEqual(2, y2.precision) + self.assertEqual(1, y1.precision) + self.assertEqual(0, y0.precision) + + self.assertEqual(amount("1234.56789"), y7) + self.assertEqual(amount("1234.56789"), y6) + self.assertEqual(amount("1234.56789"), y5) + self.assertEqual(amount("1234.5679"), y4) + self.assertEqual(amount("1234.568"), y3) + self.assertEqual(amount("1234.57"), y2) + self.assertEqual(amount("1234.6"), y1) + self.assertEqual(amount("1235"), y0) x2 = amount("9876.543210") @@ -988,7 +1113,7 @@ class AmountTestCase(unittest.TestCase): x5 = amount("0.0000000000000000000000000000000000001") self.assertEqual(amount("0.0000000000000000000000000000000000001"), - x5.round(37)) + x5.round(37)) self.assertEqual(amount(0), x5.round(36)) self.assertValid(x1) @@ -1074,22 +1199,26 @@ class AmountTestCase(unittest.TestCase): self.assertEqual("$1.13", x1.to_string()) def testReduction(self): - x1 = amount("60s") - x2 = amount("600s") - x3 = amount("6000s") - x4 = amount("360000s") - x5 = amount("10m") # 600s - x6 = amount("100m") # 6000s - x7 = amount("1000m") # 60000s - x8 = amount("10000m") # 600000s - x9 = amount("10h") # 36000s - x10 = amount("100h") # 360000s - x11 = amount("1000h") # 3600000s - x12 = amount("10000h") # 36000000s - + x0 = amount() + x1 = amount("60s") + x2 = amount("600s") + x3 = amount("6000s") + x4 = amount("360000s") + x5 = amount("10m") + x6 = amount("100m") + x7 = amount("1000m") + x8 = amount("10000m") + x9 = amount("10h") + x10 = amount("100h") + x11 = amount("1000h") + x12 = amount("10000h") + + self.assertRaises(exceptions.ArithmeticError, lambda: x0.reduce()) + self.assertRaises(exceptions.ArithmeticError, lambda: x0.unreduce()) self.assertEqual(x2, x5) self.assertEqual(x3, x6) self.assertEqual(x4, x10) + self.assertEqual("100.0h", x4.unreduce().to_string()) def testSign(self): x0 = amount() @@ -1098,7 +1227,7 @@ class AmountTestCase(unittest.TestCase): x3 = amount("1") x4 = amount("-1") - self.assertRaises(exceptions.ArithmeticError, amount.sign, x0) + self.assertRaises(exceptions.ArithmeticError, lambda: x0.sign()) self.assertTrue(x1.sign() > 0) self.assertTrue(x2.sign() < 0) self.assertTrue(x3.sign() > 0) @@ -1131,7 +1260,8 @@ class AmountTestCase(unittest.TestCase): x1 = amount("1234") x2 = amount("1234.56") - self.assertRaises(exceptions.ArithmeticError, operator.truth, x0) + self.assertRaises(exceptions.ArithmeticError, lambda: 1 if x0 else 0) + self.assertTrue(x1) self.assertTrue(x2) @@ -1144,14 +1274,14 @@ class AmountTestCase(unittest.TestCase): x2 = amount("$1234.56") if x1: - self.assertTrue(True) + self.assertTrue(True) else: - self.assertTrue(False) + self.assertTrue(False) if x2: - self.assertTrue(True) + self.assertTrue(True) else: - self.assertTrue(False) + self.assertTrue(False) self.assertValid(x1) self.assertValid(x2) @@ -1161,8 +1291,8 @@ class AmountTestCase(unittest.TestCase): x1 = amount("0.000000000000000000001") self.assertTrue(x1) - self.assertRaises(exceptions.ArithmeticError, amount.is_zero, x0) - self.assertRaises(exceptions.ArithmeticError, amount.is_realzero, x0) + self.assertRaises(exceptions.ArithmeticError, lambda: x0.is_zero()) + self.assertRaises(exceptions.ArithmeticError, lambda: x0.is_realzero()) self.assertFalse(x1.is_zero()) self.assertFalse(x1.is_realzero()) @@ -1179,45 +1309,162 @@ class AmountTestCase(unittest.TestCase): self.assertValid(x1) def testIntegerConversion(self): + x0 = amount() x1 = amount(123456) + x2 = amount("12345682348723487324") - self.assertEqual(123456, int(x1)) - self.assertEqual(123456.0, float(x1)) + self.assertRaises(exceptions.ArithmeticError, lambda: x0.to_long()) + self.assertRaises(exceptions.ArithmeticError, lambda: x0.to_double()) + self.assertFalse(x2.fits_in_long()) + self.assertEqual(123456, x1.to_long()) + self.assertEqual(123456.0, x1.to_double()) self.assertEqual("123456", x1.to_string()) - self.assertEqual("123456", x1.quantity_string()) + self.assertEqual("123456", x1.quantity_string) self.assertValid(x1) def testFractionalConversion(self): x1 = amount(1234.56) + x2 = amount("1234.5683787634678348734") - self.assertRaises(exceptions.ArithmeticError, amount.to_long, x1) + self.assertRaises(exceptions.ArithmeticError, lambda: x1.to_long()) # loses precision + self.assertRaises(exceptions.ArithmeticError, lambda: x2.to_double()) # loses precision + self.assertFalse(x2.fits_in_double()) self.assertEqual(1234, x1.to_long(True)) - self.assertEqual(1234.56, float(x1)) + self.assertEqual(1234.56, x1.to_double()) self.assertEqual("1234.56", x1.to_string()) - self.assertEqual("1234.56", x1.quantity_string()) + self.assertEqual("1234.56", x1.quantity_string) self.assertValid(x1) def testCommodityConversion(self): x1 = amount("$1234.56") - self.assertRaises(exceptions.ArithmeticError, amount.to_long, x1) + self.assertRaises(exceptions.ArithmeticError, lambda: x1.to_long()) # loses precision self.assertEqual(1234, x1.to_long(True)) - self.assertEqual(1234.56, float(x1)) + self.assertEqual(1234.56, x1.to_double()) self.assertEqual("$1234.56", x1.to_string()) - self.assertEqual("1234.56", x1.quantity_string()) + self.assertEqual("1234.56", x1.quantity_string) self.assertValid(x1) def testPrinting(self): pass + #x0 = amount() + #x1 = amount("982340823.380238098235098235098235098") + # + #bufstr = StringIO() + #self.assertRaises(exceptions.ArithmeticError, lambda: bufstr.write(x0)) + # + #bufstr = StringIO() + #bufstr.write(x1) + # + #self.assertEqual("982340823.380238098235098235098235098", + # bufstr.getvalue()) + # + #self.assertValid(x0) + #self.assertValid(x1) def testCommodityPrinting(self): pass - + #x1 = amount(internalAmount("$982340823.386238098235098235098235098")) + #x2 = amount("$982340823.38") + # + #bufstr = StringIO() + #bufstr.write(x1) + # + #self.assertEqual("$982340823.386238098235098235098235098", + # bufstr.getvalue()) + # + #bufstr = StringIO() + #bufstr.write((x1 * x2).to_string()) + # + #self.assertEqual("$964993493285024293.18099172508158508135413499124", + # bufstr.getvalue()) + # + #bufstr = StringIO() + #bufstr.write((x2 * x1).to_string()) + # + #self.assertEqual("$964993493285024293.18", bufstr.getvalue()) + # + #self.assertValid(x1) + #self.assertValid(x2) + + def testSerialization(self): + pass + #x0 = amount() + #x1 = amount("$8,192.34") + #x2 = amount("8192.34") + #x3 = amount("8192.34") + #x4 = amount("-8192.34") + #x5 = amount(x4) + # + ## Force x3's pointer to actually be set to null_commodity + ##x3.set_commodity(*x3.current_pool.null_commodity) + # + #buf = "" + #storage = StringIO() + #self.assertRaises(exceptions.ArithmeticError, lambda: x0.write(storage)) + #x1.write(storage) + #x2.write(storage) + #x3.write(storage) + #x4.write(storage) + #x5.write(storage) + #buf = storage.getvalue() + # + #x1b = amount() + #x2b = amount() + #x3b = amount() + #x4b = amount() + #x5b = amount() + # + #storage = StringIO(buf) + #x1b.read(storage) + #x2b.read(storage) + #x3b.read(storage) + #x4b.read(storage) + #x5b.read(storage) + # + #self.assertEqual(x1, x1b) + #self.assertEqual(x2, x2b) + #self.assertEqual(x3, x3b) + #self.assertEqual(x4, x4b) + # + #ptr = buf.c_str() + # + #x1c = amount() + #x2c = amount() + #x3c = amount() + #x4c = amount() + #x5c = amount() + # + #x1c.read(ptr) + #x2c.read(ptr) + #x3c.read(ptr) + #x4c.read(ptr) + #x5c.read(ptr) + # + #self.assertEqual(x1, x1b) + #self.assertEqual(x2, x2b) + #self.assertEqual(x3, x3b) + #self.assertEqual(x4, x4b) + # + #self.assertValid(x1) + #self.assertValid(x2) + #self.assertValid(x3) + #self.assertValid(x4) + #self.assertValid(x1b) + #self.assertValid(x2b) + #self.assertValid(x3b) + #self.assertValid(x4b) + #self.assertValid(x1c) + #self.assertValid(x2c) + #self.assertValid(x3c) + #self.assertValid(x4c) + + def suite(): - return unittest.TestLoader().loadTestsFromTestCase(AmountTestCase) + return unittest.TestLoader().loadTestsFromTestCase(t_amountTestCase) if __name__ == '__main__': unittest.main() |