source: geniuswebcore/test/geniusweb/voting/CollectedVotesWithValueTest.py

Last change on this file was 111, checked in by ruud, 2 months ago

Modifications for automatic java to python conversion. Overloaded methods now have different names.

File size: 12.0 KB
Line 
1# Generated from java by J2P
2from __future__ import annotations
3from geniusweb.actions.PartyId import PartyId
4from geniusweb.actions.VoteWithValue import VoteWithValue
5from geniusweb.actions.VotesWithValue import VotesWithValue
6from geniusweb.issuevalue.Bid import Bid
7from geniusweb.voting.CollectedVotesWithValue import CollectedVotesWithValue
8from mockito import mock
9from tudelft.utilities.immutablelist.Tuple import Tuple
10from tudelft.utilities.tools.dictkeys import Keys
11from typing import Dict
12from typing import List
13from typing import Optional
14from typing import Set
15from unitpy.Before import Before
16from unitpy.GeneralTests import GeneralTests
17from unitpy.RunWith import RunWith
18from unitpy.Runner import JUnit4ClassRunner
19from unitpy.Runner import runTests
20from unitpy.Test import Test
21from unittest import TestCase
22
23
24@RunWith(JUnit4ClassRunner)
25class CollectedVotesWithValueTest(GeneralTests):
26
27 #Override
28 def getGeneralTestData(self) -> Optional[List[Optional[List[Optional[CollectedVotesWithValue]]]]]:
29 return [[self.__cv1,self.__cv1a],(lambda _: _ if type(_)==list else [_])(self.__cv2)]
30
31 #Override
32 def getGeneralTestStrings(self) -> Optional[List[Optional[str]]]:
33 return ["CollectedVotesWithValue.\\{\\}.*\\{\\}.*","CollectedVotesWithValue.\\{partyP.*VotesWithValue.*partyP,.VoteWithValue.partyP.*Bid.*,2.*\\}.*\\{partyP.*1\\}.*"]
34
35 @Before()
36 def before(self) -> None:
37 self.__allpowers[self.__partyP]=1
38 self.__allpowers[self.__partyQ]=1
39 self.__allpowers[self.__partyR]=1
40 self.__allpowers[self.__partyS]=1
41 pass
42 # don't know how to translate this. NOT when(bidB).__repr__().thenReturn("bidB")
43
44 @Test()
45 def testGetAllBids(self) -> None:
46 allvotes:Dict[PartyId,VotesWithValue] = dict()
47 allvotes[self.__partyP]=VotesWithValue(self.__partyP,set([self.__votePA2]))
48 allvotes[self.__partyQ]=VotesWithValue(self.__partyQ,set([self.__voteQA2,self.__voteQB3]))
49 allbids:Dict[Bid,Set[VoteWithValue]] = CollectedVotesWithValue(allvotes,self.__allpowers).getAllBids()
50 TestCase().assertEqual(2,len(allbids))
51 TestCase().assertEqual(set([self.__votePA2,self.__voteQA2]),allbids.get(self.__bidA))
52 TestCase().assertEqual(set((lambda _: _ if type(_)==list else [_])(self.__voteQB3)),allbids.get(self.__bidB))
53
54 @Test()
55 def getBestCoalitionTest(self) -> None:
56 # Only deal is A, 150%
57 allvotes:Dict[PartyId,VotesWithValue] = dict()
58 allvotes[self.__partyP]=VotesWithValue(self.__partyP,set([self.__votePA2]))
59 allvotes[self.__partyQ]=VotesWithValue(self.__partyQ,set([self.__voteQA2,self.__voteQB3]))
60 cv:CollectedVotesWithValue = CollectedVotesWithValue(allvotes,self.__allpowers)
61 best:Optional[Tuple[Bid,int]] = cv._getBestCoalition(set([self.__partyP,self.__partyQ]))
62 TestCase().assertIsNotNone(best)
63 TestCase().assertEqual(self.__bidA,best.get1())
64 TestCase().assertEqual(150,best.get2())
65
66 @Test()
67 def getBestCoalitionTest2(self) -> None:
68 # Only deal is B, 110%
69 allvotes:Dict[PartyId,VotesWithValue] = dict()
70 allvotes[self.__partyP]=VotesWithValue(self.__partyP,set([VoteWithValue(self.__partyP,self.__bidA,2,9,40),VoteWithValue(self.__partyP,self.__bidB,2,9,60)]))
71 allvotes[self.__partyQ]=VotesWithValue(self.__partyQ,set([VoteWithValue(self.__partyQ,self.__bidA,2,9,50),VoteWithValue(self.__partyQ,self.__bidB,2,9,50)]))
72 cv:CollectedVotesWithValue = CollectedVotesWithValue(allvotes,self.__allpowers)
73 best:Tuple[Bid,int] = cv._getBestCoalition(set([self.__partyP,self.__partyQ]))
74 TestCase().assertEqual(self.__bidB,best.get1())
75 TestCase().assertEqual(110,best.get2())
76
77 @Test()
78 def getBestCoalitionTest3(self) -> None:
79 # Both deals A and B are now equivalent so both A and B are good
80 allvotes:Dict[PartyId,VotesWithValue] = dict()
81 allvotes[self.__partyP]=VotesWithValue(self.__partyP,set([VoteWithValue(self.__partyP,self.__bidA,2,9,50),VoteWithValue(self.__partyP,self.__bidB,2,9,50)]))
82 allvotes[self.__partyQ]=VotesWithValue(self.__partyQ,set([VoteWithValue(self.__partyQ,self.__bidA,2,9,50),VoteWithValue(self.__partyQ,self.__bidB,2,9,50)]))
83 cv:CollectedVotesWithValue = CollectedVotesWithValue(allvotes,self.__allpowers)
84 best:Tuple[Bid,int] = cv._getBestCoalition(set([self.__partyP,self.__partyQ]))
85 TestCase().assertEqual(100,best.get2())
86
87 @Test()
88 def getBestCoalitionTest4(self) -> None:
89 # therefore there is no 3-team coalition.
90 allvotes:Dict[PartyId,VotesWithValue] = dict()
91 allvotes[self.__partyP]=VotesWithValue(self.__partyP,set([VoteWithValue(self.__partyP,self.__bidA,2,2,60),VoteWithValue(self.__partyP,self.__bidB,2,2,40)]))
92 allvotes[self.__partyQ]=VotesWithValue(self.__partyQ,set([VoteWithValue(self.__partyQ,self.__bidA,2,9,50),VoteWithValue(self.__partyQ,self.__bidB,2,9,50)]))
93 allvotes[self.__partyR]=VotesWithValue(self.__partyR,set([VoteWithValue(self.__partyR,self.__bidA,2,9,70),VoteWithValue(self.__partyR,self.__bidB,2,9,30)]))
94 cv:CollectedVotesWithValue = CollectedVotesWithValue(allvotes,self.__allpowers)
95 best:Tuple[Bid,int] = cv._getBestCoalition(set([self.__partyP,self.__partyQ,self.__partyR]))
96 TestCase().assertIsNone(best)
97
98 @Test()
99 def getActiveParties(self) -> None:
100 allvotes:Dict[PartyId,VotesWithValue] = dict()
101 allvotes[self.__partyP]=VotesWithValue(self.__partyP,set((lambda _: _ if type(_)==list else [_])(VoteWithValue(self.__partyP,self.__bidB,2,2,100))))
102 allvotes[self.__partyQ]=VotesWithValue(self.__partyQ,set((lambda _: _ if type(_)==list else [_])(VoteWithValue(self.__partyQ,self.__bidB,2,9,100))))
103 allvotes[self.__partyR]=VotesWithValue(self.__partyR,set((lambda _: _ if type(_)==list else [_])(VoteWithValue(self.__partyR,self.__bidB,2,9,100))))
104 cv:CollectedVotesWithValue = CollectedVotesWithValue(allvotes,self.__allpowers)
105 parties:List[PartyId] = cv.getParties()
106 TestCase().assertEqual(set(),cv._getActiveParties(0))
107 TestCase().assertEqual(set((lambda _: _ if type(_)==list else [_])(parties[0])),cv._getActiveParties(1))
108 TestCase().assertEqual(set((lambda _: _ if type(_)==list else [_])(parties[1])),cv._getActiveParties(2))
109 TestCase().assertEqual(set((lambda _: _ if type(_)==list else [_])(parties[2])),cv._getActiveParties(4))
110 TestCase().assertEqual(set([parties[2],parties[0]]),cv._getActiveParties(5))
111
112 @Test()
113 def getPartiesTest(self) -> None:
114 allvotes:Dict[PartyId,VotesWithValue] = dict()
115 allvotes[self.__partyP]=VotesWithValue(self.__partyP,set((lambda _: _ if type(_)==list else [_])(VoteWithValue(self.__partyP,self.__bidB,2,2,100))))
116 allvotes[self.__partyQ]=VotesWithValue(self.__partyQ,set((lambda _: _ if type(_)==list else [_])(VoteWithValue(self.__partyQ,self.__bidB,2,9,100))))
117 allvotes[self.__partyR]=VotesWithValue(self.__partyR,set((lambda _: _ if type(_)==list else [_])(VoteWithValue(self.__partyR,self.__bidB,2,9,100))))
118 cv:CollectedVotesWithValue = CollectedVotesWithValue(allvotes,self.__allpowers)
119 parties:List[PartyId] = cv.getParties()
120 TestCase().assertEqual(set([parties[2],parties[0]]),cv._getParties([1,3]))
121
122 @Test()
123 def getCoalitionValuesTest(self) -> None:
124 # remains P,Q,R in the result.
125 allvotes:Dict[PartyId,VotesWithValue] = dict()
126 allvotes[self.__partyP]=VotesWithValue(self.__partyP,set([VoteWithValue(self.__partyP,self.__bidA,2,2,60),VoteWithValue(self.__partyP,self.__bidB,2,2,40)]))
127 allvotes[self.__partyQ]=VotesWithValue(self.__partyQ,set([VoteWithValue(self.__partyQ,self.__bidA,2,9,50),VoteWithValue(self.__partyQ,self.__bidB,2,9,50)]))
128 allvotes[self.__partyR]=VotesWithValue(self.__partyR,set([VoteWithValue(self.__partyR,self.__bidA,2,9,70),VoteWithValue(self.__partyR,self.__bidB,2,9,30)]))
129 cv:CollectedVotesWithValue = CollectedVotesWithValue(allvotes,self.__allpowers)
130 TestCase().assertFalse(any(abs(x)>.0001 for x in map(lambda x,y: x-y,[0.,0.,0.,110.,0.,130,120,0.],cv._getCoalitionValues())))
131
132 @Test()
133 def getMaxAgreementsTest(self) -> None:
134 # the max 2-team coalition is with P anyway: P+R with bid A, 130.
135 allvotes:Dict[PartyId,VotesWithValue] = dict()
136 allvotes[self.__partyP]=VotesWithValue(self.__partyP,set([VoteWithValue(self.__partyP,self.__bidA,2,2,60),VoteWithValue(self.__partyP,self.__bidB,2,2,40)]))
137 allvotes[self.__partyQ]=VotesWithValue(self.__partyQ,set([VoteWithValue(self.__partyQ,self.__bidA,2,9,50),VoteWithValue(self.__partyQ,self.__bidB,2,9,50)]))
138 allvotes[self.__partyR]=VotesWithValue(self.__partyR,set([VoteWithValue(self.__partyR,self.__bidA,2,9,70),VoteWithValue(self.__partyR,self.__bidB,2,9,30)]))
139 cv:CollectedVotesWithValue = CollectedVotesWithValue(allvotes,self.__allpowers)
140 best:Dict[PartyId,Bid] = cv.getMaxAgreements()
141 TestCase().assertEqual(set([self.__partyP,self.__partyR]),Keys(best))
142 TestCase().assertEqual(self.__bidA,best.get(self.__partyP))
143 TestCase().assertEqual(self.__bidA,best.get(self.__partyR))
144
145 @Test()
146 def getMaxAgreementsTest2(self) -> None:
147 allvotes:Dict[PartyId,VotesWithValue] = dict()
148 allvotes[self.__partyP]=VotesWithValue(self.__partyP,set([VoteWithValue(self.__partyP,self.__bidA,2,2,20),VoteWithValue(self.__partyP,self.__bidB,2,3,80)]))
149 allvotes[self.__partyQ]=VotesWithValue(self.__partyQ,set([VoteWithValue(self.__partyQ,self.__bidA,2,9,20),VoteWithValue(self.__partyQ,self.__bidB,2,9,80)]))
150 allvotes[self.__partyR]=VotesWithValue(self.__partyR,set([VoteWithValue(self.__partyR,self.__bidA,2,9,30),VoteWithValue(self.__partyR,self.__bidB,2,9,70)]))
151 allvotes[self.__partyS]=VotesWithValue(self.__partyS,set((lambda _: _ if type(_)==list else [_])(VoteWithValue(self.__partyS,self.__bidA,2,9,100))))
152 cv:CollectedVotesWithValue = CollectedVotesWithValue(allvotes,self.__allpowers)
153 best:Dict[PartyId,Bid] = cv.getMaxAgreements()
154 TestCase().assertEqual(set([self.__partyP,self.__partyQ,self.__partyR,self.__partyS]),Keys(best))
155 TestCase().assertEqual(self.__bidB,best.get(self.__partyP))
156 TestCase().assertEqual(self.__bidB,best.get(self.__partyQ))
157 TestCase().assertEqual(self.__bidA,best.get(self.__partyR))
158 TestCase().assertEqual(self.__bidA,best.get(self.__partyS))
159
160 @Test()
161 def getMaxAgreementsTest3(self) -> None:
162 # and R and S in another coalition.
163 allvotes:Dict[PartyId,VotesWithValue] = dict()
164 allvotes[self.__partyP]=VotesWithValue(self.__partyP,set((lambda _: _ if type(_)==list else [_])(VoteWithValue(self.__partyP,self.__bidA,2,2,100))))
165 allvotes[self.__partyQ]=VotesWithValue(self.__partyQ,set((lambda _: _ if type(_)==list else [_])(VoteWithValue(self.__partyQ,self.__bidA,2,9,100))))
166 allvotes[self.__partyR]=VotesWithValue(self.__partyR,set((lambda _: _ if type(_)==list else [_])(VoteWithValue(self.__partyR,self.__bidA,2,3,100))))
167 allvotes[self.__partyS]=VotesWithValue(self.__partyS,set((lambda _: _ if type(_)==list else [_])(VoteWithValue(self.__partyS,self.__bidA,2,9,100))))
168 cv:CollectedVotesWithValue = CollectedVotesWithValue(allvotes,self.__allpowers)
169 best:Dict[PartyId,Bid] = cv.getMaxAgreements()
170 TestCase().assertEqual(set([self.__partyP,self.__partyQ,self.__partyR,self.__partyS]),Keys(best))
171
172 def __init__(self):
173 self.__bidA:Bid = mock(Bid)
174 self.__bidB:Bid = mock(Bid)
175 self.__a:Bid = mock(Bid)
176 self.__b:Bid = mock(Bid)
177 self.__c:Bid = mock(Bid)
178 self.__d:Bid = mock(Bid)
179 self.__party1:PartyId = PartyId("party1")
180 self.__party2:PartyId = PartyId("party2")
181 self.__party3:PartyId = PartyId("party3")
182 self.__party4:PartyId = PartyId("party4")
183 self.__partyP:PartyId = PartyId("partyP")
184 self.__partyQ:PartyId = PartyId("partyQ")
185 self.__partyR:PartyId = PartyId("partyR")
186 self.__partyS:PartyId = PartyId("partyS")
187 self.__votePA2:VoteWithValue = VoteWithValue(self.__partyP,self.__bidA,2,9,100)
188 self.__voteQA2:VoteWithValue = VoteWithValue(self.__partyQ,self.__bidA,2,9,50)
189 self.__voteQB3:VoteWithValue = VoteWithValue(self.__partyQ,self.__bidB,3,9,50)
190 self.__voteRA4:VoteWithValue = VoteWithValue(self.__partyR,self.__bidA,4,9,50)
191 self.__allpowers:Dict[PartyId,int] = dict()
192 self.__cv1:CollectedVotesWithValue = CollectedVotesWithValue({},{})
193 self.__cv1a:CollectedVotesWithValue = CollectedVotesWithValue({},{})
194 self.__votes:VotesWithValue = VotesWithValue(self.__partyP,set([self.__votePA2]))
195 self.__cv2:CollectedVotesWithValue = CollectedVotesWithValue({self.__partyP:self.__votes},{self.__partyP:1})
196 super().__init__()
197
198class Test(TestCase):
199 def test(self):
200 runTests(CollectedVotesWithValueTest)
Note: See TracBrowser for help on using the repository browser.