[73] | 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.ReportToLogger import ReportToLogger
|
---|
| 8 | from unitpy.GeneralTests import GeneralTests
|
---|
| 9 | from uri.uri import URI
|
---|
| 10 |
|
---|
| 11 | from geniusweb.deadline.Deadline import Deadline
|
---|
| 12 | from geniusweb.deadline.DeadlineRounds import DeadlineRounds
|
---|
| 13 | from geniusweb.deadline.DeadlineTime import DeadlineTime
|
---|
| 14 | from geniusweb.protocol.session.SessionSettings import SessionSettings
|
---|
| 15 | from geniusweb.protocol.session.TeamInfo import TeamInfo
|
---|
| 16 | from geniusweb.protocol.session.mopac.MOPACSettings import MOPACSettings
|
---|
| 17 | from geniusweb.references.Parameters import Parameters
|
---|
| 18 | from geniusweb.references.PartyRef import PartyRef
|
---|
| 19 | from geniusweb.references.PartyWithParameters import PartyWithParameters
|
---|
| 20 | from geniusweb.references.PartyWithProfile import PartyWithProfile
|
---|
| 21 | from geniusweb.references.ProfileRef import ProfileRef
|
---|
| 22 | from geniusweb.voting.votingevaluators.LargestAgreement import LargestAgreement
|
---|
| 23 |
|
---|
| 24 |
|
---|
| 25 | def mockParty(partyname:str, params: Parameters) -> TeamInfo :
|
---|
| 26 | team = Mock(TeamInfo)
|
---|
| 27 | team.getSize = Mock(return_value=1)
|
---|
| 28 | pwithp = Mock(PartyWithParameters)
|
---|
| 29 | team.getParties = Mock(
|
---|
| 30 | return_value=[PartyWithProfile(pwithp, Mock(ProfileRef))])
|
---|
| 31 | team.__repr__=Mock(return_value=partyname) #type:ignore
|
---|
| 32 | pwithp.__repr__=Mock(return_value=partyname) #type:ignore
|
---|
| 33 | pwithp.getParameters = Mock(return_value=params)
|
---|
| 34 | return team;
|
---|
| 35 |
|
---|
| 36 |
|
---|
| 37 | class MOPACSettingsTest (unittest.TestCase, GeneralTests[MOPACSettings]):
|
---|
| 38 |
|
---|
| 39 | jackson = ObjectMapper()
|
---|
| 40 | noparams = Parameters()
|
---|
| 41 | partyprof1 = mockParty("party1", noparams)
|
---|
| 42 | partyprof2 = mockParty("party2", noparams)
|
---|
| 43 | partyprof3 = mockParty("party3", noparams)
|
---|
| 44 | participants2 = [partyprof1,partyprof2]
|
---|
| 45 | participants3 = [partyprof1, partyprof2,partyprof3]
|
---|
| 46 |
|
---|
| 47 | ve = LargestAgreement()
|
---|
| 48 |
|
---|
| 49 |
|
---|
| 50 | serialized = "{\"MOPACSettings\":{\"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}},\"votingevaluator\":{\"LargestAgreement\":{}}}}";
|
---|
| 51 |
|
---|
| 52 |
|
---|
| 53 | def setUp(self):
|
---|
| 54 | self.deadline = Mock(DeadlineTime)
|
---|
| 55 | self.deadline2 = Mock(Deadline)
|
---|
| 56 | self.settings1 = MOPACSettings(self.participants2, self.deadline,self.ve)
|
---|
| 57 | self.settings1a = MOPACSettings(self.participants2, self.deadline, self.ve)
|
---|
| 58 | self.settings2 = MOPACSettings(self.participants3, self.deadline, self.ve)
|
---|
| 59 | self.settings3 = MOPACSettings(self.participants2,self.deadline2,self.ve)
|
---|
| 60 |
|
---|
| 61 |
|
---|
| 62 | self.deadline.getDuration = Mock(return_value=1000)
|
---|
| 63 | self.deadline.__repr__=Mock(return_value="deadline")
|
---|
| 64 | self.deadline2.__repr__=Mock(return_value="deadline2")
|
---|
| 65 |
|
---|
| 66 | # SERIALIZABLE version with REAL objects. Still no workaround for
|
---|
| 67 | # this...
|
---|
| 68 | self.party1 = PartyWithParameters(
|
---|
| 69 | PartyRef(URI("http://party1")), Parameters())
|
---|
| 70 | self.profile1 = ProfileRef(URI("http://profile1"))
|
---|
| 71 | self.party2 = PartyWithParameters(
|
---|
| 72 | PartyRef(URI("http://party2")), Parameters())
|
---|
| 73 | self.profile2 = ProfileRef(URI("http://profile2"))
|
---|
| 74 | self.partywithprof1 = PartyWithProfile(self.party1, self.profile1);
|
---|
| 75 | self.partywithprof2 = PartyWithProfile(self.party2, self.profile2)
|
---|
| 76 | participants = [
|
---|
| 77 | TeamInfo([self.partywithprof1]),TeamInfo([self.partywithprof2])]
|
---|
| 78 |
|
---|
| 79 | deadlinetime = DeadlineTime(100)
|
---|
| 80 | self.sersettings = MOPACSettings(participants, deadlinetime, self.ve)
|
---|
| 81 |
|
---|
| 82 | def getGeneralTestData(self)-> List[List[MOPACSettings]] :
|
---|
| 83 | return [[self.settings1, self.settings1a],
|
---|
| 84 | [self.settings2], [self.settings3]]
|
---|
| 85 |
|
---|
| 86 | def getGeneralTestStrings(self)-> List[str]:
|
---|
| 87 | return [
|
---|
| 88 | "MOPACSettings.*party1.*party2.*deadline.*LargestAgreement.*",
|
---|
| 89 | "MOPACSettings.*party1.*party2.*deadline.*LargestAgreement.*",
|
---|
| 90 | "MOPACSettings.*party1.*party2.*deadline2.*LargestAgreement.*"]
|
---|
| 91 |
|
---|
| 92 | def testgetProtocolTest(self):
|
---|
| 93 | self.assertEqual("MOPAC",
|
---|
| 94 | str(self.settings1.getProtocol(
|
---|
| 95 | ReportToLogger("test")).getRef().getURI()))
|
---|
| 96 |
|
---|
| 97 | def testconstructorNoDeadlineTest(self):
|
---|
| 98 | self.assertRaises(ValueError,
|
---|
| 99 | lambda:MOPACSettings(self.participants2, None, self.ve))
|
---|
| 100 |
|
---|
| 101 | def testMaxRuntime(self):
|
---|
| 102 | self.deadline.getDuration=Mock(return_value=234)
|
---|
| 103 | self.assertEqual(0.234, self.settings1.getMaxRunTime())
|
---|
| 104 |
|
---|
| 105 | def testMaxRuntimeRounds(self):
|
---|
| 106 | deadline = Mock(DeadlineRounds)
|
---|
| 107 | deadline.getDuration=Mock(return_value=12000)
|
---|
| 108 |
|
---|
| 109 | settings = MOPACSettings(self.participants2, deadline, self.ve)
|
---|
| 110 |
|
---|
| 111 | self.assertEqual(12, settings.getMaxRunTime())
|
---|
| 112 |
|
---|
| 113 | def testDeserialize(self):
|
---|
| 114 | obj = self.jackson.parse(json.loads(self.serialized),
|
---|
| 115 | SessionSettings)
|
---|
| 116 | print(obj)
|
---|
| 117 | self.assertEqual(self.sersettings, obj);
|
---|
| 118 |
|
---|
| 119 | def testSerialize(self):
|
---|
| 120 | string = self.jackson.toJson(self.sersettings)
|
---|
| 121 | print(string)
|
---|
| 122 | self.assertEqual(json.loads(self.serialized), string)
|
---|
| 123 |
|
---|
| 124 | def testWith(self):
|
---|
| 125 | saop = MOPACSettings(self.participants2, self.deadline, self.ve)
|
---|
| 126 | saop2 = saop.With(self.partyprof3)
|
---|
| 127 | self.assertEqual(3, len(saop2.getTeams()))
|
---|
| 128 |
|
---|
| 129 | def testGetAllParties(self):
|
---|
| 130 | self.assertEqual([self.partywithprof1, self.partywithprof2],
|
---|
| 131 | self.sersettings.getAllParties())
|
---|