@@ -722,7 +722,8 @@ def mock_bfactor_parameters(p):
722722
723723
724724@mock .patch ("cryoemservices.util.ispyb_commands.models" )
725- def test_insert_tomogram_new (mock_models ):
725+ @mock .patch ("cryoemservices.util.ispyb_commands.select" )
726+ def test_insert_tomogram_new (mock_select , mock_models ):
726727 def mock_tomogram_parameters (p ):
727728 tomogram_parameters = {
728729 "dcid" : 10 ,
@@ -755,17 +756,17 @@ def mock_tomogram_parameters(p):
755756
756757 # Mock which returns None for existing objects
757758 mock_session = mock .MagicMock ()
758- mock_session .query ().filter (). first .return_value = None
759+ mock_session .execute ().one_or_none .return_value = None
759760
760761 return_value = ispyb_commands .insert_tomogram (
761762 {}, mock_tomogram_parameters , mock_session
762763 )
763764 assert return_value .get ("success" )
764765 assert return_value ["return_value" ]
765766
766- assert mock_session . query .call_count == 2
767- assert mock_session .query (). filter . call_count == 2
768- assert mock_session .query ().filter (). first . call_count == 1
767+ assert mock_select .call_count == 2
768+ assert mock_session .execute . call_count == 3
769+ assert mock_session .execute ().one_or_none . call_count == 2
769770
770771 mock_models .Tomogram .assert_called_with (
771772 tomogramId = 801 ,
@@ -798,7 +799,8 @@ def mock_tomogram_parameters(p):
798799 mock_session .commit .assert_called ()
799800
800801
801- def test_insert_tomogram_update ():
802+ @mock .patch ("cryoemservices.util.ispyb_commands.update" )
803+ def test_insert_tomogram_update_on_id (mock_update ):
802804 def mock_tomogram_parameters (p ):
803805 tomogram_parameters = {
804806 "dcid" : 10 ,
@@ -831,20 +833,97 @@ def mock_tomogram_parameters(p):
831833
832834 # Mock which returns an existing object
833835 mock_session = mock .MagicMock ()
834- mock_session .query ().filter (). first .return_value = 1
836+ mock_session .execute ().one_or_none .return_value = 1
835837
836838 return_value = ispyb_commands .insert_tomogram (
837839 {}, mock_tomogram_parameters , mock_session
838840 )
839841 assert return_value .get ("success" )
840842 assert return_value ["return_value" ]
841843
842- assert mock_session .query .call_count == 3
843- assert mock_session .query ().filter .call_count == 3
844- assert mock_session .query ().filter ().first .call_count == 1
844+ assert mock_session .execute .call_count == 3
845+ mock_session .execute ().one_or_none .assert_called_once ()
845846
846847 # Don't check the model call here, instead look at the update
847- mock_session .query ().filter ().update .assert_called_with (
848+ mock_update ().where ().values .assert_called_with (
849+ {
850+ "dataCollectionId" : 10 ,
851+ "autoProcProgramId" : 1 ,
852+ "volumeFile" : "/path/to/volume" ,
853+ "stackFile" : "/path/to/stack" ,
854+ "sizeX" : 512 ,
855+ "sizeY" : 400 ,
856+ "sizeZ" : 300 ,
857+ "pixelSpacing" : 5.2 ,
858+ "residualErrorMean" : 1.3 ,
859+ "residualErrorSD" : 0.4 ,
860+ "xAxisCorrection" : 4.3 ,
861+ "tiltAngleOffset" : 1.5 ,
862+ "zShift" : 3.2 ,
863+ "fileDirectory" : "/tomogram/directory" ,
864+ "centralSliceImage" : "/path/to/central/slice" ,
865+ "tomogramMovie" : "/path/to/movie" ,
866+ "xyShiftPlot" : "/path/to/shift/plot" ,
867+ "projXY" : "/path/to/xy" ,
868+ "projXZ" : "/path/to/xz" ,
869+ "globalAlignmentQuality" : 0.2 ,
870+ "gridSquareId" : 12345 ,
871+ "pixelLocationX" : 200 ,
872+ "pixelLocationY" : 400 ,
873+ "thickness" : 50.5 ,
874+ }
875+ )
876+ mock_session .add .assert_not_called ()
877+ mock_session .commit .assert_called ()
878+
879+
880+ @mock .patch ("cryoemservices.util.ispyb_commands.update" )
881+ def test_insert_tomogram_update_on_volume_name (mock_update ):
882+ def mock_tomogram_parameters (p ):
883+ tomogram_parameters = {
884+ "dcid" : 10 ,
885+ "tomogram_id" : 801 ,
886+ "program_id" : 1 ,
887+ "volume_file" : "/path/to/volume" ,
888+ "stack_file" : "/path/to/stack" ,
889+ "size_x" : 512 ,
890+ "size_y" : 400 ,
891+ "size_z" : 300 ,
892+ "pixel_spacing" : 5.2 ,
893+ "residual_error_mean" : 1.3 ,
894+ "residual_error_sd" : 0.4 ,
895+ "x_axis_correction" : 4.3 ,
896+ "tilt_angle_offset" : 1.5 ,
897+ "z_shift" : 3.2 ,
898+ "file_directory" : "/tomogram/directory" ,
899+ "central_slice_image" : "/path/to/central/slice" ,
900+ "tomogram_movie" : "/path/to/movie" ,
901+ "xy_shift_plot" : "/path/to/shift/plot" ,
902+ "proj_xy" : "/path/to/xy" ,
903+ "proj_xz" : "/path/to/xz" ,
904+ "alignment_quality" : 0.2 ,
905+ "grid_square_id" : 12345 ,
906+ "pixel_location_x" : 200 ,
907+ "pixel_location_y" : 400 ,
908+ "thickness" : 50.5 ,
909+ }
910+ return tomogram_parameters [p ]
911+
912+ # Mock which returns an existing object
913+ mock_session = mock .MagicMock ()
914+ mock_session .execute ().one_or_none .side_effect = [0 , 1 ]
915+
916+ return_value = ispyb_commands .insert_tomogram (
917+ {}, mock_tomogram_parameters , mock_session
918+ )
919+ assert return_value .get ("success" )
920+ assert return_value ["return_value" ]
921+
922+ assert mock_session .execute .call_count == 4
923+ assert mock_session .execute ().one_or_none .call_count == 2
924+
925+ # Don't check the model call here, instead look at the update
926+ mock_update ().where ().where ().values .assert_called_with (
848927 {
849928 "dataCollectionId" : 10 ,
850929 "autoProcProgramId" : 1 ,
0 commit comments