1 | import json
|
---|
2 | from typing import List
|
---|
3 | import unittest
|
---|
4 | from unittest.mock import Mock
|
---|
5 |
|
---|
6 | from pyson.ObjectMapper import ObjectMapper
|
---|
7 | from tudelft_utilities_logging.Reporter import Reporter
|
---|
8 | from unitpy.GeneralTests import GeneralTests
|
---|
9 | from uri.uri import URI
|
---|
10 |
|
---|
11 | from geniusweb.deadline.DeadlineRounds import DeadlineRounds
|
---|
12 | from geniusweb.protocol.session.SessionSettings import SessionSettings
|
---|
13 | from geniusweb.protocol.session.TeamInfo import TeamInfo
|
---|
14 | from geniusweb.protocol.session.learn.LearnSettings import LearnSettings
|
---|
15 | from geniusweb.references.Parameters import Parameters
|
---|
16 | from geniusweb.references.PartyRef import PartyRef
|
---|
17 | from geniusweb.references.PartyWithParameters import PartyWithParameters
|
---|
18 | from geniusweb.references.PartyWithProfile import PartyWithProfile
|
---|
19 | from geniusweb.references.ProfileRef import ProfileRef
|
---|
20 |
|
---|
21 |
|
---|
22 | class LearnSettingsTest (unittest.TestCase, GeneralTests[LearnSettings]):
|
---|
23 | jackson = ObjectMapper()
|
---|
24 |
|
---|
25 | serialized = "{\"LearnSettings\":{\"participants\":[{\"TeamInfo\":{\"parties\":[{\"party\":{\"partyref\":\"party1\",\"parameters\":{\"persistentstate\":\"6bb5f909-0079-43ac-a8ac-a31794391074\",\"negotiationdata\":[\"12b5f909-0079-43ac-a8ac-a31794391012\"]}},\"profile\":\"http://prof1\"}]}}],\"deadline\":{\"DeadlineRounds\":{\"rounds\":10,\"durationms\":10000}}}}"
|
---|
26 |
|
---|
27 | def setUp(self):
|
---|
28 | self.params = Parameters()
|
---|
29 | self.params = self.params.With("persistentstate",
|
---|
30 | "6bb5f909-0079-43ac-a8ac-a31794391074")
|
---|
31 | self.params = self.params.With("negotiationdata",
|
---|
32 | ["12b5f909-0079-43ac-a8ac-a31794391012"])
|
---|
33 |
|
---|
34 | # define participants
|
---|
35 | self.participants = self.createTeam(self.params, 1)
|
---|
36 |
|
---|
37 | # participants2
|
---|
38 | # ProfileRef profile2 = new ProfileRef("http://prof2");
|
---|
39 | # PartyWithProfile partywithp2 = new PartyWithProfile(party1, profile2);
|
---|
40 | # List<PartyWithProfile> team2pp = Arrays.asList(partywithp2);
|
---|
41 | # TeamInfo team2 = new TeamInfo(team2pp);
|
---|
42 | self.participants2 = self.createTeam(self.params, 2)
|
---|
43 |
|
---|
44 | self.deadline = DeadlineRounds(10, 10000)
|
---|
45 | self.deadline2 = DeadlineRounds(20, 10000)
|
---|
46 |
|
---|
47 | self.settings1 = LearnSettings(self.participants, self.deadline)
|
---|
48 | self.settings1a = LearnSettings(self.participants, self.deadline)
|
---|
49 | self.settings2 = LearnSettings(self.participants2, self.deadline)
|
---|
50 | self.settings3 = LearnSettings(self.participants, self.deadline2)
|
---|
51 |
|
---|
52 |
|
---|
53 | def createTeam(self, params:Parameters, partynr:int) -> List[TeamInfo] :
|
---|
54 | '''
|
---|
55 | @param params the parameters for the party
|
---|
56 | @param partynr the serialnr for the party, eg 1 or 2.
|
---|
57 | @return a List of TeamInfo with just 1 team.
|
---|
58 | '''
|
---|
59 | party1ref = PartyRef(URI("party" + str(partynr)))
|
---|
60 | party1 = PartyWithParameters(party1ref, params)
|
---|
61 | profile1 = ProfileRef(URI("http://prof" + str(partynr)))
|
---|
62 | partywithp1 = PartyWithProfile(party1, profile1)
|
---|
63 | team1pp = [partywithp1]
|
---|
64 | team1 = TeamInfo(team1pp)
|
---|
65 | return [team1]
|
---|
66 |
|
---|
67 | def getGeneralTestData(self) ->List[List[LearnSettings]]:
|
---|
68 | return [[self.settings1, self.settings1a],[self.settings2], [self.settings3]]
|
---|
69 |
|
---|
70 | def getGeneralTestStrings(self)->List[str]:
|
---|
71 | return [
|
---|
72 | "LearnSettings.TeamInfo.*PartyWithProfile.*PartyRef.*party1.*persistentstate=6b.*, negotiationdata=\\[12.*..*ProfileRef.*prof1.*,DeadlineRounds.*10,10000.*",
|
---|
73 | "LearnSettings.TeamInfo.*PartyWithProfile.*PartyRef.*party2.*persistentstate=6b.*, negotiationdata=\\[12.*..*ProfileRef.*prof2.*,DeadlineRounds.*10,10000.*",
|
---|
74 | "LearnSettings.TeamInfo.*PartyWithProfile.*PartyRef.*party1.*persistentstate=6b.*, negotiationdata=\\[12.*..*ProfileRef.*prof1.*,DeadlineRounds.*20,10000.*"]
|
---|
75 |
|
---|
76 | def testNullParticipants(self):
|
---|
77 | self.assertRaises(ValueError, lambda:LearnSettings(None, self.deadline))
|
---|
78 |
|
---|
79 | def testNullDeadline(self):
|
---|
80 | self.assertRaises(ValueError, lambda:LearnSettings(self.participants, None))
|
---|
81 |
|
---|
82 | def testgetMaxRuntimeTest(self):
|
---|
83 | self.assertEqual(10, self.settings1.getMaxRunTime(), 0.00000001)
|
---|
84 |
|
---|
85 | def testgetProtocolTest(self):
|
---|
86 | # this will throw if not Learn protocol or another problem.
|
---|
87 | protocol = self.settings1.getProtocol(Mock(Reporter))
|
---|
88 |
|
---|
89 |
|
---|
90 | def testbadPersistentTest(self):
|
---|
91 | # hack. #1933.
|
---|
92 | newparams = self.params.With("persistentstate",
|
---|
93 | "\"notproperUUID\"")
|
---|
94 | participants = self.createTeam(newparams, 1)
|
---|
95 |
|
---|
96 | self.assertRaises(ValueError, lambda:LearnSettings(participants, self.deadline))
|
---|
97 |
|
---|
98 | def testpersistentNotStringTest(self):
|
---|
99 | newparams = self.params.With("persistentstate", 32)
|
---|
100 | participants = self.createTeam(newparams, 1)
|
---|
101 |
|
---|
102 | self.assertRaises(ValueError, lambda: LearnSettings(participants, self.deadline))
|
---|
103 |
|
---|
104 | def testpersistentWithoutQuotesTest(self) :
|
---|
105 | newparams = self.params.With("persistentstate",
|
---|
106 | "6bb5f909-0079-43ac-a8ac-a31794391074")
|
---|
107 | participants = self.createTeam(newparams, 1)
|
---|
108 |
|
---|
109 | LearnSettings(participants, self.deadline)
|
---|
110 |
|
---|
111 | def testpersistentExtraQuoteTest(self) :
|
---|
112 | newparams = self.params.With("persistentstate",
|
---|
113 | "\"6bb5f909-0079-43ac-a8ac-a31794391074\"")
|
---|
114 | participants = self.createTeam(newparams, 1)
|
---|
115 |
|
---|
116 | self.assertRaises(ValueError, lambda:LearnSettings(participants, self.deadline))
|
---|
117 |
|
---|
118 | def testnegotiationDataNotList(self):
|
---|
119 | newparams = self.params.With("negotiationdata",
|
---|
120 | "\"12b5f909-0079-43ac-a8ac-a31794391012\"")
|
---|
121 | participants = self.createTeam(newparams, 1)
|
---|
122 | self.assertRaises(ValueError, lambda:LearnSettings(participants, self.deadline))
|
---|
123 |
|
---|
124 | def testnegotiationDataBadContents(self):
|
---|
125 | newparams = self.params.With("negotiationdata", "\"bad\"")
|
---|
126 | participants = self.createTeam(newparams, 1)
|
---|
127 | self.assertRaises(ValueError, lambda: LearnSettings(participants, self.deadline))
|
---|
128 |
|
---|
129 | def testnegotiationDataNotString(self):
|
---|
130 | newparams = self.params.With("negotiationdata", 12)
|
---|
131 | participants = self.createTeam(newparams, 1)
|
---|
132 | self.assertRaises(ValueError, lambda:LearnSettings(participants,self.deadline))
|
---|
133 |
|
---|
134 | def testgetTeamsTest(self):
|
---|
135 | self.assertEqual(self.participants, self.settings1.getTeams())
|
---|
136 | self.assertEqual(1, self.settings1.getTeamSize())
|
---|
137 |
|
---|
138 | def testgetDeadlineTest(self):
|
---|
139 | self.assertEqual(self.deadline, self.settings1.getDeadline())
|
---|
140 |
|
---|
141 | def testwithTeamTest(self):
|
---|
142 | newset = self.settings1.With(self.participants2[0])
|
---|
143 | self.assertEqual(2, len(newset.getTeams()))
|
---|
144 |
|
---|
145 | def testDeserialize(self) :
|
---|
146 | obj = self.jackson.parse(json.loads(self.serialized), SessionSettings)
|
---|
147 | print(obj)
|
---|
148 | self.assertEqual(self.settings1, obj)
|
---|
149 |
|
---|
150 | def testSerialize(self):
|
---|
151 | jsonobj = self.jackson.toJson(self.settings1);
|
---|
152 | print(jsonobj)
|
---|
153 | self.assertEqual(json.loads(self.serialized), jsonobj)
|
---|
154 |
|
---|