def test_create_group_of_variables(self): """Test create group.""" resp_p = Package.package(0, Message.message(0xE0)) send_p = Package.package(1, Message.message(0x30, [chr(1), chr(3)])) self.serial.UART_request.return_value = resp_p.stream response = self.bsmp.create_group_of_variables([1, 3], timeout=100) self.serial.UART_request.assert_called_once_with(send_p.stream, timeout=100) self.assertEqual(response, (0xE0, None))
def test_remove_all_groups_of_variables(self): """Test remove_all_groups_of_variables.""" resp_p = Package.package(0, Message.message(0xE0)) send_p = Package.package(1, Message.message(0x32)) self.serial.UART_request.return_value = resp_p.stream response = self.bsmp.remove_all_groups_of_variables(timeout=100) self.serial.UART_request.assert_called_once_with(send_p.stream, timeout=100) self.assertEqual(response, (0xE0, None))
def test_execute_function(self): """Test execute_function.""" resp_p = Package.package(0, Message.message(0x51, payload=[chr(0)])) send_load = [chr(0x00)] + list(map(chr, struct.pack('<f', 1.5))) send_p = Package.package(1, Message.message(0x50, payload=send_load)) self.serial.UART_request.return_value = resp_p.stream response = self.bsmp.execute_function(0, 1.5) self.serial.UART_request.assert_called_once_with(send_p.stream, timeout=100) self.assertEqual(response, (0xE0, 0))
def test_read_int2_variable(self): """Test read_variable.""" load = list(map(chr, struct.pack('<h', 1020))) pck = Package.package(0, Message.message(0x11, payload=load)) self.serial.UART_request.return_value = pck.stream response = self.bsmp.read_variable(0, 100) self.assertEqual(response, (0xE0, 1020))
def test_read_calls_serial_method(self): """Test UART_read is called.""" response = Message.message(0x11, payload=[chr(10)]) self.serial.UART_read.return_value = \ Package.package(0x01, response).stream self.channel.read() self.serial.UART_read.assert_called_once()
def test_query_group_of_variables_fail(self): """Test query_group_of_variables return None when cmd is unexpected.""" p = Package.package(0, Message.message(0xE9)) self.serial.UART_request.return_value = p.stream # with self.assertRaises(TypeError): with self.assertRaises(SerialAnomResp): self.bsmp.query_group_of_variables(1, timeout=100)
def test_package(self): """Test constructor.""" address = 1 m = Message.message(0x00) p = Package.package(address=address, message=m) self.assertEqual(p.address, address) self.assertEqual(p.message, m)
def test_read(self): """Test read method.""" response = Message.message(0x11, payload=[chr(10)]) self.serial.UART_read.return_value = \ Package.package(0x01, response).stream recv = self.channel.read() self.assertEqual(recv.cmd, response.cmd) self.assertEqual(recv.payload, response.payload)
def test_create_group_of_variables_error(self): """Test create_group_of_variables.""" resp_p = Package.package(0, Message.message(0xE8)) self.serial.UART_request.return_value = resp_p.stream with self.assertRaises(TypeError): self.bsmp.query_group_of_variables(1) response = self.bsmp.create_group_of_variables([1, 3], timeout=100) self.assertEqual(response, (0xE8, None))
def _test_query_group_of_variables_error(self): """Test query_group_of_variables return error code.""" p = Package.package(0, Message.message(0xE3)) self.serial.UART_read.return_value = p.stream with self.assertRaises(TypeError): self.bsmp.query_group_of_variables(1) response = self.bsmp.query_group_of_variables(1, timeout=100) self.assertEqual(response, (0xE3, None))
def test_read_float_variable(self): """Test read_variable.""" load = list(map(chr, struct.pack('<f', 50.52))) pck = Package.package(0, Message.message(0x11, payload=load)) self.serial.UART_request.return_value = pck.stream response = self.bsmp.read_variable(1, 100) self.assertEqual(response[0], 0xE0) self.assertAlmostEqual(response[1], 50.52, places=2)
def test_package_stream(self): """Test constructor that creates object from stream.""" for d in self.data: stream = d[3] p = Package(stream) self.assertEqual(p.address, d[0]) self.assertEqual(p.message.cmd, d[1]) self.assertEqual(p.message.payload, d[2])
def test_remove_all_groups_of_variables_error(self): """Test remove_all_groups_of_variables.""" resp_p = Package.package(0, Message.message(0xE8)) self.serial.UART_request.return_value = resp_p.stream with self.assertRaises(TypeError): self.bsmp.remove_all_groups_of_variables() response = self.bsmp.remove_all_groups_of_variables(timeout=100) self.assertEqual(response, (0xE8, None))
def test_write_calls_serial_method(self): """Test write calls UART_write.""" message = Message.message(0x10, payload=[chr(1)]) expected_stream = \ Package.package(self.channel.address, message).stream self.channel.write(message, 1000) self.serial.UART_write.assert_called_with(expected_stream, timeout=1000)
def test_request(self): """Test request.""" response = Message.message(0x11, payload=[chr(10)]) self.serial.UART_request.return_value = \ Package.package(0x01, response).stream recv = self.channel.request(Message.message(0x01, payload=[chr(1)]), timeout=1) self.assertEqual(recv.cmd, response.cmd) self.assertEqual(recv.payload, response.payload)
def test_read_group_variable_fail(self): """Test read variable returns error code.""" pck = Package.package(0, Message.message(0xFF)) self.serial.UART_request.return_value = pck.stream with self.assertRaises(TypeError): # forget pylint: not having required timeout arg is the test self.bsmp.query_group_of_variables(1) with self.assertRaises(SerialAnomResp): self.bsmp.read_group_of_variables(0, timeout=100)
def test_read_string_variable(self): """Test read_variable.""" load = ['t', 'e', 's', 't', 'e'] while len(load) < 64: load.append(chr(0)) expected_value = [c.encode() for c in load] pck = Package.package(0, Message.message(0x11, payload=load)) self.serial.UART_request.return_value = pck.stream response = self.bsmp.read_variable(3, 100) self.assertEqual(response, (0xE0, expected_value))
def test_query_group_of_variables(self): """Test query_group_of_variables.""" p = Package.package( 0, Message.message(0x07, payload=[chr(0), chr(1), chr(2), chr(3)])) self.serial.UART_request.return_value = p.stream response = self.bsmp.query_group_of_variables(1, timeout=100) self.assertEqual(response, (0xE0, [0, 1, 2, 3]))
def test_read_arrfloat_variable(self): """Test read_variable.""" values = [1.1234567, 2.7654321] load = list(map(chr, struct.pack('<f', 1.1234567))) load.extend(list(map(chr, struct.pack('<f', 2.7654321)))) pck = Package.package(0, Message.message(0x11, payload=load)) self.serial.UART_request.return_value = pck.stream response = self.bsmp.read_variable(2, 100) self.assertEqual(response[0], 0xE0) for i, value in enumerate(response[1]): self.assertAlmostEqual(value, values[i], places=7)
def test_read_group_of_variables(self): """Test read_group_of_variables.""" ld_string = ['t', 'e', 's', 't', 'e'] for _ in range(64 - len(ld_string)): ld_string.append(chr(0)) val_string = [c.encode() for c in ld_string] values = [1020, 40.7654321, [1.7654321, 0.0123456], val_string] load = list(map(chr, struct.pack('<h', 1020))) load.extend(list(map(chr, struct.pack('<f', 40.7654321)))) load.extend(list(map(chr, struct.pack('<f', 1.7654321)))) load.extend(list(map(chr, struct.pack('<f', 0.0123456)))) load.extend(ld_string) pck = Package.package(0, Message.message(0x13, payload=load)) self.serial.UART_request.return_value = pck.stream response = self.bsmp.read_group_of_variables(0, timeout=100) self.assertEqual(response[0], 0xE0) self.assertEqual(response[1][0], values[0]) self.assertAlmostEqual(response[1][1], values[1], places=5) self.assertAlmostEqual(response[1][2][0], values[2][0]) self.assertAlmostEqual(response[1][2][1], values[2][1]) self.assertEqual(response[1][3], values[3])
def test_read_group_variable_error(self): """Test read variable returns error code.""" p = Package.package(0, Message.message(0xE3)) self.serial.UART_request.return_value = p.stream response = self.bsmp.read_group_of_variables(0, timeout=100) self.assertEqual(response, (0xE3, None))
def test_remove_all_groups_of_variables_fail(self): """Test remove_all_groups_of_variables.""" resp_p = Package.package(0, Message.message(0x11)) self.serial.UART_request.return_value = resp_p.stream with self.assertRaises(SerialAnomResp): self.bsmp.remove_all_groups_of_variables(timeout=100)
def test_parse_small_stream(self): """Test constructor that tries to parse strem smaller than 5.""" stream = ['\x02', '\x00', '\x00', chr(254)] with self.assertRaises(SerialErrPckgLen): Package(stream)
def test_verify_checksum(self): """Verify checksum sucessfully.""" for d in self.data: stream = d[3] self.assertTrue(Package.verify_checksum(stream))
def test_checksum(self): """Test checksum value.""" for d in self.data: p = Package.package(d[0], Message.message(d[1], payload=d[2])) self.assertEqual(p.checksum, d[4])
def test_create_group_of_variables_fail(self): """Test create_group_of_variables.""" resp_p = Package.package(0, Message.message(0xFF)) self.serial.UART_request.return_value = resp_p.stream with self.assertRaises(SerialAnomResp): self.bsmp.create_group_of_variables([1, 3], timeout=100)
def test_read_variable_failure(self): """Test read variable returns error code.""" pck = Package.package(0, Message.message(0xFF)) self.serial.UART_request.return_value = pck.stream with self.assertRaises(SerialAnomResp): self.bsmp.read_variable(1, 100)
def test_conv_to_stream(self): """Test conversion of package to stream.""" for d in self.data: p = Package.package(d[0], Message.message(d[1], payload=d[2])) self.assertEqual(p.stream, d[3])
def test_read_variable_error(self): """Test read variable returns error code.""" pck = Package.package(0, Message.message(0xE3)) self.serial.UART_request.return_value = pck.stream response = self.bsmp.read_variable(1, 100) self.assertEqual(response, (0xE3, None))
def test_verify_false_checksum(self): """Verify checksums fail.""" for d in self.data: stream, checksum = d[3][:-1], ord(d[3][-1]) stream += [chr(checksum + 1)] self.assertFalse(Package.verify_checksum(stream))