@@ -46,111 +46,115 @@ def test_radio_init_channel():
46
46
assert r ._channel == 7 # pylint: disable=protected-access
47
47
48
48
49
- def test_radio_configure_channel (radio ):
49
+ def test_radio_configure_channel (radio_obj ):
50
50
"""
51
51
If a valid channel argument is passed to the configure method, the Radio
52
52
instance's channel is updated to reflect this.
53
53
"""
54
- assert radio ._channel == 42 # pylint: disable=protected-access
55
- radio .configure (channel = 7 )
56
- assert radio ._channel == 7 # pylint: disable=protected-access
54
+ assert radio_obj ._channel == 42 # pylint: disable=protected-access
55
+ radio_obj .configure (channel = 7 )
56
+ assert radio_obj ._channel == 7 # pylint: disable=protected-access
57
57
58
58
59
- def test_radio_configure_channel_out_of_bounds (radio ):
59
+ def test_radio_configure_channel_out_of_bounds (
60
+ radio_obj ,
61
+ ): # pylint: disable=invalid-name
60
62
"""
61
63
If a channel not in the range 0-255 is passed into the configure method,
62
64
then a ValueError exception is raised.
63
65
"""
64
66
with pytest .raises (ValueError ):
65
- radio .configure (channel = - 1 )
67
+ radio_obj .configure (channel = - 1 )
66
68
with pytest .raises (ValueError ):
67
- radio .configure (channel = 256 )
69
+ radio_obj .configure (channel = 256 )
68
70
# Add just-in-bounds checks too.
69
- radio .configure (channel = 0 )
70
- assert radio ._channel == 0 # pylint: disable=protected-access
71
- radio .configure (channel = 255 )
72
- assert radio ._channel == 255 # pylint: disable=protected-access
71
+ radio_obj .configure (channel = 0 )
72
+ assert radio_obj ._channel == 0 # pylint: disable=protected-access
73
+ radio_obj .configure (channel = 255 )
74
+ assert radio_obj ._channel == 255 # pylint: disable=protected-access
73
75
74
76
75
- def test_radio_send (radio ):
77
+ def test_radio_send (radio_obj ):
76
78
"""
77
79
The send method merely encodes to bytes and calls send_bytes.
78
80
"""
79
- radio .send_bytes = mock .MagicMock ()
81
+ radio_obj .send_bytes = mock .MagicMock ()
80
82
msg = "Testing 1, 2, 3..."
81
- radio .send (msg )
82
- radio .send_bytes .assert_called_once_with (msg .encode ("utf-8" ))
83
+ radio_obj .send (msg )
84
+ radio_obj .send_bytes .assert_called_once_with (msg .encode ("utf-8" ))
83
85
84
86
85
- def test_radio_send_bytes_too_long (radio ):
87
+ def test_radio_send_bytes_too_long (radio_obj ):
86
88
"""
87
89
A ValueError is raised if the message to be sent is too long (defined by
88
90
MAX_LENGTH).
89
91
"""
90
92
msg = bytes (adafruit_ble_radio .MAX_LENGTH + 1 )
91
93
with pytest .raises (ValueError ):
92
- radio .send_bytes (msg )
94
+ radio_obj .send_bytes (msg )
93
95
94
96
95
- def test_radio_send_bytes (radio ):
97
+ def test_radio_send_bytes (radio_obj ):
96
98
"""
97
99
Ensure the expected message is set on an instance of AdafruitRadio, and
98
100
broadcast for AD_DURATION period of time.
99
101
"""
100
- radio .uid = 255 # set up for cycle back to 0.
102
+ radio_obj .uid = 255 # set up for cycle back to 0.
101
103
msg = b"Hello"
102
104
with mock .patch ("adafruit_ble_radio.time.sleep" ) as mock_sleep :
103
- radio .send_bytes (msg )
105
+ radio_obj .send_bytes (msg )
104
106
mock_sleep .assert_called_once_with (adafruit_ble_radio .AD_DURATION )
105
107
spy_advertisement = (
106
- adafruit_ble_radio ._RadioAdvertisement ()
108
+ adafruit_ble_radio ._RadioAdvertisement () # pylint: disable=protected-access
107
109
) # pylint: disable=protected-access
108
- chan = struct .pack ("<B" , radio ._channel ) # pylint: disable=protected-access
110
+ chan = struct .pack ("<B" , radio_obj ._channel ) # pylint: disable=protected-access
109
111
uid = struct .pack ("<B" , 255 )
110
112
assert spy_advertisement .msg == chan + uid + msg
111
- radio .ble .start_advertising .assert_called_once_with (spy_advertisement )
112
- radio .ble .stop_advertising .assert_called_once_with ()
113
- assert radio .uid == 0
113
+ radio_obj .ble .start_advertising .assert_called_once_with (spy_advertisement )
114
+ radio_obj .ble .stop_advertising .assert_called_once_with ()
115
+ assert radio_obj .uid == 0
114
116
115
117
116
- def test_radio_receive_no_message (radio ):
118
+ def test_radio_receive_no_message (radio_obj ):
117
119
"""
118
120
If no message is received from the receive_bytes method, then None is
119
121
returned.
120
122
"""
121
- radio .receive_full = mock .MagicMock (return_value = None )
122
- assert radio .receive () is None
123
- radio .receive_full .assert_called_once_with ()
123
+ radio_obj .receive_full = mock .MagicMock (return_value = None )
124
+ assert radio_obj .receive () is None
125
+ radio_obj .receive_full .assert_called_once_with ()
124
126
125
127
126
- def test_radio_receive (radio ):
128
+ def test_radio_receive (radio_obj ):
127
129
"""
128
130
If bytes are received from the receive_bytes method, these are decoded
129
131
using utf-8 and returned as a string with null characters stripped from the
130
132
end.
131
133
"""
132
134
# Return value contains message bytes, RSSI (signal strength), timestamp.
133
135
msg = b"testing 1, 2, 3\x00 \x00 \x00 \x00 \x00 \x00 "
134
- radio .receive_full = mock .MagicMock (return_value = (msg , - 20 , 1.2 ))
135
- assert radio .receive () == "testing 1, 2, 3"
136
+ radio_obj .receive_full = mock .MagicMock (return_value = (msg , - 20 , 1.2 ))
137
+ assert radio_obj .receive () == "testing 1, 2, 3"
136
138
137
139
138
- def test_radio_receive_full_no_messages (radio ):
140
+ def test_radio_receive_full_no_messages (radio_obj ): # pylint: disable=invalid-name
139
141
"""
140
142
If no messages are detected by receive_full then it returns None.
141
143
"""
142
- radio .ble .start_scan .return_value = []
143
- assert radio .receive_full () is None
144
- radio .ble .start_scan .assert_called_once_with (
145
- adafruit_ble_radio ._RadioAdvertisement ,
144
+ radio_obj .ble .start_scan .return_value = []
145
+ assert radio_obj .receive_full () is None
146
+ radio_obj .ble .start_scan .assert_called_once_with (
147
+ adafruit_ble_radio ._RadioAdvertisement , # pylint: disable=protected-access
146
148
minimum_rssi = - 255 ,
147
149
timeout = 1 ,
148
150
extended = True ,
149
151
)
150
- radio .ble .stop_scan .assert_called_once_with ()
152
+ radio_obj .ble .stop_scan .assert_called_once_with ()
151
153
152
154
153
- def test_radio_receive_full_duplicate_message (radio ):
155
+ def test_radio_receive_full_duplicate_message (
156
+ radio_obj ,
157
+ ): # pylint: disable=invalid-name
154
158
"""
155
159
If a duplicate message is detected, then receive_full returns None
156
160
(indicating no *new* messages received).
@@ -159,12 +163,14 @@ def test_radio_receive_full_duplicate_message(radio):
159
163
mock_entry .msg = b"*\x00 Hello"
160
164
mock_entry .address .address_bytes = b"addr"
161
165
mock_entry .rssi = - 40
162
- radio .ble .start_scan .return_value = [mock_entry ]
163
- radio .msg_pool .add ((time .monotonic (), 42 , 0 , b"addr" ))
164
- assert radio .receive_full () is None
166
+ radio_obj .ble .start_scan .return_value = [mock_entry ]
167
+ radio_obj .msg_pool .add ((time .monotonic (), 42 , 0 , b"addr" ))
168
+ assert radio_obj .receive_full () is None
165
169
166
170
167
- def test_radio_receive_full_and_remove_expired_message_metadata (radio ):
171
+ def test_radio_receive_full_and_remove_expired_message_metadata (
172
+ radio_obj ,
173
+ ): # pylint: disable=invalid-name
168
174
"""
169
175
Return the non-duplicate message.
170
176
@@ -177,15 +183,15 @@ def test_radio_receive_full_and_remove_expired_message_metadata(radio):
177
183
mock_entry .msg = b"*\x01 Hello"
178
184
mock_entry .address .address_bytes = b"adr2"
179
185
mock_entry .rssi = - 40
180
- radio .ble .start_scan .return_value = [mock_entry ]
181
- radio .msg_pool .add (
186
+ radio_obj .ble .start_scan .return_value = [mock_entry ]
187
+ radio_obj .msg_pool .add (
182
188
(time .monotonic () - adafruit_ble_radio .AD_DURATION - 1 , 42 , 0 , b"addr" )
183
189
)
184
- result = radio .receive_full ()
190
+ result = radio_obj .receive_full ()
185
191
assert result [0 ] == b"Hello"
186
192
assert result [1 ] == - 40
187
- assert len (radio .msg_pool ) == 1
188
- metadata = radio .msg_pool .pop ()
193
+ assert len (radio_obj .msg_pool ) == 1
194
+ metadata = radio_obj .msg_pool .pop ()
189
195
assert metadata [1 ] == 42
190
196
assert metadata [2 ] == 1
191
197
assert metadata [3 ] == b"adr2"
0 commit comments