source: framspy/gui/tests/socketTest.py @ 1198

Last change on this file since 1198 was 1198, checked in by Maciej Komosinski, 15 months ago

Added simple Python GUI for Framsticks library/server

File size: 5.2 KB
Line 
1import unittest
2
3import os
4import sys
5import inspect
6
7currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
8parentdir = os.path.dirname(currentdir)
9parentdir = os.path.dirname(parentdir)
10sys.path.insert(0, parentdir)
11
12from gui.framsutils.FramsSocket import FramsSocket
13
14class SocketTest(unittest.TestCase):
15    # Make sure to launch an empty server, without any running jobs etc.
16    ADDRESS = "127.0.0.1"
17    PORT = 9009
18
19    def test_connection(self):
20        frams = FramsSocket()
21       
22        try:
23            frams.initConnection(self.ADDRESS, self.PORT)
24            self.assertTrue(frams.comm.client.connected, "Connection inner state")
25        except ConnectionError:
26            self.assertTrue(False, "ConnectionError exception")
27
28        frams.closeConnection()
29
30    def test_communication_parsing1(self):
31        frams = FramsSocket()
32        frams.initConnection(self.ADDRESS, self.PORT)
33
34        response = frams.sendRequest("get / motd")
35        data = frams._infoParser(response)
36        frams.closeConnection()
37
38        self.assertEqual(len(data), 1, "Response is to short")
39        self.assertEqual(data[0].p["_classname"], "Server")
40        self.assertIn("motd", data[0].p) #check if contains motd field
41        self.assertIsInstance(data[0].p["motd"], str) #check if response have a corect type
42
43    def test_communication_parsing2(self):
44        frams = FramsSocket()
45        frams.initConnection(self.ADDRESS, self.PORT)
46
47        response = frams.sendRequest("get /simulator{version_string,version_int}")
48        data = frams._infoParser(response)
49        frams.closeConnection()
50
51        self.assertEqual(len(data), 1, "Response is to short")
52        self.assertEqual(data[0].p["_classname"], "Simulator")
53        self.assertIn("version_string", data[0].p) #check if contains motd field
54        self.assertIsInstance(data[0].p["version_string"], str) #check if response have a corect type
55        self.assertIn("version_int", data[0].p) #check if contains motd field
56        self.assertIsInstance(data[0].p["version_int"], int) #check if response have a corect type
57
58    def test_genotype_mod(self):
59        frams = FramsSocket()
60        frams.initConnection(self.ADDRESS, self.PORT)
61
62        genotype = "X"
63        info = "test info"
64
65        frams.sendRequest("call /simulator/genepools/groups/0 clear")
66        frams.sendRequest("call /simulator/genepools/groups/0 add {}".format(genotype))
67        genotypeInfo = frams.readGenotypeInfo(0, 0)
68        prop = next((p for p in genotypeInfo if p.p["id"] == "genotype"), None)
69        self.assertTrue(prop, "genotype property not found")
70        self.assertEqual(prop.p["value"], genotype, "genotype not match")
71       
72        frams.sendRequest("set /simulator/genepools/groups/0/genotypes/0 info \"{}\"".format(info))
73        genotypeInfo = frams.readGenotypeInfo(0, 0)
74        prop = next((p for p in genotypeInfo if p.p["id"] == "info"), None)
75        self.assertTrue(prop, "info property not found")
76        self.assertEqual(prop.p["value"], info, "info not match")
77
78        frams.sendRequest("call /simulator/genepools/groups/0/genotypes/0 delete")
79
80        frams.closeConnection()
81
82    def test_multiline_strings(self):
83        frams = FramsSocket()
84        frams.initConnection(self.ADDRESS, self.PORT)
85
86        info = r"test\ntest2"
87        infoResponse = "test\ntest2"
88
89        #prepare known multiline field
90        frams.sendRequest("call /simulator/genepools/groups/0 clear")
91        frams.sendRequest("call /simulator/genepools/groups/0 add {}".format("X"))
92
93        #set and get multiline field
94        frams.sendRequest("set /simulator/genepools/groups/0/genotypes/0 info \"{}\"".format(info))
95        genotypeInfo = frams.readGenotypeInfo(0, 0)
96        prop = next((p for p in genotypeInfo if p.p["id"] == "info"), None)
97        self.assertTrue(prop, "info property not found")
98        self.assertMultiLineEqual(prop.p["value"], infoResponse, "multiline string not match")
99
100        frams.sendRequest("call /simulator/genepools/groups/0/genotypes/0 delete")
101
102        frams.closeConnection()
103
104    def test_multiline_string_encoding(self):
105        import gui.framsutils.framsProperty as framsProperty
106
107        frams = FramsSocket()
108        frams.initConnection(self.ADDRESS, self.PORT)
109
110        #prepare known multiline field
111        frams.sendRequest("call /simulator/genepools/groups/0 clear")
112        frams.sendRequest("call /simulator/genepools/groups/0 add {}".format("X"))
113
114        info = \
115r"Long multiline text\
1161\\~\
11722\~\n\
118333\ \\\
1194444"
120        encodedInfo = framsProperty.encodeString(info)
121
122        frams.sendRequest("set /simulator/genepools/groups/0/genotypes/0 info \"{}\"".format(encodedInfo))
123        genotypeInfo = frams.readGenotypeInfo(0, 0)
124        prop = next((p for p in genotypeInfo if p.p["id"] == "info"), None)
125        self.assertTrue(prop, "info property not found")
126        self.assertMultiLineEqual(prop.p["value"], info, "encoded string not match")
127
128        frams.sendRequest("call /simulator/genepools/groups/0/genotypes/0 delete")
129
130        frams.closeConnection()
131
132if __name__ == "__main__":
133    unittest.main()
Note: See TracBrowser for help on using the repository browser.