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 unitpy.GeneralTests import GeneralTests
|
---|
9 |
|
---|
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.Vote import Vote
|
---|
14 | from geniusweb.actions.Votes import Votes
|
---|
15 | from geniusweb.inform.Voting import Voting
|
---|
16 | from geniusweb.issuevalue.Bid import Bid
|
---|
17 | from geniusweb.issuevalue.DiscreteValue import DiscreteValue
|
---|
18 | from geniusweb.protocol.ProtocolException import ProtocolException
|
---|
19 | from geniusweb.protocol.session.mopac.PartyStates import PartyStates
|
---|
20 | from geniusweb.protocol.session.mopac.phase.OfferPhase import OfferPhase
|
---|
21 | from geniusweb.protocol.session.mopac.phase.OptInPhase import OptInPhase
|
---|
22 | from geniusweb.protocol.session.mopac.phase.Phase import PHASE_MINTIME, Phase
|
---|
23 | from geniusweb.protocol.session.mopac.phase.VotingPhase import VotingPhase
|
---|
24 | from geniusweb.voting.votingevaluators.LargestAgreement import LargestAgreement
|
---|
25 | from geniusweb.voting.votingevaluators.LargestAgreementsLoop import LargestAgreementsLoop
|
---|
26 |
|
---|
27 |
|
---|
28 | class VotingPhaseTest (unittest.TestCase, GeneralTests[VotingPhase]):
|
---|
29 | jackson = ObjectMapper()
|
---|
30 | '''
|
---|
31 | We also test defaultPhase here.
|
---|
32 | '''
|
---|
33 | DEADLINE = 10
|
---|
34 | party1 = PartyId("party1")
|
---|
35 | party2 = PartyId("party2")
|
---|
36 | party3 = PartyId("party3")
|
---|
37 |
|
---|
38 | # private final OfferPhase prevPhase = mock(OfferPhase.class);
|
---|
39 |
|
---|
40 | evaluator = LargestAgreement()
|
---|
41 | evaluator2 = LargestAgreementsLoop()
|
---|
42 |
|
---|
43 | # just a test bid
|
---|
44 | bid = Bid({"issue": DiscreteValue("yes")})
|
---|
45 | vote = Vote(party1, bid, 2, 3);
|
---|
46 | votes = Votes(party1, set([vote]))
|
---|
47 |
|
---|
48 | prevPhase = [ Offer(party1, bid)]
|
---|
49 |
|
---|
50 | serialized = "{\"VotingPhase\":{\"offers\":[],\"partyStates\":{\"powers\":{\"party2\":3,\"party1\":2,\"party3\":3},\"notYetActed\":[\"party2\",\"party1\",\"party3\"],\"actions\":[],\"agreements\":{},\"walkedAway\":[],\"exceptions\":{}},\"deadline\":10,\"evaluator\":{\"LargestAgreement\":{}}}}"
|
---|
51 |
|
---|
52 | def setUp(self):
|
---|
53 | self.powers:Dict[PartyId, int] = {}
|
---|
54 | self.powers[self.party1]=2
|
---|
55 | self.powers[self.party2]= 3
|
---|
56 | self.states2 = PartyStates(self.powers)
|
---|
57 | self.powers[self.party3]= 3
|
---|
58 | self.states = PartyStates(self.powers)
|
---|
59 |
|
---|
60 | self.phase = VotingPhase(self.prevPhase, self.states, self.DEADLINE, self.evaluator)
|
---|
61 |
|
---|
62 | # in these phases we just don't set prevPhase.
|
---|
63 | # this to avoid serialization troubles.
|
---|
64 | self.phase1 = VotingPhase([], self.states, 10, self.evaluator);
|
---|
65 | self.phase1a = VotingPhase([], self.states, 10, self.evaluator);
|
---|
66 | self.phase2 = VotingPhase([], self.states2, 10, self.evaluator);
|
---|
67 | self.phase3 = VotingPhase([], self.states, 20, self.evaluator);
|
---|
68 | self.phase4 = VotingPhase([], self.states, 10, self.evaluator2);
|
---|
69 |
|
---|
70 |
|
---|
71 | def getGeneralTestData(self)-> List[List[VotingPhase]]:
|
---|
72 | return [[self.phase1, self.phase1a],
|
---|
73 | [self.phase2], [self.phase3],
|
---|
74 | [self.phase4]]
|
---|
75 |
|
---|
76 | def getGeneralTestStrings(self)->List[str] :
|
---|
77 | return [
|
---|
78 | "VotingPhase.*PartyStates.*party., party., party.*\\[\\],Agreements.*\\[\\],\\{\\}.*],10,LargestAgreement.*",
|
---|
79 | "VotingPhase.*PartyStates.*party., party.*\\[\\],Agreements.*\\[\\],\\{\\}.*],10,LargestAgreement.*",
|
---|
80 | "VotingPhase.*PartyStates.*party., party., party.*\\[\\],Agreements.*\\[\\],\\{\\}.*],20,LargestAgreement.*",
|
---|
81 | "VotingPhase.*PartyStates.*party., party., party.*\\[\\],Agreements.*\\[\\],\\{\\}.*],10,LargestAgreementsLoop.*"]
|
---|
82 |
|
---|
83 | def testsmokeTest(self):
|
---|
84 | pass
|
---|
85 |
|
---|
86 | def testInitState(self):
|
---|
87 | self.assertEqual(3, len(self.phase.getPartyStates().getNotYetActed()))
|
---|
88 |
|
---|
89 | def testInform(self) :
|
---|
90 | # when(prevPhase.getOffers()).thenReturn(Arrays.asList(bid));
|
---|
91 | self.assertEqual(Voting([Offer(self.party1, self.bid)], self.powers),
|
---|
92 | self.phase.getInform())
|
---|
93 |
|
---|
94 | def testisFinalTest(self):
|
---|
95 | self.assertFalse(self.phase.isFinal(1))
|
---|
96 | self.assertTrue(self.phase.isFinal(10))
|
---|
97 |
|
---|
98 | def testisFinalTestActorNotYetActed(self):
|
---|
99 | actedstate = Mock(PartyStates)
|
---|
100 | actedstate.getNotYetActed=Mock(return_value=set([self.party1]))
|
---|
101 | testphase = OfferPhase(actedstate, 10, self.evaluator)
|
---|
102 | self.assertFalse(testphase.isFinal(1))
|
---|
103 |
|
---|
104 | def testisFinalTestAllActorsActed(self):
|
---|
105 | actedstate = Mock(PartyStates)
|
---|
106 | actedstate.getNotYetActed=Mock(return_value=set())
|
---|
107 | testphase = OfferPhase(actedstate, 10, self.evaluator)
|
---|
108 | self.assertTrue(testphase.isFinal(1))
|
---|
109 |
|
---|
110 | def testcheckIncorrectActorTest(self) :
|
---|
111 | self.assertRaises(ProtocolException,
|
---|
112 | lambda:self.phase._checkAction(self.party1,
|
---|
113 | EndNegotiation(self.party2), 1))
|
---|
114 |
|
---|
115 | def testcheckNotAllowedActionTest(self) :
|
---|
116 | self.assertRaises(ProtocolException,
|
---|
117 | lambda:self.phase._checkAction(self.party1, Votes(self.party2, set()), 1))
|
---|
118 |
|
---|
119 | def testFinish(self):
|
---|
120 | ph = self.phase.finish()
|
---|
121 | self.assertEqual(0, len(ph.getPartyStates().getNotYetActed()))
|
---|
122 | self.assertEquals(3, len(ph.getPartyStates().getExceptions()))
|
---|
123 | self.assertEqual(0, len(self.phase.getPartyStates().getAgreements().getMap()))
|
---|
124 |
|
---|
125 | def testNextWrong(self):
|
---|
126 | # state is not final, should not work
|
---|
127 | self.assertRaises(ValueError, lambda:self.phase.next(1, 1000))
|
---|
128 |
|
---|
129 | def testNextNoParties(self):
|
---|
130 | ph = self.phase.finish()
|
---|
131 | # no parties are left now, all failed
|
---|
132 | # but this is not part of the next() check.
|
---|
133 | next = ph.next(1, 1000)
|
---|
134 | self.assertTrue(isinstance(next, OptInPhase))
|
---|
135 | # no remaining parties, since finish kicked all
|
---|
136 | self.assertEqual(0, len(next.getPartyStates().getNotYetActed()))
|
---|
137 |
|
---|
138 | def testAllowed(self):
|
---|
139 | self.phase.With(self.party1, self.votes, 1)
|
---|
140 |
|
---|
141 | def testgetVotesTest(self):
|
---|
142 | self.assertEqual([], self.phase.getVotes())
|
---|
143 | vts = self.phase.With(self.party1, self.votes, 1).getVotes()
|
---|
144 | self.assertEquals([self.votes], vts)
|
---|
145 |
|
---|
146 | def testNextTooShortDuration(self):
|
---|
147 | self.assertRaises(ValueError,
|
---|
148 | lambda:self.phase.next(1, PHASE_MINTIME - 1))
|
---|
149 |
|
---|
150 | def testNextNotFinished(self):
|
---|
151 | self.assertRaises(ValueError, lambda:self.phase.next(1, PHASE_MINTIME + 1))
|
---|
152 |
|
---|
153 | def testNext(self):
|
---|
154 | self.assertRaises(ValueError, lambda:self.phase.next(11, PHASE_MINTIME + 1))
|
---|
155 |
|
---|
156 |
|
---|
157 | def testDeserialize(self):
|
---|
158 | obj = self.jackson.parse(json.loads(self.serialized), Phase)
|
---|
159 | print(obj)
|
---|
160 | self.assertEqual(self.phase1, obj)
|
---|
161 |
|
---|
162 | def testSerialize(self):
|
---|
163 | jsonobj = self.jackson.toJson(self.phase1)
|
---|
164 | print(jsonobj);
|
---|
165 | #FIXME how can we test this. The order of the notYetActed set is randomizing
|
---|
166 | #self.assertEqual(json.loads(self.serialized), jsonobj)
|
---|