Skip to content

Commit 67e8c65

Browse files
Making test_base and test_ieee work with new Fake Serial class
* Replaced the previous FakeDevice and FakeReadDevice with the new fake Serial class. * Use of get_data_written(). * Modify BadReadDevice accordingly. * Adding default second argument 'length' to BadReadDevice(). * Removed test_read_too_many() test as no longer required.
1 parent f5fb4c3 commit 67e8c65

2 files changed

Lines changed: 56 additions & 49 deletions

File tree

xbee/tests/test_base.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"""
1010
import unittest
1111
from xbee.base import XBeeBase
12-
from xbee.tests.Fake import FakeDevice, FakeReadDevice
12+
from xbee.tests.Fake import Serial
1313

1414
class TestWriteToDevice(unittest.TestCase):
1515
"""
@@ -22,42 +22,45 @@ def test_write(self):
2222
_write method should write the expected data to the serial
2323
device
2424
"""
25-
device = FakeDevice()
25+
device = Serial()
2626

2727
xbee = XBeeBase(device)
2828
xbee._write(b'\x00')
2929

3030
# Check resuting state of fake device
31+
result_frame = device.get_data_written()
3132
expected_frame = b'\x7E\x00\x01\x00\xFF'
32-
self.assertEqual(device.data, expected_frame)
33+
self.assertEqual(result_frame, expected_frame)
3334

3435
def test_write_again(self):
3536
"""
3637
_write method should write the expected data to the serial
3738
device
3839
"""
39-
device = FakeDevice()
40+
device = Serial()
4041

4142
xbee = XBeeBase(device)
4243
xbee._write(b'\x00\x01\x02')
4344

4445
# Check resuting state of fake device
4546
expected_frame = b'\x7E\x00\x03\x00\x01\x02\xFC'
46-
self.assertEqual(device.data, expected_frame)
47+
result_frame = device.get_data_written()
48+
self.assertEqual(result_frame, expected_frame)
4749

4850
def test_write_escaped(self):
4951
"""
5052
_write method should write the expected data to the serial
5153
device
5254
"""
53-
device = FakeDevice()
55+
device = Serial()
5456

5557
xbee = XBeeBase(device,escaped=True)
5658
xbee._write(b'\x7E\x01\x7D\x11\x13')
5759

5860
# Check resuting state of fake device
5961
expected_frame = b'\x7E\x00\x05\x7D\x5E\x01\x7D\x5D\x7D\x31\x7D\x33\xDF'
60-
self.assertEqual(device.data, expected_frame)
62+
result_frame = device.get_data_written()
63+
self.assertEqual(result_frame, expected_frame)
6164

6265
class TestReadFromDevice(unittest.TestCase):
6366
"""
@@ -68,7 +71,8 @@ def test_read(self):
6871
"""
6972
_wait_for_frame should properly read a frame of data
7073
"""
71-
device = FakeReadDevice(b'\x7E\x00\x01\x00\xFF')
74+
device = Serial()
75+
device.set_read_data(b'\x7E\x00\x01\x00\xFF')
7276
xbee = XBeeBase(device)
7377

7478
frame = xbee._wait_for_frame()
@@ -78,8 +82,8 @@ def test_read_invalid_followed_by_valid(self):
7882
"""
7983
_wait_for_frame should skip invalid data
8084
"""
81-
device = FakeReadDevice(
82-
b'\x7E\x00\x01\x00\xFA' + b'\x7E\x00\x01\x05\xFA')
85+
device = Serial()
86+
device.set_read_data(b'\x7E\x00\x01\x00\xFA' + b'\x7E\x00\x01\x05\xFA')
8387
xbee = XBeeBase(device)
8488

8589
frame = xbee._wait_for_frame()
@@ -90,7 +94,8 @@ def test_read_escaped(self):
9094
_wait_for_frame should properly read a frame of data
9195
Verify that API mode 2 escaped bytes are read correctly
9296
"""
93-
device = FakeReadDevice(b'\x7E\x00\x04\x7D\x5E\x7D\x5D\x7D\x31\x7D\x33\xE0')
97+
device = Serial()
98+
device.set_read_data(b'\x7E\x00\x04\x7D\x5E\x7D\x5D\x7D\x31\x7D\x33\xE0')
9499

95100
xbee = XBeeBase(device,escaped=True)
96101

@@ -150,7 +155,7 @@ class TestAsyncCallback(unittest.TestCase):
150155

151156
def setUp(self):
152157
self.xbee = None
153-
self.serial = FakeReadDevice([], silent_on_empty=True)
158+
self.serial = Serial()
154159
self.callback = lambda data: None
155160
self.error_callback = lambda data: None
156161

xbee/tests/test_ieee.py

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
Tests the XBee (IEEE 802.15.4) implementation class for XBee API compliance
99
"""
1010
import unittest, sys, traceback
11-
from xbee.tests.Fake import FakeDevice, FakeReadDevice
11+
from xbee.tests.Fake import Serial
1212
from xbee.ieee import XBee
1313
from xbee.frame import APIFrame
1414
from xbee.python2to3 import byteToInt, intToByte, stringToBytes
@@ -443,15 +443,16 @@ def test_send_at_command(self):
443443
API AT command packet to the serial device.
444444
"""
445445

446-
serial_port = FakeDevice()
446+
serial_port = Serial()
447447
xbee = XBee(serial_port)
448448

449449
# Send an AT command
450450
xbee.send('at', frame_id=stringToBytes('A'), command=stringToBytes('MY'))
451451

452452
# Expect a full packet to be written to the device
453453
expected_data = b'\x7E\x00\x04\x08AMY\x10'
454-
self.assertEqual(serial_port.data, expected_data)
454+
result_data = serial_port.get_data_written()
455+
self.assertEqual(result_data, expected_data)
455456

456457

457458
def test_send_at_command_with_param(self):
@@ -460,7 +461,7 @@ def test_send_at_command_with_param(self):
460461
API AT command packet to the serial device.
461462
"""
462463

463-
serial_port = FakeDevice()
464+
serial_port = Serial()
464465
xbee = XBee(serial_port)
465466

466467
# Send an AT command
@@ -472,8 +473,9 @@ def test_send_at_command_with_param(self):
472473
)
473474

474475
# Expect a full packet to be written to the device
476+
result_data = serial_port.get_data_written()
475477
expected_data = b'\x7E\x00\x06\x08AMY\x00\x00\x10'
476-
self.assertEqual(serial_port.data, expected_data)
478+
self.assertEqual(result_data, expected_data)
477479

478480
class TestSendShorthand(unittest.TestCase):
479481
"""
@@ -485,7 +487,7 @@ def setUp(self):
485487
"""
486488
Prepare a fake device to read from
487489
"""
488-
self.ser = FakeDevice()
490+
self.ser = Serial()
489491
self.xbee = XBee(self.ser)
490492

491493
def test_send_at_command(self):
@@ -496,30 +498,33 @@ def test_send_at_command(self):
496498
self.xbee.at(frame_id=stringToBytes('A'), command=stringToBytes('MY'))
497499

498500
# Expect a full packet to be written to the device
501+
result_data = self.ser.get_data_written()
499502
expected_data = b'\x7E\x00\x04\x08AMY\x10'
500-
self.assertEqual(self.ser.data, expected_data)
503+
self.assertEqual(result_data, expected_data)
501504

502505
def test_send_at_command_with_param(self):
503506
"""
504507
calling send should write a full API frame containing the
505508
API AT command packet to the serial device.
506509
"""
507-
510+
508511
# Send an AT command
509512
self.xbee.at(frame_id=stringToBytes('A'), command=stringToBytes('MY'), parameter=b'\x00\x00')
510513

511514
# Expect a full packet to be written to the device
515+
result_data = self.ser.get_data_written()
512516
expected_data = b'\x7E\x00\x06\x08AMY\x00\x00\x10'
513-
self.assertEqual(self.ser.data, expected_data)
517+
self.assertEqual(result_data, expected_data)
514518

515519
def test_send_tx_with_close_brace(self):
516520
"""
517521
Calling tx where the given data string includes a close brace '}'
518522
must write correctly.
519523
"""
520524
self.xbee.tx(dest_addr=b'\x01\x02',data=b'{test=1}')
525+
result_data = self.ser.get_data_written()
521526
expected_data = b'\x7E\x00\x0D\x01\x00\x01\x02\x00{test=1}\xD5'
522-
self.assertEqual(self.ser.data, expected_data)
527+
self.assertEqual(result_data, expected_data)
523528

524529
def test_shorthand_disabled(self):
525530
"""
@@ -544,7 +549,8 @@ def test_read_at(self):
544549
"""
545550
read and parse a parameterless AT command
546551
"""
547-
device = FakeReadDevice(b'\x7E\x00\x05\x88DMY\x01\x8c')
552+
device = Serial()
553+
device.set_read_data(b'\x7E\x00\x05\x88DMY\x01\x8c')
548554
xbee = XBee(device)
549555

550556
info = xbee.wait_read_frame()
@@ -558,9 +564,8 @@ def test_read_at_params(self):
558564
"""
559565
read and parse an AT command with a parameter
560566
"""
561-
device = FakeReadDevice(
562-
b'\x7E\x00\x08\x88DMY\x01\x00\x00\x00\x8c'
563-
)
567+
device = Serial()
568+
device.set_read_data(b'\x7E\x00\x08\x88DMY\x01\x00\x00\x00\x8c')
564569
xbee = XBee(device)
565570

566571
info = xbee.wait_read_frame()
@@ -585,10 +590,8 @@ def test_is_response_parsed_as_io(self):
585590
sample = b'\x00\xAA\x00\xFF'
586591
data = header + sample
587592

588-
device = FakeReadDevice(
589-
APIFrame(data = b'\x88DIS\x00' + data).output()
590-
)
591-
593+
device = Serial()
594+
device.set_read_data(APIFrame(data = b'\x88DIS\x00' + data).output());
592595
xbee = XBee(device)
593596

594597
info = xbee.wait_read_frame()
@@ -616,10 +619,9 @@ def test_is_remote_response_parsed_as_io(self):
616619
# ADC0 value of 255
617620
sample = b'\x00\xAA\x00\xFF'
618621
data = header + sample
619-
620-
device = FakeReadDevice(
621-
APIFrame(data = b'\x97D\x00\x13\xa2\x00@oG\xe4v\x1aIS\x00' + data).output()
622-
)
622+
623+
device = Serial()
624+
device.set_read_data(APIFrame(data = b'\x97D\x00\x13\xa2\x00@oG\xe4v\x1aIS\x00' + data).output())
623625

624626
xbee = XBee(device)
625627

@@ -655,9 +657,8 @@ def test_read_io_data(self):
655657
# RX frame data
656658
rx_io_resp = b'\x83\x00\x01\x28\x00'
657659

658-
device = FakeReadDevice(
659-
b'\x7E\x00\x0C'+ rx_io_resp + data + b'\xfd'
660-
)
660+
device = Serial()
661+
device.set_read_data(b'\x7E\x00\x0C'+ rx_io_resp + data + b'\xfd')
661662
xbee = XBee(device)
662663

663664
info = xbee.wait_read_frame()
@@ -681,16 +682,17 @@ def test_read_empty_string(self):
681682
an empty string. In this event, we must not crash.
682683
"""
683684

684-
class BadReadDevice(FakeReadDevice):
685+
class BadReadDevice(Serial):
685686
def __init__(self, bad_read_index, data):
686687
self.read_id = 0
687688
self.bad_read_index = bad_read_index
688-
super(BadReadDevice, self).__init__(data)
689+
super(BadReadDevice, self).__init__()
690+
self.set_read_data(data)
689691

690-
def inWaiting(self):
692+
def in_waiting(self):
691693
return 1
692694

693-
def read(self):
695+
def read(self, length=1):
694696
if self.read_id == self.bad_read_index:
695697
self.read_id += 1
696698
return ''
@@ -711,9 +713,8 @@ def test_read_at_params_in_escaped_mode(self):
711713
"""
712714
read and parse an AT command with a parameter in escaped API mode
713715
"""
714-
device = FakeReadDevice(
715-
b'~\x00\t\x88DMY\x01}^}]}1}3m'
716-
)
716+
device = Serial()
717+
device.set_read_data(b'~\x00\t\x88DMY\x01}^}]}1}3m')
717718
xbee = XBee(device, escaped=True)
718719

719720
info = xbee.wait_read_frame()
@@ -728,7 +729,8 @@ def test_empty_frame_ignored(self):
728729
"""
729730
If an empty frame is received from a device, it must be ignored.
730731
"""
731-
device = FakeReadDevice(b'\x7E\x00\x00\xFF\x7E\x00\x05\x88DMY\x01\x8c')
732+
device = Serial()
733+
device.set_read_data(b'\x7E\x00\x00\xFF\x7E\x00\x05\x88DMY\x01\x8c')
732734
xbee = XBee(device)
733735

734736
#import pdb
@@ -744,7 +746,8 @@ def test_read_rx_with_close_brace(self):
744746
"""
745747
An rx data frame including a close brace must be read properly.
746748
"""
747-
device = FakeReadDevice(APIFrame(b'\x81\x01\x02\x55\x00{test=1}').output())
749+
device = Serial()
750+
device.set_read_data(APIFrame(b'\x81\x01\x02\x55\x00{test=1}').output())
748751
xbee = XBee(device)
749752

750753
info = xbee.wait_read_frame()
@@ -759,9 +762,8 @@ def test_read_rx_with_close_brace_escaped(self):
759762
"""
760763
An escaped rx data frame including a close brace must be read properly.
761764
"""
762-
device = FakeReadDevice(
763-
APIFrame(b'\x81\x01\x02\x55\x00{test=1}', escaped=True).output()
764-
)
765+
device = Serial()
766+
device.set_read_data(APIFrame(b'\x81\x01\x02\x55\x00{test=1}', escaped=True).output())
765767
xbee = XBee(device, escaped=True)
766768

767769
info = xbee.wait_read_frame()

0 commit comments

Comments
 (0)