[73] | 1 | import unittest
|
---|
| 2 |
|
---|
| 3 | from pyson.ObjectMapper import ObjectMapper
|
---|
| 4 | from geniusweb.events.ActionEvent import ActionEvent
|
---|
| 5 | from unitpy.GeneralTests import GeneralTests
|
---|
| 6 | from geniusweb.actions.PartyId import PartyId
|
---|
| 7 | from geniusweb.actions.EndNegotiation import EndNegotiation
|
---|
| 8 | from typing import List
|
---|
| 9 | import json
|
---|
| 10 | from geniusweb.events.NegotiationEvent import NegotiationEvent
|
---|
| 11 | from unittest.mock import Mock
|
---|
| 12 |
|
---|
| 13 | class ActionEventTest(unittest.TestCase, GeneralTests[ActionEvent]):
|
---|
| 14 | NOW = 101
|
---|
| 15 | pyson = ObjectMapper()
|
---|
| 16 | string = "{\"ActionEvent\":{\"action\":{\"EndNegotiation\":{\"actor\":\"party1\"}},\"time\":101}}"
|
---|
| 17 | pid = PartyId("party1");
|
---|
| 18 | action = EndNegotiation(pid);
|
---|
| 19 | evt = ActionEvent(action, NOW);
|
---|
| 20 | evt1 = ActionEvent(action, NOW);
|
---|
| 21 | evtb = ActionEvent(action, NOW + 1);
|
---|
| 22 |
|
---|
| 23 | def getGeneralTestData(self) -> List[List[ActionEvent]]:
|
---|
| 24 | return [[self.evt, self.evt1], [self.evtb]]
|
---|
| 25 |
|
---|
| 26 | def getGeneralTestStrings(self):
|
---|
| 27 | return "ActionEvent\\[.*" + str(self.NOW) + ".*EndNegotiation.*\\]",\
|
---|
| 28 | "ActionEvent\\[.*" + str(self.NOW + 1) + ".*EndNegotiation.*\\]"
|
---|
| 29 |
|
---|
| 30 | def testSmoke(self):
|
---|
| 31 | action1 = Mock(EndNegotiation)
|
---|
| 32 | evt = ActionEvent(action1, 0)
|
---|
| 33 |
|
---|
| 34 | def testserialize(self):
|
---|
| 35 | json1 = self.pyson.toJson(self.evt)
|
---|
| 36 | print(json1)
|
---|
| 37 | self.assertEqual(json.loads(self.string), json1)
|
---|
| 38 |
|
---|
| 39 | def testdeserializeTestReadActionEvent(self):
|
---|
| 40 | evt1 = self.pyson.parse(json.loads(self.string), ActionEvent)
|
---|
| 41 | print(evt1)
|
---|
| 42 | # compare fields, as evt is a derived/new inner class
|
---|
| 43 | self.assertEqual(EndNegotiation, type(evt1.getAction()))
|
---|
| 44 | self.assertEqual(101, evt1.getTime())
|
---|
| 45 |
|
---|
| 46 | def testdeserializeTestReadEvent(self):
|
---|
| 47 | self.assertEqual(self.evt1, self.pyson.parse(json.loads(self.string), ActionEvent))
|
---|
| 48 | evt1 = self.pyson.parse(json.loads(self.string), NegotiationEvent)
|
---|
| 49 | self.assertEqual(ActionEvent, type(evt1))
|
---|
| 50 | ev1 = evt1
|
---|
| 51 | self.assertEqual(EndNegotiation, type(ev1.getAction()))
|
---|
| 52 | self.assertEqual(101, ev1.getTime())
|
---|