1 | import unittest
|
---|
2 | from pyson.ObjectMapper import ObjectMapper
|
---|
3 | from unitpy.GeneralTests import GeneralTests
|
---|
4 | from geniusweb.events.SessionStarted import SessionStarted
|
---|
5 | from geniusweb.actions.PartyId import PartyId
|
---|
6 | from typing import List
|
---|
7 | from geniusweb.events.NegotiationEvent import NegotiationEvent
|
---|
8 | import json
|
---|
9 |
|
---|
10 | class SessionStartedTest(unittest.TestCase, GeneralTests[SessionStarted]):
|
---|
11 |
|
---|
12 | pyson = ObjectMapper()
|
---|
13 |
|
---|
14 | SESSIONNR = 1
|
---|
15 | NOW = 101
|
---|
16 | NOW1 = NOW + 1
|
---|
17 |
|
---|
18 | PARTY1 = PartyId("party1")
|
---|
19 | PARTY2 = PartyId("party2")
|
---|
20 |
|
---|
21 | parties = [PARTY1, PARTY2]
|
---|
22 |
|
---|
23 | sessionStartedLater = SessionStarted(SESSIONNR, parties, NOW1)
|
---|
24 | othersessionStarted = SessionStarted(SESSIONNR + 1, parties, NOW)
|
---|
25 |
|
---|
26 | sessionStarted = SessionStarted(SESSIONNR, parties, NOW)
|
---|
27 | sessionStarted1 = SessionStarted(SESSIONNR, parties, NOW)
|
---|
28 | # jackson expected format.
|
---|
29 | sessionstartedstring = "{\"SessionStarted\":{\"sessionNumber\":1,\"parties\":[\"party1\",\"party2\"],\"time\":101}}"
|
---|
30 | # "{\"sessionstarted\":{\"time\":101,\"sessionNumber\":1,\"parties\":[\"party1\",\"party2\"]}}";
|
---|
31 | expectedstring = "SessionStarted.*101.*"
|
---|
32 |
|
---|
33 | def getGeneralTestData(self) -> List[List[SessionStarted]]:
|
---|
34 | mylist = []
|
---|
35 | mylist.append([self.sessionStarted, self.sessionStarted1])
|
---|
36 | mylist.append([self.sessionStartedLater])
|
---|
37 | mylist.append([self.othersessionStarted])
|
---|
38 | return mylist
|
---|
39 |
|
---|
40 | def getGeneralTestStrings(self) -> List[str]:
|
---|
41 | return ["SessionStarted.*" + str(self.SESSIONNR) + ".*" + str(self.NOW) + ".*",\
|
---|
42 | "SessionStarted.*" + str(self.SESSIONNR) + ".*" + str(self.NOW1) + ".*",\
|
---|
43 | "SessionStarted.*" + str(self.SESSIONNR + 1) + ".*" + str(self.NOW) + ".*"]
|
---|
44 |
|
---|
45 | def testSerialize(self):
|
---|
46 | print(self.sessionstartedstring)
|
---|
47 | self.assertEqual(json.loads(self.sessionstartedstring), self.pyson.toJson(self.sessionStarted))
|
---|
48 |
|
---|
49 | def testDeserialize(self):
|
---|
50 | evt = self.pyson.parse(json.loads(self.sessionstartedstring), NegotiationEvent)
|
---|
51 | self.assertEqual(SessionStarted, type(evt))
|
---|
52 | event = evt
|
---|
53 | # compare fields, as sessionended is a derived/new inner class
|
---|
54 | self.assertEqual(self.NOW, event.getTime())
|
---|
55 | self.assertEqual(self.SESSIONNR, event.getSessionNumber())
|
---|
56 | self.assertEqual(2, len(event.getParties()))
|
---|
57 |
|
---|