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_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_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_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_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_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_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_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_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_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_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_message_with_load(self): """Test constructor with load.""" m = Message.message(cmd=0x10, payload=[ 1, ]) self.assertEqual(m.cmd, 0x10) self.assertEqual(m.payload, [ 1, ])
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_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_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_conv_to_stream(self): """Test conversion from message to stream.""" # Example in 3.8.2 of BSMP protocol document curve_id = chr(0x07) blk_n = [chr(0x40), chr(0x00)] blk_load = [chr(0xdd) for _ in range(16384)] load = [curve_id] + blk_n + blk_load m = Message.message(cmd=0x41, payload=load) expected_stream = [chr(0x41), chr(0x40), chr(0x03)] + load self.assertEqual(m.stream, expected_stream)
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_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_message_with_extraneous_load(self): """Test constructor with loads that are not list.""" loads = [1, 'string', (1, 2, 3), 63.7] for load in loads: with self.assertRaises(TypeError): Message.message(cmd=0x10, payload=load)
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_request_fail(self): """Test exception is raised when serial fails.""" self.serial.UART_request.return_value = None with self.assertRaises(SerialError): self.channel.request(Message.message(0x10, payload=[chr(10)]))
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_message_with_no_load(self): """Test constructor with no load.""" m = Message.message(cmd=0x00) self.assertEqual(m.cmd, 0x00) self.assertEqual(m.payload, [])
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_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_message_with_big_load(self): """Test constructor with load bigger than 65535.""" load = [0 for _ in range(65536)] with self.assertRaises(ValueError): Message.message(cmd=0x10, payload=load)