Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import importlib
import json
import os
from collections.abc import AsyncGenerator
from pathlib import Path
from types import ModuleType
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
Expand Down Expand Up @@ -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
Loading