summaryrefslogtreecommitdiff
path: root/tests/python
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2007-05-07 11:36:36 +0000
committerJohn Wiegley <johnw@newartisans.com>2008-04-13 03:38:40 -0400
commita99695f69b395c9664447b5af82893fb755bd4c0 (patch)
tree8aa4790c6453e00cc68e79249885b30ef0074005 /tests/python
parent39b0a03f8281c2ee7af57326d49dcedd1eb29a47 (diff)
downloadfork-ledger-a99695f69b395c9664447b5af82893fb755bd4c0.tar.gz
fork-ledger-a99695f69b395c9664447b5af82893fb755bd4c0.tar.bz2
fork-ledger-a99695f69b395c9664447b5af82893fb755bd4c0.zip
More test reorg.
Diffstat (limited to 'tests/python')
-rw-r--r--tests/python/numerics/t_amount.py1161
1 files changed, 598 insertions, 563 deletions
diff --git a/tests/python/numerics/t_amount.py b/tests/python/numerics/t_amount.py
index 70ac0852..dd87b2cd 100644
--- a/tests/python/numerics/t_amount.py
+++ b/tests/python/numerics/t_amount.py
@@ -10,7 +10,6 @@ from ledger import amount
internalAmount = amount.exact
-
class AmountTestCase(unittest.TestCase):
def setUp(self):
# Cause the display precision for dollars to be initialized to 2.
@@ -35,6 +34,9 @@ class AmountTestCase(unittest.TestCase):
x10 = amount(x6)
self.assertEqual(amount(0), x0)
+ self.assertEqual(amount(), x0)
+ self.assertEqual(amount("0"), x0)
+ self.assertEqual(amount("0.0"), x0)
self.assertEqual(x2, x1)
self.assertEqual(x5, x1)
self.assertEqual(x6, x3)
@@ -50,29 +52,48 @@ class AmountTestCase(unittest.TestCase):
self.assertValid(x9)
self.assertValid(x10)
- def testNegation(self):
- x0 = amount()
- x1 = amount(-123456)
- x3 = amount(-123.456)
- x5 = amount("-123456")
- x6 = amount("-123.456")
- x9 = amount(- x3)
-
- self.assertEqual(amount(0), x0)
- self.assertEqual(x5, x1)
- self.assertEqual(x6, x3)
- self.assertEqual(- x6, x9)
- self.assertEqual(x3.negate(), x9)
+ 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€")
+ x10 = amount("-123.45€")
- x10 = amount(x9.negate())
+ self.assertEqual(amount("$123.45"), x1)
+ self.assertEqual(amount("-$123.45"), x2)
+ self.assertEqual(amount("$-123.45"), x3)
+ self.assertEqual(amount("DM 123.45"), x4)
+ self.assertEqual(amount("-DM 123.45"), x5)
+ self.assertEqual(amount("DM -123.45"), x6)
+ self.assertEqual(amount("123.45 euro"), x7)
+ self.assertEqual(amount("-123.45 euro"), x8)
+ self.assertEqual(amount("123.45€"), x9)
+ self.assertEqual(amount("-123.45€"), x10)
- self.assertEqual(x3, x10)
+ self.assertEqual("$123.45", x1.to_string())
+ self.assertEqual("$-123.45", x2.to_string())
+ self.assertEqual("$-123.45", x3.to_string())
+ self.assertEqual("DM 123.45", x4.to_string())
+ self.assertEqual("DM -123.45", x5.to_string())
+ self.assertEqual("DM -123.45", x6.to_string())
+ self.assertEqual("123.45 euro", x7.to_string())
+ self.assertEqual("-123.45 euro", x8.to_string())
+ self.assertEqual("123.45€", x9.to_string())
+ self.assertEqual("-123.45€", x10.to_string())
- self.assertValid(x0)
self.assertValid(x1)
+ self.assertValid(x2)
self.assertValid(x3)
+ self.assertValid(x4)
self.assertValid(x5)
self.assertValid(x6)
+ self.assertValid(x7)
+ self.assertValid(x8)
self.assertValid(x9)
self.assertValid(x10)
@@ -118,6 +139,51 @@ class AmountTestCase(unittest.TestCase):
self.assertValid(x9)
self.assertValid(x10)
+ def testCommodityAssignment(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€")
+ x10 = amount("-123.45€")
+
+ self.assertEqual(amount("$123.45"), x1)
+ self.assertEqual(amount("-$123.45"), x2)
+ self.assertEqual(amount("$-123.45"), x3)
+ self.assertEqual(amount("DM 123.45"), x4)
+ self.assertEqual(amount("-DM 123.45"), x5)
+ self.assertEqual(amount("DM -123.45"), x6)
+ self.assertEqual(amount("123.45 euro"), x7)
+ self.assertEqual(amount("-123.45 euro"), x8)
+ self.assertEqual(amount("123.45€"), x9)
+ self.assertEqual(amount("-123.45€"), x10)
+
+ self.assertEqual("$123.45", x1.to_string())
+ self.assertEqual("$-123.45", x2.to_string())
+ self.assertEqual("$-123.45", x3.to_string())
+ self.assertEqual("DM 123.45", x4.to_string())
+ self.assertEqual("DM -123.45", x5.to_string())
+ self.assertEqual("DM -123.45", x6.to_string())
+ self.assertEqual("123.45 euro", x7.to_string())
+ self.assertEqual("-123.45 euro", x8.to_string())
+ self.assertEqual("123.45€", x9.to_string())
+ self.assertEqual("-123.45€", x10.to_string())
+
+ self.assertValid(x1)
+ self.assertValid(x2)
+ self.assertValid(x3)
+ self.assertValid(x4)
+ self.assertValid(x5)
+ self.assertValid(x6)
+ self.assertValid(x7)
+ self.assertValid(x8)
+ self.assertValid(x9)
+ self.assertValid(x10)
+
def testEquality(self):
x1 = amount(123456)
x2 = amount(456789)
@@ -137,6 +203,118 @@ class AmountTestCase(unittest.TestCase):
self.assertValid(x4)
self.assertValid(x5)
+ def testCommodityEquality(self):
+ x0 = amount()
+ 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.assertTrue(x0.is_null())
+ self.assertTrue(x0.is_zero())
+ self.assertTrue(x0.is_realzero())
+ self.assertTrue(x0.sign() == 0)
+ self.assertTrue(x0.compare(x1) < 0)
+ self.assertTrue(x0.compare(x2) > 0)
+ self.assertTrue(x0.compare(x0) == 0)
+
+ self.assertTrue(x1 != x2)
+ self.assertTrue(x1 != x4)
+ self.assertTrue(x1 != x7)
+ self.assertTrue(x1 != x9)
+ self.assertTrue(x2 == x3)
+ self.assertTrue(x4 != x5)
+ self.assertTrue(x5 == x6)
+ self.assertTrue(x7 == - x8)
+ self.assertTrue(x9 == - x10)
+
+ self.assertValid(x0)
+ self.assertValid(x1)
+ self.assertValid(x2)
+ self.assertValid(x3)
+ self.assertValid(x4)
+ self.assertValid(x5)
+ self.assertValid(x6)
+ self.assertValid(x7)
+ self.assertValid(x8)
+ self.assertValid(x9)
+ self.assertValid(x10)
+
+ def testComparisons(self):
+ x0 = amount()
+ x1 = amount(-123)
+ x2 = amount(123)
+ x3 = amount(-123.45)
+ x4 = amount(123.45)
+ x5 = amount("-123.45")
+ x6 = amount("123.45")
+
+ self.assertTrue(x0 > x1)
+ self.assertTrue(x0 < x2)
+ self.assertTrue(x0 > x3)
+ self.assertTrue(x0 < x4)
+ self.assertTrue(x0 > x5)
+ self.assertTrue(x0 < x6)
+
+ self.assertTrue(x1 > x3)
+ self.assertTrue(x3 <= x5)
+ self.assertTrue(x3 >= x5)
+ self.assertTrue(x3 < x1)
+ self.assertTrue(x3 < x4)
+
+ self.assertTrue(x1 < 100)
+ self.assertTrue(x1 < 100L)
+ self.assertTrue(x1 < 100.0)
+ self.assertTrue(100 > x1)
+ self.assertTrue(100L > x1)
+ self.assertTrue(100.0 > x1)
+
+ self.assertValid(x0)
+ self.assertValid(x1)
+ self.assertValid(x2)
+ self.assertValid(x3)
+ self.assertValid(x4)
+ self.assertValid(x5)
+ self.assertValid(x6)
+
+ def testCommodityComparisons(self):
+ x0 = amount()
+ x1 = amount("$-123")
+ x2 = amount("$123.00")
+ x3 = amount(internalAmount("$-123.4544"))
+ x4 = amount(internalAmount("$123.4544"))
+ x5 = amount("$-123.45")
+ x6 = amount("$123.45")
+
+ self.assertTrue(x0 > x1)
+ self.assertTrue(x0 < x2)
+ self.assertTrue(x0 > x3)
+ self.assertTrue(x0 < x4)
+ self.assertTrue(x0 > x5)
+ self.assertTrue(x0 < x6)
+
+ self.assertTrue(x1 > x3)
+ self.assertTrue(x3 <= x5)
+ self.assertTrue(x3 < x5)
+ self.assertTrue(x3 <= x5)
+ self.assertFalse(x3 == x5)
+ self.assertTrue(x3 < x1)
+ self.assertTrue(x3 < x4)
+
+ self.assertValid(x0)
+ self.assertValid(x1)
+ self.assertValid(x2)
+ self.assertValid(x3)
+ self.assertValid(x4)
+ self.assertValid(x5)
+ self.assertValid(x6)
+
def testIntegerAddition(self):
x1 = amount(123)
y1 = amount(456)
@@ -181,6 +359,58 @@ class AmountTestCase(unittest.TestCase):
self.assertValid(y1)
self.assertValid(x2)
+ def testCommodityAddition(self):
+ x0 = amount()
+ x1 = amount("$123.45")
+ x2 = amount(internalAmount("$123.456789"))
+ x3 = amount("DM 123.45")
+ x4 = amount("123.45 euro")
+ x5 = amount("123.45€")
+ x6 = amount("123.45")
+
+ self.assertEqual(amount("$246.90"), x1 + x1)
+ self.assertNotEqual(amount("$246.91"), x1 + x2)
+ self.assertEqual(internalAmount("$246.906789"), x1 + x2)
+
+ # Converting to string drops internal precision
+ 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.assertEqual(amount("DM 246.90"), x3 + x3)
+ self.assertEqual(amount("246.90 euro"), x4 + x4)
+ self.assertEqual(amount("246.90€"), x5 + x5)
+
+ self.assertEqual("DM 246.90", (x3 + x3).to_string())
+ self.assertEqual("246.90 euro", (x4 + x4).to_string())
+ self.assertEqual("246.90€", (x5 + x5).to_string())
+
+ x1 += amount("$456.45")
+ self.assertEqual(amount("$579.90"), x1)
+ x1 += amount("$456.45")
+ self.assertEqual(amount("$1036.35"), x1)
+ x1 += amount("$456")
+ self.assertEqual(amount("$1492.35"), x1)
+
+ x7 = amount(internalAmount("$123456789123456789.123456789123456789"))
+
+ self.assertEqual(internalAmount("$246913578246913578.246913578246913578"), x7 + x7)
+
+ self.assertValid(x1)
+ self.assertValid(x2)
+ self.assertValid(x3)
+ self.assertValid(x4)
+ self.assertValid(x5)
+ self.assertValid(x6)
+ self.assertValid(x7)
+
def testIntegerSubtraction(self):
x1 = amount(123)
y1 = amount(456)
@@ -231,6 +461,83 @@ class AmountTestCase(unittest.TestCase):
self.assertValid(x2)
self.assertValid(y2)
+ def testCommoditySubtraction(self):
+ x0 = amount()
+ x1 = amount("$123.45")
+ x2 = amount(internalAmount("$123.456789"))
+ x3 = amount("DM 123.45")
+ x4 = amount("123.45 euro")
+ x5 = amount("123.45€")
+ x6 = amount("123.45")
+
+ self.assertNotEqual(amount(), x1 - x1)
+ self.assertEqual(amount("$0"), x1 - x1)
+ self.assertEqual(amount("$23.45"), x1 - amount("$100.00"))
+ self.assertEqual(amount("$-23.45"), amount("$100.00") - x1)
+ self.assertNotEqual(amount("$-0.01"), x1 - x2)
+ self.assertEqual(internalAmount("$-0.006789"), x1 - x2)
+
+ # Converting to string drops internal precision. If an amount is
+ # zero, it drops the commodity as well.
+ 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.assertEqual(amount("DM 0.00"), x3 - x3)
+ self.assertEqual(amount("DM 23.45"), x3 - amount("DM 100.00"))
+ self.assertEqual(amount("DM -23.45"), amount("DM 100.00") - x3)
+ self.assertEqual(amount("0.00 euro"), x4 - x4)
+ self.assertEqual(amount("23.45 euro"), x4 - amount("100.00 euro"))
+ self.assertEqual(amount("-23.45 euro"), amount("100.00 euro") - x4)
+ self.assertEqual(amount("0.00€"), x5 - x5)
+ self.assertEqual(amount("23.45€"), x5 - amount("100.00€"))
+ self.assertEqual(amount("-23.45€"), amount("100.00€") - x5)
+
+ self.assertEqual("DM 0.00", (x3 - x3).to_string())
+ self.assertEqual("DM 23.45", (x3 - amount("DM 100.00")).to_string())
+ self.assertEqual("DM -23.45", (amount("DM 100.00") - x3).to_string())
+ self.assertEqual("0.00 euro", (x4 - x4).to_string())
+ self.assertEqual("23.45 euro", (x4 - amount("100.00 euro")).to_string())
+ self.assertEqual("-23.45 euro", (amount("100.00 euro") - x4).to_string())
+ self.assertEqual("0.00€", (x5 - x5).to_string())
+ self.assertEqual("23.45€", (x5 - amount("100.00€")).to_string())
+ self.assertEqual("-23.45€", (amount("100.00€") - x5).to_string())
+
+ x1 -= amount("$456.45")
+ self.assertEqual(amount("$-333.00"), x1)
+ x1 -= amount("$456.45")
+ self.assertEqual(amount("$-789.45"), x1)
+ x1 -= amount("$456")
+ self.assertEqual(amount("$-1245.45"), x1)
+
+ x7 = amount(internalAmount("$123456789123456789.123456789123456789"))
+ x8 = amount(internalAmount("$2354974984698.98459845984598"))
+
+ self.assertEqual(internalAmount("$123454434148472090.138858329277476789"), x7 - x8)
+ self.assertEqual("$123454434148472090.138858329277476789", (x7 - x8).to_string())
+ self.assertEqual("$123454434148472090.14",
+ (amount("$1.00") * (x7 - x8)).to_string())
+ self.assertEqual(internalAmount("$-123454434148472090.138858329277476789"), x8 - x7)
+ self.assertEqual("$-123454434148472090.138858329277476789", (x8 - x7).to_string())
+ self.assertEqual("$-123454434148472090.14",
+ (amount("$1.00") * (x8 - x7)).to_string())
+
+ self.assertValid(x1)
+ self.assertValid(x2)
+ self.assertValid(x3)
+ self.assertValid(x4)
+ self.assertValid(x5)
+ self.assertValid(x6)
+ self.assertValid(x7)
+ self.assertValid(x8)
+
def testIntegerMultiplication(self):
x1 = amount(123)
y1 = amount(456)
@@ -299,6 +606,58 @@ class AmountTestCase(unittest.TestCase):
self.assertValid(y1)
self.assertValid(x2)
+ def testCommodityMultiplication(self):
+ x1 = amount("$123.12")
+ y1 = amount("$456.45")
+ x2 = amount(internalAmount("$123.456789"))
+ x3 = amount("DM 123.45")
+ x4 = amount("123.45 euro")
+ x5 = amount("123.45€")
+
+ self.assertEqual(amount("$0.00"), x1 * 0)
+ self.assertEqual(amount("$0.00"), 0 * x1)
+ self.assertEqual(x1, x1 * 1)
+ self.assertEqual(x1, 1 * x1)
+ self.assertEqual(- x1, x1 * -1)
+ self.assertEqual(- x1, -1 * x1)
+ self.assertEqual(internalAmount("$56198.124"), x1 * y1)
+ self.assertEqual("$56198.12", (x1 * y1).to_string())
+ self.assertEqual(internalAmount("$56198.124"), y1 * x1)
+ self.assertEqual("$56198.12", (y1 * x1).to_string())
+
+ # Internal amounts retain their precision, even when being
+ # converted to strings
+ self.assertEqual(internalAmount("$15199.99986168"), x1 * x2)
+ self.assertEqual(internalAmount("$15199.99986168"), x2 * x1)
+ 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)
+
+ x1 *= amount("123.12")
+ self.assertEqual(internalAmount("$15158.5344"), x1)
+ self.assertEqual("$15158.53", x1.to_string())
+ x1 *= 123.12
+ self.assertEqual(internalAmount("$1866318.755328"), x1)
+ self.assertEqual("$1866318.76", x1.to_string())
+ x1 *= 123
+ self.assertEqual(internalAmount("$229557206.905344"), x1)
+ self.assertEqual("$229557206.91", x1.to_string())
+
+ x7 = amount(internalAmount("$123456789123456789.123456789123456789"))
+
+ self.assertEqual(internalAmount("$15241578780673678546105778311537878.046486820281054720515622620750190521"),
+ x7 * x7)
+
+ self.assertValid(x1)
+ self.assertValid(x2)
+ self.assertValid(x3)
+ self.assertValid(x4)
+ self.assertValid(x5)
+ self.assertValid(x7)
+
def divideByZero(self, amt):
return amt / 0
@@ -375,214 +734,86 @@ class AmountTestCase(unittest.TestCase):
self.assertValid(x4)
self.assertValid(y4)
- def testIntegerConversion(self):
- x1 = amount(123456)
-
- self.assertTrue(x1)
- self.assertEqual(123456, int(x1))
- self.assertEqual(123456.0, float(x1))
- self.assertEqual("123456", x1.to_string())
- self.assertEqual("123456", x1.quantity_string())
-
- self.assertValid(x1)
-
- def testFractionalConversion(self):
- x1 = amount(1234.56)
-
- self.assertTrue(x1)
- self.assertRaises(exceptions.ArithmeticError, amount.to_long, x1)
- self.assertEqual(1234, x1.to_long(True))
- self.assertEqual(1234.56, float(x1))
- self.assertEqual("1234.56", x1.to_string())
- self.assertEqual("1234.56", x1.quantity_string())
-
- self.assertValid(x1)
-
- def testFractionalRound(self):
- 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))
-
- x2 = amount("9876.543210")
-
- self.assertEqual(amount("9876.543210"), x2.round(6))
- self.assertEqual(amount("9876.54321"), x2.round(5))
- self.assertEqual(amount("9876.5432"), x2.round(4))
- self.assertEqual(amount("9876.543"), x2.round(3))
- self.assertEqual(amount("9876.54"), x2.round(2))
- self.assertEqual(amount("9876.5"), x2.round(1))
- self.assertEqual(amount("9877"), x2.round(0))
-
- x3 = amount("-1234.567890")
-
- self.assertEqual(amount("-1234.56789"), x3.round(6))
- self.assertEqual(amount("-1234.56789"), x3.round(5))
- self.assertEqual(amount("-1234.5679"), x3.round(4))
- self.assertEqual(amount("-1234.568"), x3.round(3))
- self.assertEqual(amount("-1234.57"), x3.round(2))
- self.assertEqual(amount("-1234.6"), x3.round(1))
- self.assertEqual(amount("-1235"), x3.round(0))
-
- x4 = amount("-9876.543210")
-
- self.assertEqual(amount("-9876.543210"), x4.round(6))
- self.assertEqual(amount("-9876.54321"), x4.round(5))
- self.assertEqual(amount("-9876.5432"), x4.round(4))
- self.assertEqual(amount("-9876.543"), x4.round(3))
- self.assertEqual(amount("-9876.54"), x4.round(2))
- self.assertEqual(amount("-9876.5"), x4.round(1))
- self.assertEqual(amount("-9877"), x4.round(0))
-
- self.assertValid(x1)
- self.assertValid(x2)
- self.assertValid(x3)
- self.assertValid(x4)
-
- def testTruth(self):
- x0 = amount()
- x1 = amount("1234")
- x2 = amount("1234.56")
-
- self.assertFalse(x0)
- self.assertTrue(x1)
- self.assertTrue(x2)
-
- self.assertValid(x0)
- self.assertValid(x1)
- self.assertValid(x2)
-
- def testForZero(self):
- x0 = amount()
- x1 = amount("0.000000000000000000001")
+ def testCommodityDivision(self):
+ x1 = amount("$123.12")
+ y1 = amount("$456.45")
+ x2 = amount(internalAmount("$123.456789"))
+ x3 = amount("DM 123.45")
+ x4 = amount("123.45 euro")
+ x5 = amount("123.45€")
- self.assertFalse(x0)
- self.assertTrue(x1)
- self.assertTrue(x0.is_zero())
- self.assertTrue(x0.is_realzero())
- self.assertFalse(x1.is_zero())
- self.assertFalse(x1.is_realzero())
+ self.assertRaises(exceptions.ArithmeticError, operator.div, x1, 0)
+ self.assertEqual(amount("$0.00"), 0 / x1)
+ self.assertEqual(x1, x1 / 1)
+ self.assertEqual(internalAmount("$0.00812216"), 1 / x1)
+ self.assertEqual(- x1, x1 / -1)
+ self.assertEqual(internalAmount("$-0.00812216"), -1 / x1)
+ self.assertEqual(internalAmount("$0.26973382"), x1 / y1)
+ self.assertEqual("$0.27", (x1 / y1).to_string())
+ self.assertEqual(internalAmount("$3.70735867"), y1 / x1)
+ self.assertEqual("$3.71", (y1 / x1).to_string())
- self.assertValid(x0)
- self.assertValid(x1)
+ # Internal amounts retain their precision, even when being
+ # converted to strings
+ self.assertEqual(internalAmount("$0.99727201"), x1 / x2)
+ self.assertEqual(internalAmount("$1.00273545321637426901"), x2 / x1)
+ self.assertEqual("$1.00", (x1 / x2).to_string())
+ self.assertEqual("$1.00273545321637426901", (x2 / x1).to_string())
- def testComparisons(self):
- x0 = amount()
- x1 = amount(-123)
- x2 = amount(123)
- x3 = amount(-123.45)
- x4 = amount(123.45)
- x5 = amount("-123.45")
- x6 = amount("123.45")
+ 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.assertTrue(x0 > x1)
- self.assertTrue(x0 < x2)
- self.assertTrue(x0 > x3)
- self.assertTrue(x0 < x4)
- self.assertTrue(x0 > x5)
- self.assertTrue(x0 < x6)
+ x1 /= amount("123.12")
+ self.assertEqual(internalAmount("$1.00"), x1)
+ self.assertEqual("$1.00", x1.to_string())
+ x1 /= 123.12
+ self.assertEqual(internalAmount("$0.00812216"), x1)
+ self.assertEqual("$0.01", x1.to_string())
+ x1 /= 123
+ self.assertEqual(internalAmount("$0.00006603"), x1)
+ self.assertEqual("$0.00", x1.to_string())
- self.assertTrue(x1 > x3)
- self.assertTrue(x3 <= x5)
- self.assertTrue(x3 >= x5)
- self.assertTrue(x3 < x1)
- self.assertTrue(x3 < x4)
+ x6 = amount(internalAmount("$237235987235987.98723987235978"))
+ x7 = amount(internalAmount("$123456789123456789.123456789123456789"))
- self.assertTrue(x1 < 100)
- self.assertTrue(x1 < 100.0)
- self.assertTrue(100 > x1)
- self.assertTrue(100.0 > x1)
+ self.assertEqual(amount("$1"), x7 / x7)
+ self.assertEqual(internalAmount("$0.0019216115121765559608381226612019501046413574469262"),
+ x6 / x7)
+ self.assertEqual(internalAmount("$520.39654928343335571379527154924040947271699678158689736256"),
+ x7 / x6)
- self.assertValid(x0)
self.assertValid(x1)
self.assertValid(x2)
self.assertValid(x3)
self.assertValid(x4)
self.assertValid(x5)
self.assertValid(x6)
+ self.assertValid(x7)
- def testSign(self):
+ def testNegation(self):
x0 = amount()
- x1 = amount("0.0000000000000000000000000000000000001")
- x2 = amount("-0.0000000000000000000000000000000000001")
- x3 = amount("1")
- x4 = amount("-1")
-
- self.assertEqual(x0.sign(), 0)
- self.assertTrue(x1.sign() > 0)
- self.assertTrue(x2.sign() < 0)
- self.assertTrue(x3.sign() > 0)
- self.assertTrue(x4.sign() < 0)
+ x1 = amount(-123456)
+ x3 = amount(-123.456)
+ x5 = amount("-123456")
+ x6 = amount("-123.456")
+ x9 = amount(- x3)
- self.assertValid(x0)
- self.assertValid(x1)
- self.assertValid(x2)
- self.assertValid(x3)
- self.assertValid(x4)
+ self.assertEqual(amount(0), x0)
+ self.assertEqual(x5, x1)
+ self.assertEqual(x6, x3)
+ self.assertEqual(- x6, x9)
+ self.assertEqual(x3.negate(), x9)
- def testAbs(self):
- x0 = amount()
- x1 = amount(-1234)
- x2 = amount(1234)
+ x10 = amount(x9.negate())
- self.assertEqual(amount(), abs(x0))
- self.assertEqual(amount(1234), abs(x1))
- self.assertEqual(amount(1234), abs(x2))
+ self.assertEqual(x3, x10)
self.assertValid(x0)
self.assertValid(x1)
- self.assertValid(x2)
-
- def testPrinting(self):
- pass
-
- 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€")
- x10 = amount("-123.45€")
-
- self.assertEqual(amount("$123.45"), x1)
- self.assertEqual(amount("-$123.45"), x2)
- self.assertEqual(amount("$-123.45"), x3)
- self.assertEqual(amount("DM 123.45"), x4)
- self.assertEqual(amount("-DM 123.45"), x5)
- self.assertEqual(amount("DM -123.45"), x6)
- self.assertEqual(amount("123.45 euro"), x7)
- self.assertEqual(amount("-123.45 euro"), x8)
- self.assertEqual(amount("123.45€"), x9)
- self.assertEqual(amount("-123.45€"), x10)
-
- self.assertEqual("$123.45", x1.to_string())
- self.assertEqual("$-123.45", x2.to_string())
- self.assertEqual("$-123.45", x3.to_string())
- self.assertEqual("DM 123.45", x4.to_string())
- self.assertEqual("DM -123.45", x5.to_string())
- self.assertEqual("DM -123.45", x6.to_string())
- self.assertEqual("123.45 euro", x7.to_string())
- self.assertEqual("-123.45 euro", x8.to_string())
- self.assertEqual("123.45€", x9.to_string())
- self.assertEqual("-123.45€", x10.to_string())
-
- self.assertValid(x1)
- self.assertValid(x2)
self.assertValid(x3)
- self.assertValid(x4)
self.assertValid(x5)
self.assertValid(x6)
- self.assertValid(x7)
- self.assertValid(x8)
self.assertValid(x9)
self.assertValid(x10)
@@ -639,343 +870,84 @@ class AmountTestCase(unittest.TestCase):
self.assertValid(x9)
self.assertValid(x10)
- def testCommodityAssignment(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€")
- x10 = amount("-123.45€")
-
- self.assertEqual(amount("$123.45"), x1)
- self.assertEqual(amount("-$123.45"), x2)
- self.assertEqual(amount("$-123.45"), x3)
- self.assertEqual(amount("DM 123.45"), x4)
- self.assertEqual(amount("-DM 123.45"), x5)
- self.assertEqual(amount("DM -123.45"), x6)
- self.assertEqual(amount("123.45 euro"), x7)
- self.assertEqual(amount("-123.45 euro"), x8)
- self.assertEqual(amount("123.45€"), x9)
- self.assertEqual(amount("-123.45€"), x10)
-
- self.assertEqual("$123.45", x1.to_string())
- self.assertEqual("$-123.45", x2.to_string())
- self.assertEqual("$-123.45", x3.to_string())
- self.assertEqual("DM 123.45", x4.to_string())
- self.assertEqual("DM -123.45", x5.to_string())
- self.assertEqual("DM -123.45", x6.to_string())
- self.assertEqual("123.45 euro", x7.to_string())
- self.assertEqual("-123.45 euro", x8.to_string())
- self.assertEqual("123.45€", x9.to_string())
- self.assertEqual("-123.45€", x10.to_string())
-
- self.assertValid(x1)
- self.assertValid(x2)
- self.assertValid(x3)
- self.assertValid(x4)
- self.assertValid(x5)
- self.assertValid(x6)
- self.assertValid(x7)
- self.assertValid(x8)
- self.assertValid(x9)
- self.assertValid(x10)
-
- def testCommodityEquality(self):
+ def testAbs(self):
x0 = amount()
- 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.assertTrue(x0.is_null())
- self.assertTrue(x0.is_zero())
- self.assertTrue(x0.is_realzero())
- self.assertTrue(x0.sign() == 0)
- self.assertTrue(x0.compare(x1) < 0)
- self.assertTrue(x0.compare(x2) > 0)
- self.assertTrue(x0.compare(x0) == 0)
+ x1 = amount(-1234)
+ x2 = amount(1234)
- self.assertTrue(x1 != x2)
- self.assertTrue(x1 != x4)
- self.assertTrue(x1 != x7)
- self.assertTrue(x1 != x9)
- self.assertTrue(x2 == x3)
- self.assertTrue(x4 != x5)
- self.assertTrue(x5 == x6)
- self.assertTrue(x7 == - x8)
- self.assertTrue(x9 == - x10)
+ self.assertEqual(amount(), abs(x0))
+ self.assertEqual(amount(1234), abs(x1))
+ self.assertEqual(amount(1234), abs(x2))
self.assertValid(x0)
self.assertValid(x1)
self.assertValid(x2)
- self.assertValid(x3)
- self.assertValid(x4)
- self.assertValid(x5)
- self.assertValid(x6)
- self.assertValid(x7)
- self.assertValid(x8)
- self.assertValid(x9)
- self.assertValid(x10)
-
- def testCommodityAddition(self):
- x0 = amount()
- x1 = amount("$123.45")
- x2 = amount(internalAmount("$123.456789"))
- x3 = amount("DM 123.45")
- x4 = amount("123.45 euro")
- x5 = amount("123.45€")
- x6 = amount("123.45")
-
- self.assertEqual(amount("$246.90"), x1 + x1)
- self.assertNotEqual(amount("$246.91"), x1 + x2)
- self.assertEqual(internalAmount("$246.906789"), x1 + x2)
-
- # Converting to string drops internal precision
- 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.assertEqual(amount("DM 246.90"), x3 + x3)
- self.assertEqual(amount("246.90 euro"), x4 + x4)
- self.assertEqual(amount("246.90€"), x5 + x5)
-
- self.assertEqual("DM 246.90", (x3 + x3).to_string())
- self.assertEqual("246.90 euro", (x4 + x4).to_string())
- self.assertEqual("246.90€", (x5 + x5).to_string())
-
- x1 += amount("$456.45")
- self.assertEqual(amount("$579.90"), x1)
- x1 += amount("$456.45")
- self.assertEqual(amount("$1036.35"), x1)
- x1 += amount("$456")
- self.assertEqual(amount("$1492.35"), x1)
-
- x7 = amount(internalAmount("$123456789123456789.123456789123456789"))
-
- self.assertEqual(internalAmount("$246913578246913578.246913578246913578"), x7 + x7)
-
- self.assertValid(x1)
- self.assertValid(x2)
- self.assertValid(x3)
- self.assertValid(x4)
- self.assertValid(x5)
- self.assertValid(x6)
- self.assertValid(x7)
-
- def testCommoditySubtraction(self):
+ def testCommodityAbs(self):
x0 = amount()
- x1 = amount("$123.45")
- x2 = amount(internalAmount("$123.456789"))
- x3 = amount("DM 123.45")
- x4 = amount("123.45 euro")
- x5 = amount("123.45€")
- x6 = amount("123.45")
-
- self.assertNotEqual(amount(), x1 - x1)
- self.assertEqual(amount("$0"), x1 - x1)
- self.assertEqual(amount("$23.45"), x1 - amount("$100.00"))
- self.assertEqual(amount("$-23.45"), amount("$100.00") - x1)
- self.assertNotEqual(amount("$-0.01"), x1 - x2)
- self.assertEqual(internalAmount("$-0.006789"), x1 - x2)
-
- # Converting to string drops internal precision. If an amount is
- # zero, it drops the commodity as well.
- 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.assertEqual(amount("DM 0.00"), x3 - x3)
- self.assertEqual(amount("DM 23.45"), x3 - amount("DM 100.00"))
- self.assertEqual(amount("DM -23.45"), amount("DM 100.00") - x3)
- self.assertEqual(amount("0.00 euro"), x4 - x4)
- self.assertEqual(amount("23.45 euro"), x4 - amount("100.00 euro"))
- self.assertEqual(amount("-23.45 euro"), amount("100.00 euro") - x4)
- self.assertEqual(amount("0.00€"), x5 - x5)
- self.assertEqual(amount("23.45€"), x5 - amount("100.00€"))
- self.assertEqual(amount("-23.45€"), amount("100.00€") - x5)
-
- self.assertEqual("DM 0.00", (x3 - x3).to_string())
- self.assertEqual("DM 23.45", (x3 - amount("DM 100.00")).to_string())
- self.assertEqual("DM -23.45", (amount("DM 100.00") - x3).to_string())
- self.assertEqual("0.00 euro", (x4 - x4).to_string())
- self.assertEqual("23.45 euro", (x4 - amount("100.00 euro")).to_string())
- self.assertEqual("-23.45 euro", (amount("100.00 euro") - x4).to_string())
- self.assertEqual("0.00€", (x5 - x5).to_string())
- self.assertEqual("23.45€", (x5 - amount("100.00€")).to_string())
- self.assertEqual("-23.45€", (amount("100.00€") - x5).to_string())
-
- x1 -= amount("$456.45")
- self.assertEqual(amount("$-333.00"), x1)
- x1 -= amount("$456.45")
- self.assertEqual(amount("$-789.45"), x1)
- x1 -= amount("$456")
- self.assertEqual(amount("$-1245.45"), x1)
-
- x7 = amount(internalAmount("$123456789123456789.123456789123456789"))
- x8 = amount(internalAmount("$2354974984698.98459845984598"))
+ x1 = amount("$-1234.56")
+ x2 = amount("$1234.56")
- self.assertEqual(internalAmount("$123454434148472090.138858329277476789"), x7 - x8)
- self.assertEqual("$123454434148472090.138858329277476789", (x7 - x8).to_string())
- self.assertEqual("$123454434148472090.14",
- (amount("$1.00") * (x7 - x8)).to_string())
- self.assertEqual(internalAmount("$-123454434148472090.138858329277476789"), x8 - x7)
- self.assertEqual("$-123454434148472090.138858329277476789", (x8 - x7).to_string())
- self.assertEqual("$-123454434148472090.14",
- (amount("$1.00") * (x8 - x7)).to_string())
+ self.assertEqual(amount(), abs(x0))
+ self.assertEqual(amount("$1234.56"), abs(x1))
+ self.assertEqual(amount("$1234.56"), abs(x2))
+ self.assertValid(x0)
self.assertValid(x1)
self.assertValid(x2)
- self.assertValid(x3)
- self.assertValid(x4)
- self.assertValid(x5)
- self.assertValid(x6)
- self.assertValid(x7)
- self.assertValid(x8)
-
- def testCommodityMultiplication(self):
- x1 = amount("$123.12")
- y1 = amount("$456.45")
- x2 = amount(internalAmount("$123.456789"))
- x3 = amount("DM 123.45")
- x4 = amount("123.45 euro")
- x5 = amount("123.45€")
-
- self.assertEqual(amount("$0.00"), x1 * 0)
- self.assertEqual(amount("$0.00"), 0 * x1)
- self.assertEqual(x1, x1 * 1)
- self.assertEqual(x1, 1 * x1)
- self.assertEqual(- x1, x1 * -1)
- self.assertEqual(- x1, -1 * x1)
- self.assertEqual(internalAmount("$56198.124"), x1 * y1)
- self.assertEqual("$56198.12", (x1 * y1).to_string())
- self.assertEqual(internalAmount("$56198.124"), y1 * x1)
- self.assertEqual("$56198.12", (y1 * x1).to_string())
- # Internal amounts retain their precision, even when being
- # converted to strings
- self.assertEqual(internalAmount("$15199.99986168"), x1 * x2)
- self.assertEqual(internalAmount("$15199.99986168"), x2 * x1)
- 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)
-
- x1 *= amount("123.12")
- self.assertEqual(internalAmount("$15158.5344"), x1)
- self.assertEqual("$15158.53", x1.to_string())
- x1 *= 123.12
- self.assertEqual(internalAmount("$1866318.755328"), x1)
- self.assertEqual("$1866318.76", x1.to_string())
- x1 *= 123
- self.assertEqual(internalAmount("$229557206.905344"), x1)
- self.assertEqual("$229557206.91", x1.to_string())
-
- x7 = amount(internalAmount("$123456789123456789.123456789123456789"))
+ def testFractionalRound(self):
+ x1 = amount("1234.567890")
- self.assertEqual(internalAmount("$15241578780673678546105778311537878.046486820281054720515622620750190521"),
- x7 * x7)
+ 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.assertValid(x1)
- self.assertValid(x2)
- self.assertValid(x3)
- self.assertValid(x4)
- self.assertValid(x5)
- self.assertValid(x7)
+ x2 = amount("9876.543210")
- def testCommodityDivision(self):
- x1 = amount("$123.12")
- y1 = amount("$456.45")
- x2 = amount(internalAmount("$123.456789"))
- x3 = amount("DM 123.45")
- x4 = amount("123.45 euro")
- x5 = amount("123.45€")
+ self.assertEqual(amount("9876.543210"), x2.round(6))
+ self.assertEqual(amount("9876.54321"), x2.round(5))
+ self.assertEqual(amount("9876.5432"), x2.round(4))
+ self.assertEqual(amount("9876.543"), x2.round(3))
+ self.assertEqual(amount("9876.54"), x2.round(2))
+ self.assertEqual(amount("9876.5"), x2.round(1))
+ self.assertEqual(amount("9877"), x2.round(0))
- self.assertRaises(exceptions.ArithmeticError, operator.div, x1, 0)
- self.assertEqual(amount("$0.00"), 0 / x1)
- self.assertEqual(x1, x1 / 1)
- self.assertEqual(internalAmount("$0.00812216"), 1 / x1)
- self.assertEqual(- x1, x1 / -1)
- self.assertEqual(internalAmount("$-0.00812216"), -1 / x1)
- self.assertEqual(internalAmount("$0.26973382"), x1 / y1)
- self.assertEqual("$0.27", (x1 / y1).to_string())
- self.assertEqual(internalAmount("$3.70735867"), y1 / x1)
- self.assertEqual("$3.71", (y1 / x1).to_string())
+ x3 = amount("-1234.567890")
- # Internal amounts retain their precision, even when being
- # converted to strings
- self.assertEqual(internalAmount("$0.99727201"), x1 / x2)
- self.assertEqual(internalAmount("$1.00273545321637426901"), x2 / x1)
- self.assertEqual("$1.00", (x1 / x2).to_string())
- self.assertEqual("$1.00273545321637426901", (x2 / x1).to_string())
+ self.assertEqual(amount("-1234.56789"), x3.round(6))
+ self.assertEqual(amount("-1234.56789"), x3.round(5))
+ self.assertEqual(amount("-1234.5679"), x3.round(4))
+ self.assertEqual(amount("-1234.568"), x3.round(3))
+ self.assertEqual(amount("-1234.57"), x3.round(2))
+ self.assertEqual(amount("-1234.6"), x3.round(1))
+ self.assertEqual(amount("-1235"), x3.round(0))
- self.assertRaises(exceptions.ArithmeticError, operator.div, x1, x3)
- self.assertRaises(exceptions.ArithmeticError, operator.div, x1, x4)
- self.assertRaises(exceptions.ArithmeticError, operator.div, x1, x5)
+ x4 = amount("-9876.543210")
- x1 /= amount("123.12")
- self.assertEqual(internalAmount("$1.00"), x1)
- self.assertEqual("$1.00", x1.to_string())
- x1 /= 123.12
- self.assertEqual(internalAmount("$0.00812216"), x1)
- self.assertEqual("$0.01", x1.to_string())
- x1 /= 123
- self.assertEqual(internalAmount("$0.00006603"), x1)
- self.assertEqual("$0.00", x1.to_string())
+ self.assertEqual(amount("-9876.543210"), x4.round(6))
+ self.assertEqual(amount("-9876.54321"), x4.round(5))
+ self.assertEqual(amount("-9876.5432"), x4.round(4))
+ self.assertEqual(amount("-9876.543"), x4.round(3))
+ self.assertEqual(amount("-9876.54"), x4.round(2))
+ self.assertEqual(amount("-9876.5"), x4.round(1))
+ self.assertEqual(amount("-9877"), x4.round(0))
- x6 = amount(internalAmount("$237235987235987.98723987235978"))
- x7 = amount(internalAmount("$123456789123456789.123456789123456789"))
+ x5 = amount("0.0000000000000000000000000000000000001")
- self.assertEqual(amount("$1"), x7 / x7)
- self.assertEqual(internalAmount("$0.0019216115121765559608381226612019501046413574469262"),
- x6 / x7)
- self.assertEqual(internalAmount("$520.39654928343335571379527154924040947271699678158689736256"),
- x7 / x6)
+ self.assertEqual(amount("0.0000000000000000000000000000000000001"),
+ x5.round(37))
+ self.assertEqual(amount(), x5.round(36))
self.assertValid(x1)
self.assertValid(x2)
self.assertValid(x3)
self.assertValid(x4)
self.assertValid(x5)
- self.assertValid(x6)
- self.assertValid(x7)
-
- def testCommodityConversion(self):
- x1 = amount("$1234.56")
-
- self.assertEqual(True, bool(x1))
- self.assertRaises(exceptions.ArithmeticError, amount.to_long, x1)
- self.assertEqual(1234, x1.to_long(True))
- self.assertEqual(1234.56, float(x1))
- self.assertEqual("$1234.56", x1.to_string())
- self.assertEqual("1234.56", x1.quantity_string())
-
- self.assertValid(x1)
def testCommodityRound(self):
x1 = amount(internalAmount("$1234.567890"))
@@ -1018,10 +990,20 @@ class AmountTestCase(unittest.TestCase):
self.assertEqual(amount("$-9876.5"), x4.round(1))
self.assertEqual(amount("$-9877"), x4.round(0))
+ x5 = amount("$123.45")
+
+ x5 *= 100.12
+
+ self.assertEqual(internalAmount("$12359.814"), x5)
+ self.assertEqual("$12359.81", x5.to_string())
+ self.assertEqual("$12359.814", x5.to_fullstring())
+ self.assertEqual("$12359.814", x5.unround().to_string())
+
self.assertValid(x1)
self.assertValid(x2)
self.assertValid(x3)
self.assertValid(x4)
+ self.assertValid(x5)
def testCommodityDisplayRound(self):
x1 = amount("$0.85")
@@ -1043,6 +1025,75 @@ class AmountTestCase(unittest.TestCase):
self.assertEqual(internalAmount("$1.1305"), x1)
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
+
+ self.assertEqual(x2, x5)
+ self.assertEqual(x3, x6)
+ self.assertEqual(x4, x10)
+
+ def testSign(self):
+ x0 = amount()
+ x1 = amount("0.0000000000000000000000000000000000001")
+ x2 = amount("-0.0000000000000000000000000000000000001")
+ x3 = amount("1")
+ x4 = amount("-1")
+
+ self.assertEqual(x0.sign(), 0)
+ self.assertTrue(x1.sign() > 0)
+ self.assertTrue(x2.sign() < 0)
+ self.assertTrue(x3.sign() > 0)
+ self.assertTrue(x4.sign() < 0)
+
+ self.assertValid(x0)
+ self.assertValid(x1)
+ self.assertValid(x2)
+ self.assertValid(x3)
+ self.assertValid(x4)
+
+ def testCommoditySign(self):
+ x0 = amount()
+ x1 = amount(internalAmount("$0.0000000000000000000000000000000000001"))
+ x2 = amount(internalAmount("$-0.0000000000000000000000000000000000001"))
+ x3 = amount("$1")
+ x4 = amount("$-1")
+
+ self.assertFalse(x0.sign())
+ self.assertTrue(x1.sign() != 0)
+ self.assertTrue(x2.sign() != 0)
+ self.assertTrue(x3.sign() > 0)
+ self.assertTrue(x4.sign() < 0)
+
+ self.assertValid(x0)
+ self.assertValid(x1)
+ self.assertValid(x2)
+ self.assertValid(x3)
+ self.assertValid(x4)
+
+ def testTruth(self):
+ x0 = amount()
+ x1 = amount("1234")
+ x2 = amount("1234.56")
+
+ self.assertFalse(x0)
+ self.assertTrue(x1)
+ self.assertTrue(x2)
+
+ self.assertValid(x0)
+ self.assertValid(x1)
+ self.assertValid(x2)
+
def testCommodityTruth(self):
x1 = amount("$1234")
x2 = amount("$1234.56")
@@ -1060,6 +1111,20 @@ class AmountTestCase(unittest.TestCase):
self.assertValid(x1)
self.assertValid(x2)
+ def testForZero(self):
+ x0 = amount()
+ x1 = amount("0.000000000000000000001")
+
+ self.assertFalse(x0)
+ self.assertTrue(x1)
+ self.assertTrue(x0.is_zero())
+ self.assertTrue(x0.is_realzero())
+ self.assertFalse(x1.is_zero())
+ self.assertFalse(x1.is_realzero())
+
+ self.assertValid(x0)
+ self.assertValid(x1)
+
def testCommodityForZero(self):
x1 = amount(internalAmount("$0.000000000000000000001"))
@@ -1069,73 +1134,43 @@ class AmountTestCase(unittest.TestCase):
self.assertValid(x1)
- def testCommodityComparisons(self):
- x0 = amount()
- x1 = amount("$-123")
- x2 = amount("$123.00")
- x3 = amount(internalAmount("$-123.4544"))
- x4 = amount(internalAmount("$123.4544"))
- x5 = amount("$-123.45")
- x6 = amount("$123.45")
-
- self.assertTrue(x0 > x1)
- self.assertTrue(x0 < x2)
- self.assertTrue(x0 > x3)
- self.assertTrue(x0 < x4)
- self.assertTrue(x0 > x5)
- self.assertTrue(x0 < x6)
+ def testIntegerConversion(self):
+ x1 = amount(123456)
- self.assertTrue(x1 > x3)
- self.assertTrue(x3 <= x5)
- self.assertTrue(x3 < x5)
- self.assertTrue(x3 <= x5)
- self.assertFalse(x3 == x5)
- self.assertTrue(x3 < x1)
- self.assertTrue(x3 < x4)
+ self.assertEqual(123456, int(x1))
+ self.assertEqual(123456.0, float(x1))
+ self.assertEqual("123456", x1.to_string())
+ self.assertEqual("123456", x1.quantity_string())
- self.assertValid(x0)
self.assertValid(x1)
- self.assertValid(x2)
- self.assertValid(x3)
- self.assertValid(x4)
- self.assertValid(x5)
- self.assertValid(x6)
- def testCommoditySign(self):
- x0 = amount()
- x1 = amount(internalAmount("$0.0000000000000000000000000000000000001"))
- x2 = amount(internalAmount("$-0.0000000000000000000000000000000000001"))
- x3 = amount("$1")
- x4 = amount("$-1")
+ def testFractionalConversion(self):
+ x1 = amount(1234.56)
- self.assertFalse(x0.sign())
- self.assertTrue(x1.sign() != 0)
- self.assertTrue(x2.sign() != 0)
- self.assertTrue(x3.sign() > 0)
- self.assertTrue(x4.sign() < 0)
+ self.assertRaises(exceptions.ArithmeticError, amount.to_long, x1)
+ self.assertEqual(1234, x1.to_long(True))
+ self.assertEqual(1234.56, float(x1))
+ self.assertEqual("1234.56", x1.to_string())
+ self.assertEqual("1234.56", x1.quantity_string())
- self.assertValid(x0)
self.assertValid(x1)
- self.assertValid(x2)
- self.assertValid(x3)
- self.assertValid(x4)
- def testCommodityAbs(self):
- x0 = amount()
- x1 = amount("$-1234.56")
- x2 = amount("$1234.56")
+ def testCommodityConversion(self):
+ x1 = amount("$1234.56")
- self.assertEqual(amount(), abs(x0))
- self.assertEqual(amount("$1234.56"), abs(x1))
- self.assertEqual(amount("$1234.56"), abs(x2))
+ self.assertRaises(exceptions.ArithmeticError, amount.to_long, x1)
+ self.assertEqual(1234, x1.to_long(True))
+ self.assertEqual(1234.56, float(x1))
+ self.assertEqual("$1234.56", x1.to_string())
+ self.assertEqual("1234.56", x1.quantity_string())
- self.assertValid(x0)
self.assertValid(x1)
- self.assertValid(x2)
- def testCommodityPrinting(self):
+ def testPrinting(self):
pass
+ def testCommodityPrinting(self):
+ pass
def suite():
return unittest.TestLoader().loadTestsFromTestCase(AmountTestCase)