| 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 pytest import raises |
| 26 |
|
| 27 |
from pymeasure.test import expected_protocol |
| 28 |
from pymeasure.instruments import Instrument |
| 29 |
from pymeasure.instruments.validators import strict_range |
| 30 |
|
| 31 |
|
| 32 |
class BasicTestInstrument(Instrument): |
| 33 |
def __init__(self, adapter, name="Basic Test Instrument", **kwargs): |
| 34 |
super().__init__(adapter, name) |
| 35 |
self.kwargs = kwargs |
| 36 |
|
| 37 |
simple = Instrument.control( |
| 38 |
"VOLT?", "VOLT %s V", |
| 39 |
"""Simple property replying with plain floats""", |
| 40 |
) |
| 41 |
|
| 42 |
limited_control = Instrument.control( |
| 43 |
"AMP?", "AMP %g A", |
| 44 |
"""Property limited to 0, 10.""", |
| 45 |
values=(0, 10), |
| 46 |
validator=strict_range |
| 47 |
) |
| 48 |
|
| 49 |
with_error_checks = Instrument.control( |
| 50 |
"VOLT?", "VOLT %s V", |
| 51 |
"""Property with error checks after both setting and getting""", |
| 52 |
check_set_errors=True, |
| 53 |
check_get_errors=True, |
| 54 |
) |
| 55 |
|
| 56 |
|
| 57 |
def test_simple_protocol(): |
| 58 |
"""Test a property without parsing or channel prefixes.""" |
| 59 |
with expected_protocol( |
| 60 |
BasicTestInstrument, |
| 61 |
[('VOLT?', 3.14), |
| 62 |
('VOLT 4.5 V', None)] |
| 63 |
) as instr: |
| 64 |
assert instr.simple == 3.14 |
| 65 |
instr.simple = 4.5 |
| 66 |
|
| 67 |
|
| 68 |
def test_kwargs(): |
| 69 |
"""Test whether the kwargs are handed over correctly.""" |
| 70 |
with expected_protocol( |
| 71 |
BasicTestInstrument, |
| 72 |
[], |
| 73 |
test=5, |
| 74 |
xyz="abc" |
| 75 |
) as instr: |
| 76 |
assert instr.kwargs == {'test': 5, 'xyz': "abc"} |
| 77 |
|
| 78 |
|
| 79 |
def test_error_checks(): |
| 80 |
"""Test protocol for getting and setting with error checks.""" |
| 81 |
with expected_protocol( |
| 82 |
BasicTestInstrument, |
| 83 |
[('VOLT?', 3.14), |
| 84 |
('SYST:ERR?', '0, 0'), |
| 85 |
('VOLT 4.5 V', None), |
| 86 |
('SYST:ERR?', '0, 0'), |
| 87 |
] |
| 88 |
) as instr: |
| 89 |
assert instr.with_error_checks == 3.14 |
| 90 |
instr.with_error_checks = 4.5 |
| 91 |
|
| 92 |
|
| 93 |
def test_not_all_communication_used(): |
| 94 |
"""Test whether unused communication raises an error.""" |
| 95 |
with raises(AssertionError, match="Unprocessed protocol definitions remain"): |
| 96 |
with expected_protocol( |
| 97 |
BasicTestInstrument, |
| 98 |
[('VOLT?', 3.14), |
| 99 |
('VOLT 4.5 V', None), |
| 100 |
] |
| 101 |
) as instr: |
| 102 |
assert instr.simple == 3.14 |
| 103 |
|
| 104 |
|
| 105 |
def test_non_empty_write_buffer(): |
| 106 |
with raises(AssertionError, match="Non-empty write buffer remains"): |
| 107 |
with expected_protocol( |
| 108 |
BasicTestInstrument, |
| 109 |
[('VOLT?', 3.14)] |
| 110 |
) as instr: |
| 111 |
instr.adapter.write_bytes(b"VOLT") |
| 112 |
instr.adapter._index = 1 |
| 113 |
|
| 114 |
|
| 115 |
def test_non_empty_read_buffer(): |
| 116 |
with raises(AssertionError, match="Non-empty read buffer remains"): |
| 117 |
with expected_protocol( |
| 118 |
BasicTestInstrument, |
| 119 |
[('VOLT?', 3.14)] |
| 120 |
) as instr: |
| 121 |
instr.write("VOLT?") |
| 122 |
|
| 123 |
|
| 124 |
def test_preprocess_reply_on_values(): |
| 125 |
class InstrumentWithPreprocessValues(BasicTestInstrument): |
| 126 |
"""Workaround to get preprocess_reply working with protocol tests.""" |
| 127 |
simple2 = Instrument.control( |
| 128 |
"VOLT?", "VOLT %s V", |
| 129 |
"""Simple property replying with plain floats""", |
| 130 |
preprocess_reply=lambda v: v + "2345" |
| 131 |
) |
| 132 |
|
| 133 |
with expected_protocol( |
| 134 |
InstrumentWithPreprocessValues, |
| 135 |
[("VOLT?", "3.1")] |
| 136 |
) as instr: |
| 137 |
assert instr.simple2 == 3.12345 |
| 138 |
|
| 139 |
|
| 140 |
class TestConnectionCalls: |
| 141 |
def test_connection_method_call(self): |
| 142 |
with expected_protocol( |
| 143 |
BasicTestInstrument, |
| 144 |
[], |
| 145 |
connection_methods={'stb': 17} |
| 146 |
) as inst: |
| 147 |
assert inst.adapter.connection.stb() == 17 |
| 148 |
|
| 149 |
def test_connection_attribute(self): |
| 150 |
with expected_protocol( |
| 151 |
BasicTestInstrument, |
| 152 |
[], |
| 153 |
connection_attributes={'timeout': 100} |
| 154 |
) as inst: |
| 155 |
assert inst.adapter.connection.timeout == 100 |
| 156 |
|
| 157 |
|
| 158 |
def test_limited_control_raises_validator_exception(): |
| 159 |
"""Verify, that the validator's exception is caught.""" |
| 160 |
with expected_protocol( |
| 161 |
BasicTestInstrument, |
| 162 |
[], |
| 163 |
) as inst: |
| 164 |
with raises(ValueError, match="not in range"): |
| 165 |
inst.limited_control = 20 |