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.LargestAgreement import LargestAgreement
|
---|
16 |
|
---|
17 |
|
---|
18 | class LargestAgreementTest (unittest.TestCase, GeneralTests[LargestAgreement]):
|
---|
19 | jackson = ObjectMapper()
|
---|
20 |
|
---|
21 | lal1 = LargestAgreement()
|
---|
22 | lal1a = LargestAgreement()
|
---|
23 | party1 = PartyId("party1")
|
---|
24 | party2 = PartyId("party2")
|
---|
25 | party3 = PartyId("party3")
|
---|
26 | party4 = PartyId("party4")
|
---|
27 | votes1 = Mock(Votes)
|
---|
28 | lal2 = LargestAgreement()
|
---|
29 | # hack for testing only.
|
---|
30 | lal2._allVotes = (CollectedVotes({party1: votes1}, {party1: 1}))
|
---|
31 |
|
---|
32 | lalstring = "{\"LargestAgreement\":{}}"
|
---|
33 |
|
---|
34 | def getGeneralTestData(self)-> List[List[LargestAgreement]] :
|
---|
35 | return [[self.lal1, self.lal1a], [self.lal2]]
|
---|
36 |
|
---|
37 | def getGeneralTestStrings(self) ->List[str] :
|
---|
38 | return ["LargestAgreement", "LargestAgreement"]
|
---|
39 |
|
---|
40 | def testserializeTest(self) :
|
---|
41 | # we use lal2 to also check we don't serialize the contents
|
---|
42 | print(self.jackson.toJson(self.lal2))
|
---|
43 | self.assertEqual(json.loads(self.lalstring), self.jackson.toJson(self.lal2))
|
---|
44 |
|
---|
45 | def testdeserializeTest(self):
|
---|
46 | eval = self.jackson.parse(json.loads(self.lalstring),
|
---|
47 | VotingEvaluator)
|
---|
48 | # it should deserialize as the empty lal1 because we ignore the fields.
|
---|
49 | self.assertEqual(self.lal1, eval)
|
---|
50 |
|
---|
51 | def testCollectVotes(self):
|
---|
52 | # we set up a tricky situation where party4 has much more power
|
---|
53 | # and therefore party 1+4 >> p1+2+3
|
---|
54 | bid1 = Mock(Bid)
|
---|
55 | bid2 = Mock(Bid)
|
---|
56 |
|
---|
57 | votes = CollectedVotes({}, {})
|
---|
58 | votes = votes.With(Votes(self.party1,
|
---|
59 | set([Vote(self.party1, bid1, 1, 99),
|
---|
60 | Vote(self.party1, bid2, 1, 99)])),
|
---|
61 | 1)
|
---|
62 | votes = votes.With(
|
---|
63 | Votes(self.party2,
|
---|
64 | set([Vote(self.party2, bid1, 1, 99)])),
|
---|
65 | 1)
|
---|
66 | votes = votes.With(
|
---|
67 | Votes(self.party3,
|
---|
68 | set([Vote(self.party3, bid1, 1, 99)])),
|
---|
69 | 1)
|
---|
70 | votes = votes.With(
|
---|
71 | Votes(self.party4,
|
---|
72 | set([Vote(self.party4, bid2, 1, 99)])),
|
---|
73 | 4)
|
---|
74 | la = LargestAgreement()
|
---|
75 | la._allVotes=votes
|
---|
76 | agreement = la._collectVotes()
|
---|
77 | self.assertEqual(set([self.party1, self.party4]),
|
---|
78 | agreement.getMap().keys())
|
---|