[81] | 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 tudelft.utilities.listener.DefaultListenable import DefaultListenable
|
---|
| 23 | from uri.uri import URI
|
---|
| 24 |
|
---|
| 25 | from stupidparty.StupidParty import StupidParty
|
---|
| 26 |
|
---|
| 27 |
|
---|
| 28 | class MyConn(ConnectionEnd[Inform, Action], DefaultListenable):
|
---|
| 29 | def __init__(self):
|
---|
| 30 | super().__init__()
|
---|
| 31 | self._actions:List[Action]=[]
|
---|
| 32 |
|
---|
| 33 | def send(self,data:Action ):
|
---|
| 34 | self._actions.append(data)
|
---|
| 35 |
|
---|
| 36 | def getReference(self) -> Reference:
|
---|
| 37 | return cast(Reference,None)
|
---|
| 38 |
|
---|
| 39 | def getRemoteURI(self)->URI:
|
---|
| 40 | return URI("whatever")
|
---|
| 41 |
|
---|
| 42 | def close(self):
|
---|
| 43 | pass
|
---|
| 44 |
|
---|
| 45 | def getError(self) -> Exception:
|
---|
| 46 | return cast(Exception, None)
|
---|
| 47 |
|
---|
| 48 | def getActions(self)-> List[Action]:
|
---|
| 49 | return self._actions
|
---|
| 50 |
|
---|
| 51 | class StupidPartyTest(unittest.TestCase):
|
---|
| 52 | def test_smoke(self):
|
---|
| 53 | StupidParty()
|
---|
| 54 |
|
---|
| 55 | def testConnect(self):
|
---|
| 56 | connection = MyConn()
|
---|
| 57 | party=StupidParty()
|
---|
| 58 | party.connect(connection)
|
---|
| 59 | party.disconnect()
|
---|
| 60 |
|
---|
| 61 |
|
---|
| 62 | def testSendInfo(self):
|
---|
| 63 | id1 = PartyId("party1")
|
---|
| 64 | profileref1 = ProfileRef(URI("profile1"))
|
---|
| 65 | protocolref1 = ProtocolRef(URI("protoco1"))
|
---|
| 66 | progress1=ProgressTime(1000, datetime.fromtimestamp(12345))
|
---|
| 67 | parameters1=Parameters()
|
---|
| 68 | settings = Settings(id1, profileref1, protocolref1, progress1, parameters1 )
|
---|
| 69 |
|
---|
| 70 | connection = MyConn()
|
---|
| 71 | party=StupidParty()
|
---|
| 72 | party.connect(connection)
|
---|
| 73 | connection.notifyListeners(settings)
|
---|
| 74 | party.disconnect()
|
---|
| 75 | self.assertEquals([], connection.getActions())
|
---|
| 76 |
|
---|
| 77 | def testSendYourTurn(self):
|
---|
| 78 | id1 = PartyId("party1")
|
---|
| 79 | profileref1 = ProfileRef(URI("profile1"))
|
---|
| 80 | protocolref1 = ProtocolRef(URI("protoco1"))
|
---|
| 81 | progress1=ProgressTime(1000, datetime.fromtimestamp(12345))
|
---|
| 82 | parameters1=Parameters()
|
---|
| 83 | settings = Settings(id1, profileref1, protocolref1, progress1, parameters1 )
|
---|
| 84 |
|
---|
| 85 | connection = MyConn()
|
---|
| 86 | party=StupidParty()
|
---|
| 87 | party.connect(connection)
|
---|
| 88 | connection.notifyListeners(settings)
|
---|
| 89 | connection.notifyListeners(YourTurn())
|
---|
| 90 | party.disconnect()
|
---|
| 91 |
|
---|
| 92 | actions = connection.getActions()
|
---|
| 93 | self.assertEquals(1, len(actions))
|
---|
| 94 | self.assertTrue(isinstance(actions[0], Offer))
|
---|
| 95 |
|
---|
| 96 | def testSendOfferAndYourTurn(self):
|
---|
| 97 | id1 = PartyId("party1")
|
---|
| 98 | profileref1 = ProfileRef(URI("profile1"))
|
---|
| 99 | protocolref1 = ProtocolRef(URI("protoco1"))
|
---|
| 100 | progress1=ProgressTime(1000, datetime.fromtimestamp(12345))
|
---|
| 101 | parameters1=Parameters()
|
---|
| 102 | settings = Settings(id1, profileref1, protocolref1, progress1, parameters1 )
|
---|
| 103 |
|
---|
| 104 | bid=Bid({'a':NumberValue(Decimal(1))})
|
---|
| 105 | offer = Offer(PartyId('other'), bid)
|
---|
| 106 |
|
---|
| 107 | party=StupidParty()
|
---|
| 108 |
|
---|
| 109 | connection = MyConn()
|
---|
| 110 | party.connect(connection)
|
---|
| 111 | connection.notifyListeners(settings)
|
---|
| 112 | connection.notifyListeners(ActionDone(offer))
|
---|
| 113 | connection.notifyListeners(YourTurn())
|
---|
| 114 | party.disconnect()
|
---|
| 115 |
|
---|
| 116 | actions = connection.getActions()
|
---|
| 117 | self.assertEquals(1, len(actions))
|
---|
| 118 | self.assertEquals(Accept(id1, bid), actions[0])
|
---|
| 119 |
|
---|
| 120 | def testAccept(self):
|
---|
| 121 | id1 = PartyId("party1")
|
---|
| 122 | bid=Bid({'a':NumberValue(Decimal(1))})
|
---|
| 123 | accept = Accept(id1, bid)
|
---|
| 124 |
|
---|
| 125 | profileref1 = ProfileRef(URI("profile1"))
|
---|
| 126 | protocolref1 = ProtocolRef(URI("protoco1"))
|
---|
| 127 | progress1=ProgressTime(1000, datetime.fromtimestamp(12345))
|
---|
| 128 | parameters1=Parameters()
|
---|
| 129 | settings = Settings(id1, profileref1, protocolref1, progress1, parameters1 )
|
---|
| 130 |
|
---|
| 131 | party=StupidParty()
|
---|
| 132 |
|
---|
| 133 | connection = MyConn()
|
---|
| 134 | party.connect(connection)
|
---|
| 135 | connection.notifyListeners(settings)
|
---|
| 136 | connection.notifyListeners(accept)
|
---|
| 137 |
|
---|
| 138 | party.disconnect()
|
---|
| 139 |
|
---|
| 140 | actions = connection.getActions()
|
---|
| 141 | self.assertEquals(0, len(actions))
|
---|
| 142 |
|
---|
| 143 | def testYourTurnOfferYourTurn(self):
|
---|
| 144 | me = PartyId("party1")
|
---|
| 145 | other=PartyId('other')
|
---|
| 146 | profileref1 = ProfileRef(URI("profile1"))
|
---|
| 147 | protocolref1 = ProtocolRef(URI("protoco1"))
|
---|
| 148 | progress1=ProgressTime(1000, datetime.fromtimestamp(12345))
|
---|
| 149 | parameters1=Parameters()
|
---|
| 150 | settings = Settings(me, profileref1, protocolref1, progress1, parameters1 )
|
---|
| 151 |
|
---|
| 152 | emptybid=Bid({})
|
---|
| 153 | emptyoffer = Offer(me, emptybid)
|
---|
| 154 | bid=Bid({'a':NumberValue(Decimal(1))})
|
---|
| 155 | offer = Offer(other, bid)
|
---|
| 156 |
|
---|
| 157 | party=StupidParty()
|
---|
| 158 |
|
---|
| 159 | connection = MyConn()
|
---|
| 160 | party.connect(connection)
|
---|
| 161 | connection.notifyListeners(settings)
|
---|
| 162 | connection.notifyListeners(YourTurn())
|
---|
| 163 | connection.notifyListeners(ActionDone(emptyoffer))
|
---|
| 164 | connection.notifyListeners(ActionDone(offer))
|
---|
| 165 | connection.notifyListeners(YourTurn())
|
---|
| 166 | party.disconnect()
|
---|
| 167 |
|
---|
| 168 | actions = connection.getActions()
|
---|
| 169 | # 2 yourturns, so there should eb 2 actions
|
---|
| 170 | self.assertEquals(2, len(actions))
|
---|
| 171 | self.assertEquals(emptyoffer, actions[0])
|
---|
| 172 | self.assertEquals(Accept(me, bid), actions[1])
|
---|
| 173 |
|
---|
| 174 |
|
---|
| 175 |
|
---|
| 176 |
|
---|