Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
(#812) Use autospec in patches, also remove useless test
Browse files Browse the repository at this point in the history
  • Loading branch information
DominicOram committed Jul 18, 2023
1 parent a9d6e1f commit 6cd2c89
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 117 deletions.
6 changes: 3 additions & 3 deletions src/artemis/device_setup_plans/unit_tests/test_zebra_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ def zebra():
return i03.zebra(fake_with_ophyd_sim=True)


@patch("bluesky.plan_stubs.wait")
@patch("bluesky.plan_stubs.wait", autospec=True)
def test_zebra_set_up_for_fgs(bps_wait, RE, zebra: Zebra):
RE(setup_zebra_for_fgs(zebra, wait=True))
assert zebra.output.out_pvs[TTL_DETECTOR].get() == IN3_TTL
assert zebra.output.out_pvs[TTL_SHUTTER].get() == IN4_TTL


@patch("bluesky.plan_stubs.wait")
@patch("bluesky.plan_stubs.wait", autospec=True)
def test_zebra_set_up_for_rotation(bps_wait, RE, zebra: Zebra):
RE(setup_zebra_for_rotation(zebra, wait=True))
assert zebra.pc.gate_trigger.get(as_string=True) == I03Axes.OMEGA.value
Expand All @@ -51,7 +51,7 @@ def test_zebra_set_up_for_rotation(bps_wait, RE, zebra: Zebra):
RE(setup_zebra_for_rotation(zebra, direction=25))


@patch("bluesky.plan_stubs.wait")
@patch("bluesky.plan_stubs.wait", autospec=True)
def test_zebra_cleanup(bps_wait, RE, zebra: Zebra):
RE(set_zebra_shutter_to_manual(zebra, wait=True))
assert zebra.output.out_pvs[TTL_DETECTOR].get() == PC_PULSE
Expand Down
57 changes: 30 additions & 27 deletions src/artemis/experiment_plans/tests/test_fast_grid_scan_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ def standalone_read_hardware_for_ispyb(und, syn, slits, attn, fl):
@patch(
"dodal.devices.aperturescatterguard.ApertureScatterguard._safe_move_within_datacollection_range"
)
@patch("artemis.experiment_plans.fast_grid_scan_plan.run_gridscan")
@patch("artemis.experiment_plans.fast_grid_scan_plan.move_xyz")
@patch("artemis.experiment_plans.fast_grid_scan_plan.run_gridscan", autospec=True)
@patch("artemis.experiment_plans.fast_grid_scan_plan.move_xyz", autospec=True)
def test_results_adjusted_and_passed_to_move_xyz(
move_xyz: MagicMock,
run_gridscan: MagicMock,
Expand Down Expand Up @@ -178,7 +178,7 @@ def test_results_adjusted_and_passed_to_move_xyz(
)


@patch("bluesky.plan_stubs.mv")
@patch("bluesky.plan_stubs.mv", autospec=True)
def test_results_passed_to_move_motors(
bps_mv: MagicMock,
test_fgs_params: FGSInternalParameters,
Expand All @@ -199,17 +199,15 @@ def test_results_passed_to_move_motors(


@patch(
"dodal.devices.aperturescatterguard.ApertureScatterguard._safe_move_within_datacollection_range"
"dodal.devices.aperturescatterguard.ApertureScatterguard._safe_move_within_datacollection_range",
)
@patch("artemis.experiment_plans.fast_grid_scan_plan.run_gridscan.do_fgs")
@patch("artemis.experiment_plans.fast_grid_scan_plan.run_gridscan")
@patch("artemis.experiment_plans.fast_grid_scan_plan.move_xyz")
@patch("artemis.experiment_plans.fast_grid_scan_plan.run_gridscan", autospec=True)
@patch("artemis.experiment_plans.fast_grid_scan_plan.move_xyz", autospec=True)
@patch("bluesky.plan_stubs.rd")
def test_individual_plans_triggered_once_and_only_once_in_composite_run(
rd: MagicMock,
move_xyz: MagicMock,
run_gridscan: MagicMock,
do_fgs: MagicMock,
move_aperture: MagicMock,
mock_subscriptions: FGSCallbackCollection,
fake_fgs_composite: FGSComposite,
Expand All @@ -234,17 +232,16 @@ def test_individual_plans_triggered_once_and_only_once_in_composite_run(


@patch(
"dodal.devices.aperturescatterguard.ApertureScatterguard._safe_move_within_datacollection_range"
"dodal.devices.aperturescatterguard.ApertureScatterguard._safe_move_within_datacollection_range",
autospec=True,
)
@patch("artemis.experiment_plans.fast_grid_scan_plan.run_gridscan.do_fgs")
@patch("artemis.experiment_plans.fast_grid_scan_plan.run_gridscan")
@patch("artemis.experiment_plans.fast_grid_scan_plan.move_xyz")
@patch("artemis.experiment_plans.fast_grid_scan_plan.run_gridscan", autospec=True)
@patch("artemis.experiment_plans.fast_grid_scan_plan.move_xyz", autospec=True)
@patch("bluesky.plan_stubs.rd")
def test_logging_within_plan(
rd: MagicMock,
move_xyz: MagicMock,
run_gridscan: MagicMock,
do_fgs: MagicMock,
move_aperture: MagicMock,
mock_subscriptions: FGSCallbackCollection,
fake_fgs_composite: FGSComposite,
Expand All @@ -268,7 +265,7 @@ def test_logging_within_plan(
move_xyz.assert_called_once()


@patch("artemis.experiment_plans.fast_grid_scan_plan.bps.sleep")
@patch("artemis.experiment_plans.fast_grid_scan_plan.bps.sleep", autospec=True)
def test_GIVEN_scan_already_valid_THEN_wait_for_FGS_returns_immediately(
patch_sleep: MagicMock, RE: RunEngine
):
Expand All @@ -282,7 +279,7 @@ def test_GIVEN_scan_already_valid_THEN_wait_for_FGS_returns_immediately(
patch_sleep.assert_not_called()


@patch("artemis.experiment_plans.fast_grid_scan_plan.bps.sleep")
@patch("artemis.experiment_plans.fast_grid_scan_plan.bps.sleep", autospec=True)
def test_GIVEN_scan_not_valid_THEN_wait_for_FGS_raises_and_sleeps_called(
patch_sleep: MagicMock, RE: RunEngine
):
Expand All @@ -296,12 +293,16 @@ def test_GIVEN_scan_not_valid_THEN_wait_for_FGS_raises_and_sleeps_called(
patch_sleep.assert_called()


@patch("artemis.experiment_plans.fast_grid_scan_plan.bps.abs_set")
@patch("artemis.experiment_plans.fast_grid_scan_plan.bps.kickoff")
@patch("artemis.experiment_plans.fast_grid_scan_plan.bps.complete")
@patch("artemis.experiment_plans.fast_grid_scan_plan.bps.mv")
@patch("artemis.experiment_plans.fast_grid_scan_plan.wait_for_fgs_valid")
@patch("artemis.external_interaction.nexus.write_nexus.NexusWriter")
@patch("artemis.experiment_plans.fast_grid_scan_plan.bps.abs_set", autospec=True)
@patch("artemis.experiment_plans.fast_grid_scan_plan.bps.kickoff", autospec=True)
@patch("artemis.experiment_plans.fast_grid_scan_plan.bps.complete", autospec=True)
@patch("artemis.experiment_plans.fast_grid_scan_plan.bps.mv", autospec=True)
@patch("artemis.experiment_plans.fast_grid_scan_plan.wait_for_fgs_valid", autospec=True)
@patch(
"artemis.external_interaction.nexus.write_nexus.NexusWriter",
autospec=True,
spec_set=True,
)
def test_when_grid_scan_ran_then_eiger_disarmed_before_zocalo_end(
nexuswriter,
wait_for_valid,
Expand Down Expand Up @@ -334,17 +335,19 @@ def test_when_grid_scan_ran_then_eiger_disarmed_before_zocalo_end(
"artemis.experiment_plans.fast_grid_scan_plan.FGSCallbackCollection.from_params",
lambda _: mock_subscriptions,
), patch(
"artemis.external_interaction.callbacks.fgs.nexus_callback.NexusWriter.create_nexus_file"
"artemis.external_interaction.callbacks.fgs.nexus_callback.NexusWriter.create_nexus_file",
autospec=True,
), patch(
"artemis.external_interaction.callbacks.fgs.nexus_callback.NexusWriter.update_nexus_file_timestamp"
"artemis.external_interaction.callbacks.fgs.nexus_callback.NexusWriter.update_nexus_file_timestamp",
autospec=True,
):
RE(get_plan(test_fgs_params))

mock_parent.assert_has_calls([call.disarm(), call.run_end(0), call.run_end(0)])


@patch("artemis.experiment_plans.fast_grid_scan_plan.bps.wait")
@patch("artemis.experiment_plans.fast_grid_scan_plan.bps.complete")
@patch("artemis.experiment_plans.fast_grid_scan_plan.bps.wait", autospec=True)
@patch("artemis.experiment_plans.fast_grid_scan_plan.bps.complete", autospec=True)
def test_fgs_arms_eiger_without_grid_detect(
mock_complete,
mock_wait,
Expand All @@ -360,8 +363,8 @@ def test_fgs_arms_eiger_without_grid_detect(
fake_fgs_composite.eiger.unstage.assert_called_once()


@patch("artemis.experiment_plans.fast_grid_scan_plan.bps.wait")
@patch("artemis.experiment_plans.fast_grid_scan_plan.bps.complete")
@patch("artemis.experiment_plans.fast_grid_scan_plan.bps.wait", autospec=True)
@patch("artemis.experiment_plans.fast_grid_scan_plan.bps.complete", autospec=True)
def test_when_grid_scan_fails_then_detector_disarmed_and_correct_exception_returned(
mock_complete,
mock_wait,
Expand Down
11 changes: 8 additions & 3 deletions src/artemis/experiment_plans/tests/test_full_grid_scan_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def _fake_grid_detection(
return []


@patch("artemis.experiment_plans.full_grid_scan.get_beamline_parameters")
@patch("artemis.experiment_plans.full_grid_scan.get_beamline_parameters", autospec=True)
def test_create_devices(mock_beamline_params):
with (
patch("artemis.experiment_plans.full_grid_scan.i03") as i03,
Expand Down Expand Up @@ -215,8 +215,12 @@ def test_when_full_grid_scan_run_then_parameters_sent_to_fgs_as_expected(
params.json()


@patch("artemis.experiment_plans.full_grid_scan.grid_detection_plan")
@patch("artemis.experiment_plans.full_grid_scan.OavSnapshotCallback")
@patch("artemis.experiment_plans.full_grid_scan.grid_detection_plan", autospec=True)
@patch(
"artemis.experiment_plans.full_grid_scan.OavSnapshotCallback",
autospec=True,
spec_set=True,
)
def test_grid_detection_running_when_exception_raised_then_eiger_disarmed_and_correct_exception_returned(
mock_oav_callback: MagicMock,
mock_grid_detection_plan: MagicMock,
Expand All @@ -237,6 +241,7 @@ class DetectException(Exception):
with patch(
"artemis.external_interaction.callbacks.fgs.fgs_callback_collection.FGSCallbackCollection.from_params",
return_value=mock_subscriptions,
autospec=True,
):
with pytest.raises(DetectException):
RE(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def test_grid_detection_plan_gives_warningerror_if_tip_not_found(
assert "No pin found" in excinfo.value.args[0]


@patch("dodal.beamlines.i03.device_instantiation")
@patch("dodal.beamlines.i03.device_instantiation", autospec=True)
def test_create_devices(create_device: MagicMock):
create_devices()
create_device.assert_has_calls(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ def test_calculate_new_direction_gives_correct_value(


@patch(
"artemis.experiment_plans.optimise_attenuation_plan.do_device_optimise_iteration"
"artemis.experiment_plans.optimise_attenuation_plan.do_device_optimise_iteration",
autospec=True,
)
def test_deadtime_optimisation_calculates_deadtime_correctly(
mock_do_device_optimise_iteration, RE: RunEngine
Expand All @@ -133,7 +134,8 @@ def test_deadtime_optimisation_calculates_deadtime_correctly(
is_deadtime_optimised.return_value = True

with patch(
"artemis.experiment_plans.optimise_attenuation_plan.is_deadtime_optimised"
"artemis.experiment_plans.optimise_attenuation_plan.is_deadtime_optimised",
autospec=True,
) as mock_is_deadtime_optimised:
RE(
deadtime_optimisation(
Expand Down Expand Up @@ -286,7 +288,7 @@ def test_create_new_devices():
i03.attenuator.assert_called()


@patch("artemis.experiment_plans.optimise_attenuation_plan.arm_devices")
@patch("artemis.experiment_plans.optimise_attenuation_plan.arm_devices", autospec=True)
def test_total_counts_gets_within_target(mock_arm_devices, RE: RunEngine):
sample_shutter, xspress3mini, attenuator = fake_create_devices()

Expand Down Expand Up @@ -324,9 +326,17 @@ def update_data(_):
"optimisation_type",
[("total_counts"), ("deadtime")],
)
@patch("artemis.experiment_plans.optimise_attenuation_plan.total_counts_optimisation")
@patch("artemis.experiment_plans.optimise_attenuation_plan.deadtime_optimisation")
@patch("artemis.experiment_plans.optimise_attenuation_plan.check_parameters")
@patch(
"artemis.experiment_plans.optimise_attenuation_plan.total_counts_optimisation",
autospec=True,
)
@patch(
"artemis.experiment_plans.optimise_attenuation_plan.deadtime_optimisation",
autospec=True,
)
@patch(
"artemis.experiment_plans.optimise_attenuation_plan.check_parameters", autospec=True
)
def test_optimisation_attenuation_plan_runs_correct_functions(
mock_check_parameters,
mock_deadtime_optimisation,
Expand Down
8 changes: 4 additions & 4 deletions src/artemis/experiment_plans/tests/test_rotation_scan_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def test_move_to_end(smargon: Smargon, RE):


@patch("dodal.beamlines.beamline_utils.active_device_is_same_type", lambda a, b: True)
@patch("artemis.experiment_plans.rotation_scan_plan.rotation_scan_plan")
@patch("artemis.experiment_plans.rotation_scan_plan.rotation_scan_plan", autospec=True)
def test_get_plan(
plan: MagicMock,
RE,
Expand Down Expand Up @@ -89,7 +89,7 @@ def test_get_plan(
eiger.unstage.assert_called()


@patch("bluesky.plan_stubs.wait")
@patch("bluesky.plan_stubs.wait", autospec=True)
def test_rotation_plan(
bps_wait: MagicMock,
RE,
Expand Down Expand Up @@ -125,8 +125,8 @@ def test_rotation_plan(
assert mock_omega_sets.call_count == 4


@patch("artemis.experiment_plans.rotation_scan_plan.cleanup_plan")
@patch("bluesky.plan_stubs.wait")
@patch("artemis.experiment_plans.rotation_scan_plan.cleanup_plan", autospec=True)
@patch("bluesky.plan_stubs.wait", autospec=True)
def test_cleanup_happens(
bps_wait: MagicMock,
cleanup_plan: MagicMock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def setup_mock_return_values(ispyb_conn):
mx_acquisition.upsert_dc_grid.return_value = TEST_GRID_INFO_ID


@patch("ispyb.open")
@patch("ispyb.open", autospec=True)
def test_param_keys(ispyb_conn, dummy_ispyb, dummy_params):
setup_mock_return_values(ispyb_conn)

Expand Down Expand Up @@ -210,7 +210,7 @@ def _test_when_grid_scan_stored_then_data_present_in_upserts(
assert test_function(MXAcquisition.get_data_collection_group_params(), actual)


@patch("ispyb.open")
@patch("ispyb.open", autospec=True)
def test_given_sampleid_of_none_when_grid_scan_stored_then_sample_id_not_set(
ispyb_conn, dummy_ispyb, dummy_params
):
Expand All @@ -223,7 +223,7 @@ def test_sample_id(default_params, actual):
)


@patch("ispyb.open")
@patch("ispyb.open", autospec=True)
def test_given_real_sampleid_when_grid_scan_stored_then_sample_id_set(
ispyb_conn, dummy_ispyb: StoreInIspyb2D, dummy_params: FGSInternalParameters
):
Expand All @@ -239,7 +239,7 @@ def test_sample_id(default_params, actual):
)


@patch("ispyb.open")
@patch("ispyb.open", autospec=True)
def test_fail_result_run_results_in_bad_run_status(
mock_ispyb_conn: MagicMock,
dummy_ispyb: StoreInIspyb2D,
Expand All @@ -262,7 +262,7 @@ def test_fail_result_run_results_in_bad_run_status(
assert "DataCollection Successful" not in upserted_param_value_list


@patch("ispyb.open")
@patch("ispyb.open", autospec=True)
def test_no_exception_during_run_results_in_good_run_status(
mock_ispyb_conn: MagicMock,
dummy_ispyb: StoreInIspyb2D,
Expand All @@ -283,7 +283,7 @@ def test_no_exception_during_run_results_in_good_run_status(
assert "DataCollection Successful" in upserted_param_value_list


@patch("ispyb.open")
@patch("ispyb.open", autospec=True)
def test_ispyb_deposition_comment_correct(
mock_ispyb_conn: MagicMock,
dummy_ispyb: StoreInIspyb2D,
Expand All @@ -303,7 +303,7 @@ def test_ispyb_deposition_comment_correct(
)


@patch("ispyb.open")
@patch("ispyb.open", autospec=True)
def test_ispyb_deposition_rounds_to_int(
mock_ispyb_conn: MagicMock,
dummy_ispyb: StoreInIspyb2D,
Expand All @@ -326,7 +326,7 @@ def test_ispyb_deposition_rounds_to_int(
)


@patch("ispyb.open")
@patch("ispyb.open", autospec=True)
def test_ispyb_deposition_comment_for_3D_correct(
mock_ispyb_conn: MagicMock,
dummy_ispyb_3d: StoreInIspyb3D,
Expand All @@ -349,7 +349,7 @@ def test_ispyb_deposition_comment_for_3D_correct(
)


@patch("ispyb.open")
@patch("ispyb.open", autospec=True)
def test_given_x_and_y_steps_different_from_total_images_when_grid_scan_stored_then_num_images_correct(
ispyb_conn, dummy_ispyb: StoreInIspyb2D, dummy_params: FGSInternalParameters
):
Expand Down
Loading

0 comments on commit 6cd2c89

Please sign in to comment.