source: geniuswebcore/test/geniusweb/protocol/session/mopac/phase/OptInPhaseTest.py

Last change on this file was 100, checked in by ruud, 21 months ago

python installs also wheel to avoid error messages

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