1
|
|
|
'''# test json message format |
2
|
|
|
# scenarios: |
3
|
|
|
# - Miner with miner stats |
4
|
|
|
# - Miner with command |
5
|
|
|
# - command by itself |
6
|
|
|
# - sensor |
7
|
|
|
''' |
8
|
|
|
import unittest |
9
|
|
|
import datetime |
10
|
|
|
import domain.minerstatistics |
11
|
|
|
from domain.mining import Miner, MinerStatus, MinerInfo, MinerCurrentPool, MinerCommand, Pool, AvailablePool |
12
|
|
|
from domain.sensors import SensorValue, Sensor |
13
|
|
|
import messaging.messages |
14
|
|
|
import messaging.sensormessages |
15
|
|
|
import messaging.schema |
16
|
|
|
from messaging.sensormessages import SensorValueSchema |
17
|
|
|
from backend.fcmapp import ApplicationService |
18
|
|
|
import backend.fcmutils as utils |
19
|
|
|
|
20
|
|
|
class TestSerialization(unittest.TestCase): |
21
|
|
|
def test_sensors(self): |
22
|
|
|
msg = messaging.sensormessages.SensorValueMessage('','','') |
23
|
|
|
self.assertTrue(msg) |
24
|
|
|
msg = messaging.sensormessages.SensorMessage('','','') |
25
|
|
|
self.assertTrue(msg) |
26
|
|
|
|
27
|
|
|
def test_messages(self): |
28
|
|
|
msg = messaging.messages.MinerCommandMessage() |
29
|
|
|
|
30
|
|
|
def test_minercommand(self): |
31
|
|
|
sch = messaging.schema.MinerCommandSchema() |
32
|
|
|
cmd = MinerCommand() |
33
|
|
|
j = sch.dumps(cmd).data |
34
|
|
|
recommand = sch.loads(j).data |
35
|
|
|
self.assertTrue(isinstance(recommand, MinerCommand)) |
36
|
|
|
|
37
|
|
|
def test_minermessage(self): |
38
|
|
|
sch = messaging.messages.MinerMessageSchema() |
39
|
|
|
entity = messaging.messages.MinerMessage(Miner('test')) |
40
|
|
|
entity.command = MinerCommand('test','test') |
41
|
|
|
entity.minerpool = MinerCurrentPool(entity.miner, 'test pool', 'test worker', allpools={}) |
42
|
|
|
entity.minerstats = domain.minerstatistics.MinerStatistics(entity.miner, datetime.datetime.utcnow(), 0, 1, 0, 99, 98, 97, 123, '', '', '') |
43
|
|
|
j = sch.dumps(entity).data |
44
|
|
|
reentity = sch.loads(j).data |
45
|
|
|
self.assertTrue(isinstance(reentity, messaging.messages.MinerMessage)) |
46
|
|
|
|
47
|
|
|
def test_pool(self): |
48
|
|
|
sch = messaging.schema.PoolSchema() |
49
|
|
|
entity = Pool('','','','',1,'') |
50
|
|
|
j = sch.dumps(entity).data |
51
|
|
|
reentity = sch.loads(j).data |
52
|
|
|
self.assertTrue(isinstance(reentity, Pool)) |
53
|
|
|
|
54
|
|
|
def test_availablepool(self): |
55
|
|
|
sch = messaging.schema.AvailablePoolSchema() |
56
|
|
|
entity = AvailablePool('type') |
57
|
|
|
j = sch.dumps(entity).data |
58
|
|
|
reentity = sch.loads(j).data |
59
|
|
|
self.assertTrue(isinstance(reentity, AvailablePool)) |
60
|
|
|
|
61
|
|
|
def test_minerserialization(self): |
62
|
|
|
sch = messaging.messages.MinerSchema() |
63
|
|
|
miner = Miner('test') |
64
|
|
|
miner.status = MinerStatus.Offline |
65
|
|
|
miner.status = MinerStatus.Online |
66
|
|
|
miner.minerinfo = MinerInfo('Antminer S9', '123') |
67
|
|
|
miner.minerpool = MinerCurrentPool(miner, 'test pool', 'test worker', allpools={}) |
68
|
|
|
miner.minerpool.poolname = 'unittest' |
69
|
|
|
miner.minerstats = domain.minerstatistics.MinerStatistics(miner, datetime.datetime.utcnow(), 0, 1, 0, 99, 98, 97, 123, '', '', '') |
70
|
|
|
miner.minerstats.boardstatus1 = 'o' |
71
|
|
|
miner.minerstats.boardstatus2 = 'oo' |
72
|
|
|
miner.minerstats.boardstatus3 = 'ooo' |
73
|
|
|
miner.location = 'unittest' |
74
|
|
|
miner.in_service_date = datetime.datetime.today().replace(microsecond=0) |
75
|
|
|
jminer = sch.dumps(miner).data |
76
|
|
|
|
77
|
|
|
#rehydrate miner |
78
|
|
|
reminer = messaging.messages.MinerSchema().loads(jminer).data |
79
|
|
|
self.assertTrue(isinstance(reminer.minerinfo, MinerInfo)) |
80
|
|
|
self.assertTrue(isinstance(reminer.minerpool, MinerCurrentPool)) |
81
|
|
|
self.assertTrue(reminer.minerpool.poolname == 'unittest') |
82
|
|
|
self.assertTrue(isinstance(reminer.minerstats, domain.minerstatistics.MinerStatistics)) |
83
|
|
|
self.assertTrue(reminer.laststatuschanged) |
84
|
|
|
self.assertTrue(reminer.minerstats.boardstatus1 == 'o') |
85
|
|
|
self.assertTrue(reminer.minerstats.boardstatus2 == 'oo') |
86
|
|
|
self.assertTrue(reminer.minerstats.boardstatus3 == 'ooo') |
87
|
|
|
self.assertTrue(reminer.location == miner.location) |
88
|
|
|
self.assertTrue(reminer.in_service_date == miner.in_service_date) |
89
|
|
|
|
90
|
|
|
def test_miner_deserialize(self): |
91
|
|
|
miner = Miner("unittest", None, "", "unitip", "unitport", "", "") |
92
|
|
|
jminer = utils.serialize(miner, messaging.messages.MinerSchema()) |
93
|
|
|
reminer = utils.deserialize(messaging.messages.MinerSchema(),jminer) #().loads(jminer).data |
94
|
|
|
self.assertTrue(isinstance(reminer, Miner),"object from MinerSchema should be a miner") |
95
|
|
|
|
96
|
|
|
def test_sensorvalue_serialization(self): |
97
|
|
|
'''on windows the deserialization seems to lose the fractions of seconds |
98
|
|
|
so this test is only for seconds''' |
99
|
|
|
sch = SensorValueSchema() |
100
|
|
|
sensorvalue = SensorValue('testid', '99.9', 'temperature') |
101
|
|
|
sensorvalue.sensor = Sensor('testid', 'temperature', 'controller') |
102
|
|
|
sensortime = sensorvalue.sensortime |
103
|
|
|
jsensor = sch.dumps(sensorvalue).data |
104
|
|
|
|
105
|
|
|
#rehydrate sensor |
106
|
|
|
resensor = SensorValueSchema().loads(jsensor).data |
107
|
|
|
self.assertTrue(isinstance(resensor, SensorValue)) |
108
|
|
|
self.assertTrue(resensor.sensortime.day == sensortime.day) |
109
|
|
|
self.assertTrue(resensor.sensortime.hour == sensortime.hour) |
110
|
|
|
self.assertTrue(resensor.sensortime.minute == sensortime.minute) |
111
|
|
|
self.assertTrue(resensor.sensortime.second == sensortime.second) |
112
|
|
|
self.assertTrue(isinstance(resensor.sensor, Sensor)) |
113
|
|
|
self.assertTrue(resensor.sensorid == resensor.sensor.sensorid) |
114
|
|
|
|
115
|
|
|
def test_config_serialization(self): |
116
|
|
|
sch = messaging.messages.ConfigurationMessageSchema() |
117
|
|
|
msg = messaging.messages.ConfigurationMessage('save', '', 'pool', {"configuration_message_id":"name"}, [{"name":"my pool"}]) |
118
|
|
|
jconfig = sch.dumps(msg).data |
119
|
|
|
reconfig = sch.loads(jconfig).data |
120
|
|
|
self.assertTrue(isinstance(reconfig, messaging.messages.ConfigurationMessage)) |
121
|
|
|
self.assertTrue(isinstance(reconfig.command, str)) |
122
|
|
|
self.assertTrue(isinstance(reconfig.parameter, str)) |
123
|
|
|
self.assertTrue(isinstance(reconfig.configuration_message_id, dict)) |
124
|
|
|
self.assertTrue(isinstance(reconfig.values, list)) |
125
|
|
|
|
126
|
|
|
def test_message_inapp(self): |
127
|
|
|
app = ApplicationService(component='test') |
128
|
|
|
values = '{"version":"1.1","sender":"fullcyclereact","type":"configuration","timestamp":"2018-09-16T07:18:34.431Z","body":"{\\"command\\":\\"save\\",\\"parameter\\":\\"\\",\\"id\\":\\"unknown\\",\\"entity\\":\\"miner\\",\\"values\\":[{\\"name\\":\\"S9102\\"},{\\"ipaddress\\":\\"test.com\\"},{\\"port\\":\\"4102\\"},{\\"location\\":\\"222\\"},{\\"in_service_date\\":null}]}"}' |
129
|
|
|
msg = app.messagedecode_configuration(values) |
130
|
|
|
self.assertTrue(isinstance(msg, messaging.messages.ConfigurationMessage)) |
131
|
|
|
self.assertTrue(msg.entity == 'miner') |
132
|
|
|
miner = Miner.create(msg.values) |
133
|
|
|
self.assertTrue(miner.name == "S9102") |
134
|
|
|
|
135
|
|
|
if __name__ == '__main__': |
136
|
|
|
unittest.main() |
137
|
|
|
|