1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
#
# This file is part of the PyMeasure package.
#
# Copyright (c) 2013-2024 PyMeasure Developers
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
import logging
from unittest import mock
import pytest
from pymeasure.adapters import Adapter, FakeAdapter, ProtocolAdapter
@pytest.fixture()
def adapter():
return Adapter()
@pytest.fixture()
def fake():
return FakeAdapter()
def test_init(adapter):
assert adapter.connection is None
assert adapter.log == logging.getLogger("Adapter")
def test_init_log():
adapter = Adapter(log=logging.getLogger("parent"))
assert adapter.log == logging.getLogger("parent.Adapter")
def test_deprecated_preprocess_reply():
with pytest.warns(FutureWarning):
adapter = Adapter(preprocess_reply=lambda v: v)
assert adapter.preprocess_reply is not None
def test_del(adapter):
adapter.connection = mock.MagicMock()
adapter.__del__()
assert adapter.connection.method_calls == [mock.call.close()]
def test_write(fake):
fake.write("abc")
assert fake._buffer == "abc"
def test_write_bytes(fake):
fake.write_bytes(b"abc")
assert fake._buffer == "abc"
def test_read(fake):
fake._buffer = "abc"
assert fake.read() == "abc"
def test_read_bytes(fake):
fake._buffer = "abc"
assert fake.read_bytes(5) == b"abc"
@pytest.mark.parametrize("method, args", (['_write', ['5']],
['_read', []],
['_write_bytes', ['8']],
['_read_bytes', ['5', False]],
))
def test_not_implemented_methods(adapter, method, args):
with pytest.raises(NotImplementedError):
getattr(adapter, method)(*args)
def test_ask_deprecation_warning():
a = FakeAdapter()
with pytest.warns(FutureWarning):
assert a.ask("abc") == "abc"
@pytest.mark.parametrize("value, kwargs, result",
(("5,6,7", {}, [5, 6, 7]),
("5.6.7", {'separator': '.'}, [5, 6, 7]),
("5,6,7", {'cast': str}, ['5', '6', '7']),
("X,Y,Z", {}, ['X', 'Y', 'Z']),
("X,Y,Z", {'cast': str}, ['X', 'Y', 'Z']),
("X.Y.Z", {'separator': '.'}, ['X', 'Y', 'Z']),
("0,5,7.1", {'cast': bool}, [False, True, True]),
))
def test_adapter_values(value, kwargs, result):
a = FakeAdapter()
with pytest.warns(FutureWarning):
assert a.values(value, **kwargs) == result
def test_read_binary_values():
a = ProtocolAdapter([(None, "1 2")])
assert list(a.read_binary_values(dtype=int, sep=" ")) == pytest.approx([1, 2])
def test_write_binary_values():
"""Test write_binary_values in the ieee header format."""
a = ProtocolAdapter([(b'CMD#212\x00\x00\x80?\x00\x00\x00@\x00\x00@@\n', None)])
a.write_binary_values("CMD", [1, 2, 3], termination="\n")
def test_adapter_preprocess_reply():
with pytest.warns(FutureWarning):
a = FakeAdapter(preprocess_reply=lambda v: v[1:])
assert str(a) == "<FakeAdapter>"
assert a.values("R42.1") == [42.1]
assert a.values("A4,3,2") == [4, 3, 2]
assert a.values("TV 1", preprocess_reply=lambda v: v.split()[0]) == ['TV']
assert a.values("15", preprocess_reply=lambda v: v) == [15]
a = FakeAdapter()
assert a.values("V 3.4", preprocess_reply=lambda v: v.split()[1]) == [3.4]
def test_binary_values_deprecation_warning():
a = FakeAdapter()
with pytest.warns(FutureWarning):
a.binary_values("abcdefgh")
class TestLoggingForTestGenerator:
"""The test Generator relies on specific logging in the adapter, these tests ensure that."""
message = b"some written message"
@pytest.fixture
def adapter(self, caplog):
adapter = ProtocolAdapter()
caplog.set_level(logging.DEBUG)
return adapter
def test_write(self, adapter, caplog):
adapter.comm_pairs = [(self.message, None)]
written = self.message.decode()
adapter.write(written)
record = caplog.records[0]
assert record.msg == "WRITE:%s"
assert record.args == (written,)
def test_write_bytes(self, adapter, caplog):
adapter.comm_pairs = [(self.message, None)]
adapter.write(self.message)
record = caplog.records[0]
assert record.msg == "WRITE:%s"
assert record.args == (self.message,)
def test_read(self, adapter, caplog):
adapter.comm_pairs = [(None, self.message)]
read = adapter.read()
assert read == self.message.decode()
record = caplog.records[0]
assert record.msg == "READ:%s"
assert record.args == (read,)
def test_read_bytes(self, adapter, caplog):
adapter.comm_pairs = [(None, self.message)]
read = adapter.read_bytes(-1)
assert read == self.message
record = caplog.records[0]
assert record.msg == "READ:%s"
assert record.args == (read,)
|