"""Tests for VideoSingleHandler.""" from mock import Mock, patch import pytest from switchboard_consumer.constants.error import ( UNKNOWN_ERROR_CODE, UNKNOWN_ERROR_MESSAGE ) from switchboard_consumer.constants.exceptions import \ ParticipantHandlerError, ParticipantHandlerGraphQLError from switchboard_consumer.constants.product_metadata_update import ( CANNOT_RELEASE_CORRECT_VIDEO_PRODUCT, CANNOT_RELEASE_CORRECT_VIDEO_PRODUCT_CODE ) from switchboard_consumer.constants.validation import REJECT_RESPONSE, \ WARNING_RESPONSE from switchboard_consumer.logic.video_single_handler import VideoSingleHandler @pytest.fixture() def mock_data(): return { 'labelAccount': { 'companyCode': '12345' }, 'subAccount': None } @patch( 'switchboard_consumer.logic.validation.video_single_product.validate.' 'validate_video_single_product') def test_validation_step_success( mock_validate_product, mock_logger, orchard_client, mock_data): """Test for successful validation.""" mock_message = Mock() mock_message.sending_system_local_id = {} mock_validate_product.return_value = [] handler = VideoSingleHandler( mock_data, mock_message, mock_logger, orchard_client) handler.validate() assert handler.validation_errors == [] @patch( 'switchboard_consumer.logic.validation.video_single_product.validate.' 'validate_video_single_product') def test_validation_step_failure( mock_validate_product, mock_logger, orchard_client, mock_data): """Test for failed validation.""" mock_message = Mock() mock_message.sending_system_local_id = {} mock_error = { 'message': 'ErrorMessage', 'response': WARNING_RESPONSE } mock_validate_product.return_value = [mock_error] handler = VideoSingleHandler( mock_data, mock_message, mock_logger, orchard_client) result = handler.validate() assert result is True assert handler.validation_errors == [mock_error] @patch( 'switchboard_consumer.logic.validation.video_single_product.validate.' 'validate_video_single_product') def test_validation_step_failure_with_reject( mock_validate_product, mock_logger, orchard_client, mock_data): """Test for failed validation.""" mock_message = Mock() mock_message.sending_system_local_id = {} mock_error = { 'message': 'ErrorMessage', 'response': WARNING_RESPONSE } mock_reject_error = { 'message': 'ErrorMessage', 'response': REJECT_RESPONSE } mock_validate_product.return_value = [mock_error, mock_reject_error] handler = VideoSingleHandler( mock_data, mock_message, mock_logger, orchard_client) result = handler.validate() assert result is False assert handler.validation_errors == [] assert handler.processing_results[-1]['errors'] == [ mock_error, mock_reject_error] @patch('switchboard_consumer.logic.video_single_handler.' 'VideoSingleParticipantHandler.__init__') @patch('switchboard_consumer.logic.video_single_handler.' 'VideoSingleParticipantHandler.process') def test_participants_step_success( mock_process, mock_init, mock_logger, orchard_client, mock_data): """Test for successful participants processing.""" mock_message = Mock() mock_message.sending_system_local_id = {} handler = VideoSingleHandler( mock_data, mock_message, mock_logger, orchard_client) pre_existing_result = Mock('pre_existing_result') handler.processing_results.append(pre_existing_result) mock_artist_result = Mock('mock_artist_result') mock_artists = [Mock('mock_artist_1'), Mock('mock_artist_2')] mock_process.return_value = ([mock_artist_result], mock_artists) mock_init.return_value = None vendor_id = int(mock_data['labelAccount']['companyCode']) result = handler.process_participants() assert mock_init.mock_calls[0][1] == ( mock_logger, orchard_client, mock_message, mock_data, vendor_id, None ) assert result is True assert handler.processing_results == [pre_existing_result] + [ mock_artist_result] assert handler.artists == mock_artists @patch('switchboard_consumer.logic.video_single_handler.' 'VideoSingleParticipantHandler.__init__') @patch('switchboard_consumer.logic.video_single_handler.' 'VideoSingleParticipantHandler.process') def test_participants_step_graphql_error( mock_process, mock_init, mock_logger, orchard_client, mock_data): """Test for handling ParticipantHandlerGraphQLError.""" mock_message = Mock() mock_message.sending_system_local_id = {} handler = VideoSingleHandler( mock_data, mock_message, mock_logger, orchard_client) mock_process.side_effect = Mock( side_effect=ParticipantHandlerGraphQLError( 'Test error', [{ 'extensions': { 'code': 'create_participant_error_code', }, 'message': 'An error message', }])) mock_init.return_value = None result = handler.process_participants() assert result is False assert len(handler.processing_results) == 1 assert handler.processing_results[0]['errors'] == [ { 'code': 'create_participant_error_code', 'message': 'An error message' } ] assert handler.artists == [] @patch('switchboard_consumer.logic.video_single_handler.' 'VideoSingleParticipantHandler.__init__') @patch('switchboard_consumer.logic.video_single_handler.' 'VideoSingleParticipantHandler.process') def test_participants_step_participant_handler_error( mock_process, mock_init, mock_logger, orchard_client, mock_data): """Test for handling ParticipantHandlerError.""" mock_message = Mock() mock_message.sending_system_local_id = {} handler = VideoSingleHandler( mock_data, mock_message, mock_logger, orchard_client) mock_process.side_effect = Mock( side_effect=ParticipantHandlerError('Test error')) mock_init.return_value = None result = handler.process_participants() assert result is False assert len(handler.processing_results) == 1 assert handler.processing_results[0]['errors'] == [ { 'code': 'daemon_exception', 'message': 'ParticipantHandlerError: Test error' } ] assert handler.artists == [] @patch('switchboard_consumer.logic.video_single_handler.' 'format_video_single_create_input') def test_create_product_success(mock_format, mock_logger, orchard_client, mock_data): """Test for successful creation of product.""" mock_message = Mock() sony_id = { 'businessKey': 'UPC00001', 'businessKeyType': 'UPC', 'localId': 1001, 'system': 'SONY' } mock_message.ids = [sony_id] mock_message.sending_system_local_id = sony_id mock_message.correlation_id = 'CORRELATION_ID' orchard_id = 1234321 mock_format_video_single_create_input_result = Mock('formatted_product') mock_format.return_value = mock_format_video_single_create_input_result orchard_client.create_video_single_product = Mock(return_value={ 'productId': orchard_id }) handler = VideoSingleHandler( mock_data, mock_message, mock_logger, orchard_client) handler.validate_channel_selection_before_create = Mock(return_value=True) result = handler.create_product() assert result is True assert orchard_client.create_video_single_product.call_args_list[0][0] == ( mock_format_video_single_create_input_result, mock_message.correlation_id) assert len(handler.processing_results) == 1 processing_result = handler.processing_results[0] assert processing_result['ids'] == [ sony_id, { 'businessKey': 'UPC00001', 'businessKeyType': 'UPC', 'localId': str(orchard_id), 'system': 'ORCHARD' } ] @patch('switchboard_consumer.logic.video_single_handler.' 'format_video_single_create_input') def test_create_product_channel_not_available( mock_format, mock_logger, orchard_client, mock_data): """Test for successful creation of product without vevo channel.""" mock_message = Mock() sony_id = { 'businessKey': 'UPC00001', 'businessKeyType': 'UPC', 'localId': 1001, 'system': 'SONY' } mock_message.ids = [sony_id] mock_message.sending_system_local_id = sony_id mock_message.correlation_id = 'CORRELATION_ID' orchard_id = 1234321 mock_format_video_single_create_input_result = { 'channelSelection': 'BAD_CHANNEL', 'videoTitle': 'TITLE' } mock_format.return_value = mock_format_video_single_create_input_result orchard_client.create_video_single_product = Mock(return_value={ 'productId': orchard_id }) handler = VideoSingleHandler( mock_data, mock_message, mock_logger, orchard_client) handler.validate_channel_selection_before_create = Mock(return_value=False) result = handler.create_product() assert result is True assert orchard_client.create_video_single_product.call_args_list[0][0][0][ 'channelSelection'] is None assert len(handler.processing_results) == 1 processing_result = handler.processing_results[0] assert processing_result['ids'] == [ sony_id, { 'businessKey': 'UPC00001', 'businessKeyType': 'UPC', 'localId': str(orchard_id), 'system': 'ORCHARD' } ] @patch('switchboard_consumer.logic.video_single_handler.' 'format_video_single_create_input') def test_create_product_failure(mock_format, mock_logger, orchard_client, mock_data): """Test for failed creation of product.""" mock_message = Mock() sony_id = { 'businessKey': 'UPC00001', 'businessKeyType': 'UPC', 'localId': 1001, 'system': 'SONY' } mock_message.ids = [sony_id] mock_message.sending_system_local_id = sony_id mock_format_video_single_create_input_result = Mock('formatted_product') mock_format.return_value = mock_format_video_single_create_input_result orchard_client.create_video_single_product = Mock(return_value={ 'errors': [{ 'code': 'SOME_ORCHARD_ERROR' }] }) handler = VideoSingleHandler( mock_data, mock_message, mock_logger, orchard_client) handler.validate_channel_selection_before_create = Mock(return_value=True) result = handler.create_product() assert result is False assert len(handler.processing_results) == 1 processing_result = handler.processing_results[0] assert processing_result['ids'] == [sony_id] assert processing_result['errors'] == [ { 'code': 'unknown_code', 'message': UNKNOWN_ERROR_MESSAGE } ] @patch('switchboard_consumer.logic.video_single_handler.' 'format_video_single_update_input') @patch('switchboard_consumer.logic.video_single_handler.' 'VideoSingleHandler.validate_channel_selection_before_update') def test_update_in_progress_product_success( validate_channel_mock, mock_format, mock_logger, orchard_client, mock_data): """Test for successful update of product.""" validate_channel_mock.return_value = True mock_format.return_value = { 'productId': 12345 } mock_message = Mock() sony_id = { 'businessKey': 'UPC00001', 'businessKeyType': 'UPC', 'localId': 1001, 'system': 'SONY' } mock_message.ids = [sony_id] mock_message.sending_system_local_id = sony_id mock_message.correlation_id = 'CORRELATION_ID' orchard_id = 1234321 orchard_client.update_video_single_product = Mock(return_value={ 'productId': orchard_id }) handler = VideoSingleHandler( mock_data, mock_message, mock_logger, orchard_client) result = handler.update_in_progress_product(orchard_id) assert result is True assert orchard_client.update_video_single_product.call_args_list[0][0] == ( mock_format.return_value, mock_message.correlation_id) assert len(handler.processing_results) == 1 processing_result = handler.processing_results[0] assert processing_result['ids'] == [ sony_id, { 'businessKey': 'UPC00001', 'businessKeyType': 'UPC', 'localId': str(orchard_id), 'system': 'ORCHARD' } ] @patch('switchboard_consumer.logic.video_single_handler.' 'format_video_single_update_input') @patch('switchboard_consumer.logic.video_single_handler.' 'VideoSingleHandler.validate_channel_selection_before_update') def test_update_in_progress_product_not_valid_vevo_channel( validate_channel_mock, mock_format, mock_logger, orchard_client, mock_data): """Test for successful update of product with not valid vevo channel.""" validate_channel_mock.return_value = False mock_format.return_value = { 'productId': 12345, 'channelSelection': 'BAD_CHANNEL' } mock_message = Mock() sony_id = { 'businessKey': 'UPC00001', 'businessKeyType': 'UPC', 'localId': 1001, 'system': 'SONY' } mock_message.ids = [sony_id] mock_message.sending_system_local_id = sony_id mock_message.correlation_id = 'CORRELATION_ID' orchard_id = 1234321 orchard_client.update_video_single_product = Mock(return_value={ 'productId': orchard_id }) handler = VideoSingleHandler( mock_data, mock_message, mock_logger, orchard_client) result = handler.update_in_progress_product(orchard_id) assert result is True assert orchard_client.update_video_single_product.call_args_list[0][0][0][ 'channelSelection'] is None assert len(handler.processing_results) == 1 processing_result = handler.processing_results[0] assert processing_result['ids'] == [ sony_id, { 'businessKey': 'UPC00001', 'businessKeyType': 'UPC', 'localId': str(orchard_id), 'system': 'ORCHARD' } ] @patch('switchboard_consumer.logic.video_single_handler.' 'format_video_single_update_input') @patch('switchboard_consumer.logic.video_single_handler.' 'VideoSingleHandler.validate_channel_selection_before_update') def test_update_product_failure( validate_channel_mock, mock_format, mock_logger, orchard_client, mock_data): """Test for failed update of product.""" validate_channel_mock.return_value = True mock_format.return_value = { 'productId': 12345, 'channelSelection': 'BAD_CHANNEL' } mock_message = Mock() sony_id = { 'businessKey': 'UPC00001', 'businessKeyType': 'UPC', 'localId': 1001, 'system': 'SONY' } mock_message.ids = [sony_id] mock_message.sending_system_local_id = sony_id orchard_id = 1234321 orchard_client.update_video_single_product = Mock(return_value={ 'errors': [{ 'code': 'SOME_ORCHARD_ERROR' }] }) handler = VideoSingleHandler( mock_data, mock_message, mock_logger, orchard_client) result = handler.update_in_progress_product(orchard_id) assert result is False assert len(handler.processing_results) == 1 processing_result = handler.processing_results[0] assert processing_result['ids'] == [sony_id] assert processing_result['errors'] == [ { 'code': UNKNOWN_ERROR_CODE, 'message': UNKNOWN_ERROR_MESSAGE } ] def test_process_channel_selection_data_success( mock_logger, orchard_client, mock_data): """Test _process_channel_selection_data success.""" mock_message = Mock() mock_message.sending_system_local_id = {} mock_message.correlation_id = 'CORRELATION_ID' handler = VideoSingleHandler( mock_data, mock_message, mock_logger, orchard_client) channels_response = [ { 'channelName': 'channel1' }, { 'channelName': 'channel2' } ] result = handler._process_channel_selection_data( channels_response, {'channelSelection': 'channel2'}) assert result is True def test_process_channel_selection_data_not_found( mock_logger, orchard_client, mock_data): """Test _process_channel_selection_data not found.""" mock_message = Mock() mock_message.sending_system_local_id = {} mock_message.correlation_id = 'CORRELATION_ID' handler = VideoSingleHandler( mock_data, mock_message, mock_logger, orchard_client) channels_response = [ { 'channelName': 'channel1' }, { 'channelName': 'channel2' } ] handler._process_channel_selection_data( channels_response, {'channelSelection': 'channel3'}) assert handler.validation_errors == [ {'code': 'validation', 'message': 'Channel channel3 is not available'}] def test_process_channel_selection_data_graphql_error( mock_logger, orchard_client, mock_data): """Test _process_channel_selection_data fail with graphql error.""" mock_message = Mock() mock_message.sending_system_local_id = {} mock_message.correlation_id = 'CORRELATION_ID' handler = VideoSingleHandler( mock_data, mock_message, mock_logger, orchard_client) channels_response = { 'errors': [{ 'code': 'SOME_ORCHARD_ERROR' }] } handler._process_channel_selection_data( channels_response, {'channelSelection': 'channel3'}) assert handler.validation_errors == [ {'code': 'validation', 'message': 'Failed to validate VEVO channel'}] @pytest.mark.parametrize('process_channel_data_result', [True, False]) def test_validate_channel_selection_before_create( process_channel_data_result, mock_logger, orchard_client, mock_data): """Test for validate_channel_selection_before_create.""" mock_message = Mock() mock_message.sending_system_local_id = {} handler = VideoSingleHandler( mock_data, mock_message, mock_logger, orchard_client) payload = { 'projectId': 101, 'accountId': 2000, 'subaccountId': 0, 'channelSelection': 'someVevo', } handler._process_channel_selection_data = Mock( return_value=process_channel_data_result) orchard_client.get_project_available_channels = Mock( return_value='channels_data') result = handler.validate_channel_selection_before_create(payload) assert result is process_channel_data_result @pytest.mark.parametrize('process_channel_data_result', [True, False]) def test_validate_channel_selection_before_update( process_channel_data_result, mock_logger, orchard_client, mock_data): """Test for validate_channel_selection_before_update.""" mock_message = Mock() mock_message.sending_system_local_id = {} handler = VideoSingleHandler( mock_data, mock_message, mock_logger, orchard_client) payload = { 'channelSelection': 'someVevo', 'productId': 101 } handler._process_channel_selection_data = Mock( return_value=process_channel_data_result) orchard_client.get_product_available_channels = Mock( return_value='channels_data') result = handler.validate_channel_selection_before_update(payload) assert result is process_channel_data_result @patch('switchboard_consumer.logic.video_single_handler.' 'VideoSingleHandler.validate_local_ids') @patch('switchboard_consumer.logic.video_single_handler.' 'VideoSingleHandler.validate') @patch('switchboard_consumer.logic.video_single_handler.' 'VideoSingleHandler.process_participants') @patch('switchboard_consumer.logic.video_single_handler.' 'VideoSingleHandler.create_or_update') def test_process_enrich_validation_errors( validate_local_ids_mock, create_or_update_mock, process_participants_mock, validate_mock, mock_logger, orchard_client, mock_data): """Test for returning validation errors.""" mock_message = Mock() mock_message.sending_system_local_id = {} create_or_update_mock.return_value = True process_participants_mock.return_value = True validate_mock.return_value = True validate_local_ids_mock.return_value = True handler = VideoSingleHandler( mock_data, mock_message, mock_logger, orchard_client) validation_errors = [{ 'code': 'validation', 'message': 'ValidationError' }] handler.validation_errors = validation_errors handler.processing_results = [{ 'entityType': 'PRODUCT', 'sendingSystem': 'ORCHARD', 'errors': [] }] results = handler.process() assert results[-1]['errors'] == validation_errors def test_release_correct_completed_product(mock_logger, orchard_client, mock_data): """Test for release correct video product in completed state.""" mock_message = Mock() mock_message.sending_system_local_id = {} handler = VideoSingleHandler( mock_data, mock_message, mock_logger, orchard_client) result = handler.update_via_release_correction('12345') assert result is False assert handler.processing_results[0]['errors'] == [ { 'code': CANNOT_RELEASE_CORRECT_VIDEO_PRODUCT_CODE, 'message': CANNOT_RELEASE_CORRECT_VIDEO_PRODUCT }]