[81] | 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 unitpy.GeneralTests import GeneralTests
|
---|
| 8 |
|
---|
| 9 | from geniusweb.actions.PartyId import PartyId
|
---|
| 10 | from geniusweb.actions.Vote import Vote
|
---|
| 11 | from geniusweb.actions.Votes import Votes
|
---|
| 12 | from geniusweb.issuevalue.Bid import Bid
|
---|
| 13 | from geniusweb.voting.CollectedVotes import CollectedVotes
|
---|
| 14 | from geniusweb.voting.VotingEvaluator import VotingEvaluator
|
---|
| 15 | from geniusweb.voting.votingevaluators.LargestAgreementsLoop import LargestAgreementsLoop
|
---|
| 16 |
|
---|
| 17 |
|
---|
| 18 | class LargestAgreementsLoopTest(unittest.TestCase, GeneralTests[LargestAgreementsLoop]):
|
---|
| 19 | jackson = ObjectMapper()
|
---|
| 20 |
|
---|
| 21 | a = Mock(Bid)
|
---|
| 22 | b = Mock(Bid)
|
---|
| 23 | c = Mock(Bid)
|
---|
| 24 | d = Mock(Bid)
|
---|
| 25 |
|
---|
| 26 |
|
---|
| 27 | lal1 = LargestAgreementsLoop()
|
---|
| 28 | lal1a = LargestAgreementsLoop()
|
---|
| 29 | party1 = PartyId("party1")
|
---|
| 30 | party2 = PartyId("party2")
|
---|
| 31 | party3 = PartyId("party3")
|
---|
| 32 | party4 = PartyId("party4")
|
---|
| 33 | votes1 = Mock(Votes)
|
---|
| 34 | lal2 = LargestAgreementsLoop().create(
|
---|
| 35 | CollectedVotes({party1:votes1}, {party1: 1}))
|
---|
| 36 |
|
---|
| 37 | lalstring = "{\"LargestAgreementsLoop\":{}}"
|
---|
| 38 |
|
---|
| 39 | def getGeneralTestData(self)-> List[List[LargestAgreementsLoop]] :
|
---|
| 40 | return [[self.lal1, self.lal1a], [self.lal2]]
|
---|
| 41 |
|
---|
| 42 | def getGeneralTestStrings(self)-> List[str]:
|
---|
| 43 | return ["LargestAgreementsLoop", "LargestAgreementsLoop"]
|
---|
| 44 |
|
---|
| 45 | def testserializeAcceptTest(self):
|
---|
| 46 | # we use lal2 to also check we don't serialize the contents
|
---|
| 47 | print(self.jackson.toJson(self.lal2))
|
---|
| 48 | self.assertEqual(json.loads(self.lalstring), self.jackson.toJson(self.lal2))
|
---|
| 49 |
|
---|
| 50 | def testdeserializeAcceptTest(self):
|
---|
| 51 | eval = self.jackson.parse(json.loads(self.lalstring),VotingEvaluator)
|
---|
| 52 | # it should deserialize as the empty lal1 because we ignore the fields.
|
---|
| 53 | self.assertEquals(self.lal1, eval)
|
---|
| 54 |
|
---|
| 55 | def testCollectVotes(self):
|
---|
| 56 | vote1AB = Votes(self.party1, set([Vote(self.party1, self.a, 2, 9),
|
---|
| 57 | Vote(self.party1, self.b, 2, 9)]))
|
---|
| 58 | vote2AB = Votes(self.party2, set([Vote(self.party2, self.a, 2, 9),
|
---|
| 59 | Vote(self.party2,self.b, 2, 9)]))
|
---|
| 60 | vote3C = Votes(self.party3,
|
---|
| 61 | set([Vote(self.party3, self.c, 2, 9)]))
|
---|
| 62 | vote4AC = Votes(self.party4, set([Vote(self.party4, self.a, 2, 9),
|
---|
| 63 | Vote(self.party4, self.c, 2, 9)]))
|
---|
| 64 | # party 1,2,4 vote for A, party 1,2 vote for B, party 3,4 vote for C.
|
---|
| 65 | # the biggest vote is P,Q,S
|
---|
| 66 | votes = {}
|
---|
| 67 | votes[self.party1]= vote1AB
|
---|
| 68 | votes[self.party2]= vote2AB
|
---|
| 69 | votes[self.party3]= vote3C
|
---|
| 70 | votes[self.party4]= vote4AC
|
---|
| 71 |
|
---|
| 72 | actions1 = [[vote1AB, vote2AB, vote3C, vote4AC]];
|
---|
| 73 | power = {}
|
---|
| 74 | power[self.party1]=1
|
---|
| 75 | power[self.party2]=1
|
---|
| 76 | power[self.party3]= 1
|
---|
| 77 | power[self.party4]= 1
|
---|
| 78 | agrees = LargestAgreementsLoop().create(
|
---|
| 79 | CollectedVotes(votes, power)).getAgreements()
|
---|
| 80 | print(agrees)
|
---|
| 81 |
|
---|
| 82 | # biggest agreement is party 1,2,4 for bid a.
|
---|
| 83 | self.assertEqual(set([self.party1, self.party2, self.party4]),
|
---|
| 84 | agrees.getMap().keys())
|
---|