From 6389803762d5edd10fd69e4d80108eb84972d06c Mon Sep 17 00:00:00 2001 From: Peter Hill Date: Mon, 23 Aug 2021 17:43:17 +0100 Subject: [PATCH] Generalise test mock data some more --- pyxpad/mock_data.py | 53 ++++++++++++++++++++++++++++++++++++++++ tests/conftest.py | 49 +------------------------------------ tests/test_pyxpad.py | 4 ++- tests/test_xpadsource.py | 11 +++------ 4 files changed, 60 insertions(+), 57 deletions(-) create mode 100644 pyxpad/mock_data.py diff --git a/pyxpad/mock_data.py b/pyxpad/mock_data.py new file mode 100644 index 0000000..5472d88 --- /dev/null +++ b/pyxpad/mock_data.py @@ -0,0 +1,53 @@ +from collections import defaultdict +from typing import Union +import numpy as np +from pyxpad.pyxpad_utils import XPadDataDim, XPadDataItem + + +class MockSignalData: + pass + + +MOCK_SIGNALS = [ + ["signal_a", ""], + ["signal_b", "This one has something"], + ["signal_c", ""], + ["first_signal", ""], + ["second_signal", "a description"], +] + +MOCK_DEFAULT_SHOT = [MOCK_SIGNALS[0], MOCK_SIGNALS[1], MOCK_SIGNALS[2], MOCK_SIGNALS[4]] + +MOCK_LAST_SHOT = "12354" + +MOCK_SHOTS = defaultdict( + lambda: MOCK_DEFAULT_SHOT, + ( + (MOCK_LAST_SHOT, [MOCK_SIGNALS[1], MOCK_SIGNALS[2], MOCK_SIGNALS[3]]), + ("45321", [MOCK_SIGNALS[0], MOCK_SIGNALS[1], MOCK_SIGNALS[4]]), + ("1", MOCK_SIGNALS), + ), +) + + +class MockClient: + """Mock UDA Client""" + + @staticmethod + def get(name: str, shot: Union[int, str]): + if name == "lastshot": + child = MockSignalData() + child.lastshot = int(MOCK_LAST_SHOT) + signal = MockSignalData() + signal.children = [child] + return signal + if name.startswith("meta::list"): + signal = MockSignalData() + shot_data = MOCK_SHOTS[str(shot).strip()] + signal.signal_name = [item[0] for item in shot_data] + signal.description = [item[1] for item in shot_data] + return {"data": signal} + x = np.linspace(0.0, 1.0) + dim = XPadDataDim({"name": "time", "data": x}) + data = XPadDataItem({"name": name, "data": np.sin(x), "dim": [dim]}) + return data diff --git a/tests/conftest.py b/tests/conftest.py index e15b97c..899cdfb 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,3 @@ -from collections import defaultdict import numpy as np import pytest import shutil @@ -7,6 +6,7 @@ from pyxpad.pyxpad_utils import XPadDataDim, XPadDataItem from pyxpad import xpadsource, PyXPad +from pyxpad.mock_data import MockClient @pytest.fixture @@ -36,53 +36,6 @@ def copy_tree_data(tmp_path): return dest -class MockSignalData: - pass - - -MOCK_SIGNALS = [ - ["signal_a", ""], - ["signal_b", "This one has something"], - ["signal_c", ""], - ["first_signal", ""], - ["second_signal", "a description"], -] - -MOCK_DEFAULT_SHOT = [MOCK_SIGNALS[0], MOCK_SIGNALS[1], MOCK_SIGNALS[2], MOCK_SIGNALS[4]] - -MOCK_SHOTS = defaultdict( - lambda: MOCK_DEFAULT_SHOT, - ( - ("12354", [MOCK_SIGNALS[1], MOCK_SIGNALS[2], MOCK_SIGNALS[3]]), - ("45321", [MOCK_SIGNALS[0], MOCK_SIGNALS[1], MOCK_SIGNALS[4]]), - ("1", MOCK_SIGNALS), - ), -) - - -class MockClient: - """Mock UDA Client""" - - @staticmethod - def get(name: str, shot: Union[int, str]): - if name == "lastshot": - child = MockSignalData() - child.lastshot = 12354 - signal = MockSignalData() - signal.children = [child] - return signal - if name.startswith("meta::list"): - signal = MockSignalData() - shot_data = MOCK_SHOTS[str(shot).strip()] - signal.signal_name = [item[0] for item in shot_data] - signal.description = [item[1] for item in shot_data] - return {"data": signal} - x = np.linspace(0.0, 1.0) - dim = XPadDataDim({"name": "time", "data": x}) - data = XPadDataItem({"name": name, "data": np.sin(x), "dim": [dim]}) - return data - - class MockIdamModule: """Mock UDA module""" diff --git a/tests/test_pyxpad.py b/tests/test_pyxpad.py index 5c2a01f..a1df5b6 100644 --- a/tests/test_pyxpad.py +++ b/tests/test_pyxpad.py @@ -1,6 +1,8 @@ from pyxpad.xpadsource import XPadSource from Qt import QtCore +from pyxpad.mock_data import MOCK_LAST_SHOT + import pytest @@ -24,7 +26,7 @@ def test_get_last_shot(mock_uda, copy_tree_data, pyxpadbot, qtbot): main.sources.updateDisplay() main.lastShotButton.setEnabled(True) qtbot.mouseClick(main.lastShotButton, QtCore.Qt.LeftButton) - assert str(main.shotInput.text()).strip() == "12354" + assert str(main.shotInput.text()).strip() == MOCK_LAST_SHOT def test_get_available_signals(mock_uda, pyxpadbot, qtbot): diff --git a/tests/test_xpadsource.py b/tests/test_xpadsource.py index ebec298..1865eb9 100644 --- a/tests/test_xpadsource.py +++ b/tests/test_xpadsource.py @@ -1,14 +1,9 @@ from pyxpad.xpadsource import XPadSource from pyxpad.pyxpad_utils import XPadDataDim, XPadDataItem +from pyxpad.mock_data import MOCK_LAST_SHOT, MOCK_SIGNALS -EXPECTED_VARIABLE_NAMES = [ - "signal_a", - "signal_b", - "signal_c", - "first_signal", - "second_signal", -] +EXPECTED_VARIABLE_NAMES = [signal[0] for signal in MOCK_SIGNALS] def test_init_with_tree(copy_tree_data): @@ -27,7 +22,7 @@ def test_read(copy_tree_data, mock_uda): def test_latest_shot(mock_uda): source = XPadSource() - assert source.last_shot_number == 12354 + assert source.last_shot_number == int(MOCK_LAST_SHOT) def test_from_signals(mock_uda):