source: geniuswebcore/test/geniusweb/actions/VoteTest.py@ 88

Last change on this file since 88 was 88, checked in by Bart Vastenhouw, 3 years ago

Added python SimpleRunner GUI

File size: 2.0 KB
Line 
1from decimal import Decimal
2import json
3from typing import Dict
4import unittest
5
6from pyson.ObjectMapper import ObjectMapper
7
8from geniusweb.actions.Action import Action
9from geniusweb.actions.Offer import Offer
10from geniusweb.actions.PartyId import PartyId
11from geniusweb.actions.Vote import Vote
12from geniusweb.issuevalue.Bid import Bid
13from geniusweb.issuevalue.DiscreteValue import DiscreteValue
14from geniusweb.issuevalue.NumberValue import NumberValue
15from geniusweb.issuevalue.Value import Value
16
17
18class 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))
Note: See TracBrowser for help on using the repository browser.