Skip to content

Commit

Permalink
Start trying to fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
noemifrisina committed Jan 23, 2025
1 parent a759de1 commit f6758bf
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 61 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ dependencies = [
"ophyd == 1.9.0",
"ophyd-async >= 0.8a5",
"bluesky >= 1.13",
"dls-dodal @ git+https://github.com/DiamondLightSource/dodal.git@fc08b89",
"dls-dodal @ git+https://github.com/DiamondLightSource/dodal.git@3d5f2a9",
]


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def do_darks(

# Gain 0
yield from set_gain_mode(
jungfrau, GainMode.dynamic, check_for_errors=check_for_errors
jungfrau, GainMode.DYNAMIC, check_for_errors=check_for_errors
)
yield from abs_set(jungfrau.file_directory, directory_prefix.as_posix(), wait=True)
yield from abs_set(jungfrau.file_name, "G0", wait=True)
Expand All @@ -79,7 +79,7 @@ def do_darks(

# Gain 1
yield from set_gain_mode(
jungfrau, GainMode.forceswitchg1, check_for_errors=check_for_errors
jungfrau, GainMode.FORCESWITCHG1, check_for_errors=check_for_errors
)
yield from abs_set(jungfrau.file_name, "G1", wait=True)
yield from do_manual_acquisition(
Expand All @@ -89,7 +89,7 @@ def do_darks(

# Gain 2
yield from set_gain_mode(
jungfrau, GainMode.forceswitchg2, check_for_errors=check_for_errors
jungfrau, GainMode.FORCESWITCHG2, check_for_errors=check_for_errors
)
yield from abs_set(jungfrau.file_name, "G2", wait=True)
yield from do_manual_acquisition(
Expand All @@ -99,7 +99,7 @@ def do_darks(

# Leave on dynamic after finishing
yield from set_gain_mode(
jungfrau, GainMode.dynamic, check_for_errors=check_for_errors
jungfrau, GainMode.DYNAMIC, check_for_errors=check_for_errors
)


Expand Down Expand Up @@ -130,7 +130,7 @@ def do_pedestal_darks(
)

yield from set_gain_mode(
jungfrau, GainMode.dynamic, check_for_errors=check_for_errors
jungfrau, GainMode.DYNAMIC, check_for_errors=check_for_errors
)
yield from abs_set(jungfrau.file_directory, directory_prefix.as_posix(), wait=True)
yield from abs_set(jungfrau.file_name, "Pedestal", wait=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def do_darks(

# Gain 0
yield from set_gain_mode(
jungfrau, GainMode.dynamic, check_for_errors=check_for_errors
jungfrau, GainMode.DYNAMIC, check_for_errors=check_for_errors
)
yield from abs_set(
jungfrau.file_directory,
Expand All @@ -72,7 +72,7 @@ def do_darks(

# Gain 1
yield from set_gain_mode(
jungfrau, GainMode.forceswitchg1, check_for_errors=check_for_errors
jungfrau, GainMode.FORCESWITCHG1, check_for_errors=check_for_errors
)
yield from abs_set(
jungfrau.file_directory,
Expand All @@ -82,7 +82,7 @@ def do_darks(

# Gain 2
yield from set_gain_mode(
jungfrau, GainMode.forceswitchg2, check_for_errors=check_for_errors
jungfrau, GainMode.FORCESWITCHG2, check_for_errors=check_for_errors
)
yield from abs_set(
jungfrau.file_directory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def rotation_scan_plan(
params.get_num_images(),
wait=True,
)
yield from set_gain_mode(jungfrau, GainMode.dynamic)
yield from set_gain_mode(jungfrau, GainMode.DYNAMIC)
yield from bps.abs_set(jungfrau.file_directory, directory.as_posix(), wait=True)
yield from bps.abs_set(jungfrau.file_name, params.data_filename, wait=True)
LOGGER.info("Setting Acquire to arm detector")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def setup_and_run_jungfrau_for_darks_plan(
SSX_LOGGER.debug("Collect G0")
dark_acquire_period = exposure_time * DARK_MULTIPLIER["G0"]
timeout_factor = max(10, timeout_factor * 0.001 / dark_acquire_period)
yield from bps.abs_set(jungfrau.gain_mode, GainMode.dynamic, wait=True)
yield from bps.abs_set(jungfrau.gain_mode, GainMode.DYNAMIC, wait=True)
yield from bps.abs_set(jungfrau.file_directory, filepath, wait=True)
yield from bps.abs_set(jungfrau.file_name, f"{filename}_dark_G0", wait=True)
yield from do_manual_acquisition(
Expand All @@ -39,7 +39,7 @@ def setup_and_run_jungfrau_for_darks_plan(

SSX_LOGGER.debug("Collect G1")
dark_acquire_period = exposure_time * DARK_MULTIPLIER["G1"]
yield from bps.abs_set(jungfrau.gain_mode, GainMode.forceswitchg1, wait=True)
yield from bps.abs_set(jungfrau.gain_mode, GainMode.FORCESWITCHG1, wait=True)
yield from bps.abs_set(jungfrau.file_directory, filepath, wait=True)
yield from bps.abs_set(jungfrau.file_name, f"{filename}_dark_G1", wait=True)
yield from do_manual_acquisition(
Expand All @@ -52,7 +52,7 @@ def setup_and_run_jungfrau_for_darks_plan(
yield from bps.sleep(0.3)

SSX_LOGGER.debug("Collect G2")
yield from bps.abs_set(jungfrau.gain_mode, GainMode.forceswitchg2, wait=True)
yield from bps.abs_set(jungfrau.gain_mode, GainMode.FORCESWITCHG2, wait=True)
yield from bps.abs_set(jungfrau.file_directory, filepath, wait=True)
yield from bps.abs_set(jungfrau.file_name, f"{filename}_dark_G2", wait=True)
yield from do_manual_acquisition(
Expand All @@ -79,7 +79,7 @@ def setup_jungfrau_for_fixed_target_plan(
):
SSX_LOGGER.info("Setup JF for fixed-target collection.")
yield from bps.abs_set(jungfrau.trigger_mode, TriggerMode.HARDWARE, group=group)
yield from bps.abs_set(jungfrau.gain_mode, GainMode.dynamic, group=group)
yield from bps.abs_set(jungfrau.gain_mode, GainMode.DYNAMIC, group=group)

yield from bps.abs_set(jungfrau.file_directory, filepath, group=group)
yield from bps.abs_set(jungfrau.file_name, filename, group=group)
Expand Down
86 changes: 52 additions & 34 deletions tests/unit_tests/beamlines/i24/jungfrau_commissioning/conftest.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import threading
import time
from collections.abc import Callable
from unittest.mock import MagicMock

import pytest
from dodal.devices.i24.i24_vgonio import VGonio
from dodal.beamlines import i24
from dodal.devices.attenuator.attenuator import EnumFilterAttenuator
from dodal.devices.i24.beam_params import ReadOnlyEnergyAndAttenuator
from dodal.devices.i24.jungfrau import AcquireState, JungFrau1M, WriteState
from dodal.devices.i24.vgonio import VerticalGoniometer
from ophyd.status import Status
from ophyd_async.epics.motor import Motor
from ophyd_async.testing import callback_on_mock_put, set_mock_value

from mx_bluesky.beamlines.i24.jungfrau_commissioning.plans.rotation_scan_plans import (
JfDevices,
)
from mx_bluesky.beamlines.i24.jungfrau_commissioning.utils import i24
from mx_bluesky.beamlines.i24.jungfrau_commissioning.utils.jf_commissioning_devices import (
JungfrauM1,
ReadOnlyEnergyAndAttenuator,
SetAttenuator,
)
from mx_bluesky.beamlines.i24.jungfrau_commissioning.utils.params import (
RotationScanParameters,
)
Expand All @@ -35,57 +34,76 @@ def completed_status():
return result


def patch_motor(motor: Motor, initial_position: float = 0):
set_mock_value(motor.user_setpoint, initial_position)
set_mock_value(motor.user_readback, initial_position)
set_mock_value(motor.deadband, 0.001)
set_mock_value(motor.motor_done_move, 1)
set_mock_value(motor.velocity, 3)
return callback_on_mock_put(
motor.user_setpoint,
lambda pos, *args, **kwargs: set_mock_value(motor.user_readback, pos),
)


@pytest.fixture
def fake_vgonio(completed_status) -> VGonio:
gon: VGonio = i24.vgonio(fake_with_ophyd_sim=True)
def fake_vgonio(RE):
gon: VerticalGoniometer = i24.vgonio(fake_with_ophyd_sim=True)

def set_omega_side_effect(val):
gon.omega.user_readback.sim_put(val) # type: ignore
return completed_status
with (
patch_motor(gon.x),
patch_motor(gon.yh),
patch_motor(gon.z),
patch_motor(gon.omega),
):
yield gon
# def set_omega_side_effect(val):
# gon.omega.user_readback.sim_put(val) # type: ignore
# return completed_status

gon.omega.set = MagicMock(side_effect=set_omega_side_effect)
# gon.omega.set = MagicMock(side_effect=set_omega_side_effect)

gon.x.user_setpoint._use_limits = False
gon.yh.user_setpoint._use_limits = False
gon.z.user_setpoint._use_limits = False
gon.omega.user_setpoint._use_limits = False
return gon
# gon.x.user_setpoint._use_limits = False
# gon.yh.user_setpoint._use_limits = False
# gon.z.user_setpoint._use_limits = False
# gon.omega.user_setpoint._use_limits = False
# return gon


@pytest.fixture
def fake_jungfrau(completed_status) -> JungfrauM1:
JF: JungfrauM1 = i24.jungfrau(fake_with_ophyd_sim=True)
def fake_jungfrau(RE) -> JungFrau1M:
JF: JungFrau1M = i24.jungfrau(fake_with_ophyd_sim=True)

def set_acquire_side_effect(val):
JF.acquire_rbv.sim_put(1) # type: ignore
JF.writing_rbv.sim_put(1) # type: ignore
def set_acquire_side_effect(_, wait):
set_mock_value(JF.acquire_rbv, AcquireState.ACQUIRE)
set_mock_value(JF.writing_rbv, WriteState.WRITE)

def go_low():
time.sleep(1)
JF.acquire_rbv.sim_put(0) # type: ignore
set_mock_value(JF.acquire_rbv, AcquireState.DONE)
time.sleep(0.5)
JF.writing_rbv.sim_put(0) # type: ignore
set_mock_value(JF.writing_rbv, WriteState.DONE)

threading.Thread(target=go_low, daemon=True).start()
return completed_status
# return completed_status

JF.acquire_start.set = MagicMock(side_effect=set_acquire_side_effect)
callback_on_mock_put(JF.acquire_start, set_acquire_side_effect)

return JF


@pytest.fixture
def fake_beam_params() -> ReadOnlyEnergyAndAttenuator:
def fake_beam_params(RE) -> ReadOnlyEnergyAndAttenuator:
BP: ReadOnlyEnergyAndAttenuator = i24.beam_params(fake_with_ophyd_sim=True)
BP.transmission.sim_put(0.1) # type: ignore
BP.energy.sim_put(20000) # type: ignore
BP.wavelength.sim_put(0.65) # type: ignore
BP.intensity.sim_put(9999999) # type: ignore
set_mock_value(BP.transmission, 0.1)
set_mock_value(BP.energy, 20000)
set_mock_value(BP.wavelength, 0.65)
set_mock_value(BP.intensity, 9999999)
return BP


@pytest.fixture
def attenuator() -> SetAttenuator:
def attenuator(RE) -> EnumFilterAttenuator:
return i24.attenuator(fake_with_ophyd_sim=True)


Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from unittest.mock import MagicMock, patch

from bluesky.run_engine import RunEngine
from dodal.devices.i24.jungfrau import GainMode, JungFrau1M

from mx_bluesky.beamlines.i24.jungfrau_commissioning.plans.gain_mode_darks_plans import (
GainMode,
do_manual_acquisition,
set_gain_mode,
)
Expand All @@ -15,14 +15,14 @@
@patch(
"bluesky.plan_stubs.wait",
)
def test_set_gain_mode(
async def test_set_gain_mode(
bps_wait: MagicMock,
fake_devices,
RE: RunEngine,
):
jungfrau: JungfrauM1 = fake_devices["jungfrau"]
jungfrau: JungFrau1M = fake_devices["jungfrau"]

RE(set_gain_mode(jungfrau, GainMode.dynamic))
RE(set_gain_mode(jungfrau, GainMode.DYNAMIC))
assert jungfrau.gain_mode.get() == "dynamic"
RE(set_gain_mode(jungfrau, GainMode.forceswitchg1))
assert jungfrau.gain_mode.get() == "forceswitchg1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import bluesky.plan_stubs as bps
from bluesky.run_engine import RunEngine
from dodal.devices.i24.i24_vgonio import VGonio
from dodal.devices.zebra import RotationDirection, Zebra
from dodal.devices.i24.vgonio import VerticalGoniometer
from dodal.devices.zebra.zebra import RotationDirection, Zebra

from mx_bluesky.beamlines.i24.jungfrau_commissioning.plans.rotation_scan_plans import (
JfDevices,
Expand Down Expand Up @@ -62,14 +62,14 @@ async def test_cleanup_plan(bps_wait, fake_devices: JfDevices, RE: RunEngine):
@patch(
"mx_bluesky.beamlines.i24.jungfrau_commissioning.plans.rotation_scan_plans.JsonMetadataWriter",
)
def test_move_to_start(
async def test_move_to_start(
nexus_callback: MagicMock,
bps_wait: MagicMock,
fake_devices: JfDevices,
RE: RunEngine,
params: RotationScanParameters,
):
gonio: VGonio = fake_devices["gonio"]
gonio: VerticalGoniometer = fake_devices["gonio"]
RE(
move_to_start_w_buffer(
gonio.omega,
Expand All @@ -79,7 +79,7 @@ def test_move_to_start(
direction=RotationDirection.POSITIVE,
)
)
assert gonio.omega.user_readback.get() == -2
assert await gonio.omega.user_readback.get_value() == -2


@patch(
Expand All @@ -92,7 +92,7 @@ def test_move_to_start(
@patch(
"mx_bluesky.beamlines.i24.jungfrau_commissioning.plans.rotation_scan_plans.JsonMetadataWriter",
)
def test_rotation_scan_do_plan(
async def test_rotation_scan_do_plan(
nexus_callback: MagicMock,
bps_wait: MagicMock,
fake_create_devices_function: Callable[..., JfDevices],
Expand All @@ -107,5 +107,5 @@ def test_rotation_scan_do_plan(

RE(plan)
devices = fake_create_devices_function()
gonio: VGonio = devices["gonio"]
assert gonio.omega.user_readback.get() == -367.6
gonio: VerticalGoniometer = devices["gonio"]
assert await gonio.omega.user_readback.get_value() == -367.6
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from unittest.mock import patch

import pytest
from dodal.devices.zebra import I24Axes, TrigSource, Zebra
from dodal.devices.zebra.zebra import I24Axes, TrigSource, Zebra

from mx_bluesky.beamlines.i24.jungfrau_commissioning.plans.zebra_plans import (
setup_zebra_for_rotation,
Expand Down

0 comments on commit f6758bf

Please sign in to comment.