"""Test handler.""" from unittest.mock import MagicMock, patch import pytest from ddex_ingester_common.lambda_exceptions import InvalidProductTypeException from ddex_ingester_common.schemas.state_machine_schema import \ StateMachineSchema import index from constants import queries @patch('index.logger') @patch('index.graphql_gateway.execute') @patch('index.get_associated_track_tuid') @patch('index.load_ddex_json') def test_handler( mock_load_ddex_json, mock_get_associated_track_tuid, mock_graphql_execute, mock_current_logger, context_data): """Test the main handler.""" mock_load_ddex_json.return_value = context_data mock_logger = MagicMock(name='info') mock_current_logger.return_value = mock_logger expected_product_id = 234324 expected_channel = "I don't see the channel I want" mock_graphql_execute.return_value = { 'data': { 'productByUpc': { 'productId': 234324, 'tracks': [], 'notForDistribution': 'N', 'vendorId': 123, 'subaccountId': 999, 'typeOfVideo': 'Official Music Video', 'channelSelection': expected_channel, } } } response = index.handler(context_data, None) mock_get_associated_track_tuid.assert_called_once() assert response.get('product').get('product_id') == expected_product_id assert response.get('video').get('current_channel') == expected_channel @patch('index.logger') @patch('index.graphql_gateway.execute') @patch('index.get_associated_track_tuid') @patch('index.load_ddex_json') def test_handler_does_not_lookup_associated_track_tuid( mock_load_ddex_json, mock_get_associated_track_tuid, mock_graphql_execute, mock_current_logger, context_data): """Test the main handler does not lookup associated track tuid.""" context_data['video']['associated_track_isrc'] = None mock_load_ddex_json.return_value = context_data mock_logger = MagicMock(name='info') mock_current_logger.return_value = mock_logger expected_product_id = 234324 expected_channel = "I don't see the channel I want" mock_graphql_execute.return_value = { 'data': { 'productByUpc': { 'productId': 234324, 'tracks': [], 'notForDistribution': 'N', 'vendorId': 123, 'subaccountId': 999, 'typeOfVideo': 'Official Music Video', 'channelSelection': expected_channel, } } } response = index.handler(context_data, None) mock_get_associated_track_tuid.assert_not_called() assert response.get('product').get('product_id') == expected_product_id assert response.get('video').get('current_channel') == expected_channel @patch('index.logger') @patch('index.graphql_gateway.execute') @patch('index.get_associated_track_tuid') @patch('index.load_ddex_json') def test_invalid_product_type_exception( mock_load_ddex_json, mock_get_associated_track_tuid, mock_graphql_execute, mock_current_logger, context_data): """Test that an exception is thrown for non video products.""" mock_load_ddex_json.return_value = context_data mock_logger = MagicMock(name='info') mock_current_logger.return_value = mock_logger mock_graphql_execute.return_value = { 'data': { 'productByUpc': { 'productId': 234324, 'tracks': [{ 'tuid': '32279606', 'isrc': 'QZCDB2000001' }], 'notForDistribution': 'N', 'vendorId': 123, 'subaccountId': 999, 'status': 'in_content', 'typeOfVideo': None, } } } with pytest.raises(InvalidProductTypeException): index.handler(context_data, None) @patch('index.logger') @patch('index.graphql_gateway.execute') def test_get_associated_track_tuid_single( mock_graphql_execute, mock_current_logger, context_data): """Test get_associated_track_tuid with a single release.""" state_machine_context = StateMachineSchema().load(context_data) isrc = context_data['video']['associated_track_isrc'] vendor_id = context_data['product']['vendor_id'] subaccount_id = context_data['product']['subaccount_id'] expected_payload = { 'isrc': isrc, 'orchardLabelId': { 'vendorId': vendor_id, 'subaccountId': subaccount_id, } } mock_graphql_execute.return_value = { 'data': { 'orchardLabel': { 'labelSoundRecording': { 'tracks': [ { 'product': {'format': 'Full Length'}, 'trackType': 'music', 'tuid': '34807848' }, { 'product': {'format': 'Single'}, 'trackType': 'music', 'tuid': '34808683' }, ] } } } } result = index.get_associated_track_tuid(state_machine_context, isrc) mock_graphql_execute.assert_called_with( queries.get_tracks_by_isrc, expected_payload ) assert result == '34808683' @patch('index.logger') @patch('index.graphql_gateway.execute') def test_get_associated_track_tuid_default( mock_graphql_execute, mock_current_logger, context_data): """Test get_associated_track_tuid without a single release.""" state_machine_context = StateMachineSchema().load(context_data) isrc = context_data['video']['associated_track_isrc'] mock_graphql_execute.return_value = { 'data': { 'orchardLabel': { 'labelSoundRecording': { 'tracks': [ { 'product': {'format': 'Full Length'}, 'trackType': 'music', 'tuid': '34807848' }, { 'product': {'format': 'Not a Single'}, 'trackType': 'music', 'tuid': '34808683' }, ] } } } } result = index.get_associated_track_tuid(state_machine_context, isrc) assert result == '34807848' @patch('index.logger') @patch('index.graphql_gateway.execute') def test_get_associated_track_tuid_video( mock_graphql_execute, mock_current_logger, context_data): """Test get_associated_track_tuid with a video release.""" state_machine_context = StateMachineSchema().load(context_data) isrc = context_data['video']['associated_track_isrc'] mock_graphql_execute.return_value = { 'data': { 'orchardLabel': { 'labelSoundRecording': { 'tracks': [ { 'product': {'format': 'Full Length'}, 'trackType': 'video', 'tuid': '34807848' }, { 'product': {'format': 'Single'}, 'trackType': 'video', 'tuid': '34808683' }, ] } } } } result = index.get_associated_track_tuid(state_machine_context, isrc) assert result is None @patch('index.logger') @patch('index.graphql_gateway.execute') def test_get_associated_track_tuid_null( mock_graphql_execute, mock_current_logger, context_data): """Test get_associated_track_tuid without a release.""" state_machine_context = StateMachineSchema().load(context_data) isrc = context_data['video']['associated_track_isrc'] mock_graphql_execute.return_value = { 'data': { 'orchardLabel': None } } result = index.get_associated_track_tuid(state_machine_context, isrc) assert result is None @patch('index.logger') @patch('index.graphql_gateway.execute') def test_get_associated_track_tuid_null_2( mock_graphql_execute, mock_current_logger, context_data): """Test get_associated_track_tuid without a release.""" state_machine_context = StateMachineSchema().load(context_data) isrc = context_data['video']['associated_track_isrc'] mock_graphql_execute.return_value = { 'data': { 'orchardLabel': { 'labelSoundRecording': None } } } result = index.get_associated_track_tuid(state_machine_context, isrc) assert result is None