Skip to content
Closed
Show file tree
Hide file tree
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
10 changes: 2 additions & 8 deletions src/mx_bluesky/hyperion/utils/context.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from blueapi.core import BlueskyContext
from dodal.common.beamlines.beamline_utils import clear_devices, get_config_client
from dodal.utils import collect_factories, get_beamline_based_on_environment_variable
from dodal.common.beamlines.beamline_utils import get_config_client
from dodal.utils import get_beamline_based_on_environment_variable


def setup_context(dev_mode: bool = False) -> BlueskyContext:
Expand All @@ -11,12 +11,6 @@ def setup_context(dev_mode: bool = False) -> BlueskyContext:

def clear_all_device_caches(context: BlueskyContext):
context.unregister_all_devices()
clear_devices()

for f in collect_factories(get_beamline_based_on_environment_variable()).values():
if hasattr(f, "cache_clear"):
f.cache_clear() # type: ignore

get_config_client().reset_cache()


Expand Down
35 changes: 0 additions & 35 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import json
import logging
import os
import sys
from collections.abc import Callable, Generator, Sequence
from functools import partial
from pathlib import Path
from types import ModuleType
from typing import Any, TypedDict, TypeVar
from unittest.mock import MagicMock, patch

Expand All @@ -20,10 +18,8 @@
from bluesky.utils import Msg, MsgGenerator
from daq_config_server import ConfigClient
from dodal.beamlines import aithre, i03
from dodal.common.beamlines import beamline_utils
from dodal.common.beamlines.beamline_utils import (
clear_config_client,
clear_devices,
set_config_client,
)
from dodal.common.beamlines.commissioning_mode import set_commissioning_signal
Expand Down Expand Up @@ -70,7 +66,6 @@
from dodal.devices.zocalo.zocalo_results import _NO_SAMPLE_ID
from dodal.log import LOGGER as DODAL_LOGGER
from dodal.log import set_up_all_logging_handlers
from dodal.utils import AnyDeviceFactory, collect_factories
from event_model.documents import Event, EventDescriptor, RunStart, RunStop
from ophyd_async.core import (
AsyncStatus,
Expand Down Expand Up @@ -258,29 +253,6 @@ def ispyb_config_path():
yield ispyb_config_path


@pytest.fixture(scope="session")
def active_device_factories() -> set[AnyDeviceFactory]:
"""Obtain the set of device factories that should have their caches cleared
after every test invocation.
Override this in sub-packages for the specific beamlines under test."""
return device_factories_for_beamline(i03)


def device_factories_for_beamline(beamline_module: ModuleType) -> set[AnyDeviceFactory]:
return {
f
for f in collect_factories(beamline_module, include_skipped=True).values()
if hasattr(f, "cache_clear")
}


@pytest.fixture(scope="function", autouse=True)
def clear_device_factory_caches_after_every_test(active_device_factories):
yield None
for f in active_device_factories:
f.cache_clear() # type: ignore


def replace_all_tmp_paths(d: dict[str, Any], tmp_path: Path):
d = d.copy()
for k, v in d.items():
Expand Down Expand Up @@ -361,8 +333,6 @@ def pytest_runtest_setup(item):


def pytest_runtest_teardown(item):
if "dodal.common.beamlines.beamline_utils" in sys.modules:
sys.modules["dodal.common.beamlines.beamline_utils"].clear_devices()
markers = [m.name for m in item.own_markers]
if "skip_log_setup" in markers:
_reset_loggers([*ALL_LOGGERS, DODAL_LOGGER])
Expand Down Expand Up @@ -411,7 +381,6 @@ def smargon() -> Generator[Smargon, None, None]:
# Initial positions, needed for stub_offsets
set_mock_value(smargon.stub_offsets.center_at_current_position.disp, 0)
yield smargon
clear_devices()


@pytest.fixture
Expand Down Expand Up @@ -600,7 +569,6 @@ def locate_beamstop(_):
)

yield beamstop
beamline_utils.clear_devices()


@pytest.fixture
Expand All @@ -610,7 +578,6 @@ def xbpm_feedback(
xbpm = i03.xbpm_feedback.build(connect_immediately=True, mock=True)
xbpm.trigger = MagicMock(side_effect=lambda: completed_status())
yield xbpm
beamline_utils.clear_devices()


def set_up_dcm(dcm: DCM, sim_run_engine: RunEngineSimulator):
Expand Down Expand Up @@ -661,7 +628,6 @@ def mirror_voltages():
for vc in voltages.horizontal_voltages.values():
vc.set = MagicMock(side_effect=lambda _: completed_status())
yield voltages
beamline_utils.clear_devices()


@pytest.fixture
Expand All @@ -675,7 +641,6 @@ def undulator_dcm(sim_run_engine, dcm, undulator) -> Generator[UndulatorDCM]:
)
set_up_dcm(undulator_dcm.dcm_ref(), sim_run_engine)
yield undulator_dcm
# beamline_utils.clear_devices()


@pytest.fixture
Expand Down
8 changes: 0 additions & 8 deletions tests/unit_tests/beamlines/i24/serial/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from dodal.devices.attenuator.attenuator import ReadOnlyAttenuator
from dodal.devices.beamlines.i24.beam_center import DetectorBeamCenter
from dodal.devices.zebra.zebra import Zebra
from dodal.utils import AnyDeviceFactory
from ophyd_async.core import set_mock_value

from mx_bluesky.beamlines.i24.serial.fixed_target.ft_utils import ChipType
Expand All @@ -19,8 +18,6 @@
)
from mx_bluesky.beamlines.i24.serial.parameters.constants import DetectorName

from .....conftest import device_factories_for_beamline

TEST_PATH = Path("tests/test_data/test_daq_configuration")

TEST_LUT = {
Expand All @@ -33,11 +30,6 @@ def fake_generator(value):
return value


@pytest.fixture(scope="session")
def active_device_factories(active_device_factories) -> set[AnyDeviceFactory]:
return active_device_factories | device_factories_for_beamline(i24)


@pytest.fixture
def dummy_params_without_pp():
oxford_defaults = get_chip_format(ChipType.Oxford)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,6 @@ def do_grid_and_edge_detect(composite, parameters, tmp_dir):
)


@patch(
"dodal.common.beamlines.beamline_utils.active_device_is_same_type",
lambda a, b: True,
)
@patch("bluesky.plan_stubs.sleep", new=MagicMock())
def test_grid_detection_plan_runs_and_triggers_snapshots(
run_engine: RunEngine,
Expand All @@ -132,10 +128,6 @@ def test_grid_detection_plan_runs_and_triggers_snapshots(
assert mock_save.call_count == 2


@patch(
"dodal.common.beamlines.beamline_utils.active_device_is_same_type",
lambda a, b: True,
)
@patch("bluesky.plan_stubs.sleep", new=MagicMock())
async def test_grid_detection_plan_gives_warning_error_if_tip_not_found(
run_engine: RunEngine,
Expand Down Expand Up @@ -165,10 +157,6 @@ async def test_grid_detection_plan_gives_warning_error_if_tip_not_found(
assert "No pin found" in excinfo.value.args[0]


@patch(
"dodal.common.beamlines.beamline_utils.active_device_is_same_type",
lambda a, b: True,
)
@patch("bluesky.plan_stubs.sleep", new=MagicMock())
async def test_given_when_grid_detect_then_start_position_as_expected(
fake_devices: tuple[OavGridDetectionComposite, MagicMock],
Expand Down Expand Up @@ -209,10 +197,6 @@ def decorated():
assert gridscan_params["z_start_um"] == pytest.approx(-534, abs=1)


@patch(
"dodal.common.beamlines.beamline_utils.active_device_is_same_type",
lambda a, b: True,
)
@patch("bluesky.plan_stubs.sleep", new=MagicMock())
async def test_when_grid_detection_plan_run_then_ispyb_callback_gets_correct_values(
fake_devices: tuple[OavGridDetectionComposite, MagicMock],
Expand Down Expand Up @@ -280,10 +264,6 @@ async def test_when_grid_detection_plan_run_then_ispyb_callback_gets_correct_val
)


@patch(
"dodal.common.beamlines.beamline_utils.active_device_is_same_type",
lambda a, b: True,
)
@patch("bluesky.plan_stubs.sleep", new=MagicMock())
def test_when_grid_detection_plan_run_then_grid_detection_callback_gets_correct_values(
fake_devices: tuple[OavGridDetectionComposite, MagicMock],
Expand Down Expand Up @@ -322,10 +302,6 @@ def test_when_grid_detection_plan_run_then_grid_detection_callback_gets_correct_
assert cb.x_step_size_um == cb.y_step_size_um == cb.z_step_size_um == box_size_um


@patch(
"dodal.common.beamlines.beamline_utils.active_device_is_same_type",
lambda a, b: True,
)
@patch("bluesky.plan_stubs.sleep", new=MagicMock())
def test_when_grid_detection_plan_run_with_different_omega_order_then_grid_detection_callback_gets_correct_values(
fake_devices: tuple[OavGridDetectionComposite, MagicMock],
Expand Down Expand Up @@ -405,10 +381,6 @@ def test_given_unexpected_omega_then_grid_detect_raises(tmp_path: Path):
"odd",
[(True), (False)],
)
@patch(
"dodal.common.beamlines.beamline_utils.active_device_is_same_type",
lambda a, b: True,
)
@patch("bluesky.plan_stubs.sleep", new=MagicMock())
@patch("mx_bluesky.common.experiment_plans.oav_grid_detection_plan.LOGGER")
async def test_when_detected_grid_has_odd_y_steps_then_add_a_y_step_and_shift_grid(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,6 @@ def test_calculate_motion_profile_computes_values_for_wrapped_axis(
assert motion_values.distance_to_move_deg == 180.3075 * -sign


@patch(
"dodal.common.beamlines.beamline_utils.active_device_is_same_type",
lambda a, b: True,
)
@patch(
"mx_bluesky.hyperion.experiment_plans.rotation_scan_plan.rotation_scan_plan",
autospec=True,
Expand Down
6 changes: 0 additions & 6 deletions tests/unit_tests/hyperion/test_baton_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,7 @@ def run_plan_in_sim(plan):

faked_run_engine = MagicMock(spec=RunEngine, side_effect=run_plan_in_sim) # type: ignore

# wait_for_connection in ensure_connected creates a bunch of awaitables
# that will never be awaited by the simulator, let's not create them
def dont_connect(*args, **kwargs):
yield from bps.null()

with (
patch("blueapi.utils.connect_devices.ensure_connected", dont_connect),
patch.dict(os.environ, {"BEAMLINE": "i03"}),
):
context = BlueskyContext(run_engine=faked_run_engine)
Expand Down
4 changes: 2 additions & 2 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading