source: geniuswebcore/test/geniusweb/protocol/session/SessionResultTest.py@ 88

Last change on this file since 88 was 88, checked in by Bart Vastenhouw, 2 years ago

Added python SimpleRunner GUI

File size: 4.1 KB
Line 
1from collections import OrderedDict
2import json
3from typing import List, Dict
4import unittest
5
6from pyson.ObjectMapper import ObjectMapper
7from unitpy.GeneralTests import GeneralTests
8from uri.uri import URI
9
10from geniusweb.actions.PartyId import PartyId
11from geniusweb.inform.Agreements import Agreements
12from geniusweb.issuevalue.Bid import Bid
13from geniusweb.issuevalue.DiscreteValue import DiscreteValue
14from geniusweb.issuevalue.Value import Value
15from geniusweb.protocol.session.SessionResult import SessionResult
16from geniusweb.references.Parameters import Parameters
17from geniusweb.references.PartyRef import PartyRef
18from geniusweb.references.PartyWithParameters import PartyWithParameters
19from geniusweb.references.PartyWithProfile import PartyWithProfile
20from geniusweb.references.ProfileRef import ProfileRef
21
22
23class SessionResultTest(unittest.TestCase, GeneralTests[SessionResult]):
24 jackson = ObjectMapper()
25 error = ValueError("test");
26
27 ISSUE1 = "issue1";
28 jsonstring = "{\"participants\":{\"party2\":{\"party\":{\"partyref\":\"party2\",\"parameters\":{}},\"profile\":\"profile2\"},\"party1\":{\"party\":{\"partyref\":\"party1\",\"parameters\":{}},\"profile\":\"profile1\"}},\"agreements\":{\"party2\":{\"issuevalues\":{\"issue1\":\"a\"}},\"party1\":{\"issuevalues\":{\"issue1\":\"a\"}}},\"penalties\":{\"party2\":0.0,\"party1\":0.0},\"error\":null}";
29 nopenalties:Dict[PartyId, float] = {}
30 penalties:Dict[PartyId, float] = {}
31
32 PARTY1 = PartyId("party1")
33 PARTY2 = PartyId("party2")
34 PARTY3 = PartyId("party3")
35
36 penalties[PARTY1]= 0.1
37 penalties[PARTY2]= 0.2
38 nopenalties[PARTY1]= 0.
39 nopenalties[PARTY2]= 0.
40
41 errorstring = "\"error\":{\"java.lang.RuntimeException\":"\
42 + json.dumps(jackson.toJson(error)) + "}"
43 print(errorstring)
44
45 party1 = PartyWithParameters(PartyRef(URI("party1")), Parameters())
46 party2 = PartyWithParameters(PartyRef(URI("party2")), Parameters())
47
48 partyprofile1 = PartyWithProfile(party1, ProfileRef(URI("profile1")))
49 partyprofile2 = PartyWithProfile(party2, ProfileRef(URI("profile2")))
50
51 issuevalues1:Dict[str, Value] = {}
52 issuevalues1[ISSUE1]=DiscreteValue("a")
53 bid1 = Bid(issuevalues1)
54 agreement1 = Agreements().With(Agreements({PARTY1:bid1, PARTY2:bid1} ))
55
56 # different order but that shouldn't matter
57 issuevalues2:Dict[str, Value] =OrderedDict()
58 issuevalues2[ISSUE1]= DiscreteValue("b")
59 bid2 = Bid(issuevalues2)
60 agreement2 = Agreements().With(Agreements({ PARTY1:bid2, PARTY3:bid2} ))
61
62 partiesmap:Dict[PartyId, PartyWithProfile] = OrderedDict()
63 partiesmap[PARTY2]= partyprofile2
64 partiesmap[PARTY1]= partyprofile1
65
66 partiesmap2:Dict[PartyId, PartyWithProfile] = OrderedDict()
67 partiesmap2[PARTY1]= partyprofile2
68 partiesmap2[PARTY3]= partyprofile1
69
70 result1 = SessionResult(partiesmap, agreement1, nopenalties, None)
71 result1a = SessionResult(partiesmap, agreement1, nopenalties, None)
72 result2 = SessionResult(partiesmap, agreement2, nopenalties, None)
73 result3 = SessionResult(partiesmap2, agreement1, nopenalties, None)
74 result4 = SessionResult(partiesmap2, agreement1, penalties, None)
75
76 # IGNORE ERROR for now, it fails somewhere deep in maven suddenly.
77 # result4 = new SessionResult(Arrays.asList(partyprofile1,
78 # partyprofile2), bid1, error);
79
80 def getGeneralTestData(self) ->List[List[SessionResult]]:
81 return [[self.result1, self.result1a],
82 [self.result2], [self.result3],
83 [self.result4]]
84
85 def getGeneralTestStrings(self) -> List[str]:
86 return [
87 "SessionResult.*party2.*profile2.*,.*party1.*profile1.*Agreements.*Bid.*issue1=\"a\".*0\\.0.*0\\.0.*None.*",
88 "SessionResult.*party2.*profile2.*,.*party1.*profile1.*Agreements.*Bid.*issue1=\"b\".*0\\.0.*0\\.0.*None.*",
89 "SessionResult.*party1.*profile2.*,.*party3.*profile1.*Agreements.*Bid.*issue1=\"a\".*0\\.0.*0\\.0.*None.*",
90 "SessionResult.*party1.*profile2.*,.*party3.*profile1.*Agreements.*Bid.*issue1=\"a\".*party1.*0\\.1.*party2.*0\\.2.*None.*"
91 ]
92
93 def serializeTest(self) :
94 print(self.jackson.toJson(self.result1))
95 self.assertEqual(json.loads(self.jsonstring), self.jackson.toJson(self.result1))
96
97 def deserializeTest(self):
98 act = self.jackson.parse(json.loads(self.jsonstring), SessionResult)
99 self.assertEqual(self.result1, act)
100
Note: See TracBrowser for help on using the repository browser.