@@ -529,6 +529,7 @@ def test_handle_entity_mapping_with_856_no_u(mapper_base, caplog):
529529
530530def test_map_field_according_to_mapping_exception_logging (mapper_base , caplog ):
531531 """Test that map_field_according_to_mapping logs error on exception."""
532+ from folio_migration_tools .custom_exceptions import TransformationFieldMappingError
532533 from unittest .mock import MagicMock
533534
534535 mapper = mapper_base
@@ -538,20 +539,23 @@ def test_map_field_according_to_mapping_exception_logging(mapper_base, caplog):
538539 subfields = [Subfield (code = "a" , value = "Test Author" )],
539540 )
540541
541- # Create a mapping that will cause an exception
542- bad_mapping = [{"target " : "invalid.path[0]" , "rules " : [{ "conditions" : [{ "type" : "nonexistent_condition" }]}] }]
542+ # Create a mapping that will trigger entity mapping
543+ entity_mapping = [{"entity " : [] , "target " : "contributors" }]
543544 folio_record = {}
544545 legacy_ids = ["legacy-id-1" ]
545546
546- # Mock map_field_according_to_mapping to raise an exception
547+ # Mock handle_entity_mapping to raise a TransformationFieldMappingError
547548 original_method = mapper .handle_entity_mapping
548- mapper .handle_entity_mapping = MagicMock (side_effect = Exception ("Mapping error" ))
549+ mapping_error = TransformationFieldMappingError ("legacy-id-1" , "Test error" , "test data" )
550+ mapper .handle_entity_mapping = MagicMock (side_effect = mapping_error )
549551
550- with caplog .at_level ("ERROR" ):
551- try :
552- mapper .map_field_according_to_mapping (marc_field , bad_mapping , folio_record , legacy_ids )
553- except Exception :
554- pass # Expected
552+ with caplog .at_level (26 ): # DATA_ISSUES level
553+ mapper .map_field_according_to_mapping (marc_field , entity_mapping , folio_record , legacy_ids )
554+
555+ # Verify that the error was logged (log_it was called on the exception)
556+ assert "FIELD MAPPING FAILED" in caplog .text
557+ assert "legacy-id-1" in caplog .text
558+ assert "Test error" in caplog .text
555559
556560 # Restore
557561 mapper .handle_entity_mapping = original_method
0 commit comments