[88] | 1 | import json
|
---|
| 2 | from typing import List
|
---|
| 3 | from unittest import mock
|
---|
| 4 | import unittest
|
---|
| 5 | from unittest.mock import Mock
|
---|
| 6 |
|
---|
| 7 | from pyson.ObjectMapper import ObjectMapper
|
---|
| 8 | from tudelft_utilities_logging.ReportToLogger import ReportToLogger
|
---|
| 9 | from unitpy.GeneralTests import GeneralTests
|
---|
| 10 | from uri.uri import URI
|
---|
| 11 |
|
---|
| 12 | from geniusweb.deadline.Deadline import Deadline
|
---|
| 13 | from geniusweb.deadline.DeadlineRounds import DeadlineRounds
|
---|
| 14 | from geniusweb.deadline.DeadlineTime import DeadlineTime
|
---|
| 15 | from geniusweb.protocol.NegoSettings import NegoSettings
|
---|
| 16 | from geniusweb.protocol.session.SessionSettings import SessionSettings
|
---|
| 17 | from geniusweb.protocol.session.TeamInfo import TeamInfo
|
---|
| 18 | from geniusweb.protocol.session.saop.SAOPSettings import SAOPSettings
|
---|
| 19 | from geniusweb.references.Parameters import Parameters
|
---|
| 20 | from geniusweb.references.PartyRef import PartyRef
|
---|
| 21 | from geniusweb.references.PartyWithParameters import PartyWithParameters
|
---|
| 22 | from geniusweb.references.PartyWithProfile import PartyWithProfile
|
---|
| 23 | from geniusweb.references.ProfileRef import ProfileRef
|
---|
| 24 |
|
---|
| 25 |
|
---|
| 26 | class SAOPSettingsTest (unittest.TestCase, GeneralTests[SAOPSettings]):
|
---|
| 27 |
|
---|
| 28 | partyprof1 = Mock(TeamInfo)
|
---|
| 29 | partyprof2 = Mock(TeamInfo)
|
---|
| 30 | partyprof3 = Mock(TeamInfo)
|
---|
| 31 | participants2:List[TeamInfo] = [partyprof1,partyprof2]
|
---|
| 32 | participants3:List[TeamInfo] = [partyprof1, partyprof2, partyprof3]
|
---|
| 33 |
|
---|
| 34 | deadline = Mock(DeadlineTime)
|
---|
| 35 | deadline2 = Mock(Deadline)
|
---|
| 36 | settings1 = SAOPSettings(participants2, deadline)
|
---|
| 37 | settings1a = SAOPSettings(participants2, deadline)
|
---|
| 38 | settings2 = SAOPSettings(participants3, deadline)
|
---|
| 39 | settings3 = SAOPSettings(participants2, deadline2)
|
---|
| 40 | jackson = ObjectMapper()
|
---|
| 41 |
|
---|
| 42 | serialized = "{\"SAOPSettings\":{\"participants\":[{\"TeamInfo\":{\"parties\":[{\"party\":{\"partyref\":\"http://party1\",\"parameters\":{}},\"profile\":\"http://profile1\"}]}},{\"TeamInfo\":{\"parties\":[{\"party\":{\"partyref\":\"http://party2\",\"parameters\":{}},\"profile\":\"http://profile2\"}]}}],\"deadline\":{\"DeadlineTime\":{\"durationms\":100}}}}";
|
---|
| 43 |
|
---|
| 44 | deadline.getDuration=Mock(return_value=1000)
|
---|
| 45 | #work around mypy bug
|
---|
| 46 | deadline.__repr__=Mock(return_value="deadline") #type:ignore
|
---|
| 47 | deadline2.__repr__=Mock(return_value="deadline2") #type:ignore
|
---|
| 48 |
|
---|
| 49 | partyprof1.getSize=Mock(return_value=1)
|
---|
| 50 | partyprof2.getSize=Mock(return_value=1)
|
---|
| 51 | partyprof3.getSize=Mock(return_value=1)
|
---|
| 52 |
|
---|
| 53 | partyprof1.__repr__=Mock(return_value="party and profile 1") #type:ignore
|
---|
| 54 | partyprof2.__repr__=Mock(return_value="party and profile 2") #type:ignore
|
---|
| 55 | partyprof3.__repr__=Mock(return_value="party and profile 3") #type:ignore
|
---|
| 56 |
|
---|
| 57 | # SERIALIZABLE version with REAL objects. Still no workaround for
|
---|
| 58 | # this...
|
---|
| 59 | party1 = PartyWithParameters(PartyRef(URI("http://party1")), Parameters())
|
---|
| 60 | profile1 = ProfileRef(URI("http://profile1"))
|
---|
| 61 | party2 = PartyWithParameters(PartyRef(URI("http://party2")), Parameters())
|
---|
| 62 | profile2 = ProfileRef(URI("http://profile2"))
|
---|
| 63 | partywithprof1 = TeamInfo([PartyWithProfile(party1, profile1)])
|
---|
| 64 | partywithprof2 = TeamInfo([PartyWithProfile(party2, profile2)])
|
---|
| 65 | participants = [partywithprof1, partywithprof2]
|
---|
| 66 |
|
---|
| 67 | deadlinetime = DeadlineTime(100)
|
---|
| 68 | sersettings = SAOPSettings(participants, deadlinetime)
|
---|
| 69 |
|
---|
| 70 |
|
---|
| 71 | def getGeneralTestData(self) -> List[List[SAOPSettings]] :
|
---|
| 72 | return [[self.settings1, self.settings1a],
|
---|
| 73 | [self.settings2], [self.settings3]]
|
---|
| 74 |
|
---|
| 75 | def getGeneralTestStrings(self)-> List[str]:
|
---|
| 76 | return [
|
---|
| 77 | "SAOPSettings.party and profile 1, party and profile 2.,deadline.",
|
---|
| 78 | "SAOPSettings.party and profile 1, party and profile 2, party and profile 3.,deadline.",
|
---|
| 79 | "SAOPSettings.party and profile 1, party and profile 2.,deadline2."]
|
---|
| 80 |
|
---|
| 81 | def testGetProtocol(self):
|
---|
| 82 | self.assertEqual("SAOP", str(self.settings1.getProtocol(ReportToLogger("test"))
|
---|
| 83 | .getRef().getURI()))
|
---|
| 84 |
|
---|
| 85 | def testConstructorNoDeadline(self):
|
---|
| 86 | self.assertRaises(ValueError, lambda:SAOPSettings(self.participants2, None))
|
---|
| 87 |
|
---|
| 88 | def testMaxRuntime(self):
|
---|
| 89 | self.deadline.getDuration=Mock(return_value=234000)
|
---|
| 90 | self.assertEqual(234, self.settings1.getMaxRunTime())
|
---|
| 91 |
|
---|
| 92 | def testMaxRuntimeRounds(self):
|
---|
| 93 | deadline = Mock(DeadlineRounds)
|
---|
| 94 | deadline.getDuration=Mock(return_value=12000)
|
---|
| 95 |
|
---|
| 96 | settings = SAOPSettings(self.participants2, deadline)
|
---|
| 97 |
|
---|
| 98 | self.assertEqual(12, settings.getMaxRunTime())
|
---|
| 99 |
|
---|
| 100 | def testDeserialize(self) :
|
---|
| 101 | obj = self.jackson.parse(json.loads(self.serialized),SessionSettings);
|
---|
| 102 | print(obj)
|
---|
| 103 | self.assertEqual(self.sersettings, obj)
|
---|
| 104 |
|
---|
| 105 | def testDeserializeAsNego(self) :
|
---|
| 106 | obj = self.jackson.parse(json.loads(self.serialized),NegoSettings);
|
---|
| 107 | print(obj)
|
---|
| 108 | self.assertEqual(self.sersettings, obj)
|
---|
| 109 |
|
---|
| 110 |
|
---|
| 111 | def testSerialize(self):
|
---|
| 112 | jsonobj = self.jackson.toJson(self.sersettings)
|
---|
| 113 | print(jsonobj)
|
---|
| 114 | self.assertEqual(json.loads(self.serialized), jsonobj)
|
---|
| 115 |
|
---|
| 116 | def testWith(self):
|
---|
| 117 | saop = SAOPSettings(self.participants2, self.deadline)
|
---|
| 118 | saop2 = saop.With(self.partyprof3);
|
---|
| 119 | self.assertEqual(3, len(saop2.getTeams()))
|
---|