1 | import json
|
---|
2 | from typing import List
|
---|
3 | import unittest
|
---|
4 |
|
---|
5 | from pyson.ObjectMapper import ObjectMapper
|
---|
6 |
|
---|
7 | from geniusweb.issuevalue.DiscreteValue import DiscreteValue
|
---|
8 | from geniusweb.issuevalue.DiscreteValueSet import DiscreteValueSet
|
---|
9 | from geniusweb.issuevalue.ValueSet import ValueSet
|
---|
10 |
|
---|
11 |
|
---|
12 | class DiscreteValueSetTest(unittest.TestCase):
|
---|
13 |
|
---|
14 | A = DiscreteValue("a");
|
---|
15 | A1 = DiscreteValue("a");
|
---|
16 | B = DiscreteValue("b");
|
---|
17 | values:List[DiscreteValue] = [A, B]
|
---|
18 | valuesReverse = [B, A]
|
---|
19 |
|
---|
20 | set1 = DiscreteValueSet(values)
|
---|
21 | setReverse = DiscreteValueSet(valuesReverse)
|
---|
22 | asString = "{\"values\":[\"a\",\"b\"]}"
|
---|
23 | # String asString = "{\"DiscreteValueSet
|
---|
24 | # \":[\"a\",\"b\"]}";
|
---|
25 | asString1 = "[\"a\",\"b\"]"
|
---|
26 |
|
---|
27 | # SPECIAL. note that the "numbers" have no leading "=" and therefore are
|
---|
28 | # parsed as discrete values.
|
---|
29 | discretenumbers = "{\"values\":[\"0\",\"1\"]}"
|
---|
30 | jackson = ObjectMapper()
|
---|
31 |
|
---|
32 | def testSerialize(self) :
|
---|
33 | jsonobj = self.jackson.toJson(self.set1)
|
---|
34 | print(jsonobj)
|
---|
35 | self.assertEqual(json.loads(self.asString), jsonobj)
|
---|
36 |
|
---|
37 | def testDeserialize(self):
|
---|
38 | p = self.jackson.parse(json.loads(self.asString), DiscreteValueSet)
|
---|
39 | print(p)
|
---|
40 | self.assertEqual(self.set1, p)
|
---|
41 |
|
---|
42 | def testDeserializeNumberValues(self):
|
---|
43 | p = self.jackson.parse(json.loads(self.discretenumbers), DiscreteValueSet)
|
---|
44 | print(p)
|
---|
45 | self.assertEqual("0", p.get(0).getValue()) # not the number but string
|
---|
46 | self.assertEquals("1", p.get(1).getValue())
|
---|
47 |
|
---|
48 | def testEquality(self):
|
---|
49 | self.assertEqual(self.set1, self.setReverse)
|
---|
50 |
|
---|
51 | def testHashCodeInsensitiveToOrder(self):
|
---|
52 | self.assertEqual(hash(self.set1), hash(self.setReverse))
|
---|
53 |
|
---|
54 | def testSize(self):
|
---|
55 | self.assertEqual(2, self.set1.size())
|
---|
56 |
|
---|
57 | def testGet(self):
|
---|
58 | self.assertEqual(self.A, self.set1.get(0))
|
---|
59 | self.assertEqual(self.B, self.set1.get(1))
|
---|
60 | self.assertEqual(self.A, self.set1.get(0))
|
---|
61 | self.assertEqual(self.B, self.set1.get(1))
|
---|
62 |
|
---|
63 | def testgetValues(self):
|
---|
64 | self.assertEqual(self.values, self.set1.getValues())
|
---|
65 |
|
---|
66 | def testConstructor2(self):
|
---|
67 | self.assertEqual(self.set1, DiscreteValueSet([self.A, self.B]))
|
---|
68 |
|
---|
69 | def testContains(self):
|
---|
70 | self.assertTrue(self.set1.contains(self.A))
|
---|
71 | self.assertTrue(self.set1.contains(self.A1))
|
---|
72 | self.assertTrue(self.set1.contains(self.B))
|
---|
73 | self.assertFalse(self.set1.contains(DiscreteValue("c")))
|
---|
74 | self.assertFalse(self.set1.contains(None))
|
---|
75 |
|
---|
76 | def testiterator1(self) :
|
---|
77 | it = iter(self.set1);
|
---|
78 | self.assertEqual(self.A, next(it));
|
---|
79 | self.assertEqual(self.B, next(it));
|
---|
80 | self.assertRaises(StopIteration, lambda: next(it))
|
---|
81 |
|
---|
82 | def testiterator2(self):
|
---|
83 | it = iter(self.setReverse)
|
---|
84 | self.assertEqual(self.B, next(it))
|
---|
85 | self.assertEqual(self.A, next(it));
|
---|
86 | self.assertRaises(StopIteration,lambda: next(it))
|
---|