diff --git a/tests/conftest.py b/tests/conftest.py index b326effeba..e0c936160d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,5 @@ import importlib +import json import os from collections.abc import AsyncGenerator from pathlib import Path @@ -6,6 +7,7 @@ from unittest.mock import patch import pytest +from daq_config_server.converters.models import ConfigModel from ophyd_async.core import init_devices, set_mock_value from conftest import mock_attributes_table @@ -88,3 +90,33 @@ async def baton_in_commissioning_mode() -> AsyncGenerator[Baton]: set_mock_value(baton.commissioning, True) yield baton set_commissioning_signal(None) + + +def _fake_config_server_get_file_contents( + filepath: str | Path, + desired_return_type: type[str] | type[dict] | ConfigModel = str, + reset_cached_result: bool = True, +): + filepath = Path(filepath) + # Minimal logic required for unit tests + with filepath.open("r") as f: + contents = f.read() + print(contents) + if desired_return_type is str: + return contents + elif desired_return_type is dict: + print("return type is dict") + return json.loads(contents) + elif issubclass(desired_return_type, ConfigModel): # type: ignore + return desired_return_type.model_validate(json.loads(contents)) + + +@pytest.fixture(autouse=True) +def mock_config_server(): + # Don't actually talk to central service during unit tests, and reset caches between test + + with patch( + "daq_config_server.client.ConfigServer.get_file_contents", + side_effect=_fake_config_server_get_file_contents, + ): + yield