1 | from typing import Dict, Any, List
|
---|
2 | from geniusweb.inform.Settings import Settings
|
---|
3 | from unitpy.GeneralTests import GeneralTests
|
---|
4 | from geniusweb.actions.PartyId import PartyId
|
---|
5 | from datetime import datetime
|
---|
6 | from pyson.ObjectMapper import ObjectMapper
|
---|
7 | from geniusweb.references.ProfileRef import ProfileRef
|
---|
8 | from uri.uri import URI
|
---|
9 | from geniusweb.references.ProtocolRef import ProtocolRef
|
---|
10 | from geniusweb.progress.ProgressRounds import ProgressRounds
|
---|
11 | from geniusweb.references.Parameters import Parameters
|
---|
12 | from geniusweb.references.PartyRef import PartyRef
|
---|
13 | import json
|
---|
14 | from geniusweb.inform.Inform import Inform
|
---|
15 | import unittest
|
---|
16 |
|
---|
17 | class SettingsTest(unittest.TestCase, GeneralTests[Settings]):
|
---|
18 |
|
---|
19 | id1 = PartyId("party1")
|
---|
20 | id2 = PartyId("party2")
|
---|
21 |
|
---|
22 | asJson = "{\"Settings\":"\
|
---|
23 | + "{\"id\":\"party1\",\"profile\":\"ws:profile1\","\
|
---|
24 | + "\"protocol\":\"ws:localhost/protocol1\","\
|
---|
25 | + "\"progress\":{\"ProgressRounds\":{\"duration\":10,\"currentRound\":0,\"endtime\":999}},"\
|
---|
26 | + "\"parameters\":{}}}"
|
---|
27 |
|
---|
28 | date = datetime.fromtimestamp(999/1000.)
|
---|
29 | jackson = ObjectMapper()
|
---|
30 |
|
---|
31 | # we can't mock because we want to test the serializer
|
---|
32 | profile1 = ProfileRef( URI("ws:profile1"))
|
---|
33 | # jackson.readValue(serialized, Profile.class);
|
---|
34 |
|
---|
35 | protocol1 = ProtocolRef(URI("ws:localhost/protocol1"))
|
---|
36 | protocol1a = ProtocolRef(URI("ws:localhost/protocol1"))
|
---|
37 | protocol2 = ProtocolRef(URI("ws:localhost/protocol2"))
|
---|
38 | progress1 = ProgressRounds(10, 0, date)
|
---|
39 | progress1a = ProgressRounds(10, 0, date)
|
---|
40 | progress2 = ProgressRounds(12, 0, date)
|
---|
41 |
|
---|
42 | settings1 = Parameters()
|
---|
43 | settings2 = Parameters().With("a", 1)
|
---|
44 |
|
---|
45 | negoinfo1 = Settings(id1, profile1, protocol1, progress1, settings1)
|
---|
46 | negoinfo1a = Settings(id1, profile1, protocol1a, progress1a,settings1)
|
---|
47 | negoinfo2 = Settings(id1, profile1, protocol2, progress1, settings1)
|
---|
48 | negoinfo3 = Settings(id1, profile1, protocol1, progress2, settings1)
|
---|
49 | negoinfo4 = Settings(id2, profile1, protocol1, progress2, settings1)
|
---|
50 | negoinfo5 = Settings(id1, profile1, protocol1, progress1, settings2)
|
---|
51 |
|
---|
52 | def getGeneralTestData(self)-> List[List[Settings]] :
|
---|
53 | return [[self.negoinfo1, self.negoinfo1a],
|
---|
54 | [self.negoinfo2], [self.negoinfo3],
|
---|
55 | [self.negoinfo4], [self.negoinfo5]]
|
---|
56 |
|
---|
57 | def getGeneralTestStrings(self) -> List[str]:
|
---|
58 | return [
|
---|
59 | "Settings.*party1.*ProtocolRef.*protocol1.*ProgressRounds.*0.*10.*",
|
---|
60 | "Settings.*party1.*ProtocolRef.*protocol2.*ProgressRounds.*0.*10.*",
|
---|
61 | "Settings.*party1.*ProtocolRef.*protocol1.*ProgressRounds.*0.*12.*",
|
---|
62 | "Settings.*party2.*ProtocolRef.*protocol1.*ProgressRounds.*0.*12.*",
|
---|
63 | "Settings.*party1.*ProtocolRef.*protocol1.*ProgressRounds.*0.*10.*"]
|
---|
64 |
|
---|
65 | def testSmoke(self):
|
---|
66 | PartyRef(URI("ws:localhost"))
|
---|
67 |
|
---|
68 | def testNull(self):
|
---|
69 | self.assertRaises(ValueError,lambda: PartyRef(None))
|
---|
70 |
|
---|
71 | def testSerialize(self):
|
---|
72 | json1 = self.jackson.toJson(self.negoinfo1)
|
---|
73 | print(json1)
|
---|
74 | self.assertEqual(json.loads(self.asJson), json1)
|
---|
75 |
|
---|
76 | def testDeserialize(self):
|
---|
77 | p = self.jackson.parse(json.loads(self.asJson), Inform)
|
---|
78 | print(p)
|
---|
79 | self.assertEqual(self.negoinfo1, p)
|
---|
80 |
|
---|
81 | def testGeneralDict(self):
|
---|
82 | val = self.jackson.parse(json.loads("{\"a\":0.3,\"b\":{\"x\":3},\"c\":[1,2,3]}"),
|
---|
83 | Dict[str,Any])
|
---|
84 | print(val)
|
---|