1 | from datetime import datetime
|
---|
2 | import json
|
---|
3 | from typing import List
|
---|
4 | import unittest
|
---|
5 |
|
---|
6 | from pyson.ObjectMapper import ObjectMapper
|
---|
7 | from unitpy.GeneralTests import GeneralTests
|
---|
8 | from uri.uri import URI
|
---|
9 |
|
---|
10 | from geniusweb.actions.PartyId import PartyId
|
---|
11 | from geniusweb.actions.Vote import Vote
|
---|
12 | from geniusweb.actions.Votes import Votes
|
---|
13 | from geniusweb.inform.Inform import Inform
|
---|
14 | from geniusweb.inform.OptIn import OptIn
|
---|
15 | from geniusweb.issuevalue.Bid import Bid
|
---|
16 | from geniusweb.issuevalue.DiscreteValue import DiscreteValue
|
---|
17 |
|
---|
18 |
|
---|
19 | class OptInTest(unittest.TestCase, GeneralTests[OptIn]):
|
---|
20 | maxDiff = None
|
---|
21 |
|
---|
22 | bid1 = Bid({"iss": DiscreteValue("val1")})
|
---|
23 | bid2 = Bid({"iss": DiscreteValue("val2")})
|
---|
24 | partyA = PartyId("partyA")
|
---|
25 | partyB = PartyId("partyB")
|
---|
26 | voteA1 = Vote(partyA, bid1, 2, 9)
|
---|
27 | voteB1 = Vote(partyB, bid1, 2, 9)
|
---|
28 | voteB2 = Vote(partyB, bid2, 2, 9)
|
---|
29 |
|
---|
30 | votesA = Votes(partyA, set([voteA1]))
|
---|
31 | votesB = Votes(partyB, set([voteB1, voteB2]))
|
---|
32 |
|
---|
33 | optIn1 = OptIn([votesA, votesB])
|
---|
34 | optIn1a = OptIn([votesA, votesB])
|
---|
35 | optIn2 = OptIn([votesA])
|
---|
36 | jackson = ObjectMapper();
|
---|
37 |
|
---|
38 | asJson = "{\"OptIn\":{\"votes\":[{\"Votes\":{\"actor\":\"partyA\",\"votes\":[{\"Vote\":{\"actor\":\"partyA\",\"bid\":{\"issuevalues\":{\"iss\":\"val1\"}},\"minPower\":2,\"maxPower\":9}}]}},{\"Votes\":{\"actor\":\"partyB\",\"votes\":[{\"Vote\":{\"actor\":\"partyB\",\"bid\":{\"issuevalues\":{\"iss\":\"val2\"}},\"minPower\":2,\"maxPower\":9}},{\"Vote\":{\"actor\":\"partyB\",\"bid\":{\"issuevalues\":{\"iss\":\"val1\"}},\"minPower\":2,\"maxPower\":9}}]}}]}}"
|
---|
39 |
|
---|
40 | def getGeneralTestData(self) -> List[List[OptIn]]:
|
---|
41 | return [[self.optIn1, self.optIn1a], [self.optIn2]]
|
---|
42 |
|
---|
43 | def getGeneralTestStrings(self) -> List[str]:
|
---|
44 | return ["OptIn.*Votes.*Vote.*partyA.*iss.*val1.*2.*Votes.*Vote.*partyB.*iss.*val.*.*2.*Vote.*partyB.*iss.*val.*2.*",
|
---|
45 | "OptIn.*Votes.*Vote.*partyA.*iss.*val.*2.*"]
|
---|
46 |
|
---|
47 | def testSerialize(self):
|
---|
48 | jsonstruct = self.jackson.toJson(self.optIn1)
|
---|
49 | print(jsonstruct)
|
---|
50 | # test does not work, python keeps changing the field order...
|
---|
51 | # self.assertEqual(self.bla, jsonstruct)
|
---|
52 |
|
---|
53 | def testDeserialize(self):
|
---|
54 | p = self.jackson.parse(json.loads(self.asJson), Inform)
|
---|
55 | print(p);
|
---|
56 | self.assertEqual(self.optIn1, p)
|
---|
57 |
|
---|
58 | def testOneVotePerParty(self):
|
---|
59 | self.assertRaises(ValueError, lambda: OptIn([self.votesA, self.votesA]))
|
---|
60 |
|
---|