class TestFakeSerialRead(unittest.TestCase): """ Fake Serial class should work as intended to emluate reading from a serial port. """ def setUp(self): """ Create a fake read device for each test. """ self.device = Serial() self.device.set_read_data("test") def test_read_single_byte(self): """ Reading one byte at a time should work as expected. """ self.assertEqual(self.device.read(), 't') self.assertEqual(self.device.read(), 'e') self.assertEqual(self.device.read(), 's') self.assertEqual(self.device.read(), 't') def test_read_multiple_bytes(self): """ Reading multiple bytes at a time should work as expected. """ self.assertEqual(self.device.read(3), 'tes') self.assertEqual(self.device.read(), 't') def test_write(self): """ Test serial write function. """ self.device.write("Hello World") self.assertEqual(self.device.get_data_written(), "Hello World")
class TestFakeSerialRead(unittest.TestCase): """ Fake Serial class should work as intended to emluate reading from a serial port. """ def setUp(self): """ Create a fake read device for each test. """ self.device = Serial() self.device.set_read_data("test") def test_read_single_byte(self): """ Reading one byte at a time should work as expected. """ self.assertEqual(self.device.read(), 't') self.assertEqual(self.device.read(), 'e') self.assertEqual(self.device.read(), 's') self.assertEqual(self.device.read(), 't') def test_read_multiple_bytes(self): """ Reading multiple bytes at a time should work as expected. """ self.assertEqual(self.device.read(3), 'tes') self.assertEqual(self.device.read(), 't') def test_write(self): """ Test serial write function. """ self.device.write("Hello World") self.assertEqual(self.device.get_data_written(), "Hello World")
def test_send(self): """ Test send() with AT command. """ device = Serial() xbee = ZigBee(device) xbee.send('at', command='MY') result = device.get_data_written() expected = b'~\x00\x04\x08\x01MYP' self.assertEqual(result, expected)
def test_send(self): """ Test send() with AT command. """ device = Serial() xbee = ZigBee(device) xbee.send('at', command='MY') result = device.get_data_written() expected = b'~\x00\x04\x08\x01MYP' self.assertEqual(result, expected)
def test_write_escaped(self): """ _write method should write the expected data to the serial device """ device = Serial() xbee = XBeeBase(device,escaped=True) xbee._write(b'\x7E\x01\x7D\x11\x13') # Check resuting state of fake device expected_frame = b'\x7E\x00\x05\x7D\x5E\x01\x7D\x5D\x7D\x31\x7D\x33\xDF' result_frame = device.get_data_written() self.assertEqual(result_frame, expected_frame)
def test_write_again(self): """ _write method should write the expected data to the serial device """ device = Serial() xbee = XBeeBase(device) xbee._write(b'\x00\x01\x02') # Check resuting state of fake device expected_frame = b'\x7E\x00\x03\x00\x01\x02\xFC' result_frame = device.get_data_written() self.assertEqual(result_frame, expected_frame)
def test_send_at_command(self): """ calling send should write a full API frame containing the API AT command packet to the serial device. """ serial_port = Serial() xbee = XBee(serial_port) # Send an AT command xbee.send('at', frame_id=stringToBytes('A'), command=stringToBytes('MY')) # Expect a full packet to be written to the device expected_data = b'\x7E\x00\x04\x08AMY\x10' result_data = serial_port.get_data_written() self.assertEqual(result_data, expected_data)
def test_send_at_command(self): """ calling send should write a full API frame containing the API AT command packet to the serial device. """ serial_port = Serial() xbee = XBee(serial_port) # Send an AT command xbee.send('at', frame_id=stringToBytes('A'), command=stringToBytes('MY')) # Expect a full packet to be written to the device expected_data = b'\x7E\x00\x04\x08AMY\x10' result_data = serial_port.get_data_written() self.assertEqual(result_data, expected_data)
class TestSendShorthand(InitXBee): """ Tests shorthand for sending commands to an XBee provided by XBee.__getattr__ """ def setUp(self): """ Prepare a fake device to read from """ super(TestSendShorthand, self).setUp() self.ser = Serial() self.xbee = XBee(self.ser) def test_send_at_command(self): """ Send an AT command with a shorthand call """ # Send an AT command self.xbee.at(frame_id=stringToBytes('A'), command=stringToBytes('MY')) # Expect a full packet to be written to the device result_data = self.ser.get_data_written() expected_data = b'\x7E\x00\x04\x08AMY\x10' self.assertEqual(result_data, expected_data) def test_send_at_command_with_param(self): """ calling send should write a full API frame containing the API AT command packet to the serial device. """ # Send an AT command self.xbee.at(frame_id=stringToBytes('A'), command=stringToBytes('MY'), parameter=b'\x00\x00') # Expect a full packet to be written to the device result_data = self.ser.get_data_written() expected_data = b'\x7E\x00\x06\x08AMY\x00\x00\x10' self.assertEqual(result_data, expected_data) def test_send_tx_with_close_brace(self): """ Calling tx where the given data string includes a close brace '}' must write correctly. """ self.xbee.tx(dest_addr=b'\x01\x02', data=b'{test=1}') result_data = self.ser.get_data_written() expected_data = b'\x7E\x00\x0D\x01\x00\x01\x02\x00{test=1}\xD5' self.assertEqual(result_data, expected_data) def test_shorthand_disabled(self): """ When shorthand is disabled, any attempt at calling a non-existant attribute should raise AttributeError """ self.xbee = XBee(self.ser, shorthand=False) try: self.xbee.at except AttributeError: pass else: self.fail("Specified shorthand command should not exist")
class TestSendShorthand(unittest.TestCase): """ Tests shorthand for sending commands to an XBee provided by XBee.__getattr__ """ def setUp(self): """ Prepare a fake device to read from """ self.ser = Serial() self.xbee = XBee(self.ser) def test_send_at_command(self): """ Send an AT command with a shorthand call """ # Send an AT command self.xbee.at(frame_id=stringToBytes('A'), command=stringToBytes('MY')) # Expect a full packet to be written to the device result_data = self.ser.get_data_written() expected_data = b'\x7E\x00\x04\x08AMY\x10' self.assertEqual(result_data, expected_data) def test_send_at_command_with_param(self): """ calling send should write a full API frame containing the API AT command packet to the serial device. """ # Send an AT command self.xbee.at(frame_id=stringToBytes('A'), command=stringToBytes('MY'), parameter=b'\x00\x00') # Expect a full packet to be written to the device result_data = self.ser.get_data_written() expected_data = b'\x7E\x00\x06\x08AMY\x00\x00\x10' self.assertEqual(result_data, expected_data) def test_send_tx_with_close_brace(self): """ Calling tx where the given data string includes a close brace '}' must write correctly. """ self.xbee.tx(dest_addr=b'\x01\x02', data=b'{test=1}') result_data = self.ser.get_data_written() expected_data = b'\x7E\x00\x0D\x01\x00\x01\x02\x00{test=1}\xD5' self.assertEqual(result_data, expected_data) def test_shorthand_disabled(self): """ When shorthand is disabled, any attempt at calling a non-existant attribute should raise AttributeError """ self.xbee = XBee(self.ser, shorthand=False) try: self.xbee.at except AttributeError: pass else: self.fail("Specified shorthand command should not exist")