source: geniuswebcore/test/geniusweb/issuevalue/NumberValueTest.py@ 78

Last change on this file since 78 was 73, checked in by Bart Vastenhouw, 3 years ago

Fix for IssueValue hashcode.

File size: 2.9 KB
Line 
1from _decimal import Decimal
2import json
3from typing import List, Any
4import unittest
5
6from pyson.ObjectMapper import ObjectMapper
7from unitpy.GeneralTests import GeneralTests
8
9from geniusweb.issuevalue.NumberValue import NumberValue
10from geniusweb.issuevalue.Value import Value
11
12
13class NumberValueTest(unittest.TestCase, GeneralTests[NumberValue]):
14 value = NumberValue(Decimal("12.31"))
15 value1 = NumberValue(Decimal("12.31"))
16 value1c = NumberValue(Decimal("1.231E+1"))
17 valueb = NumberValue(Decimal("12.310000000000000001"))
18 serialized = "12.31"
19
20 bigserialized = "8748703924870439218876954320948372058"
21 bigvalue = NumberValue(Decimal(bigserialized))
22 jackson = ObjectMapper()
23
24 def getGeneralTestData(self)->List[List[NumberValue]]:
25 return [ [self.value, self.value1, self.value1c], [self.valueb]]
26
27 def getGeneralTestStrings(self)->List[str]:
28 return ["12.31", "12.310000000000000001"]
29
30 def testSerialize(self) :
31 self.assertEquals(json.loads(self.serialized), self.jackson.toJson(self.value))
32
33 def testDeserialize(self):
34 self.assertEqual(self.value, self.jackson.parse(json.loads(self.serialized), NumberValue))
35
36 def testDeserializeFromValue(self):
37 self.assertEqual(self.value, self.jackson.parse(json.loads(self.serialized), Value))
38
39 def testBigSerialize(self):
40 self.assertEqual(json.loads(self.bigserialized), self.jackson.toJson(self.bigvalue))
41
42 def testBigDeserialize(self):
43 self.assertEquals(self.bigvalue,
44 self.jackson.parse(json.loads(self.bigserialized), NumberValue))
45
46 def testSerializeShorts(self):
47 print(self.jackson.toJson([ 1, 2, 3, 4, 5 ]))
48
49 def testDeserializeShorts(self) :
50 list = self.jackson.parse(json.loads("[1,2,3,4,5]"),List[Any])
51 print(list)
52 print(list[0].__class__)
53
54 def testDeserializeMix(self):
55 list = self.jackson.parse(json.loads('["short",1,2,3,4,5]'), List[Any])
56 print(list)
57 print(list[0].__class__)
58
59 def testDeserializePlainNumber(self):
60 '''
61 Showing that we CAN deserialize big numbers without double quotes
62 correctly, if we tell jackson upfront that it's a BigDecimal.
63 '''
64 valstr = "483958743698732691487326987569213874694328974329874328947320984372498327493827432987231874681273648127";
65 val = self.jackson.parse(valstr, Decimal)
66 self.assertEqual(valstr, str(val))
67
68
69 def testNumberBlab(self):
70 # jackson.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
71 self.assertEqual(self.value, self.jackson.parse(json.loads("12.31"), Value))
72
73 def testNonNumber(self):
74 self.assertRaises(ValueError, lambda: NumberValue("abc"))
75
76 def testDeserializeScientificNotation(self):
77 valstr = "9E+2";
78 val = self.jackson.parse(json.loads(valstr), Value)
79 # self.assertEqual(valstr, str(val)) FIXME?
80 self.assertEqual(val, NumberValue(Decimal("900")))
81
Note: See TracBrowser for help on using the repository browser.