[81] | 1 | from decimal import Decimal
|
---|
| 2 | import json
|
---|
| 3 | from typing import Dict
|
---|
| 4 | import unittest
|
---|
| 5 |
|
---|
| 6 | from pyson.ObjectMapper import ObjectMapper
|
---|
| 7 |
|
---|
| 8 | from geniusweb.actions.Action import Action
|
---|
| 9 | from geniusweb.actions.Offer import Offer
|
---|
| 10 | from geniusweb.actions.PartyId import PartyId
|
---|
| 11 | from geniusweb.actions.Vote import Vote
|
---|
| 12 | from geniusweb.issuevalue.Bid import Bid
|
---|
| 13 | from geniusweb.issuevalue.DiscreteValue import DiscreteValue
|
---|
| 14 | from geniusweb.issuevalue.NumberValue import NumberValue
|
---|
| 15 | from geniusweb.issuevalue.Value import Value
|
---|
| 16 |
|
---|
| 17 |
|
---|
| 18 | class VoteTest (unittest.TestCase) :
|
---|
| 19 | jackson = ObjectMapper()
|
---|
| 20 |
|
---|
| 21 | id1 = PartyId("party1")
|
---|
| 22 | id2 = PartyId("party2")
|
---|
| 23 | issuevalues: Dict[str, Value]={}
|
---|
| 24 | issuevaluesb: Dict[str, Value] = {}
|
---|
| 25 | bid:Bid
|
---|
| 26 | bidb:Bid
|
---|
| 27 | ISSUE1 = "issue1";
|
---|
| 28 | VALUE1 = DiscreteValue("value1");
|
---|
| 29 | ISSUE2 = "issue2";
|
---|
| 30 | VALUE2 = NumberValue(Decimal(10));
|
---|
| 31 | # issue 2 is NUMBER and thus serializes with leading '='
|
---|
| 32 | votestring = "{\"Vote\":{\"actor\":\"party1\",\"bid\":{\"issuevalues\":{\"issue1\":\"value1\",\"issue2\":10}},\"minPower\":1,\"maxPower\":2}}";
|
---|
| 33 |
|
---|
| 34 | issuevalues[ISSUE1]=VALUE1
|
---|
| 35 | issuevalues[ISSUE2]=VALUE2
|
---|
| 36 | bid = Bid(issuevalues)
|
---|
| 37 | vote1 = Vote(id1, bid, 1, 2)
|
---|
| 38 | vote1a = Vote(id1, bid, 1, 2)
|
---|
| 39 |
|
---|
| 40 | vote2 = Vote(id2, bid, 1, 2)
|
---|
| 41 |
|
---|
| 42 | # values swapped, so different issuevalues.
|
---|
| 43 | issuevaluesb[ISSUE1]= VALUE2
|
---|
| 44 | issuevaluesb[ISSUE2]= VALUE2
|
---|
| 45 | bidb = Bid(issuevaluesb)
|
---|
| 46 | vote3 = Vote(id1, bidb, 1, 2)
|
---|
| 47 |
|
---|
| 48 | vote4 = Vote(id1, bid, 2, 2)
|
---|
| 49 | vote5 = Vote(id1, bid, 1, 3)
|
---|
| 50 |
|
---|
| 51 |
|
---|
| 52 | def testSerialize(self):
|
---|
| 53 | print(self.jackson.toJson(self.vote1))
|
---|
| 54 | self.assertEqual(self.votestring, json.dumps(self.jackson.toJson(self.vote1)).replace(" ",""))
|
---|
| 55 |
|
---|
| 56 |
|
---|
| 57 | def testDeserialize(self):
|
---|
| 58 | act = self.jackson.parse(json.loads(self.votestring), Action)
|
---|
| 59 | self.assertEqual(self.vote1, act)
|
---|
| 60 |
|
---|
| 61 | def testEqual(self):
|
---|
| 62 | self.assertEqual(self.vote1, self.vote1a)
|
---|
| 63 | self.assertNotEqual(self.vote1, self.vote2)
|
---|
| 64 | self.assertEqual(hash(self.vote1), hash(self.vote1a))
|
---|
| 65 | self.assertNotEqual(hash(self.vote1), hash(self.vote2))
|
---|