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

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

Refactor to help reusing partiesserver.

File size: 2.8 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 # a is integer!
48 self.assertEqual(1, self.params2.getDouble("a", 0., -0., 2.))
49
50 def testGetDoubleWithErrors(self):
51 # a does not contain a double
52 self.assertEqual(7., self.params2.getDouble("a", 7., 4., 6.));
53 # c does not exist
54 self.assertEqual(7., self.params2.getDouble("c", 7., 4., 6.));
55
56 def testWith(self):
57 p = self.params2.With("a", 2)
58 self.assertEqual(1, self.params2.get("a"))
59 self.assertEqual(2., self.params2.get("b"))
60 self.assertEqual(2, p.get("a"))
61 self.assertEqual(2., p.get("b"))
62
63 def testWithParams(self):
64 upd = Parameters().With("a", 2)
65 p = self.params2.WithParameters(upd)
66 self.assertEqual(1, self.params2.get("a"))
67 self.assertEqual(2., self.params2.get("b"))
68 self.assertEqual(2, p.get("a"))
69 self.assertEqual(2., p.get("b"))
70
71 def testIsEmpty(self) :
72 self.assertTrue(self.params1.isEmpty())
73 self.assertFalse(self.params2.isEmpty())
74
75 def testContainsKey(self):
76 self.assertTrue(self.params2.containsKey("a"))
77 self.assertFalse(self.params2.containsKey("c"))
78
79
80 def testSerialize(self):
81 print(str(self.pyson.toJson(self.params2)))
82 self.assertEqual(json.loads(self.paramsjson), self.pyson.toJson(self.params2))
83
84 def testDeserialize(self):
85 self.assertEqual(self.params2, self.pyson.parse(json.loads(self.paramsjson), Parameters))
86
87
88 def testWith2(self):
89 p=Parameters({'a':1}).With('b', 2)
90 self.assertEqual({'a':1, 'b':2}, p.getParameters())
91
92 def testParamNotAvailable(self):
93 self.assertEqual(None, self.params1.get("nosuchkey"))
94
Note: See TracBrowser for help on using the repository browser.