1 | import json
|
---|
2 | from typing import List, Dict
|
---|
3 | import unittest
|
---|
4 |
|
---|
5 | from pyson.ObjectMapper import ObjectMapper
|
---|
6 | from unitpy.GeneralTests import GeneralTests
|
---|
7 |
|
---|
8 | from geniusweb.actions.PartyId import PartyId
|
---|
9 | from geniusweb.inform.Agreements import Agreements
|
---|
10 | from geniusweb.protocol.ProtocolException import ProtocolException
|
---|
11 | from geniusweb.protocol.session.mopac.PartyStates import PartyStates
|
---|
12 |
|
---|
13 |
|
---|
14 | class PartyStatesTest (unittest.TestCase, GeneralTests[PartyStates]):
|
---|
15 | jackson = ObjectMapper()
|
---|
16 |
|
---|
17 | party1 = PartyId("party1")
|
---|
18 | party2 = PartyId("party2")
|
---|
19 | party3 = PartyId("party3")
|
---|
20 |
|
---|
21 | powers:Dict[PartyId, int] = {}
|
---|
22 | serialized = "{\"notYetActed\":[\"party2\",\"party1\",\"party3\"],\"actions\":[],\"agreements\":{},\"walkedAway\":[],\"exceptions\":{},\"powers\":{\"party2\":3,\"party1\":2,\"party3\":3}}";
|
---|
23 |
|
---|
24 |
|
---|
25 | powers[party1]= 2
|
---|
26 | powers[party2]=3
|
---|
27 | states2 = PartyStates(powers)
|
---|
28 | powers[party3]= 3
|
---|
29 | states1 = PartyStates(powers)
|
---|
30 | states1a = PartyStates(powers)
|
---|
31 |
|
---|
32 | def getGeneralTestData(self)->List[List[PartyStates]] :
|
---|
33 | return [[self.states1, self.states1a],
|
---|
34 | [self.states2]]
|
---|
35 |
|
---|
36 | def getGeneralTestStrings(self)->List[str] :
|
---|
37 | return [
|
---|
38 | "PartyStates.*\\[party., party., party.\\],\\[\\],Agreements\\{\\},\\[\\],\\{\\}.*",
|
---|
39 | "PartyStates.*\\[party., party.\\],\\[\\],Agreements\\{\\},\\[\\],\\{\\}.*"]
|
---|
40 |
|
---|
41 | def testBasics(self):
|
---|
42 | self.assertEqual(self.powers.keys(), self.states1.getNegotiatingParties())
|
---|
43 | self.assertEqual(self.powers.keys(), self.states1.getNotYetActed());
|
---|
44 | self.assertEqual(0, len(self.states1.getExceptions()))
|
---|
45 |
|
---|
46 | def testException(self):
|
---|
47 | newstates = self.states1\
|
---|
48 | .WithException(ProtocolException("bla", self.party1))
|
---|
49 | self.assertEqual(Agreements(), newstates.getAgreements())
|
---|
50 | self.assertEqual(self.powers.keys(), self.states1.getNegotiatingParties())
|
---|
51 | self.assertEqual(set([self.party2, self.party3]),
|
---|
52 | newstates.getNotYetActed())
|
---|
53 |
|
---|
54 | def testFinish(self):
|
---|
55 | newstates = self.states1.finish()
|
---|
56 | self.assertEqual(Agreements(), newstates.getAgreements())
|
---|
57 | self.assertEqual(self.powers.keys(), self.states1.getNegotiatingParties())
|
---|
58 | self.assertEqual(set(), newstates.getNotYetActed())
|
---|
59 |
|
---|
60 | def testDeserialize(self):
|
---|
61 | obj = self.jackson.parse(json.loads(self.serialized), PartyStates)
|
---|
62 | print(obj)
|
---|
63 | self.assertEqual(self.states1, obj)
|
---|
64 |
|
---|
65 | def testSerialize(self):
|
---|
66 | jsonobj = self.jackson.toJson(self.states1)
|
---|
67 | print(jsonobj);
|
---|
68 | jsonloads=json.loads(self.serialized)
|
---|
69 | # BRUTE HACK workaround set ordering
|
---|
70 | jsonloads['notYetActed']=jsonobj['notYetActed']
|
---|
71 | self.assertEqual(jsonloads, jsonobj)
|
---|
72 |
|
---|
73 | def testWalkAway(self):
|
---|
74 | walkawaystate = self.states1.WithWalkAway(self.party2)
|
---|
75 | self.assertEqual(set([self.party1, self.party3]),
|
---|
76 | walkawaystate.getNotYetActed())
|
---|
77 | self.assertEqual([self.party2], walkawaystate.getWalkedAway())
|
---|