From 6cd2c8919f236ae48b8d8170db776d92019855b1 Mon Sep 17 00:00:00 2001 From: Dominic Oram Date: Tue, 18 Jul 2023 16:03:50 +0100 Subject: [PATCH] (#812) Use autospec in patches, also remove useless test --- .../unit_tests/test_zebra_setup.py | 6 +- .../tests/test_fast_grid_scan_plan.py | 57 ++++++------- .../tests/test_full_grid_scan_plan.py | 11 ++- .../tests/test_grid_detection_plan.py | 2 +- .../tests/test_optimise_attenuation_plan.py | 22 ++++-- .../tests/test_rotation_scan_plan.py | 8 +- .../unit_tests/test_store_in_ispyb.py | 18 ++--- .../unit_tests/test_zocalo_interaction.py | 22 +++--- src/artemis/system_tests/test_fgs_plan.py | 52 +++++++----- src/artemis/system_tests/test_main_system.py | 79 +++++++++++-------- src/artemis/unit_tests/test_log.py | 6 +- 11 files changed, 166 insertions(+), 117 deletions(-) diff --git a/src/artemis/device_setup_plans/unit_tests/test_zebra_setup.py b/src/artemis/device_setup_plans/unit_tests/test_zebra_setup.py index abf4ebffb..70da04d3d 100644 --- a/src/artemis/device_setup_plans/unit_tests/test_zebra_setup.py +++ b/src/artemis/device_setup_plans/unit_tests/test_zebra_setup.py @@ -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 @@ -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 diff --git a/src/artemis/experiment_plans/tests/test_fast_grid_scan_plan.py b/src/artemis/experiment_plans/tests/test_fast_grid_scan_plan.py index 2a1be4688..a5da658a9 100644 --- a/src/artemis/experiment_plans/tests/test_fast_grid_scan_plan.py +++ b/src/artemis/experiment_plans/tests/test_fast_grid_scan_plan.py @@ -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, @@ -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, @@ -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, @@ -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, @@ -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 ): @@ -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 ): @@ -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, @@ -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, @@ -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, diff --git a/src/artemis/experiment_plans/tests/test_full_grid_scan_plan.py b/src/artemis/experiment_plans/tests/test_full_grid_scan_plan.py index 6f53b8b80..c53604677 100644 --- a/src/artemis/experiment_plans/tests/test_full_grid_scan_plan.py +++ b/src/artemis/experiment_plans/tests/test_full_grid_scan_plan.py @@ -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, @@ -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, @@ -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( diff --git a/src/artemis/experiment_plans/tests/test_grid_detection_plan.py b/src/artemis/experiment_plans/tests/test_grid_detection_plan.py index ced141cac..483176d5a 100644 --- a/src/artemis/experiment_plans/tests/test_grid_detection_plan.py +++ b/src/artemis/experiment_plans/tests/test_grid_detection_plan.py @@ -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( diff --git a/src/artemis/experiment_plans/tests/test_optimise_attenuation_plan.py b/src/artemis/experiment_plans/tests/test_optimise_attenuation_plan.py index 8f76bbc1b..c60f63526 100644 --- a/src/artemis/experiment_plans/tests/test_optimise_attenuation_plan.py +++ b/src/artemis/experiment_plans/tests/test_optimise_attenuation_plan.py @@ -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 @@ -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( @@ -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() @@ -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, diff --git a/src/artemis/experiment_plans/tests/test_rotation_scan_plan.py b/src/artemis/experiment_plans/tests/test_rotation_scan_plan.py index 4d8bb76d1..b882eb9a7 100644 --- a/src/artemis/experiment_plans/tests/test_rotation_scan_plan.py +++ b/src/artemis/experiment_plans/tests/test_rotation_scan_plan.py @@ -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, @@ -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, @@ -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, diff --git a/src/artemis/external_interaction/unit_tests/test_store_in_ispyb.py b/src/artemis/external_interaction/unit_tests/test_store_in_ispyb.py index ddf15ef2a..b461a836e 100644 --- a/src/artemis/external_interaction/unit_tests/test_store_in_ispyb.py +++ b/src/artemis/external_interaction/unit_tests/test_store_in_ispyb.py @@ -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) @@ -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 ): @@ -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 ): @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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 ): diff --git a/src/artemis/external_interaction/unit_tests/test_zocalo_interaction.py b/src/artemis/external_interaction/unit_tests/test_zocalo_interaction.py index 536dbd43e..7314eeb07 100644 --- a/src/artemis/external_interaction/unit_tests/test_zocalo_interaction.py +++ b/src/artemis/external_interaction/unit_tests/test_zocalo_interaction.py @@ -25,8 +25,8 @@ } -@patch("zocalo.configuration.from_file") -@patch("artemis.external_interaction.zocalo.zocalo_interaction.lookup") +@patch("zocalo.configuration.from_file", autospec=True) +@patch("artemis.external_interaction.zocalo.zocalo_interaction.lookup", autospec=True) def _test_zocalo( func_testing: Callable, expected_params: dict, mock_transport_lookup, mock_from_file ): @@ -96,9 +96,9 @@ def test__run_start_and_end( _test_zocalo(function_to_run, expected_message) -@patch("workflows.recipe.wrap_subscribe") -@patch("zocalo.configuration.from_file") -@patch("artemis.external_interaction.zocalo.zocalo_interaction.lookup") +@patch("workflows.recipe.wrap_subscribe", autospec=True) +@patch("zocalo.configuration.from_file", autospec=True) +@patch("artemis.external_interaction.zocalo.zocalo_interaction.lookup", autospec=True) def test_when_message_recieved_from_zocalo_then_point_returned( mock_transport_lookup, mock_from_file, mock_wrap_subscribe ): @@ -145,9 +145,9 @@ def test_when_message_recieved_from_zocalo_then_point_returned( ) -@patch("workflows.recipe.wrap_subscribe") -@patch("zocalo.configuration.from_file") -@patch("artemis.external_interaction.zocalo.zocalo_interaction.lookup") +@patch("workflows.recipe.wrap_subscribe", autospec=True) +@patch("zocalo.configuration.from_file", autospec=True) +@patch("artemis.external_interaction.zocalo.zocalo_interaction.lookup", autospec=True) def test_when_exception_caused_by_zocalo_message_then_exception_propagated( mock_transport_lookup, mock_from_file, mock_wrap_subscribe ): @@ -182,9 +182,9 @@ def test_when_exception_caused_by_zocalo_message_then_exception_propagated( assert str(actual_exception.value) == str(failure_exception) -@patch("workflows.recipe.wrap_subscribe") -@patch("zocalo.configuration.from_file") -@patch("artemis.external_interaction.zocalo.zocalo_interaction.lookup") +@patch("workflows.recipe.wrap_subscribe", autospec=True) +@patch("zocalo.configuration.from_file", autospec=True) +@patch("artemis.external_interaction.zocalo.zocalo_interaction.lookup", autospec=True) def test_when_no_results_returned_then_no_diffraction_exception_raised( mock_transport_lookup, mock_from_file, mock_wrap_subscribe ): diff --git a/src/artemis/system_tests/test_fgs_plan.py b/src/artemis/system_tests/test_fgs_plan.py index 91166d3ae..56eed25a0 100644 --- a/src/artemis/system_tests/test_fgs_plan.py +++ b/src/artemis/system_tests/test_fgs_plan.py @@ -71,10 +71,10 @@ def fgs_composite(): @pytest.mark.skip(reason="Broken due to eiger issues in s03") @pytest.mark.s03 -@patch("bluesky.plan_stubs.wait") -@patch("bluesky.plan_stubs.kickoff") -@patch("bluesky.plan_stubs.complete") -@patch("artemis.fast_grid_scan_plan.wait_for_fgs_valid") +@patch("bluesky.plan_stubs.wait", autospec=True) +@patch("bluesky.plan_stubs.kickoff", autospec=True) +@patch("bluesky.plan_stubs.complete", autospec=True) +@patch("artemis.fast_grid_scan_plan.wait_for_fgs_valid", autospec=True) def test_run_gridscan( wait_for_fgs_valid: MagicMock, complete: MagicMock, @@ -106,12 +106,20 @@ def read_run(u, s, g): @pytest.mark.s03 -@patch("artemis.experiment_plans.fast_grid_scan_plan.fast_grid_scan_composite") -@patch("bluesky.plan_stubs.wait") -@patch("bluesky.plan_stubs.kickoff") -@patch("bluesky.plan_stubs.complete") -@patch("artemis.experiment_plans.fast_grid_scan_plan.run_gridscan_and_move") -@patch("artemis.experiment_plans.fast_grid_scan_plan.set_zebra_shutter_to_manual") +@patch( + "artemis.experiment_plans.fast_grid_scan_plan.fast_grid_scan_composite", + autospec=True, +) +@patch("bluesky.plan_stubs.wait", autospec=True) +@patch("bluesky.plan_stubs.kickoff", autospec=True) +@patch("bluesky.plan_stubs.complete", autospec=True) +@patch( + "artemis.experiment_plans.fast_grid_scan_plan.run_gridscan_and_move", autospec=True +) +@patch( + "artemis.experiment_plans.fast_grid_scan_plan.set_zebra_shutter_to_manual", + autospec=True, +) def test_full_plan_tidies_at_end( set_shutter_to_manual: MagicMock, run_gridscan_and_move: MagicMock, @@ -132,12 +140,20 @@ def test_full_plan_tidies_at_end( @pytest.mark.s03 -@patch("artemis.experiment_plans.fast_grid_scan_plan.fast_grid_scan_composite") -@patch("bluesky.plan_stubs.wait") -@patch("bluesky.plan_stubs.kickoff") -@patch("bluesky.plan_stubs.complete") -@patch("artemis.experiment_plans.fast_grid_scan_plan.run_gridscan_and_move") -@patch("artemis.experiment_plans.fast_grid_scan_plan.set_zebra_shutter_to_manual") +@patch( + "artemis.experiment_plans.fast_grid_scan_plan.fast_grid_scan_composite", + autospec=True, +) +@patch("bluesky.plan_stubs.wait", autospec=True) +@patch("bluesky.plan_stubs.kickoff", autospec=True) +@patch("bluesky.plan_stubs.complete", autospec=True) +@patch( + "artemis.experiment_plans.fast_grid_scan_plan.run_gridscan_and_move", autospec=True +) +@patch( + "artemis.experiment_plans.fast_grid_scan_plan.set_zebra_shutter_to_manual", + autospec=True, +) def test_full_plan_tidies_at_end_when_plan_fails( set_shutter_to_manual: MagicMock, run_gridscan_and_move: MagicMock, @@ -186,8 +202,8 @@ def test_GIVEN_scan_invalid_WHEN_plan_run_THEN_ispyb_entry_made_but_no_zocalo_en @pytest.mark.s03 -@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.kickoff", autospec=True) +@patch("artemis.experiment_plans.fast_grid_scan_plan.bps.complete", autospec=True) def test_WHEN_plan_run_THEN_move_to_centre_returned_from_zocalo_expected_centre( complete: MagicMock, kickoff: MagicMock, diff --git a/src/artemis/system_tests/test_main_system.py b/src/artemis/system_tests/test_main_system.py index 345f1108b..55ad54f2d 100644 --- a/src/artemis/system_tests/test_main_system.py +++ b/src/artemis/system_tests/test_main_system.py @@ -300,21 +300,24 @@ def test_cli_args_parse(): assert test_args == ("DEBUG", True, True, True) -@patch("dodal.beamlines.i03.Attenuator") -@patch("dodal.beamlines.i03.Flux") -@patch("dodal.beamlines.i03.DetectorMotion") -@patch("dodal.beamlines.i03.OAV") -@patch("dodal.beamlines.i03.ApertureScatterguard") -@patch("dodal.beamlines.i03.Backlight") -@patch("dodal.beamlines.i03.EigerDetector") -@patch("dodal.beamlines.i03.FastGridScan") -@patch("dodal.beamlines.i03.S4SlitGaps") -@patch("dodal.beamlines.i03.Smargon") -@patch("dodal.beamlines.i03.Synchrotron") -@patch("dodal.beamlines.i03.Undulator") -@patch("dodal.beamlines.i03.Zebra") -@patch("artemis.experiment_plans.fast_grid_scan_plan.get_beamline_parameters") -@patch("dodal.beamlines.beamline_utils.active_device_is_same_type") +@patch("dodal.beamlines.i03.Attenuator", autospec=True, spec_set=True) +@patch("dodal.beamlines.i03.Flux", autospec=True, spec_set=True) +@patch("dodal.beamlines.i03.DetectorMotion", autospec=True, spec_set=True) +@patch("dodal.beamlines.i03.OAV", autospec=True, spec_set=True) +@patch("dodal.beamlines.i03.ApertureScatterguard", autospec=True, spec_set=True) +@patch("dodal.beamlines.i03.Backlight", autospec=True, spec_set=True) +@patch("dodal.beamlines.i03.EigerDetector", autospec=True, spec_set=True) +@patch("dodal.beamlines.i03.FastGridScan", autospec=True, spec_set=True) +@patch("dodal.beamlines.i03.S4SlitGaps", autospec=True, spec_set=True) +@patch("dodal.beamlines.i03.Smargon", autospec=True, spec_set=True) +@patch("dodal.beamlines.i03.Synchrotron", autospec=True, spec_set=True) +@patch("dodal.beamlines.i03.Undulator", autospec=True, spec_set=True) +@patch("dodal.beamlines.i03.Zebra", autospec=True, spec_set=True) +@patch( + "artemis.experiment_plans.fast_grid_scan_plan.get_beamline_parameters", + autospec=True, +) +@patch("dodal.beamlines.beamline_utils.active_device_is_same_type", autospec=True) def test_when_blueskyrunner_initiated_then_plans_are_setup_and_devices_connected( type_comparison, mock_get_beamline_params, @@ -349,20 +352,21 @@ def test_when_blueskyrunner_initiated_then_plans_are_setup_and_devices_connected flux.return_value.wait_for_connection.assert_called() -@patch("artemis.experiment_plans.fast_grid_scan_plan.EigerDetector") -@patch("artemis.experiment_plans.fast_grid_scan_plan.FGSComposite") -@patch("artemis.experiment_plans.fast_grid_scan_plan.get_beamline_parameters") -def test_when_blueskyrunner_initiated_and_skip_flag_is_set_then_plans_are_setup_and_devices_are_not_connected( - mock_get_beamline_params, mock_fgs, mock_eiger -): - BlueskyRunner(MagicMock(), skip_startup_connection=True) - mock_fgs.return_value.wait_for_connection.assert_not_called() - - -@patch("artemis.experiment_plans.fast_grid_scan_plan.EigerDetector") -@patch("artemis.experiment_plans.fast_grid_scan_plan.FGSComposite") -@patch("artemis.experiment_plans.fast_grid_scan_plan.get_beamline_parameters") -@patch("artemis.experiment_plans.fast_grid_scan_plan.create_devices") +@patch( + "artemis.experiment_plans.fast_grid_scan_plan.EigerDetector", + autospec=True, + spec_set=True, +) +@patch( + "artemis.experiment_plans.fast_grid_scan_plan.FGSComposite", + autospec=True, + spec_set=True, +) +@patch( + "artemis.experiment_plans.fast_grid_scan_plan.get_beamline_parameters", + autospec=True, +) +@patch("artemis.experiment_plans.fast_grid_scan_plan.create_devices", autospec=True) def test_when_blueskyrunner_initiated_and_skip_flag_is_set_then_setup_called_upon_start( mock_setup, mock_get_beamline_params, mock_fgs, mock_eiger ): @@ -384,9 +388,20 @@ def test_when_blueskyrunner_initiated_and_skip_flag_is_set_then_setup_called_upo mock_setup.assert_called_once() -@patch("artemis.experiment_plans.fast_grid_scan_plan.EigerDetector") -@patch("artemis.experiment_plans.fast_grid_scan_plan.FGSComposite") -@patch("artemis.experiment_plans.fast_grid_scan_plan.get_beamline_parameters") +@patch( + "artemis.experiment_plans.fast_grid_scan_plan.EigerDetector", + autospec=True, + spec_set=True, +) +@patch( + "artemis.experiment_plans.fast_grid_scan_plan.FGSComposite", + autospec=True, + spec_set=True, +) +@patch( + "artemis.experiment_plans.fast_grid_scan_plan.get_beamline_parameters", + autospec=True, +) def test_when_blueskyrunner_initiated_and_skip_flag_is_not_set_then_all_plans_setup( mock_get_beamline_params, mock_fgs, diff --git a/src/artemis/unit_tests/test_log.py b/src/artemis/unit_tests/test_log.py index 412ef2d75..4efcf2f4f 100644 --- a/src/artemis/unit_tests/test_log.py +++ b/src/artemis/unit_tests/test_log.py @@ -22,8 +22,8 @@ def clear_loggers(): [dodal_logger.removeHandler(h) for h in dodal_logger.handlers] -@patch("dodal.log.config_bluesky_logging") -@patch("dodal.log.config_ophyd_logging") +@patch("dodal.log.config_bluesky_logging", autospec=True) +@patch("dodal.log.config_ophyd_logging", autospec=True) def test_no_env_variable_sets_correct_file_handler( mock_config_ophyd, mock_config_bluesky, @@ -37,7 +37,7 @@ def test_no_env_variable_sets_correct_file_handler( assert file_handlers.baseFilename.endswith("/tmp/dev/artemis.txt") -@patch("artemis.log.Path.mkdir") +@patch("artemis.log.Path.mkdir", autospec=True) @patch.dict( os.environ, {"ARTEMIS_LOG_DIR": "./dls_sw/s03/logs/bluesky"} ) # Note we use a relative path here so it works in CI