-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.py
140 lines (113 loc) · 4.42 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# pylint: disable-msg=R0201
"""
unit tests
"""
#import sys
import codecs
import datetime
import unittest
from mock import patch #, Mock, MagicMock
from hrp import HRP
from hrp.exception import HRPNetworkError
try:
unittest.TestCase.assertRaisesRegex
except AttributeError:
unittest.TestCase.assertRaisesRegex = unittest.TestCase.assertRaisesRegexp
BASIC_CONNECT = [ # up to reader_band_channels
#header: query_reader_info
codecs.decode('aa0100001b', 'hex'),
codecs.decode('000101100011434c3732303642325f3230313730363036000160424a14', 'hex'),
#5 byte header query_reader_ability
codecs.decode('AA0200000E', 'hex'),
codecs.decode('0024040005000102030400020001527C', 'hex'),
#reader band region
codecs.decode('aa02040001', 'hex'),
codecs.decode('03d6f9', 'hex'), # 3 = FCC
# reader band channels list
codecs.decode('aa0206001c', 'hex'),
codecs.decode('000019191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f30312673', 'hex'), # fixed
# reader power!
codecs.decode('aa02020004', 'hex'),
codecs.decode('011e021e3a10', 'hex'),
]
class HRPTest(unittest.TestCase):
""" HRP unit testing """
def setup(self):
""" init test """
pass
def tearDown(self):
""" clear test """
HRP.set_log_level()# reset
@patch('hrp.hrp.subprocess')
def test_incorrect_ping(self, subprocess):
"""
test for incorrect ping
"""
#HRP.set_log_level_debug()
subprocess.call.return_value = 1
conn = HRP('192.168.1.116')
self.assertRaisesRegex(HRPNetworkError, "can't reach reader", conn.connect)
@patch('hrp.hrp.socket')
@patch('hrp.hrp.subprocess')
def test_correct_ping(self, subprocess, socket):
"""
test for correct ping, but missing tcp connection
"""
#HRP.set_log_level_debug()
subprocess.call.return_value = 0
socket.return_value.connect_ex.return_value = 1
conn = HRP('192.168.1.116')
self.assertRaisesRegex(HRPNetworkError, "can't connect tcp reader", conn.connect)
@patch('hrp.hrp.socket')
@patch('hrp.hrp.subprocess')
def test_correct_tcp(self, subprocess, socket):
"""
Test for correct tcp connection
"""
#HRP.set_log_level_debug()
subprocess.call.return_value = 0
socket.return_value.connect_ex.return_value = 0
socket.return_value.recv.side_effect = BASIC_CONNECT
conn = HRP('192.168.1.116')
conn.connect()
subprocess.call.assert_called()
socket.return_value.connect_ex.assert_called_with(('192.168.1.116', 9090))
#don't check packets?
@patch('hrp.hrp.socket')
@patch('hrp.hrp.subprocess')
def test_correct_connect(self, subprocess, socket):
"""
test for correct tco and initial data reading
"""
#HRP.set_log_level_debug()
subprocess.call.return_value = 0
socket.return_value.connect_ex.return_value = 0
socket.return_value.recv.side_effect = BASIC_CONNECT
conn = HRP('192.168.1.116')
conn.connect() # parsed reader_info & reader_ability
self.assertEqual(conn.name, "CL7206B2_20170606", "incorrect name %s" % conn.name)
self.assertEqual(conn.version, "1.1.16", "incorrect version %s" % conn.version)
self.assertEqual(conn.deltalive, datetime.timedelta(seconds=90178), "incorrect live %s" % conn.deltalive)
self.assertEqual(conn.min_power, 0, "incorrect min_pow %s" % conn.min_power)
self.assertEqual(conn.max_power, 36, "incorrect max_pow %s" % conn.max_power)
self.assertEqual(conn.antennas, 4, "incorrect antennas %s" % conn.antennas)
@patch('hrp.hrp.socket')
@patch('hrp.hrp.subprocess')
def test_command_send(self, subprocess, socket):
"""
test send packets
"""
#HRP.set_log_level_debug()
subprocess.call.return_value = 0
socket.return_value.connect_ex.return_value = 0
socket.return_value.recv.side_effect = BASIC_CONNECT
conn = HRP('192.168.1.116')
conn.connect()
conn._send_packet(5, 5) # TEST,swr detect,""
socket.return_value.send.assert_called_with(codecs.decode("AA050500004444",'hex'))
conn._send_packet(2, 255) # OP, stop command
socket.return_value.send.assert_called_with(codecs.decode("AA02FF0000A40F",'hex'))
if __name__ == '__main__':
unittest.main()