Debian Codegraph @

tests/adapters/test_protocol.py

raw
1 #
2 # This file is part of the PyMeasure package.
3 #
4 # Copyright (c) 2013-2024 PyMeasure Developers
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 # THE SOFTWARE.
23 #
24
25 from unittest.mock import call
26 import pytest
27
28 from pymeasure.adapters.protocol import to_bytes, ProtocolAdapter
29
30 from pytest import mark, raises, fixture, warns
31
32
33 @pytest.fixture
34 def adapter():
35 return ProtocolAdapter()
36
37
38 @mark.parametrize("input, output", (("superXY", b"superXY"),
39 ([1, 2, 3, 4], b"\x01\x02\x03\x04"),
40 (5, b"5"),
41 (4.6, b"4.6"),
42 (None, None),
43 ))
44 def test_to_bytes(input, output):
45 assert to_bytes(input) == output
46
47
48 def test_to_bytes_invalid():
49 with raises(TypeError):
50 to_bytes(5.5j)
51
52
53 def test_protocol_instantiation():
54 a = ProtocolAdapter([("write", "read"), ("write_only", None)])
55 assert a.comm_pairs == [("write", "read"), ("write_only", None)]
56
57
58 @pytest.fixture
59 def mockAdapter():
60 adapter = ProtocolAdapter(connection_attributes={'timeout': 100},
61 connection_methods={'stb': 17})
62 return adapter
63
64
65 def test_connection_call(mockAdapter):
66 """Test whether a call to the connection is registered."""
67 mockAdapter.connection.clear(7)
68 assert mockAdapter.connection.clear.call_args_list == [call(7)]
69
70
71 def test_connection_attribute(mockAdapter):
72 assert mockAdapter.connection.timeout == 100
73
74
75 def test_connection_method(mockAdapter):
76 assert mockAdapter.connection.stb() == 17
77
78
79 class Test_write:
80 def test_write(self):
81 a = ProtocolAdapter([("written 5", 5)])
82 a.write("written 5")
83 assert a._read_buffer == b"5"
84
85 def test_write_without_response(self):
86 a = ProtocolAdapter([("Hey ho", None)])
87 a.write("Hey ho")
88 assert a._read_buffer is None
89
90 def test_wrong_command(self):
91 a = ProtocolAdapter([("Hey ho", None)])
92 with raises(AssertionError, match="do not match"):
93 a.write("Something different")
94
95
96 class Test_write_bytes:
97 @fixture(scope="class", params=[1, 2])
98 def written(self, request):
99 """Write in a single turn."""
100 a = ProtocolAdapter([("written", 5)])
101 if request.param == 1:
102 a.write_bytes(b"written")
103 else:
104 a.write_bytes(b"writ")
105 a.write_bytes(b"ten")
106 return a
107
108 def test_write_write_buffer(self, written):
109 assert written._write_buffer is None
110
111 def test_write_index(self, written):
112 assert written._index == 1
113
114 def test_write_read_buffer(self, written):
115 assert written._read_buffer == b"5"
116
117 def test_partial_write(self):
118 a = ProtocolAdapter([("written", 5)])
119 a.write_bytes(b"writ")
120 assert a._index == 0
121
122 def test_leftover_response(self):
123 a = ProtocolAdapter([("written", 5)])
124 a._read_buffer = b"5"
125 with raises(AssertionError):
126 a.write_bytes(b"written")
127
128 def test_no_response(self):
129 a = ProtocolAdapter([("written", None)])
130 a.write_bytes(b"writ")
131 assert a._read_buffer is None
132
133 def test_not_enough_pairs(self):
134 a = ProtocolAdapter([("a", None)])
135 a.write_bytes(b"a")
136 with raises(ValueError):
137 a.write_bytes(b"b")
138
139
140 class Test_read:
141 @mark.parametrize("buffer, returned", ((b"\x03\x65", "\x03e"),
142 (b"Bytes", "Bytes"),
143 ))
144 def test_works(self, buffer, returned):
145 a = ProtocolAdapter()
146 a._read_buffer = buffer
147 assert a.read() == returned
148
149 def test_read_empties_read_buffer(self):
150 a = ProtocolAdapter()
151 a._read_buffer = b"jklasdf"
152 a.read()
153 assert a._read_buffer is None
154
155 def test_read_empty_message(self):
156 a = ProtocolAdapter()
157 a._read_buffer = b""
158 assert a.read() == ""
159 assert a._read_buffer is None
160
161
162 class Test_read_bytes:
163 def test_read_full_buffer(self):
164 a = ProtocolAdapter()
165 a._read_buffer = b"Super 5"
166 assert a.read_bytes(3) == b"Sup"
167 assert a._read_buffer == b"er 5"
168
169 def test_read_empty_buffer(self):
170 a = ProtocolAdapter([(None, "Super 5")])
171 assert a.read_bytes(3) == b"Sup"
172 assert a._index == 1
173 assert a._read_buffer == b"er 5"
174
175 def test_read_without_write(self):
176 a = ProtocolAdapter([("written", 5)])
177 with raises(AssertionError):
178 a.read_bytes(3)
179
180 def test_read_all_bytes_empties_read_buffer(self):
181 a = ProtocolAdapter()
182 a._read_buffer = b"jklasdf"
183 a.read_bytes(7)
184 assert a._read_buffer is None
185
186 def test_read_all_bytes_from_pairs_empties_read_buffer(self):
187 a = ProtocolAdapter([(None, b"jklasdf")])
188 a.read_bytes(7)
189 assert a._read_buffer is None
190
191 def test_no_messages(self):
192 a = ProtocolAdapter()
193 with raises(ValueError):
194 a.read_bytes(3)
195
196 def test_not_enough_pairs(self):
197 a = ProtocolAdapter([(b"a", None)])
198 a.write_bytes(b"a")
199 with raises(ValueError):
200 a.read_bytes(10)
201
202 def test_unsolicited_response(self):
203 a = ProtocolAdapter([(None, b"Response")])
204 assert a.read_bytes(10) == b"Response"
205
206 def test_read_with_missing_write(self):
207 a = ProtocolAdapter([(b"a", b"b")])
208 with raises(AssertionError):
209 a.read_bytes(10)
210
211 def test_catch_None_None_pair(self):
212 a = ProtocolAdapter([(None, None)])
213 with raises(AssertionError, match="None, None"):
214 a.read_bytes(1)
215
216 def test_break_on_termchar_raises_warning(self):
217 a = ProtocolAdapter([(None, b"Response")])
218 with warns(UserWarning, match="cannot be tested"):
219 assert a.read_bytes(10, break_on_termchar=True) == b"Response"
220
221
222 def test_read_write_sequence():
223 """Test several consecutive writes and reads, including ask."""
224 a = ProtocolAdapter(
225 [("c1", "a1"), ("c2", None), (None, "a3"), ("c4", "a4")])
226 a.write("c1")
227 assert a.read() == "a1"
228 a.write("c2")
229 assert a.read_bytes(-1) == b"a3"
230 a.write_bytes(b"c4")
231 assert a.read_bytes(2) == b"a4"
232
233
234 def test_write_and_read_with_and_without_bytes():
235 """Test writing and reading, normal and bytes in combination."""
236 a = ProtocolAdapter([("c1", "a1")])
237 a.write_bytes(b"c")
238 a.write("1")
239 assert a.read_bytes(1) == b"a"
240 assert a.read() == "1"
241
242
243 @mark.parametrize("pairs", ([("c2",)],
244 [(None, "a3", "c4")],
245 [("c1", None), ("c2",)],
246 [(None, "a3", "c4"), (None, None)],
247 ))
248 def test_comm_pairs_are_all_length_2(pairs):
249 with raises(ValueError):
250 ProtocolAdapter(pairs)