[88] | 1 | import json
|
---|
| 2 | from typing import List, Dict
|
---|
| 3 | import unittest
|
---|
| 4 | from unittest.mock import Mock
|
---|
| 5 |
|
---|
| 6 | from pyson.ObjectMapper import ObjectMapper
|
---|
| 7 | from unitpy.GeneralTests import GeneralTests
|
---|
| 8 |
|
---|
| 9 | from geniusweb.actions.Accept import Accept
|
---|
| 10 | from geniusweb.actions.EndNegotiation import EndNegotiation
|
---|
| 11 | from geniusweb.actions.Offer import Offer
|
---|
| 12 | from geniusweb.actions.PartyId import PartyId
|
---|
| 13 | from geniusweb.actions.Votes import Votes
|
---|
| 14 | from geniusweb.inform.YourTurn import YourTurn
|
---|
| 15 | from geniusweb.issuevalue.Bid import Bid
|
---|
| 16 | from geniusweb.issuevalue.DiscreteValue import DiscreteValue
|
---|
| 17 | from geniusweb.protocol.ProtocolException import ProtocolException
|
---|
| 18 | from geniusweb.protocol.session.mopac.PartyStates import PartyStates
|
---|
| 19 | from geniusweb.protocol.session.mopac.phase.OfferPhase import OfferPhase
|
---|
| 20 | from geniusweb.protocol.session.mopac.phase.Phase import Phase, PHASE_MINTIME
|
---|
| 21 | from geniusweb.protocol.session.mopac.phase.VotingPhase import VotingPhase
|
---|
| 22 | from geniusweb.voting.votingevaluators.LargestAgreement import LargestAgreement
|
---|
| 23 | from geniusweb.voting.votingevaluators.LargestAgreementsLoop import LargestAgreementsLoop
|
---|
| 24 |
|
---|
| 25 |
|
---|
| 26 | class OfferPhaseTest(unittest.TestCase, GeneralTests[OfferPhase]):
|
---|
| 27 | '''
|
---|
| 28 | We also test defaultPhase here.
|
---|
| 29 | '''
|
---|
| 30 | maxDiff=None
|
---|
| 31 |
|
---|
| 32 | jackson = ObjectMapper()
|
---|
| 33 |
|
---|
| 34 | DEADLINE = 10
|
---|
| 35 | party1 = PartyId("party1")
|
---|
| 36 | party2 = PartyId("party2")
|
---|
| 37 | party3 = PartyId("party3")
|
---|
| 38 |
|
---|
| 39 | powers:Dict[PartyId, int] = {}
|
---|
| 40 | evaluator = LargestAgreement()
|
---|
| 41 | evaluator2 = LargestAgreementsLoop()
|
---|
| 42 |
|
---|
| 43 | # just a test bid
|
---|
| 44 | bid = Bid({"issue": DiscreteValue("yes")})
|
---|
| 45 | offer = Offer(party1, bid)
|
---|
| 46 |
|
---|
| 47 | serialized = "{\"OfferPhase\":{\"partyStates\":{\"powers\":{\"party2\":3,\"party1\":2,\"party3\":3},\"notYetActed\":[\"party2\",\"party1\",\"party3\"],\"actions\":[],\"agreements\":{},\"walkedAway\":[],\"exceptions\":{}},\"deadline\":10,\"evaluator\":{\"LargestAgreement\":{}}}}"
|
---|
| 48 |
|
---|
| 49 | powers[party1]= 2
|
---|
| 50 | powers[party2]= 3
|
---|
| 51 | states2 = PartyStates(powers)
|
---|
| 52 | powers[party3]= 3
|
---|
| 53 | states = PartyStates(powers);
|
---|
| 54 | phase = OfferPhase(states, DEADLINE, evaluator)
|
---|
| 55 |
|
---|
| 56 | phase1 = OfferPhase(states, 10, evaluator)
|
---|
| 57 | phase1a =OfferPhase(states, 10, evaluator)
|
---|
| 58 | phase2 = OfferPhase(states2, 10, evaluator)
|
---|
| 59 | phase3 = OfferPhase(states, 20, evaluator)
|
---|
| 60 | phase4 = OfferPhase(states, 10, evaluator2)
|
---|
| 61 |
|
---|
| 62 | def getGeneralTestData(self)->List[List[OfferPhase]] :
|
---|
| 63 | return [[self.phase1, self.phase1a],
|
---|
| 64 | [self.phase2], [self.phase3],
|
---|
| 65 | [self.phase4]]
|
---|
| 66 |
|
---|
| 67 | def getGeneralTestStrings(self)->List[str] :
|
---|
| 68 | return [
|
---|
| 69 | "OfferPhase.*PartyStates.*party., party., party..*\\[\\],Agreements.*\\[\\],\\{\\}.*],10,LargestAgreement.*",
|
---|
| 70 | "OfferPhase.*PartyStates.*party., party..*\\[\\],Agreements.*\\[\\],\\{\\}.*],10,LargestAgreement.*",
|
---|
| 71 | "OfferPhase.*PartyStates.*party., party., party.*\\[\\],Agreements.*\\[\\],\\{\\}.*],20,LargestAgreement.*",
|
---|
| 72 | "OfferPhase.*PartyStates.*party., party., party..*\\[\\],Agreements.*\\[\\],\\{\\}.*],10,LargestAgreementsLoop.*"]
|
---|
| 73 |
|
---|
| 74 | def testsmokeTest(self) :
|
---|
| 75 | pass
|
---|
| 76 |
|
---|
| 77 | def testInitState(self):
|
---|
| 78 | self.assertEqual(3, len(self.phase.getPartyStates().getNotYetActed()))
|
---|
| 79 |
|
---|
| 80 | def testInform(self):
|
---|
| 81 | self.assertEqual(YourTurn(), self.phase.getInform())
|
---|
| 82 |
|
---|
| 83 | def testisFinalTest(self):
|
---|
| 84 | self.assertFalse(self.phase.isFinal(1))
|
---|
| 85 | self.assertTrue(self.phase.isFinal(10))
|
---|
| 86 |
|
---|
| 87 | def testisFinalTestActorNotYetActed(self):
|
---|
| 88 | actedstate = Mock(PartyStates)
|
---|
| 89 | actedstate.getNotYetActed=Mock(return_value=set([self.party1]))
|
---|
| 90 | testphase = OfferPhase(actedstate, 10, self.evaluator)
|
---|
| 91 | self.assertFalse(testphase.isFinal(1))
|
---|
| 92 |
|
---|
| 93 | def testisFinalTestAllActorsActed(self):
|
---|
| 94 | actedstate = Mock(PartyStates)
|
---|
| 95 | actedstate.getNotYetActed=Mock(return_value=set())
|
---|
| 96 | testphase = OfferPhase(actedstate, 10, self.evaluator)
|
---|
| 97 | self.assertTrue(testphase.isFinal(1))
|
---|
| 98 |
|
---|
| 99 | def testcheckIncorrectActorTest(self):
|
---|
| 100 | self.assertRaises(ProtocolException, lambda:
|
---|
| 101 | self.phase._checkAction(self.party1, EndNegotiation(self.party2), 1))
|
---|
| 102 |
|
---|
| 103 | def testcheckNotAllowedActionTest(self):
|
---|
| 104 | self.assertRaises(ProtocolException, lambda:
|
---|
| 105 | self.phase._checkAction(self.party1, Votes(self.party2,set()), 1))
|
---|
| 106 |
|
---|
| 107 | def testException(self):
|
---|
| 108 | newphase = self.phase.WithException(ProtocolException("bla", self.party1))
|
---|
| 109 | self.assertEqual(10, newphase.getDeadline())
|
---|
| 110 | self.assertEqual(0, len(self.phase.getPartyStates().getActions()))
|
---|
| 111 |
|
---|
| 112 | def testFinish(self):
|
---|
| 113 | ph = self.phase.finish()
|
---|
| 114 | self.assertEqual(0, len(ph.getPartyStates().getNotYetActed()))
|
---|
| 115 | self.assertEquals(3, len(ph.getPartyStates().getExceptions()))
|
---|
| 116 | self.assertEqual(0, len(self.phase.getPartyStates().getAgreements().getMap()))
|
---|
| 117 |
|
---|
| 118 | def testNextWrong(self):
|
---|
| 119 | # state is not final, should not work
|
---|
| 120 | self.assertRaises(ValueError, lambda:self.phase.next(1, 1000))
|
---|
| 121 |
|
---|
| 122 | def testNextNoParties(self):
|
---|
| 123 | ph = self.phase.finish()
|
---|
| 124 | # no parties are left now, all failed
|
---|
| 125 | # but this is not part of the next() check.
|
---|
| 126 | next = ph.next(1, 1000)
|
---|
| 127 | self.assertTrue(isinstance(next, VotingPhase))
|
---|
| 128 | # no remaining parties, since finish kicked all
|
---|
| 129 | self.assertEqual(0, len(next.getPartyStates().getNotYetActed()))
|
---|
| 130 |
|
---|
| 131 | def testAllowed(self):
|
---|
| 132 | self.phase.With(self.party1, self.offer, 1)
|
---|
| 133 |
|
---|
| 134 | def testAllowedWrongClass(self):
|
---|
| 135 | try:
|
---|
| 136 | self.phase._checkAction(self.party1, Accept(self.party1, self.bid), 1)
|
---|
| 137 | except ProtocolException as e:
|
---|
| 138 | self.assertTrue("not allowed in OfferPhase" in e.args[0])
|
---|
| 139 | return
|
---|
| 140 | self.fail("checkAction did not throw as expected")
|
---|
| 141 |
|
---|
| 142 | def testAllowedWrongActor(self):
|
---|
| 143 | try:
|
---|
| 144 | self.phase._checkAction(self.party1, Accept(self.party2, self.bid), 1)
|
---|
| 145 | except ProtocolException as e:
|
---|
| 146 | self.assertTrue("Incorrect actor info in action" in e.args[0])
|
---|
| 147 | return
|
---|
| 148 | self.fail("checkAction did not throw as expected")
|
---|
| 149 |
|
---|
| 150 | def testAllowedActorAlreadyActed(self):
|
---|
| 151 | testphase = self.phase.With(self.party1, self.offer, 1)
|
---|
| 152 | try:
|
---|
| 153 | testphase._checkAction(self.party1, self.offer, 2)
|
---|
| 154 | except ProtocolException as e:
|
---|
| 155 | self.assertTrue("can not act anymore" in e.args[0])
|
---|
| 156 | return
|
---|
| 157 | self.fail("checkAction did not throw as expected")
|
---|
| 158 |
|
---|
| 159 | def testAllowedActorAlreadyActed1(self):
|
---|
| 160 | # as theoprevious test, but using with() instead of checkAction
|
---|
| 161 | testphase = self.phase.With(self.party1, self.offer, 1)
|
---|
| 162 |
|
---|
| 163 | newphase = testphase.With(self.party1, self.offer, 2)
|
---|
| 164 | # the party must remain as acted, because we can't retract his
|
---|
| 165 | # action...
|
---|
| 166 | self.assertEqual(1, len(newphase.getPartyStates().getActions()))
|
---|
| 167 | self.assertEqual(self.offer, newphase.getPartyStates().getActions()[0])
|
---|
| 168 | self.assertFalse(self.party1 in
|
---|
| 169 | newphase.getPartyStates().getExceptions())
|
---|
| 170 |
|
---|
| 171 | def testAllowedActingTooLate(self):
|
---|
| 172 | try:
|
---|
| 173 | self.phase._checkAction(self.party1, self.offer, self.DEADLINE + 1)
|
---|
| 174 | except ProtocolException as e:
|
---|
| 175 | self.assertTrue("passed deadline" in e.args[0])
|
---|
| 176 | return
|
---|
| 177 | self.fail("checkAction did not throw as expected")
|
---|
| 178 |
|
---|
| 179 | def testgetOffersTest(self) :
|
---|
| 180 | self.assertEquals([], self.phase._getOffers())
|
---|
| 181 | offs = self.phase.With(self.party1, self.offer, 1)._getOffers()
|
---|
| 182 | self.assertEqual(1, len(offs))
|
---|
| 183 | self.assertEquals(self.bid, offs[0].getBid())
|
---|
| 184 |
|
---|
| 185 | def testNextTooShortDuration(self):
|
---|
| 186 | self.assertRaises(ValueError, lambda:self.phase.next(1, PHASE_MINTIME - 1))
|
---|
| 187 |
|
---|
| 188 | def testNextNotFinished(self):
|
---|
| 189 | self.assertRaises(ValueError, lambda:self.phase.next(1, PHASE_MINTIME + 1))
|
---|
| 190 |
|
---|
| 191 | def testNext(self):
|
---|
| 192 | self.assertRaises(ValueError, lambda:self.phase.next(11, PHASE_MINTIME + 1))
|
---|
| 193 |
|
---|
| 194 | def testDeserialize(self):
|
---|
| 195 | obj = self.jackson.parse(json.loads(self.serialized), Phase)
|
---|
| 196 | print(obj)
|
---|
| 197 | self.assertEqual(self.phase1, obj)
|
---|
| 198 |
|
---|
| 199 | def testSerialize(self):
|
---|
| 200 | jsonobj = self.jackson.toJson(self.phase1)
|
---|
| 201 | print(jsonobj);
|
---|
| 202 | #FIXME how can we test this. The order of the notYetActed set is randomizing
|
---|
| 203 | #self.assertEqual(json.loads(self.serialized), jsonobj)
|
---|