From d74da5609919e8e57c126b5a66ee40a29d4f7c28 Mon Sep 17 00:00:00 2001 From: Eddy Pedroni Date: Mon, 30 May 2022 20:20:18 +0200 Subject: Added error handling to JDS6600 test cases --- lab_control/jds6600.py | 18 +++++++++++++----- lab_control/test/jds6600_test.py | 35 +++++++++++++++++++++++++---------- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/lab_control/jds6600.py b/lab_control/jds6600.py index 6282fd1..2321a4b 100644 --- a/lab_control/jds6600.py +++ b/lab_control/jds6600.py @@ -3,8 +3,9 @@ import re from lab_control.function_generator import FunctionGenerator -# TODO figure out error handling class JDS6600(FunctionGenerator): + AVAILABLE_CHANNELS = [1, 2] + def __init__(self, portName): self._port = serial.Serial(portName) self._port.baudrate = 115200 @@ -27,30 +28,37 @@ class JDS6600(FunctionGenerator): return [response[5], response[7]] def setOn(self, channel: int) -> None: + assert channel in JDS6600.AVAILABLE_CHANNELS, f"JDS6600: Invalid channel {channel}" + state = self._queryOnOff() state[channel - 1] = "1" response = self._sendRequest("w20", f"{state[0]},{state[1]}") def setOff(self, channel: int) -> None: + assert channel in JDS6600.AVAILABLE_CHANNELS, f"JDS6600: Invalid channel {channel}" + state = self._queryOnOff() state[channel - 1] = "0" response = self._sendRequest("w20", f"{state[0]},{state[1]}") def setFrequency(self, channel: int, frequency: float) -> None: + assert channel in JDS6600.AVAILABLE_CHANNELS, f"JDS6600: Invalid channel {channel}" + assert frequency is not None and frequency >= 0.0 and frequency <= 60e6, f"JDS6600: Invalid frequency {frequency}" + # TODO implement after testing on actual device opcode = f"w{23 + channel - 1}" def setAmplitude(self, channel: int, amplitude: float) -> None: - if amplitude is None or amplitude < 0.0 or amplitude > 20.0: - return + assert channel in JDS6600.AVAILABLE_CHANNELS, f"JDS6600: Invalid channel {channel}" + assert amplitude is not None and amplitude >= 0.0 and amplitude <= 20.0, f"JDS6600: Invalid amplitude {amplitude}" opcode = f"w{25 + channel - 1}" arg = int(amplitude * 1000.0) response = self._sendRequest(opcode, str(arg)) def setFunction(self, channel: int, function: int) -> None: - if function is None or function < 0 or function > 16: - return + assert channel in JDS6600.AVAILABLE_CHANNELS, f"JDS6600: Invalid channel {channel}" + assert function is not None and function >= 0 and function <= 16, f"JDS6600: Invalid function code {function}" opcode = f"w{21 + channel - 1}" response = self._sendRequest(opcode, str(function)) diff --git a/lab_control/test/jds6600_test.py b/lab_control/test/jds6600_test.py index 256a5d0..c53388a 100644 --- a/lab_control/test/jds6600_test.py +++ b/lab_control/test/jds6600_test.py @@ -3,8 +3,6 @@ import pytest from lab_control.jds6600 import JDS6600 from lab_control.test.mock_jds6600_device import MockJDS6600Device -AVAILABLE_CHANNELS = [1, 2] - @pytest.fixture def mockDevice(): d = MockJDS6600Device() @@ -17,14 +15,19 @@ def uut(mockDevice): yield uut uut.closePort() -def checkNumericalParameter(testValues, writeValue, valueInMock, expectValid=True): - for ch in AVAILABLE_CHANNELS: +def checkNumericalParameter(testValues, writeValue, valueInMock): + for ch in JDS6600.AVAILABLE_CHANNELS: assert valueInMock(ch) == None for value in testValues: writeValue(ch, value) - expectedValue = value if expectValid else None - assert valueInMock(ch) == expectedValue + assert valueInMock(ch) == value + +def checkInvalidNumericalParameter(testValues, writeValue, valueInMock): + for ch in JDS6600.AVAILABLE_CHANNELS: + for value in testValues: + with pytest.raises(AssertionError): + writeValue(ch, value) def test_serialConfiguration(mockDevice): with pytest.raises(AssertionError): @@ -34,7 +37,7 @@ def test_serialConfiguration(mockDevice): mockDevice.checkPortConfiguration() def test_channelOnAndOff(uut, mockDevice): - for ch in AVAILABLE_CHANNELS: + for ch in JDS6600.AVAILABLE_CHANNELS: assert not mockDevice.isOn(ch) uut.setOn(ch) assert mockDevice.isOn(ch) @@ -45,17 +48,29 @@ def disabled_test_setFrequency(uut, mockDevice): checkNumericalParameter([0.0, 100.0, 100000.0], uut.setFrequency, mockDevice.getFrequency) def test_setInvalidFrequency(uut, mockDevice): - checkNumericalParameter([-10.0, 60000000.1, None], uut.setFrequency, mockDevice.getFrequency, expectValid=False) + checkInvalidNumericalParameter([-10.0, 60000000.1, None], uut.setFrequency, mockDevice.getFrequency) def test_setAmplitude(uut, mockDevice): checkNumericalParameter([0.0, 0.1, 1.0, 10.0, 20.0], uut.setAmplitude, mockDevice.getAmplitude) def test_setInvalidAmplitude(uut, mockDevice): - checkNumericalParameter([-0.1, -10.0, 20.1, None], uut.setAmplitude, mockDevice.getAmplitude, expectValid=False) + checkInvalidNumericalParameter([-0.1, -10.0, 20.1, None], uut.setAmplitude, mockDevice.getAmplitude) def test_setFunction(uut, mockDevice): checkNumericalParameter(range(0, 17), uut.setFunction, mockDevice.getFunction) def test_setInvalidFunction(uut, mockDevice): - checkNumericalParameter([-1, -10, 17, 20, None], uut.setFunction, mockDevice.getFunction, expectValid=False) + checkInvalidNumericalParameter([-1, -10, 17, 20, None], uut.setFunction, mockDevice.getFunction) + +def test_invalidChannel(uut): + testMethods = [uut.setFrequency, uut.setAmplitude, uut.setFunction] + for ch in [-1, 0, 3, None]: + for method in testMethods: + with pytest.raises(AssertionError): + method(ch, 0) + + with pytest.raises(AssertionError): + uut.setOn(ch) + with pytest.raises(AssertionError): + uut.setOff(ch) -- cgit v1.2.3