Debian Codegraph @

tests/instruments/hp/test_hp437b_with_device.py

raw
1 #
2 # This file is part of the PyMeasure package.
3 #
4 # Copyright (c) 2013-2023 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 import pytest
26 from time import sleep
27 from pymeasure.instruments.hp import HP437B
28 from pymeasure.instruments.hp.hp437b import MeasurementUnit, OperatingMode, TriggerMode, \
29 GroupTriggerMode
30 import numpy as np
31
32
33 class TestHP437B:
34 """
35 Unit tests for HP437B class.
36
37 This test suite, needs the following setup to work properly:
38 - A HP437B device should be connected to a gpib capable adapter;
39 - The device's address must be set in the RESOURCE constant.
40 - Some power sensor connected to the power reference reading around 0 dBm (+/- 1 dBm)
41 """
42
43 BOOLEANS = [True, False]
44
45 FILTER_VALUES = reversed([1, 2, 4, 8, 16, 32, 64, 128, 256, 512])
46
47 GROUP_TRIGGER_MODES = [e for e in GroupTriggerMode]
48
49 @pytest.fixture
50 def makeHp437b(self, connected_device_address):
51 return HP437B(connected_device_address)
52
53 @pytest.fixture
54 def make_resetted_instr(self, makeHp437b):
55 makeHp437b.reset()
56 makeHp437b.clear_status_registers()
57 return makeHp437b
58
59 def test_range(self, make_resetted_instr):
60 instr = make_resetted_instr
61 instr.range = 5
62 assert instr.range == 5
63 assert instr.automatic_range_enabled is False
64
65 def test_operating_mode(self, make_resetted_instr):
66 instr = make_resetted_instr
67 assert instr.operating_mode == OperatingMode.NORMAL
68
69 def test_trigger_mode(self, make_resetted_instr):
70 instr = make_resetted_instr
71 instr.trigger_mode = TriggerMode.HOLD
72 assert instr.trigger_mode == TriggerMode.HOLD
73
74 instr.trigger_mode = TriggerMode.FREE_RUNNING
75 assert instr.trigger_mode == TriggerMode.FREE_RUNNING
76
77 @pytest.mark.parametrize('boolean', BOOLEANS)
78 def test_automatic_range_enabled(self, make_resetted_instr, boolean):
79 instr = make_resetted_instr
80 instr.automatic_range_enabled = boolean
81 assert instr.automatic_range_enabled == boolean
82
83 @pytest.mark.parametrize('gtmode', GROUP_TRIGGER_MODES)
84 def test_group_trigger_mode(self, make_resetted_instr, gtmode):
85 instr = make_resetted_instr
86 instr.group_trigger_mode = gtmode
87 assert instr.group_trigger_mode == gtmode
88
89 @pytest.mark.parametrize('boolean', BOOLEANS)
90 def test_duty_cycle_enabled(self, make_resetted_instr, boolean):
91 instr = make_resetted_instr
92 instr.duty_cycle_enabled = boolean
93 assert instr.duty_cycle_enabled == boolean
94
95 @pytest.mark.parametrize('boolean', BOOLEANS)
96 def test_filter_automatic_enabled(self, make_resetted_instr, boolean):
97 instr = make_resetted_instr
98 instr.filter_automatic_enabled = boolean
99 sleep(2)
100 assert instr.filter_automatic_enabled == boolean
101
102 @pytest.mark.parametrize('filter', FILTER_VALUES)
103 def test_filter(self, make_resetted_instr, filter):
104 instr = make_resetted_instr
105 instr.filter = filter
106 assert instr.filter == filter
107
108 @pytest.mark.parametrize('boolean', BOOLEANS)
109 def test_limits_enabled(self, make_resetted_instr, boolean):
110 instr = make_resetted_instr
111 instr.limits_enabled = boolean
112 assert instr.limits_enabled == boolean
113
114 def test_limit_high_hit(self, make_resetted_instr):
115 instr = make_resetted_instr
116 instr.power_reference_enabled = True
117 sleep(2)
118 assert (-1 <= instr.power <= 1)
119 instr.limit_low = -10
120 instr.limit_high = -1
121 instr.limits_enabled = True
122 sleep(1)
123 assert instr.limit_high_hit is True
124 instr.limits_enabled = False
125
126 def test_limit_low_hit(self, make_resetted_instr):
127 instr = make_resetted_instr
128 instr.power_reference_enabled = True
129 sleep(2)
130 assert (-1 <= instr.power <= 1)
131 instr.limit_high = 10
132 instr.limit_low = 1
133 instr.limits_enabled = True
134 sleep(1)
135 assert instr.limit_low_hit is True
136 instr.limits_enabled = False
137
138 def test_limit_high(self, make_resetted_instr):
139 instr = make_resetted_instr
140 instr.limit_high = 299.999
141 assert instr.limit_high == 299.999
142
143 @pytest.mark.parametrize('boolean', BOOLEANS)
144 def test_offset_enabled(self, make_resetted_instr, boolean):
145 instr = make_resetted_instr
146 instr.offset_enabled = boolean
147 assert instr.offset_enabled == boolean
148
149 def test_offset(self, make_resetted_instr):
150 instr = make_resetted_instr
151 instr.offset_enabled = True
152 instr.offset = 20.22
153 assert instr.offset == 20.22
154
155 @pytest.mark.parametrize('boolean', BOOLEANS)
156 def test_relative_mode_enabled(self, make_resetted_instr, boolean):
157 instr = make_resetted_instr
158 instr.relative_mode_enabled = boolean
159 assert instr.relative_mode_enabled == boolean
160
161 def test_measurement_units_and_linear_display_enabled(self, make_resetted_instr):
162 instr = make_resetted_instr
163 instr.relative_mode_enabled = True
164 instr.linear_display_enabled = True
165 assert instr.measurement_unit == MeasurementUnit.PERCENT
166
167 instr.linear_display_enabled = False
168 assert instr.measurement_unit == MeasurementUnit.DB
169
170 instr.relative_mode_enabled = False
171 instr.linear_display_enabled = True
172 assert instr.measurement_unit == MeasurementUnit.WATTS
173 assert instr.linear_display_enabled is True
174
175 instr.linear_display_enabled = False
176 assert instr.measurement_unit == MeasurementUnit.DBM
177 assert instr.linear_display_enabled is False
178
179 @pytest.mark.parametrize('resolution', [1, 0.1, 0.01])
180 def test_resolution_linear(self, make_resetted_instr, resolution):
181 instr = make_resetted_instr
182
183 instr.linear_display_enabled = True
184 instr.resolution = resolution
185 assert instr.resolution == resolution
186
187 @pytest.mark.parametrize('resolution', [0.1, 0.01, 0.001])
188 def test_resolution_logarithmic(self, make_resetted_instr, resolution):
189 instr = make_resetted_instr
190
191 instr.linear_display_enabled = False
192 instr.resolution = resolution
193 assert instr.resolution == resolution
194
195 def test_frequency(self, make_resetted_instr):
196 instr = make_resetted_instr
197 instr.frequency = 50e6
198 assert instr.frequency == 50e6
199
200 def test_duty_cycle(self, make_resetted_instr):
201 instr = make_resetted_instr
202 instr.duty_cycle = 0.500
203 assert instr.duty_cycle == 0.500
204
205 def test_calibration_factor(self, make_resetted_instr):
206 instr = make_resetted_instr
207 instr.calibration_factor = 50.00
208 assert instr.calibration_factor == 50.00
209
210 def test_sensor_data_read_cal_factor_table(self, make_resetted_instr):
211 instr = make_resetted_instr
212 frequencies = np.arange(1e9, 81e9, 1e9)
213 cal_factors = [100.0 for _ in range(0, 80)]
214 instr.sensor_data_write_cal_factor_table(9, frequencies, cal_factors)
215 freq_data, cal_fac_data = instr.sensor_data_read_cal_factor_table(9)
216
217 assert freq_data.sort() == frequencies.sort()
218 assert cal_fac_data.sort() == cal_fac_data.sort()