source: geniuswebcore/test/geniusweb/references/ParametersTest.py@ 77

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

Fix for IssueValue hashcode.

File size: 2.7 KB
Line 
1import json
2from typing import List
3import unittest
4
5from pyson.ObjectMapper import ObjectMapper
6from unitpy.GeneralTests import GeneralTests
7
8from geniusweb.references.Parameters import Parameters
9
10
11class ParametersTest (unittest.TestCase, GeneralTests[Parameters]) :
12 pyson=ObjectMapper()
13 params1=Parameters()
14 params1a=Parameters()
15 params2=Parameters({'a':1, 'b':2.0})
16
17 paramsjson='{"a":1, "b":2.0}'
18
19 def getGeneralTestData(self)-> List[List[Parameters]] :
20 return [[ self.params1, self.params1a],[self.params2]]
21
22 def getGeneralTestStrings(self)->List[str]:
23 return ["\\{\\}", "\\{a=1, b=2.0\\}"]
24
25
26 def testGet(self):
27 self.assertEqual({'a':1, 'b':2.0}, self.params2.getParameters())
28
29
30 def testGetWrongClass(self):
31 self.assertRaises(ValueError, lambda:self.params2.getType("a", str))
32
33 def testGetInteger(self) :
34 self.assertEqual(1, self.params2.getType("a", int))
35 self.assertEqual(2.0, self.params2.getType("b", float))
36
37 def testGetDouble(self):
38 self.assertEquals( 2., self.params2.getDouble("b", 7.0, None, None))
39 self.assertEquals(2., self.params2.getDouble("b", 7.0, 0., 4.))
40
41 # test outside range
42 self.assertEqual(7., self.params2.getDouble("b", 7., 4., 6.))
43 self.assertEqual(7., self.params2.getDouble("b", 7., -2., 1.))
44
45 # test fallback None
46 self.assertEqual(None, self.params2.getDouble("b", None, 4., 6.))
47
48 def testGetDoubleWithErrors(self):
49 # a does not contain a double
50 self.assertEqual(7., self.params2.getDouble("a", 7., 4., 6.));
51 # c does not exist
52 self.assertEqual(7., self.params2.getDouble("c", 7., 4., 6.));
53
54 def testWith(self):
55 p = self.params2.With("a", 2)
56 self.assertEqual(1, self.params2.get("a"))
57 self.assertEqual(2., self.params2.get("b"))
58 self.assertEqual(2, p.get("a"))
59 self.assertEqual(2., p.get("b"))
60
61 def testWithParams(self):
62 upd = Parameters().With("a", 2)
63 p = self.params2.WithParameters(upd)
64 self.assertEqual(1, self.params2.get("a"))
65 self.assertEqual(2., self.params2.get("b"))
66 self.assertEqual(2, p.get("a"))
67 self.assertEqual(2., p.get("b"))
68
69 def testIsEmpty(self) :
70 self.assertTrue(self.params1.isEmpty())
71 self.assertFalse(self.params2.isEmpty())
72
73 def testContainsKey(self):
74 self.assertTrue(self.params2.containsKey("a"))
75 self.assertFalse(self.params2.containsKey("c"))
76
77
78 def testSerialize(self):
79 print(str(self.pyson.toJson(self.params2)))
80 self.assertEqual(json.loads(self.paramsjson), self.pyson.toJson(self.params2))
81
82 def testDeserialize(self):
83 self.assertEqual(self.params2, self.pyson.parse(json.loads(self.paramsjson), Parameters))
84
85
86 def testWith2(self):
87 p=Parameters({'a':1}).With('b', 2)
88 self.assertEqual({'a':1, 'b':2}, p.getParameters())
89
90 def testParamNotAvailable(self):
91 self.assertEqual(None, self.params1.get("nosuchkey"))
92
Note: See TracBrowser for help on using the repository browser.