source: exampleparties/randomparty/test/RandomPartyTest.py

Last change on this file was 100, checked in by ruud, 20 months ago

python installs also wheel to avoid error messages

File size: 5.5 KB
Line 
1from datetime import datetime
2from decimal import Decimal
3import json
4from pathlib import Path
5from typing import cast, List
6import unittest
7
8from geniusweb.actions.Accept import Accept
9from geniusweb.actions.Action import Action
10from geniusweb.actions.Offer import Offer
11from geniusweb.actions.PartyId import PartyId
12from geniusweb.actions.Votes import Votes
13from geniusweb.bidspace.AllBidsList import AllBidsList
14from geniusweb.connection.ConnectionEnd import ConnectionEnd
15from geniusweb.inform.ActionDone import ActionDone
16from geniusweb.inform.Inform import Inform
17from geniusweb.inform.Settings import Settings
18from geniusweb.inform.Voting import Voting
19from geniusweb.inform.YourTurn import YourTurn
20from geniusweb.issuevalue.Bid import Bid
21from geniusweb.issuevalue.NumberValue import NumberValue
22from geniusweb.profile.Profile import Profile
23from geniusweb.profile.utilityspace.LinearAdditive import LinearAdditive
24from geniusweb.profile.utilityspace.UtilitySpace import UtilitySpace
25from geniusweb.progress.ProgressTime import ProgressTime
26from geniusweb.references.Parameters import Parameters
27from geniusweb.references.ProfileRef import ProfileRef
28from geniusweb.references.ProtocolRef import ProtocolRef
29from geniusweb.references.Reference import Reference
30from pyson.ObjectMapper import ObjectMapper
31from tudelft.utilities.listener.DefaultListenable import DefaultListenable
32from uri.uri import URI # type: ignore
33
34from randomparty.RandomParty import RandomParty
35
36
37class MyConn(ConnectionEnd[Inform, Action], DefaultListenable):
38 def __init__(self):
39 super().__init__()
40 self._actions:List[Action]=[]
41
42 def send(self,data:Action ):
43 self._actions.append(data)
44
45 def getReference(self) -> Reference:
46 return cast(Reference,None)
47
48 def getRemoteURI(self)->URI:
49 return URI("whatever")
50
51 def close(self):
52 pass
53
54 def getError(self) -> Exception:
55 return cast(Exception, None)
56
57 def getActions(self)-> List[Action]:
58 return self._actions
59
60class RandomPartyTest(unittest.TestCase):
61 pyson = ObjectMapper()
62
63 PARTY1 = PartyId("party1")
64 profileref = ProfileRef(URI("file:test/resources/japantrip1.json"))
65 PROFILE = ProfileRef(URI("file:test/resources/testprofile.json"))
66 protocolref = ProtocolRef(URI("SAOP"))
67 mopacProtocol = ProtocolRef(URI("MOPAC"));
68 progress=ProgressTime(1000, datetime.fromtimestamp(12345))
69 parameters=Parameters()
70 mopacSettings = Settings(PARTY1, PROFILE, mopacProtocol,progress, parameters)
71 serialized = Path("test/resources/testprofile.json").read_text("utf-8")
72 profile:UtilitySpace = pyson.parse(json.loads(serialized), LinearAdditive) #type:ignore
73
74 def setUp(self):
75 self.party=RandomParty()
76 self.connection = MyConn()
77 # we load the profile here too, to find a good bid
78
79
80 def test_smoke(self):
81 RandomParty()
82
83 def testConnect(self):
84 party=RandomParty()
85 party.connect(self.connection)
86 party.disconnect()
87
88
89 def testSendInfo(self):
90 settings = Settings(self.PARTY1, self.profileref, self.protocolref, self.progress, self.parameters )
91
92 self.party.connect(self.connection)
93 self.connection.notifyListeners(settings)
94 self.party.disconnect()
95 self.assertEquals([], self.connection.getActions())
96
97 def testSendYourTurn(self):
98 self.assertEqual(0, len(self.connection.getActions()))
99 settings = Settings(self.PARTY1, self.profileref, self.protocolref, self.progress, self.parameters )
100
101 self.party.connect(self.connection)
102 self.connection.notifyListeners(settings)
103 self.connection.notifyListeners(YourTurn())
104 self.party.disconnect()
105
106 actions = self.connection.getActions()
107 self.assertEquals(1, len(actions))
108 self.assertTrue(isinstance(actions[0], Offer))
109 print("party did an offer: "+repr(actions[0]))
110
111 def testSendOfferAndYourTurn(self):
112 settings = Settings(self.PARTY1, self.profileref, self.protocolref, self.progress, self.parameters )
113
114 # nonsense bid, party should not accept
115 bid=Bid({'a':NumberValue(Decimal(1))})
116 offer = Offer(PartyId('other'), bid)
117
118 self.party.connect(self.connection)
119 self.connection.notifyListeners(settings)
120 self.connection.notifyListeners(ActionDone(offer))
121 self.connection.notifyListeners(YourTurn())
122 self.party.disconnect()
123
124 actions = self.connection.getActions()
125 self.assertEquals(1, len(actions))
126 self.assertTrue(isinstance(actions[0], Offer))
127
128
129 def testVoting(self) :
130 self.assertEqual(0, len(self.connection.getActions()))
131 self.party.connect(self.connection);
132 self.party.notifyChange(self.mopacSettings);
133
134 bid = self._findGoodBid()
135 offer = Offer(self.PARTY1, bid)
136 self.party.notifyChange(Voting([offer],{self.PARTY1: 1}))
137 self.assertEqual(1, len(self.connection.getActions()))
138 action = self.connection.getActions()[0]
139 self.assertTrue(isinstance(action,Votes))
140 self.assertEqual(1, len(action.getVotes()))
141 self.assertEqual(bid, next(iter(action.getVotes())).getBid())
142
143 def _findGoodBid(self)-> Bid:
144 for bid in AllBidsList(self.profile.getDomain()):
145 if self.profile.getUtility(bid) > 0.7:
146 return bid;
147 raise ValueError("Test can not be done: there is no good bid with utility>0.7");
Note: See TracBrowser for help on using the repository browser.