@@ -759,9 +759,8 @@ def test_process_docdb_record_invalid_location(
759759 )
760760 expected_log_messages = [
761761 "WARNING:root:Record location "
762- "no_s3/bucket/prefix1_2024-01-01_01-01-01 or name "
763- "prefix1_2024-01-01_01-01-01 not valid for bucket "
764- "aind-ephys-data-dev-u5u0i5!"
762+ "no_s3/bucket/prefix1_2024-01-01_01-01-01 not valid "
763+ "for bucket aind-ephys-data-dev-u5u0i5! Skipping."
765764 ]
766765 self .assertEqual (expected_log_messages , captured .output )
767766 self .assertIsNone (docdb_id_to_delete )
@@ -787,8 +786,8 @@ def test_process_docdb_record_invalid_prefix(
787786 },
788787 )
789788 expected_log_messages = [
790- "WARNING:root:Record location s3://bucket/prefix1 or name prefix1 "
791- "not valid for bucket aind-ephys-data-dev-u5u0i5!"
789+ "WARNING:root:Record location s3://bucket/prefix1 "
790+ "not valid for bucket aind-ephys-data-dev-u5u0i5! Skipping. "
792791 ]
793792 self .assertEqual (expected_log_messages , captured .output )
794793 self .assertIsNone (docdb_id_to_delete )
@@ -1444,6 +1443,7 @@ def test_process_prefix_no_record_yes_file_good_file_no__id(
14441443 mock_cond_copy_then_sync_core_json_files .assert_not_called ()
14451444 mock_upload_metadata_json_str_to_s3 .assert_not_called ()
14461445
1446+ @patch ("aind_data_asset_indexer.aind_bucket_indexer.uuid4" )
14471447 @patch (
14481448 "aind_data_asset_indexer.aind_bucket_indexer."
14491449 "upload_metadata_json_str_to_s3"
@@ -1467,6 +1467,7 @@ def test_process_prefix_no_record_yes_file_good_file_bad_location(
14671467 mock_download_json_file_from_s3 : MagicMock ,
14681468 mock_cond_copy_then_sync_core_json_files : MagicMock ,
14691469 mock_upload_metadata_json_str_to_s3 : MagicMock ,
1470+ mock_uuid : MagicMock ,
14701471 ):
14711472 """Tests _process_prefix method when there is no record in DocDb,
14721473 there is and there is metadata.nd.json file in S3, and the file can
@@ -1475,12 +1476,15 @@ def test_process_prefix_no_record_yes_file_good_file_bad_location(
14751476 mock_does_s3_object_exist .return_value = True
14761477 # Test what happens when the location in the record does not match the
14771478 # expected location
1478- mocked_downloaded_record = deepcopy (self .example_md_record )
1479- mocked_downloaded_record ["location" ] = (
1480- f"s3://{ self .basic_job .job_settings .s3_bucket } /"
1481- f"ecephys_642478_2020-01-10_10-10-10"
1479+ mock_download_json_file_from_s3 .return_value = {
1480+ "_id" : "5ca4a951-d374-4f4b-8279-d570a35b2286" ,
1481+ "name" : "ecephys_642478_2023-01-17_13-56-29" ,
1482+ "location" : "s3://bucket/ecephys_642478_2020-01-10_10-10-10" ,
1483+ }
1484+ mock_uuid .return_value = "a62344ff-1cec-48f4-914e-7482797e6332"
1485+ mock_docdb_client .insert_one_docdb_record .return_value = MagicMock (
1486+ status_code = 200 , json = MagicMock (return_value = "inserted" )
14821487 )
1483- mock_download_json_file_from_s3 .return_value = mocked_downloaded_record
14841488
14851489 location_to_id_map = dict ()
14861490 with self .assertLogs (level = "DEBUG" ) as captured :
@@ -1490,16 +1494,34 @@ def test_process_prefix_no_record_yes_file_good_file_bad_location(
14901494 s3_client = mock_s3_client ,
14911495 location_to_id_map = location_to_id_map ,
14921496 )
1497+ actual_location = (
1498+ f"s3://{ self .basic_job .job_settings .s3_bucket } /"
1499+ "ecephys_642478_2023-01-17_13-56-29"
1500+ )
14931501 expected_log_messages = [
1494- "WARNING:root:Location field s3://aind-ephys-data-dev-u5u0i5/"
1495- "ecephys_642478_2020-01-10_10-10-10 or name field "
1496- "ecephys_642478_2023-01-17_13-56-29 does not match actual "
1497- "location of record s3://aind-ephys-data-dev-u5u0i5/"
1498- "ecephys_642478_2023-01-17_13-56-29!"
1502+ "WARNING:root:Location field s3://bucket/"
1503+ "ecephys_642478_2020-01-10_10-10-10 does not match actual "
1504+ f"location of record { actual_location } ! Updating "
1505+ "record with correct location and new id." ,
1506+ f"INFO:root:Adding record to docdb for: { actual_location } " ,
1507+ "DEBUG:root:inserted" ,
14991508 ]
1509+ expected_record = {
1510+ "_id" : mock_uuid .return_value ,
1511+ "name" : "ecephys_642478_2023-01-17_13-56-29" ,
1512+ "location" : actual_location ,
1513+ }
15001514 self .assertEqual (expected_log_messages , captured .output )
1501- mock_docdb_client .insert_one_docdb_record .assert_not_called ()
1502- mock_cond_copy_then_sync_core_json_files .assert_not_called ()
1515+ mock_docdb_client .insert_one_docdb_record .assert_called_once_with (
1516+ record = expected_record
1517+ )
1518+ mock_cond_copy_then_sync_core_json_files .assert_called_once_with (
1519+ metadata_json = json .dumps (expected_record ),
1520+ bucket = self .basic_job .job_settings .s3_bucket ,
1521+ prefix = "ecephys_642478_2023-01-17_13-56-29" ,
1522+ s3_client = mock_s3_client ,
1523+ copy_original_md_subdir = "original_metadata" ,
1524+ )
15031525 mock_upload_metadata_json_str_to_s3 .assert_not_called ()
15041526
15051527 @patch ("aind_data_asset_indexer.aind_bucket_indexer.does_s3_object_exist" )
0 commit comments