"""Tests for the message utilities.""" import json import pytest from switchboard_consumer.constants import entity_type, message_type, system from switchboard_consumer.constants.message_fields import ISRC from switchboard_consumer.kafka_message import KafkaMessage from switchboard_consumer.utils.message import ( create_processing_result, create_track_identifiers, get_system_local_id, ) @pytest.mark.parametrize('message, expected_result', [ ( { 'sendingSystem': 'SONY', 'ids': [ {'localId': '1', 'system': 'SONY'}, ], }, None, ), ( { 'sendingSystem': 'SONY', 'ids': [ {'localId': '2', 'system': 'ORCHARD'}, {'localId': '1', 'system': 'SONY'}, ], }, {'localId': '2', 'system': 'ORCHARD'}, ), ( { 'sendingSystem': 'ORCHARD', 'ids': [ {'localId': '2', 'system': 'ORCHARD'}, {'localId': '3', 'system': 'ORCHARD'}, {'localId': '1', 'system': 'SONY'}, ], }, {'localId': '2', 'system': 'ORCHARD'}, ), ]) def test_get_system_local_id(message, expected_result): """Test getting the Orchard localId & system pair from a message.""" result_response = get_system_local_id(message) assert result_response == expected_result def test_create_processing_result_with_no_errors(): """Test creating a processing result with no errors.""" local_ids = { 'sendingSystem': 'ORCHARD', 'entityType': entity_type.PROJECT, 'ids': [ { 'localId': 2, 'system': 'ORCHARD' } ] } kafka_message = KafkaMessage(json.dumps(local_ids)) result_response = create_processing_result(local_ids, kafka_message) assert result_response['entityType'] == entity_type.PROJECT assert result_response['messageType'] == message_type.PROCESSING_RESULT assert result_response['sendingSystem'] == system.ORCHARD assert result_response['ids'] == local_ids def test_create_processing_result_with_errors(): """Test creating a processing result with errors.""" local_ids = { 'sendingSystem': 'ORCHARD', 'entityType': entity_type.PROJECT, 'ids': [ { 'localId': '2', 'system': 'ORCHARD' } ] } errors = [ { 'code': 'some_code', 'message': 'An error message' } ] kafka_message = KafkaMessage(json.dumps(local_ids)) result_response = create_processing_result(local_ids, kafka_message, errors=errors) assert result_response['entityType'] == entity_type.PROJECT assert result_response['messageType'] == message_type.PROCESSING_RESULT assert result_response['sendingSystem'] == system.ORCHARD assert result_response['ids'] == local_ids assert result_response['errors'] == errors def test_create_track_identifiers(): """Test create track identifier.""" identifiers = [ { 'localId': '2', 'system': 'ORCHARD' } ] isrc = 'GBARL7700005' expected = [ { 'localId': '2', 'system': 'ORCHARD', 'businessKeyType': ISRC, 'businessKey': isrc } ] track_identifiers = create_track_identifiers(identifiers, isrc) assert track_identifiers == expected