source: exampleparties/randomparty/test/RandomPartyTest.py@ 60

Last change on this file since 60 was 59, checked in by Wouter Pasman, 3 years ago

#44 manual commit of first public release, because this will cause the dist directory to move

File size: 3.7 KB
Line 
1from datetime import datetime
2from decimal import Decimal
3from typing import cast, List
4import unittest
5
6from geniusweb.actions.Accept import Accept
7from geniusweb.actions.Action import Action
8from geniusweb.actions.Offer import Offer
9from geniusweb.actions.PartyId import PartyId
10from geniusweb.connection.ConnectionEnd import ConnectionEnd
11from geniusweb.inform.ActionDone import ActionDone
12from geniusweb.inform.Inform import Inform
13from geniusweb.inform.Settings import Settings
14from geniusweb.inform.YourTurn import YourTurn
15from geniusweb.issuevalue.Bid import Bid
16from geniusweb.issuevalue.NumberValue import NumberValue
17from geniusweb.progress.ProgressTime import ProgressTime
18from geniusweb.references.Parameters import Parameters
19from geniusweb.references.ProfileRef import ProfileRef
20from geniusweb.references.ProtocolRef import ProtocolRef
21from geniusweb.references.Reference import Reference
22from randomparty.RandomParty import RandomParty
23from tudelft.utilities.listener.DefaultListenable import DefaultListenable
24from uri import URI # type: ignore
25
26
27class MyConn(ConnectionEnd[Inform, Action], DefaultListenable):
28 def __init__(self):
29 super().__init__()
30 self._actions:List[Action]=[]
31
32 def send(self,data:Action ):
33 self._actions.append(data)
34
35 def getReference(self) -> Reference:
36 return cast(Reference,None)
37
38 def getRemoteURI(self)->URI:
39 return None
40
41 def close(self):
42 pass
43
44 def getError(self) -> Exception:
45 return cast(Exception, None)
46
47 def getActions(self)-> List[Action]:
48 return self._actions
49
50class RandomPartyTest(unittest.TestCase):
51 id = PartyId("party1")
52 profileref = ProfileRef(URI("file:test/resources/japantrip1.json"))
53 protocolref = ProtocolRef(URI("SAOP"))
54 progress=ProgressTime(1000, datetime.fromtimestamp(12345))
55 parameters=Parameters()
56
57 def test_smoke(self):
58 RandomParty()
59
60 def testConnect(self):
61 connection = MyConn()
62 party=RandomParty()
63 party.connect(connection)
64 party.disconnect()
65
66
67 def testSendInfo(self):
68 settings = Settings(self.id, self.profileref, self.protocolref, self.progress, self.parameters )
69
70 connection = MyConn()
71 party=RandomParty()
72 party.connect(connection)
73 connection.notifyListeners(settings)
74 party.disconnect()
75 self.assertEquals([], connection.getActions())
76
77 def testSendYourTurn(self):
78 settings = Settings(self.id, self.profileref, self.protocolref, self.progress, self.parameters )
79
80 connection = MyConn()
81 party=RandomParty()
82 party.connect(connection)
83 connection.notifyListeners(settings)
84 connection.notifyListeners(YourTurn())
85 party.disconnect()
86
87 actions = connection.getActions()
88 self.assertEquals(1, len(actions))
89 self.assertTrue(isinstance(actions[0], Offer))
90 print("party did an offer: "+repr(actions[0]))
91
92 def testSendOfferAndYourTurn(self):
93 settings = Settings(self.id, self.profileref, self.protocolref, self.progress, self.parameters )
94
95 # nonsense bid, party should not accept
96 bid=Bid({'a':NumberValue(Decimal(1))})
97 offer = Offer(PartyId('other'), bid)
98
99 connection = MyConn()
100 party=RandomParty()
101 party.connect(connection)
102 connection.notifyListeners(settings)
103 connection.notifyListeners(ActionDone(offer))
104 connection.notifyListeners(YourTurn())
105 party.disconnect()
106
107 actions = connection.getActions()
108 self.assertEquals(1, len(actions))
109 self.assertTrue(isinstance(actions[0], Offer))
110
Note: See TracBrowser for help on using the repository browser.