from _decimal import Decimal import unittest from pyson.ObjectMapper import ObjectMapper from geniusweb.issuevalue.NumberValue import NumberValue from geniusweb.issuevalue.Value import Value class NumberValueTest(unittest.TestCase): pyson=ObjectMapper() val = NumberValue(Decimal('0.1237')) val2 = NumberValue(Decimal('2')) valjson = 0.1237 def testSerialize(self): print(str(self.pyson.toJson(self.val))) self.assertEqual(self.valjson, self.pyson.toJson(self.val)) def testDeserialize(self): self.assertEqual(self.val, self.pyson.parse(self.valjson, Value)) def testSerialize2(self): print(str(self.pyson.toJson(NumberValue(Decimal("900"))))) def testNonDecimal(self): self.assertRaises(ValueError, lambda:NumberValue(1200)) def testHash(self): self.assertEqual(self.val,self.val) self.assertNotEqual(self.val,self.val2) self.assertNotEqual(hash(self.val), hash(self.val2)) def testRepr(self): self.assertEqual('2', repr(self.val2))