1 | from datetime import datetime
|
---|
2 | from decimal import Decimal
|
---|
3 | from typing import cast, List
|
---|
4 | import unittest
|
---|
5 |
|
---|
6 | from geniusweb.actions.Accept import Accept
|
---|
7 | from geniusweb.actions.Action import Action
|
---|
8 | from geniusweb.actions.Offer import Offer
|
---|
9 | from geniusweb.actions.PartyId import PartyId
|
---|
10 | from geniusweb.connection.ConnectionEnd import ConnectionEnd
|
---|
11 | from geniusweb.inform.ActionDone import ActionDone
|
---|
12 | from geniusweb.inform.Inform import Inform
|
---|
13 | from geniusweb.inform.Settings import Settings
|
---|
14 | from geniusweb.inform.YourTurn import YourTurn
|
---|
15 | from geniusweb.issuevalue.Bid import Bid
|
---|
16 | from geniusweb.issuevalue.NumberValue import NumberValue
|
---|
17 | from geniusweb.progress.ProgressTime import ProgressTime
|
---|
18 | from geniusweb.references.Parameters import Parameters
|
---|
19 | from geniusweb.references.ProfileRef import ProfileRef
|
---|
20 | from geniusweb.references.ProtocolRef import ProtocolRef
|
---|
21 | from geniusweb.references.Reference import Reference
|
---|
22 | from randomparty.RandomParty import RandomParty
|
---|
23 | from tudelft.utilities.listener.DefaultListenable import DefaultListenable
|
---|
24 | from uri import URI # type: ignore
|
---|
25 |
|
---|
26 |
|
---|
27 | class 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 |
|
---|
50 | class 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 |
|
---|