import pytest import time from lab_control.sds1000xe import SDS1000XE from lab_control.test.mock_sds1000xe_device import MockSDS1000XEDevice MOCK_DEVICE_IP = "127.0.0.1" @pytest.fixture def mockDevice(): d = MockSDS1000XEDevice() yield d d.stop() @pytest.fixture def uut(mockDevice): return SDS1000XE(MOCK_DEVICE_IP) def checkFloatMeasurement(testValues, setValue, measureValue): for channel in SDS1000XE.AVAILABLE_CHANNELS: for value in testValues: setValue(channel, value) measuredValue = measureValue(channel) assert measuredValue == value def test_amplitudeMeasurement(uut, mockDevice): testValues = [16.23987, 0.0, -0.0164, 10.1] checkFloatMeasurement(testValues, mockDevice.setAmplitude, uut.measureAmplitude) def test_peakToPeakMeasurement(uut, mockDevice): testValues = [16.23987, 0.0, -0.0164, 10.1] checkFloatMeasurement(testValues, mockDevice.setPeakToPeak, uut.measurePeakToPeak) def test_RMSMeasurement(uut, mockDevice): testValues = [16.23987, 0.0, -0.0164, 10.1] checkFloatMeasurement(testValues, mockDevice.setRMS, uut.measureRMS) def test_frequencyMeasurement(uut, mockDevice): testValues = [16.23987, 0.0, -0.0164, 93489.15] checkFloatMeasurement(testValues, mockDevice.setFrequency, uut.measureFrequency) def test_invalidChannel(uut, mockDevice): # Channel is checked by the UUT before the request is sent testCases = [-1, 0, 5, None] testMethods = [uut.measureAmplitude, uut.measurePeakToPeak, uut.measureRMS, uut.measureFrequency] for t in testCases: for m in testMethods: with pytest.raises(AssertionError): m(t) def test_setVoltsPerDivision(uut, mockDevice): testValues = [5e-3, 50e-3, 1e0, 5e0, 10e0, 100e0] for channel in SDS1000XE.AVAILABLE_CHANNELS: assert mockDevice.getVoltsPerDivision(channel) is None for value in testValues: uut.setVoltsPerDivision(channel, value) time.sleep(0.1) # Allow time for the mock to receive and process the request assert mockDevice.getVoltsPerDivision(channel) == value