From d35cd475954cde1bf902c438fb034fdc7ab50a00 Mon Sep 17 00:00:00 2001 From: Eddy Pedroni Date: Sun, 5 Jun 2022 16:54:51 +0200 Subject: File rename --- lab_control/test/frequency_response_test.py | 86 ------------------------- lab_control/test/frequency_response_unittest.py | 86 +++++++++++++++++++++++++ lab_control/test/measurement_test.py | 53 --------------- lab_control/test/measurement_unittest.py | 53 +++++++++++++++ 4 files changed, 139 insertions(+), 139 deletions(-) delete mode 100644 lab_control/test/frequency_response_test.py create mode 100644 lab_control/test/frequency_response_unittest.py delete mode 100644 lab_control/test/measurement_test.py create mode 100644 lab_control/test/measurement_unittest.py (limited to 'lab_control/test') diff --git a/lab_control/test/frequency_response_test.py b/lab_control/test/frequency_response_test.py deleted file mode 100644 index eb7d0d0..0000000 --- a/lab_control/test/frequency_response_test.py +++ /dev/null @@ -1,86 +0,0 @@ -import pytest - -from lab_control.test.mock.mock_lab import MockLab -from lab_control.frequency_response import FrequencyResponseMeasurement -from lab_control.measurement import getLinearRange -from lab_control.function_generator import FunctionGenerator - -@pytest.fixture -def mockLab(): - return MockLab() - -@pytest.fixture -def uut(mockLab): - return FrequencyResponseMeasurement() - -def prepareRampResponse(uut, mockLab): - # Expect a ramp response from 0.5 to 1.5 * input amplitude - minScale = 0.5 - maxScale = 1.5 - - def testFunction(f: float) -> float: - assert f >= uut.minFrequency and f <= uut.maxFrequency - frequencyPu = (f - uut.minFrequency) / (uut.maxFrequency - uut.minFrequency) - return minScale + maxScale * frequencyPu - - mockLab.connectChannels(uut.functionGeneratorChannel, uut.oscilloscopeChannel) - mockLab.setTestFunction(uut.oscilloscopeChannel, testFunction) - - return [(f, testFunction(f) * uut.inputAmplitude) for f in getLinearRange(uut.minFrequency, uut.maxFrequency, uut.steps)] - -def test_frequencyResponseDefaults(uut): - assert uut.minFrequency == 20e0 - assert uut.maxFrequency == 16e3 - assert uut.steps == 50 - assert uut.inputAmplitude == 0.1 - assert uut.functionGeneratorChannel == 1 - assert uut.oscilloscopeChannel == 1 - -def test_frequencyResponseRamp(uut, mockLab): - uut.minFrequency = 100.0 - uut.maxFrequency = 200.0 - uut.steps = 11 - uut.functionGeneratorChannel = 1 - uut.oscilloscopeChannel = 1 - uut.inputAmplitude = 2.0 - - fg = mockLab.getFunctionGeneratorChannel(uut.functionGeneratorChannel) - osc = mockLab.getOscilloscopeChannel(uut.oscilloscopeChannel) - - expectedData = prepareRampResponse(uut, mockLab) - - assert fg.amplitude is None - assert fg.function is None - assert not fg.on - assert osc.voltsPerDiv is None - assert not uut.measurementDone - assert uut.data is None - - uut.measure(mockLab, mockLab) - - assert fg.amplitude == uut.inputAmplitude - assert fg.function == FunctionGenerator.SINE - assert fg.on - assert osc.voltsPerDiv == uut.inputAmplitude / (mockLab.getDivisionsDisplayed() - 2) - assert uut.measurementDone - assert uut.data == expectedData - -def test_dumpToCSV(uut, mockLab, tmp_path): - uut.minFrequency = 10.0 - uut.maxFrequency = 30.0 - uut.steps = 5 - csv = tmp_path / "data.csv" - - assert not csv.exists() - uut.dumpToCSV(csv) - assert not csv.exists() - - expectedData = prepareRampResponse(uut, mockLab) - uut.measure(mockLab, mockLab) - - uut.dumpToCSV(csv) - savedData = csv.read_text().split("\n") - - assert savedData[0] == "Frequency (Hz),Amplitude (V)" - for i, l in enumerate(savedData[1:]): - assert l == f"{expectedData[i][0]},{expectedData[i][1]}" diff --git a/lab_control/test/frequency_response_unittest.py b/lab_control/test/frequency_response_unittest.py new file mode 100644 index 0000000..eb7d0d0 --- /dev/null +++ b/lab_control/test/frequency_response_unittest.py @@ -0,0 +1,86 @@ +import pytest + +from lab_control.test.mock.mock_lab import MockLab +from lab_control.frequency_response import FrequencyResponseMeasurement +from lab_control.measurement import getLinearRange +from lab_control.function_generator import FunctionGenerator + +@pytest.fixture +def mockLab(): + return MockLab() + +@pytest.fixture +def uut(mockLab): + return FrequencyResponseMeasurement() + +def prepareRampResponse(uut, mockLab): + # Expect a ramp response from 0.5 to 1.5 * input amplitude + minScale = 0.5 + maxScale = 1.5 + + def testFunction(f: float) -> float: + assert f >= uut.minFrequency and f <= uut.maxFrequency + frequencyPu = (f - uut.minFrequency) / (uut.maxFrequency - uut.minFrequency) + return minScale + maxScale * frequencyPu + + mockLab.connectChannels(uut.functionGeneratorChannel, uut.oscilloscopeChannel) + mockLab.setTestFunction(uut.oscilloscopeChannel, testFunction) + + return [(f, testFunction(f) * uut.inputAmplitude) for f in getLinearRange(uut.minFrequency, uut.maxFrequency, uut.steps)] + +def test_frequencyResponseDefaults(uut): + assert uut.minFrequency == 20e0 + assert uut.maxFrequency == 16e3 + assert uut.steps == 50 + assert uut.inputAmplitude == 0.1 + assert uut.functionGeneratorChannel == 1 + assert uut.oscilloscopeChannel == 1 + +def test_frequencyResponseRamp(uut, mockLab): + uut.minFrequency = 100.0 + uut.maxFrequency = 200.0 + uut.steps = 11 + uut.functionGeneratorChannel = 1 + uut.oscilloscopeChannel = 1 + uut.inputAmplitude = 2.0 + + fg = mockLab.getFunctionGeneratorChannel(uut.functionGeneratorChannel) + osc = mockLab.getOscilloscopeChannel(uut.oscilloscopeChannel) + + expectedData = prepareRampResponse(uut, mockLab) + + assert fg.amplitude is None + assert fg.function is None + assert not fg.on + assert osc.voltsPerDiv is None + assert not uut.measurementDone + assert uut.data is None + + uut.measure(mockLab, mockLab) + + assert fg.amplitude == uut.inputAmplitude + assert fg.function == FunctionGenerator.SINE + assert fg.on + assert osc.voltsPerDiv == uut.inputAmplitude / (mockLab.getDivisionsDisplayed() - 2) + assert uut.measurementDone + assert uut.data == expectedData + +def test_dumpToCSV(uut, mockLab, tmp_path): + uut.minFrequency = 10.0 + uut.maxFrequency = 30.0 + uut.steps = 5 + csv = tmp_path / "data.csv" + + assert not csv.exists() + uut.dumpToCSV(csv) + assert not csv.exists() + + expectedData = prepareRampResponse(uut, mockLab) + uut.measure(mockLab, mockLab) + + uut.dumpToCSV(csv) + savedData = csv.read_text().split("\n") + + assert savedData[0] == "Frequency (Hz),Amplitude (V)" + for i, l in enumerate(savedData[1:]): + assert l == f"{expectedData[i][0]},{expectedData[i][1]}" diff --git a/lab_control/test/measurement_test.py b/lab_control/test/measurement_test.py deleted file mode 100644 index 07716a3..0000000 --- a/lab_control/test/measurement_test.py +++ /dev/null @@ -1,53 +0,0 @@ -import pytest - -from lab_control.measurement import * - -@pytest.fixture -def uut(): - return Measurement() - -def test_linearRangeAscending(): - first = 100.0 - last = 200.0 - steps = 11 - - expectedRange = [ - 100.0, 110.0, 120.0, 130.0, - 140.0, 150.0, 160.0, 170.0, - 180.0, 190.0, 200.0 - ] - - actualRange = getLinearRange(first, last, steps) - assert actualRange == expectedRange - -def test_linearRangeDescending(): - first = 200.0 - last = 180.0 - steps = 3 - - expectedRange = [ - 200.0, 190.0, 180.0 - ] - - actualRange = getLinearRange(first, last, steps) - assert actualRange == expectedRange - -def test_linearRangeTwoSteps(): - first = 20.0 - last = -20.0 - steps = 2 - - expectedRange = [ - 20.0, -20.0 - ] - - actualRange = getLinearRange(first, last, steps) - assert actualRange == expectedRange - -def test_linearRangeOneStep(): - first = 20.0 - last = -20.0 - steps = 1 - - with pytest.raises(AssertionError): - actualRange = getLinearRange(first, last, steps) diff --git a/lab_control/test/measurement_unittest.py b/lab_control/test/measurement_unittest.py new file mode 100644 index 0000000..07716a3 --- /dev/null +++ b/lab_control/test/measurement_unittest.py @@ -0,0 +1,53 @@ +import pytest + +from lab_control.measurement import * + +@pytest.fixture +def uut(): + return Measurement() + +def test_linearRangeAscending(): + first = 100.0 + last = 200.0 + steps = 11 + + expectedRange = [ + 100.0, 110.0, 120.0, 130.0, + 140.0, 150.0, 160.0, 170.0, + 180.0, 190.0, 200.0 + ] + + actualRange = getLinearRange(first, last, steps) + assert actualRange == expectedRange + +def test_linearRangeDescending(): + first = 200.0 + last = 180.0 + steps = 3 + + expectedRange = [ + 200.0, 190.0, 180.0 + ] + + actualRange = getLinearRange(first, last, steps) + assert actualRange == expectedRange + +def test_linearRangeTwoSteps(): + first = 20.0 + last = -20.0 + steps = 2 + + expectedRange = [ + 20.0, -20.0 + ] + + actualRange = getLinearRange(first, last, steps) + assert actualRange == expectedRange + +def test_linearRangeOneStep(): + first = 20.0 + last = -20.0 + steps = 1 + + with pytest.raises(AssertionError): + actualRange = getLinearRange(first, last, steps) -- cgit v1.2.3