"""Test handler.""" from unittest.mock import MagicMock, patch import pytest from ddex_ingester_common.constants.country_codes import (ALL_COUNTRY_CODES, WORLDWIDE) from ddex_ingester_common.lambda_exceptions import CarveoutException from ddex_ingester_common.release_correction.release_correction_constants import \ RELEASE_CORRECTION_CARVEOUT_FIELDS # noqa from ddex_ingester_common.schemas.s3_schema import S3Schema from ddex_ingester_common.schemas.state_machine_schema import \ StateMachineSchema import index from constants import queries @patch('index.logger') @patch('index.graphql.GraphQLConnector.set_headers') @patch('index.graphql.GraphQLConnector.execute') @patch('index.load_ddex_json') def test_handler( mock_load_ddex_json, mock_graphql_execute, mock_graphql_set_headers, mock_current_logger): """Test the main handler.""" mock_logger = MagicMock(name='info') mock_current_logger.return_value = mock_logger mock_event = { 'product': { 'status': None, 'display_artist_name': 'Cool Artist', 'vendor_id': None, 'subaccount_id': None, 'upc': '231232563123', 'product_id': 234324, 'is_main_release': True, 'release_type': 'release', 'release_reference': 'R0', 'artwork': None, 'original_release_date': None, 'sale_start_date': None, 'grid': 'A0145345234523453452', 'catalog_number': 'G0134523452345245', 'original_values': {'sale_start_date': None}, 'product_name': 'Test Artist', 'genres': [ { 'genre': 'ELECTRONICA-00', 'subgenre': 'Alternative' } ], }, 'tracks': None, 'deals': [ { 'release_references': ['R0'], 'deal_terms': [ { 'start_date': None, 'start_date_time': '2020-03-27T00:00:00', 'territories': [ 'AE', 'US', 'GB', 'BQ', 'CW', 'ME', 'SS'], 'commercial_model_types': [ 'AdvertisementSupportedModel' ], 'usage_type': [ 'NonInteractiveStream', 'OnDemandStream' ], 'takedown': None, }, ] }, { 'release_references': ['R1', 'R2'], 'deal_terms': [ { 'start_date': None, 'start_date_time': '2020-03-27T00:00:00', 'territories': ['US', 'GB'], 'commercial_model_types': [ 'AdvertisementSupportedModel' ], 'usage_type': [ 'NonInteractiveStream', 'OnDemandStream' ], 'takedown': None, }, ] } ], 'bucket': 'ddex-bucket', 'key': 'key', 'errors': None, 'message_id': '12345', 'message_thread_id': '12345_12345', 'party_id': 'PADPIDA2012041004F', 'update_indicator': 'OriginalMessage' } mock_load_ddex_json.return_value = mock_event mock_graphql_execute.return_value = True index.handler(mock_event, None) assert mock_graphql_set_headers.called mock_graphql_execute.assert_called_with( queries.set_carveouts, { 'data': { 'productId': 234324, 'upc': '231232563123', 'countryCodes': [ 'AD', 'AF', 'AG', 'AI', 'AL', 'AM', 'AO', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AW', 'AX', 'AZ', 'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BM', 'BN', 'BO', 'BR', 'BS', 'BT', 'BV', 'BW', 'BY', 'BZ', 'CA', 'CC', 'CD', 'CF', 'CG', 'CH', 'CI', 'CK', 'CL', 'CM', 'CN', 'CO', 'CR', 'CU', 'CV', 'CX', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE', 'EG', 'EH', 'ER', 'ES', 'ET', 'FI', 'FJ', 'FK', 'FM', 'FO', 'FR', 'GA', 'GD', 'GE', 'GF', 'GG', 'GH', 'GI', 'GL', 'GM', 'GN', 'GP', 'GQ', 'GR', 'GS', 'GT', 'GU', 'GW', 'GY', 'HK', 'HM', 'HN', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IM', 'IN', 'IO', 'IQ', 'IR', 'IS', 'IT', 'JE', 'JM', 'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM', 'KN', 'KP', 'KR', 'KW', 'KY', 'KZ', 'LA', 'LB', 'LC', 'LI', 'LK', 'LR', 'LS', 'LT', 'LU', 'LV', 'LY', 'MA', 'MC', 'MD', 'MG', 'MH', 'MK', 'ML', 'MM', 'MN', 'MO', 'MP', 'MQ', 'MR', 'MS', 'MT', 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ', 'NA', 'NC', 'NE', 'NF', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NU', 'NZ', 'OM', 'PA', 'PE', 'PF', 'PG', 'PH', 'PK', 'PL', 'PM', 'PN', 'PR', 'PS', 'PT', 'PW', 'PY', 'QA', 'RE', 'RO', 'RU', 'RW', 'SA', 'SB', 'SC', 'SD', 'SE', 'SG', 'SH', 'SI', 'SJ', 'SK', 'SL', 'SM', 'SN', 'SO', 'SR', 'ST', 'SV', 'SY', 'SZ', 'TC', 'TD', 'TF', 'TG', 'TH', 'TJ', 'TK', 'TL', 'TM', 'TN', 'TO', 'TR', 'TT', 'TV', 'TW', 'TZ', 'UA', 'UG', 'UM', 'UY', 'UZ', 'VA', 'VC', 'VE', 'VG', 'VI', 'VN', 'VU', 'WF', 'WS', 'YE', 'YT', 'ZA', 'ZM', 'ZW', ] } } ) @patch('index.logger') @patch('index.graphql.GraphQLConnector.set_headers') @patch('index.graphql.GraphQLConnector.execute') @patch('index.load_ddex_json') def test_handler_carveout_updates_with_nfd_n( mock_load_ddex_json, mock_graphql_execute, mock_graphql_set_headers, mock_current_logger, mock_event): """Test the main handler rejects carveouts update.""" mock_logger = MagicMock(name='info') mock_current_logger.return_value = mock_logger mock_event['product']['original_values']['carveout_country_codes'] =\ ['TA'] mock_event['deals'][0]['deal_terms'][0]['territories'] =\ ['AE', 'US', 'GB', 'BQ', 'CW', 'ME', 'SS'] mock_load_ddex_json.return_value = mock_event mock_graphql_execute.return_value = True context = index.handler(mock_event, None) expected = [ { 'field': 'carveouts', 'warning_type': 'ReleaseCorrectionUpdateException', 'exception': "Product Carveouts update attempt. DDEX values ['AD', 'AF', 'AG', 'AI', 'AL', 'AM', 'AO', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AW', 'AX', 'AZ', 'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BM', 'BN', 'BO', 'BR', 'BS', 'BT', 'BV', 'BW', 'BY', 'BZ', 'CA', 'CC', 'CD', 'CF', 'CG', 'CH', 'CI', 'CK', 'CL', 'CM', 'CN', 'CO', 'CR', 'CU', 'CV', 'CX', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE', 'EG', 'EH', 'ER', 'ES', 'ET', 'FI', 'FJ', 'FK', 'FM', 'FO', 'FR', 'GA', 'GD', 'GE', 'GF', 'GG', 'GH', 'GI', 'GL', 'GM', 'GN', 'GP', 'GQ', 'GR', 'GS', 'GT', 'GU', 'GW', 'GY', 'HK', 'HM', 'HN', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IM', 'IN', 'IO', 'IQ', 'IR', 'IS', 'IT', 'JE', 'JM', 'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM', 'KN', 'KP', 'KR', 'KW', 'KY', 'KZ', 'LA', 'LB', 'LC', 'LI', 'LK', 'LR', 'LS', 'LT', 'LU', 'LV', 'LY', 'MA', 'MC', 'MD', 'MG', 'MH', 'MK', 'ML', 'MM', 'MN', 'MO', 'MP', 'MQ', 'MR', 'MS', 'MT', 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ', 'NA', 'NC', 'NE', 'NF', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NU', 'NZ', 'OM', 'PA', 'PE', 'PF', 'PG', 'PH', 'PK', 'PL', 'PM', 'PN', 'PR', 'PS', 'PT', 'PW', 'PY', 'QA', 'RE', 'RO', 'RU', 'RW', 'SA', 'SB', 'SC', 'SD', 'SE', 'SG', 'SH', 'SI', 'SJ', 'SK', 'SL', 'SM', 'SN', 'SO', 'SR', 'ST', 'SV', 'SY', 'SZ', 'TC', 'TD', 'TF', 'TG', 'TH', 'TJ', 'TK', 'TL', 'TM', 'TN', 'TO', 'TR', 'TT', 'TV', 'TW', 'TZ', 'UA', 'UG', 'UM', 'UY', 'UZ', 'VA', 'VC', 'VE', 'VG', 'VI', 'VN', 'VU', 'WF', 'WS', 'YE', 'YT', 'ZA', 'ZM', 'ZW'] does not match The Orchard's Carveouts ['TA']" # noqa } ] assert mock_graphql_set_headers.called assert context.get('warnings') == expected mock_graphql_execute.assert_not_called() @patch('index.logger') @patch('index.graphql.GraphQLConnector.set_headers') @patch('index.graphql.GraphQLConnector.execute') @patch('index.load_ddex_json') def test_handler_carveout_no_updates_with_nfd_n( mock_load_ddex_json, mock_graphql_execute, mock_graphql_set_headers, mock_current_logger, mock_event): """Test handler passes when there are no carveout updates.""" mock_logger = MagicMock(name='info') mock_current_logger.return_value = mock_logger mock_load_ddex_json.return_value = mock_event mock_graphql_execute.return_value = True index.handler(mock_event, None) assert mock_graphql_set_headers.called mock_graphql_execute.assert_not_called() def test_map_country_codes(): """Test map_country_codes.""" country_codes = ['US', 'BQ', 'ME'] # Countries not to be carved out allowed_country_codes = {'US', 'BQ', 'ME', 'AN', 'CS'} assert index.map_country_codes(country_codes) == sorted(list( set(ALL_COUNTRY_CODES) - allowed_country_codes)) def test_format_carveout_data(mock_event_2): """Test format_carveout_data.""" product_id = 123 parsed_ddex = S3Schema().load(mock_event_2) expected_response = { 'productId': product_id, 'upc': '231232563123', 'countryCodes': [ 'AE', 'US', 'GB', 'BQ', 'CW', 'ME', 'SS', 'BE' ] } carveout_data = index.format_carveout_data( parsed_ddex.get_product_deal_terms(), product_id, parsed_ddex.product.upc ) assert carveout_data == expected_response def test_format_carveout_data_end_date_in_past(mock_event_2): """Test format_carveout_data where term EndDate is in the past.""" product_id = 123 parsed_ddex = S3Schema().load(mock_event_2) parsed_ddex.get_product_deal_terms()[0].end_date_time = '2000-01-01' carveout_data = index.format_carveout_data( parsed_ddex.get_product_deal_terms(), product_id, parsed_ddex.product.upc ) assert carveout_data['countryCodes'] == ['BE'] @patch('index.logger') @patch('index.graphql.GraphQLConnector.set_headers') @patch('index.graphql.GraphQLConnector.execute') @patch('index.load_ddex_json') def test_no_r1_or_r0( mock_load_ddex_json, mock_graphql_execute, mock_graphql_set_headers, mock_current_logger): """Test the main handler.""" mock_logger = MagicMock(name='info') mock_current_logger.return_value = mock_logger mock_event = { 'product': { 'status': None, 'display_artist_name': 'Cool Artist', 'vendor_id': None, 'subaccount_id': None, 'upc': '231232563123', 'product_id': 234324, 'is_main_release': True, 'release_type': 'release', 'release_reference': 'R0', 'artwork': None, 'original_release_date': None, 'sale_start_date': None, 'grid': 'A0145345234523453452', 'catalog_number': 'G0134523452345245', 'original_values': {'sale_start_date': None}, 'product_name': 'Test Artist', 'genres': [ { 'genre': 'ELECTRONICA-00', 'subgenre': 'Alternative' } ], }, 'tracks': None, 'deals': [ { 'release_references': ['R2', 'R3'], 'deal_terms': [ { 'start_date': None, 'start_date_time': '2020-03-27T00:00:00', 'territories': [ 'AE', 'US', 'GB', 'BQ', 'CW', 'ME', 'SS'], 'commercial_model_types': [ 'AdvertisementSupportedModel' ], 'usage_type': [ 'NonInteractiveStream', 'OnDemandStream' ], 'takedown': None, }, ] } ], 'bucket': 'ddex-bucket', 'key': 'key', 'errors': None, 'message_id': '12345', 'message_thread_id': '12345_12345', 'party_id': 'PADPIDA2012041004F', 'update_indicator': 'OriginalMessage' } mock_load_ddex_json.return_value = mock_event mock_graphql_execute.return_value = True with pytest.raises(CarveoutException): index.handler(mock_event, None) @patch('index.logger') @patch('index.graphql.GraphQLConnector.set_headers') @patch('index.graphql.GraphQLConnector.execute') @patch('index.load_ddex_json') def test_with_r1_no_r0( mock_load_ddex_json, mock_graphql_execute, mock_graphql_set_headers, mock_current_logger): """Test the main handler.""" mock_logger = MagicMock(name='info') mock_current_logger.return_value = mock_logger mock_event = { 'product': { 'status': None, 'display_artist_name': 'Cool Artist', 'vendor_id': None, 'subaccount_id': None, 'upc': '231232563123', 'product_id': 234324, 'is_main_release': True, 'release_type': 'release', 'release_reference': 'R0', 'artwork': None, 'original_release_date': None, 'sale_start_date': None, 'grid': 'A0145345234523453452', 'catalog_number': 'G0134523452345245', 'original_values': {'sale_start_date': None}, 'product_name': 'Test Artist', 'genres': [ { 'genre': 'ELECTRONICA-00', 'subgenre': 'Alternative' } ], }, 'tracks': None, 'deals': [ { 'release_references': ['R0', 'R1'], 'deal_terms': [ { 'start_date': None, 'start_date_time': '2020-03-27T00:00:00', 'territories': [ 'AE', 'US', 'GB', 'BQ', 'CW', 'ME', 'SS'], 'commercial_model_types': [ 'AdvertisementSupportedModel' ], 'usage_type': [ 'NonInteractiveStream', 'OnDemandStream' ], 'takedown': None, }, ] } ], 'bucket': 'ddex-bucket', 'key': 'key', 'errors': None, 'message_id': '12345', 'message_thread_id': '12345_12345', 'party_id': 'PADPIDA2012041004F', 'update_indicator': 'OriginalMessage' } mock_load_ddex_json.return_value = mock_event mock_graphql_execute.return_value = True index.handler(mock_event, None) assert mock_graphql_set_headers.called mock_graphql_execute.assert_called_with( queries.set_carveouts, { 'data': { 'productId': 234324, 'upc': '231232563123', 'countryCodes': [ 'AD', 'AF', 'AG', 'AI', 'AL', 'AM', 'AO', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AW', 'AX', 'AZ', 'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BM', 'BN', 'BO', 'BR', 'BS', 'BT', 'BV', 'BW', 'BY', 'BZ', 'CA', 'CC', 'CD', 'CF', 'CG', 'CH', 'CI', 'CK', 'CL', 'CM', 'CN', 'CO', 'CR', 'CU', 'CV', 'CX', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE', 'EG', 'EH', 'ER', 'ES', 'ET', 'FI', 'FJ', 'FK', 'FM', 'FO', 'FR', 'GA', 'GD', 'GE', 'GF', 'GG', 'GH', 'GI', 'GL', 'GM', 'GN', 'GP', 'GQ', 'GR', 'GS', 'GT', 'GU', 'GW', 'GY', 'HK', 'HM', 'HN', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IM', 'IN', 'IO', 'IQ', 'IR', 'IS', 'IT', 'JE', 'JM', 'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM', 'KN', 'KP', 'KR', 'KW', 'KY', 'KZ', 'LA', 'LB', 'LC', 'LI', 'LK', 'LR', 'LS', 'LT', 'LU', 'LV', 'LY', 'MA', 'MC', 'MD', 'MG', 'MH', 'MK', 'ML', 'MM', 'MN', 'MO', 'MP', 'MQ', 'MR', 'MS', 'MT', 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ', 'NA', 'NC', 'NE', 'NF', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NU', 'NZ', 'OM', 'PA', 'PE', 'PF', 'PG', 'PH', 'PK', 'PL', 'PM', 'PN', 'PR', 'PS', 'PT', 'PW', 'PY', 'QA', 'RE', 'RO', 'RU', 'RW', 'SA', 'SB', 'SC', 'SD', 'SE', 'SG', 'SH', 'SI', 'SJ', 'SK', 'SL', 'SM', 'SN', 'SO', 'SR', 'ST', 'SV', 'SY', 'SZ', 'TC', 'TD', 'TF', 'TG', 'TH', 'TJ', 'TK', 'TL', 'TM', 'TN', 'TO', 'TR', 'TT', 'TV', 'TW', 'TZ', 'UA', 'UG', 'UM', 'UY', 'UZ', 'VA', 'VC', 'VE', 'VG', 'VI', 'VN', 'VU', 'WF', 'WS', 'YE', 'YT', 'ZA', 'ZM', 'ZW', ] } } ) @patch('index.check_for_unsupported_updates', wraps=index.check_for_unsupported_updates) def test_unsupported_update_audio( mock_check_unsupported_updates, mock_event): """Test check_for_unsupported_updates method.""" state_machine_context = StateMachineSchema().load(mock_event) new_country_codes = ['US', 'CA'] index.check_for_unsupported_updates( state_machine_context, new_country_codes) assert state_machine_context.warnings @patch('index.create_blank_rc_json') @patch('index.write_rc_json') @patch('index.check_for_unsupported_updates', wraps=index.check_for_unsupported_updates) def test_unsupported_update_video( mock_check_unsupported_updates, mock_write_rc, mock_create_blank_rc_json, mock_event, s3_release_corrections): """Test check_for_unsupported_updates with video updates.""" state_machine_context = StateMachineSchema().load(mock_event) new_country_codes = ['US', 'CA'] index.check_for_unsupported_updates( state_machine_context, new_country_codes, True) mock_write_rc.assert_called_once() @patch('index.check_for_unsupported_updates', wraps=index.check_for_unsupported_updates) def test_unsupported_update_no_updates( mock_check_unsupported_updates, mock_event): """Test check_for_unsupported_updates method returns without exception.""" state_machine_context = StateMachineSchema().load(mock_event) expected_codes = ['AU', 'DE'] state_machine_context.product.original_values.carveout_country_codes =\ expected_codes new_country_codes = expected_codes index.check_for_unsupported_updates( state_machine_context, new_country_codes, {}) assert not state_machine_context.warnings def test_format_carveout_data_excluded_territories(mock_event_2): """Test format_carveout_data with excluded territories.""" product_id = 123 parsed_ddex = S3Schema().load(mock_event_2) deal_terms = parsed_ddex.deals[0].deal_terms deal_terms[0].excluded_territories = deal_terms[0].territories deal_terms[1].excluded_territories = deal_terms[1].territories deal_terms[0].territories = None deal_terms[1].territories = None expected_response = { 'productId': product_id, 'upc': '231232563123', 'countryCodes': [ 'BD', 'BE', 'BY', 'UG', 'BR', 'NG', 'FI', 'PH', 'VA', 'WS', 'FK', 'CU', 'GE', 'KP', 'AQ', 'IL', 'PA', 'DJ', 'PM', 'GD', 'TV', 'YT', 'QA', 'SV', 'BH', 'MU', 'ES', 'DO', 'CH', 'GA', 'NZ', 'AD', 'BN', 'BO', 'GT', 'AZ', 'CI', 'PS', 'OM', 'TO', 'SB', 'AG', 'MQ', 'FJ', 'LS', 'WF', 'JO', 'ER', 'MH', 'TF', 'CM', 'TJ', 'IM', 'UM', 'JE', 'LV', 'HK', 'ID', 'VN', 'NU', 'CY', 'TL', 'KR', 'AS', 'SI', 'MK', 'MV', 'GF', 'KY', 'NE', 'LC', 'GH', 'ZA', 'BA', 'BF', 'EG', 'MC', 'PE', 'PG', 'GP', 'FO', 'CG', 'IO', 'MD', 'MX', 'AT', 'CC', 'FR', 'KN', 'PL', 'CL', 'DK', 'KI', 'GS', 'CK', 'HT', 'MN', 'ST', 'VC', 'SL', 'ZW', 'IT', 'KM', 'NF', 'GQ', 'GW', 'LR', 'NL', 'MZ', 'LI', 'GL', 'VI', 'YE', 'AM', 'TR', 'PN', 'TD', 'AO', 'LU', 'BJ', 'CS', 'ML', 'PW', 'UA', 'MM', 'ZM', 'AN', 'AR', 'BI', 'BS', 'NP', 'EE', 'IN', 'NI', 'SK', 'MW', 'BZ', 'VE', 'RE', 'NR', 'GN', 'BV', 'KW', 'BT', 'DZ', 'SM', 'BW', 'CR', 'SY', 'MO', 'PT', 'TM', 'MP', 'JM', 'LB', 'MA', 'AU', 'EH', 'VU', 'LA', 'MT', 'CA', 'ET', 'GG', 'GI', 'GU', 'HM', 'TT', 'BM', 'NC', 'RO', 'CD', 'DE', 'FM', 'MS', 'SN', 'CO', 'TG', 'EC', 'SD', 'PR', 'HR', 'VG', 'CN', 'MR', 'IE', 'TH', 'HN', 'SA', 'IQ', 'TK', 'CV', 'CX', 'DM', 'AL', 'MY', 'SC', 'LY', 'SG', 'SZ', 'TZ', 'UZ', 'UY', 'PY', 'GM', 'SR', 'SH', 'PK', 'JP', 'BB', 'KG', 'SO', 'TN', 'RU', 'KZ', 'SE', 'CF', 'KE', 'AF', 'TW', 'AX', 'GR', 'BG', 'AI', 'RW', 'NO', 'AW', 'LT', 'HU', 'KH', 'MG', 'GY', 'TC', 'LK', 'CZ', 'IS', 'NA', 'PF', 'IR', 'SJ', 'BD', 'BY', 'UG', 'BR', 'NG', 'FI', 'PH', 'VA', 'WS', 'FK', 'CU', 'GE', 'KP', 'AQ', 'IL', 'PA', 'DJ', 'PM', 'GD', 'TV', 'YT', 'QA', 'SV', 'BH', 'MU', 'ES', 'DO', 'CH', 'GA', 'NZ', 'AD', 'BN', 'BO', 'GT', 'AZ', 'CI', 'PS', 'OM', 'TO', 'SB', 'AG', 'MQ', 'FJ', 'LS', 'WF', 'JO', 'ER', 'MH', 'TF', 'CM', 'TJ', 'IM', 'UM', 'JE', 'LV', 'HK', 'ID', 'VN', 'NU', 'CY', 'TL', 'KR', 'AS', 'SI', 'MK', 'MV', 'GF', 'KY', 'NE', 'LC', 'GH', 'ZA', 'BA', 'BF', 'EG', 'MC', 'PE', 'PG', 'GP', 'FO', 'CG', 'IO', 'MD', 'MX', 'AT', 'CC', 'FR', 'KN', 'PL', 'CL', 'DK', 'KI', 'GS', 'CK', 'HT', 'MN', 'ST', 'VC', 'SL', 'ZW', 'IT', 'KM', 'NF', 'GQ', 'GW', 'LR', 'NL', 'MZ', 'LI', 'GL', 'VI', 'YE', 'AM', 'TR', 'GB', 'PN', 'TD', 'AO', 'LU', 'BJ', 'CS', 'ML', 'PW', 'UA', 'MM', 'ZM', 'AN', 'AR', 'BI', 'BS', 'NP', 'EE', 'IN', 'NI', 'SK', 'MW', 'BZ', 'VE', 'RE', 'NR', 'GN', 'BV', 'KW', 'BT', 'DZ', 'SM', 'BW', 'CR', 'SY', 'MO', 'PT', 'TM', 'MP', 'JM', 'LB', 'MA', 'AU', 'EH', 'VU', 'LA', 'MT', 'CA', 'ET', 'GG', 'GI', 'GU', 'HM', 'TT', 'BM', 'NC', 'RO', 'CD', 'DE', 'FM', 'MS', 'SN', 'CO', 'TG', 'EC', 'SD', 'PR', 'HR', 'VG', 'CN', 'MR', 'IE', 'TH', 'HN', 'SA', 'IQ', 'TK', 'CV', 'CX', 'DM', 'US', 'AL', 'MY', 'SC', 'LY', 'SG', 'SZ', 'TZ', 'UZ', 'UY', 'PY', 'GM', 'SR', 'SH', 'PK', 'JP', 'BB', 'KG', 'SO', 'TN', 'RU', 'KZ', 'SE', 'CF', 'KE', 'AF', 'TW', 'AX', 'GR', 'BG', 'AI', 'RW', 'NO', 'AW', 'LT', 'HU', 'KH', 'MG', 'GY', 'TC', 'LK', 'CZ', 'IS', 'NA', 'PF', 'IR', 'AE', 'SJ' ] } carveout_data = index.format_carveout_data( parsed_ddex.get_product_deal_terms(), product_id, parsed_ddex.product.upc ) assert sorted(carveout_data['countryCodes']) == sorted( expected_response['countryCodes']) def test_format_carveout_data_worldwide(mock_event_2): """Test format_carveout_data with worldwide territory.""" product_id = 123 parsed_ddex = S3Schema().load(mock_event_2) parsed_ddex.deals[0].deal_terms[0].territories = WORLDWIDE carveout_data = index.format_carveout_data( parsed_ddex.get_product_deal_terms(), product_id, parsed_ddex.product.upc ) assert carveout_data['productId'] == product_id assert carveout_data['upc'] == '231232563123' assert set(carveout_data['countryCodes']) == set(ALL_COUNTRY_CODES) def test_set_store_carveouts(mock_event, mock_event_2): """Test set_store_carveouts.""" context = StateMachineSchema().load(mock_event) s3_data = S3Schema().load(mock_event_2) s3_data.product.stores[0].store_id = '286' mock_graphql_conn = MagicMock() expected_payload = { 'data': { 'productId': context.product.product_id, 'storeCarveouts': [{ 'storeId': 286, 'distributionTypeIds': [1], }] } } index.set_store_carveouts(mock_graphql_conn, s3_data, context) mock_graphql_conn.execute.assert_called_with( queries.set_store_carveouts, expected_payload ) def test_set_store_carveouts_distribution_type_ids(mock_event, mock_event_2): """Test set_store_carveouts with a special case of distributionTypeIds.""" context = StateMachineSchema().load(mock_event) context.ddex_provider = 'AWAL' context.product.release_type = 'VideoSingle' s3_data = S3Schema().load(mock_event_2) s3_data.product.stores[0].store_id = '286' mock_graphql_conn = MagicMock() expected_payload = { 'data': { 'productId': context.product.product_id, 'storeCarveouts': [{ 'storeId': 286, 'distributionTypeIds': [3], }] } } index.set_store_carveouts(mock_graphql_conn, s3_data, context) mock_graphql_conn.execute.assert_called_with( queries.set_store_carveouts, expected_payload )