1 | from collections import OrderedDict
|
---|
2 | from decimal import Decimal
|
---|
3 | import json
|
---|
4 | from typing import List, Dict
|
---|
5 | import unittest
|
---|
6 | from unittest.mock import Mock
|
---|
7 |
|
---|
8 | from pyson.ObjectMapper import ObjectMapper
|
---|
9 | from unitpy.GeneralTests import GeneralTests
|
---|
10 |
|
---|
11 | from geniusweb.issuevalue.Bid import Bid
|
---|
12 | from geniusweb.issuevalue.DiscreteValue import DiscreteValue
|
---|
13 | from geniusweb.issuevalue.NumberValue import NumberValue
|
---|
14 | from geniusweb.issuevalue.Value import Value
|
---|
15 |
|
---|
16 |
|
---|
17 | class BidTest (unittest.TestCase, GeneralTests[Bid]):
|
---|
18 |
|
---|
19 | asString1 = "{\"issuevalues\":{\"issue3\":901234567.89,\"issue2\":1,\"issue1\":\"b\"}}"
|
---|
20 | ISSUE1 = "issue1"
|
---|
21 | VALUE1:Value = DiscreteValue("value1")
|
---|
22 | ISSUE2 = "issue2"
|
---|
23 | VALUE2:Value = NumberValue(Decimal("10"))
|
---|
24 | list:List[List[Bid]] = []
|
---|
25 |
|
---|
26 | issuevalues:Dict[str, Value] = OrderedDict()
|
---|
27 | issuevalues["issue3"]= NumberValue(Decimal("901234567.89"))
|
---|
28 | issuevalues[ISSUE2]= NumberValue(Decimal(1))
|
---|
29 | issuevalues[ISSUE1]= DiscreteValue("b")
|
---|
30 | bid = Bid(issuevalues);
|
---|
31 |
|
---|
32 | # different order but that shouldn't matter
|
---|
33 | issuevalues1:Dict[str, Value] = OrderedDict()
|
---|
34 | issuevalues1["issue3"]= NumberValue(Decimal("901234567.89"))
|
---|
35 | issuevalues1[ISSUE2]= NumberValue(Decimal(1))
|
---|
36 | issuevalues1[ISSUE1]= DiscreteValue("b")
|
---|
37 | bid1 = Bid(issuevalues1)
|
---|
38 |
|
---|
39 | issuevaluesb:Dict[str, Value] = OrderedDict()
|
---|
40 | issuevaluesb[ISSUE2]= NumberValue(Decimal(1))
|
---|
41 | issuevaluesb[ISSUE1]= DiscreteValue("b")
|
---|
42 | bidb = Bid(issuevaluesb)
|
---|
43 |
|
---|
44 | # bidc and bidd have values swapped, so different issuevalues.
|
---|
45 | issuevaluesc:Dict[str, Value] = OrderedDict()
|
---|
46 | issuevaluesc[ISSUE2]=VALUE2
|
---|
47 | issuevaluesc[ISSUE1]= VALUE1
|
---|
48 | bidc = Bid(issuevaluesc)
|
---|
49 |
|
---|
50 | list.append([bid, bid1])
|
---|
51 | list.append([bidb])
|
---|
52 | list.append([bidc])
|
---|
53 |
|
---|
54 | def getGeneralTestData(self)->List[List[Bid]] :
|
---|
55 | return self.list
|
---|
56 |
|
---|
57 | def getGeneralTestStrings(self)->List[str] :
|
---|
58 | return [
|
---|
59 | "Bid\\{issue3=901234567.89, issue2=1, issue1=\"b\"\\}",
|
---|
60 | "Bid\\{issue2=1, issue1=\"b\"\\}",
|
---|
61 | "Bid\\{issue2=10, issue1=\"value1\"\\}"]
|
---|
62 |
|
---|
63 |
|
---|
64 | def testNullConstructor(self):
|
---|
65 | issuevalues = None;
|
---|
66 | self.assertRaises(ValueError, lambda: Bid(issuevalues))
|
---|
67 |
|
---|
68 | def testUnkownIssue(self):
|
---|
69 | self.assertEqual(None, self.bid.getValue("NotAnIssue"))
|
---|
70 |
|
---|
71 | def testBidSimple(self):
|
---|
72 | issuevalues = {}
|
---|
73 | Bid(issuevalues) # no issues, no values should be ok
|
---|
74 |
|
---|
75 | def testBidNull(self):
|
---|
76 | issuevalues = {}
|
---|
77 | issuevalues[self.ISSUE1]=None
|
---|
78 | self.assertRaises(ValueError, lambda:Bid(issuevalues)) # smokes
|
---|
79 |
|
---|
80 | def testbidOkIssueValue(self):
|
---|
81 | issuevalues = OrderedDict()
|
---|
82 | issuevalues[self.ISSUE1]=Mock(Value)
|
---|
83 | Bid(issuevalues) # shouldn't smoke
|
---|
84 |
|
---|
85 | def testSerialize(self) :
|
---|
86 | jackson = ObjectMapper()
|
---|
87 | print(str(jackson.toJson(self.bid)))
|
---|
88 | self.assertEqual(json.loads(self.asString1), jackson.toJson(self.bid))
|
---|
89 |
|
---|
90 | def testDeserialize(self) :
|
---|
91 | jackson = ObjectMapper()
|
---|
92 | read = jackson.parse(json.loads(self.asString1), Bid)
|
---|
93 | self.assertEquals(self.bid, read)
|
---|
94 |
|
---|
95 | def testMerge(self):
|
---|
96 | issuevalues = {}
|
---|
97 | issuevalues[self.ISSUE1]= self.VALUE1
|
---|
98 | partial1 = Bid(issuevalues)
|
---|
99 |
|
---|
100 | issuevalues = {}
|
---|
101 | issuevalues[self.ISSUE2]= self.VALUE2
|
---|
102 | partial2 = Bid(issuevalues)
|
---|
103 |
|
---|
104 | mergedbid = partial1.merge(partial2)
|
---|
105 | self.assertEqual(self.bidc, mergedbid)
|
---|
106 |
|
---|
107 | def testMergeIssuesOverlap(self):
|
---|
108 | issuevalues = {}
|
---|
109 | issuevalues[self.ISSUE1]=self.VALUE1
|
---|
110 | partial1 = Bid(issuevalues)
|
---|
111 |
|
---|
112 | issuevalues = {}
|
---|
113 | issuevalues[self.ISSUE1]=self.VALUE2
|
---|
114 | partial2 = Bid(issuevalues)
|
---|
115 |
|
---|
116 | self.assertRaises(ValueError, lambda:partial1.merge(partial2))
|
---|
117 |
|
---|
118 | def testSmokeConstructor2(self):
|
---|
119 | Bid({"issue": DiscreteValue("dss")})
|
---|
120 |
|
---|
121 | def testSmokeConstructor2b(self):
|
---|
122 | Bid({"issue": NumberValue(Decimal("0.5"))})
|
---|
123 |
|
---|
124 | def testSmokeConstructor2Null1(self):
|
---|
125 | self.assertRaises(ValueError, lambda:Bid({None: DiscreteValue("dss")}))
|
---|
126 |
|
---|
127 | def testSmokeConstructor2Null2(self):
|
---|
128 | self.assertRaises(ValueError, lambda: Bid({"issue": None}))
|
---|
129 |
|
---|