"""Test handler.""" import json from unittest.mock import MagicMock, patch from constants.exceptions import ( CheckProductException, CreateProductException, UpdateProductException) import index from lambdacommon.graphql.graphql import GraphQLError import pytest from utils import product_utils @pytest.mark.parametrize( 'product_result,handler_result,is_new', [ pytest.param( 'test_check_for_product_exists_result', 'test_handler_output_exists', False, id='Product Exists'), pytest.param( 'test_check_for_product_missing_result', 'test_handler_output_missing', True, id='New Product') ], ) # @patch('index.graphql_gateway.execute') @patch('index.get_release_track_list') # @patch('index.config.catalog_ingestion_session') @patch('index.check_for_product') @patch('index.create_product') @patch('index.update_product') @patch('index.get_orchard_genre_mapping') def test_handler( mock_get_orchard_genre_mapping, mock_update_product, mock_create_product, mock_check_for_product, mock_retrieve_release, product_result, handler_result, is_new, test_get_genre_mapping_result, test_create_product_result, test_update_product_result, test_release_json, test_event, request): """Tests the handler function with different parameters.""" release_idx_dict = \ json.loads(test_release_json)['7892920191004_____1362655'] if not is_new: # Clear out the upc so it's replaced. release_idx_dict[0]['Digital UPC'] = '' mock_retrieve_release.return_value = release_idx_dict product_result = request.getfixturevalue(product_result) handler_result = request.getfixturevalue(handler_result) mock_check_for_product.return_value = product_result mock_create_product.return_value = test_create_product_result mock_update_product.return_value = test_update_product_result mock_get_orchard_genre_mapping.return_value = test_get_genre_mapping_result event = index.handler(test_event, None) assert event == handler_result @pytest.mark.parametrize( 'test_method, test_check_return_value, expected_exception', [ pytest.param( 'check', 'test_check_for_product_missing_result', CheckProductException, id='Check Fails'), pytest.param( 'create', 'test_check_for_product_missing_result', CreateProductException, id='Create Fails'), pytest.param( 'update', 'test_check_for_product_exists_result', UpdateProductException, id='Update Fails') ], ) @patch('index.check_for_product') @patch('index.create_product') @patch('index.update_product') @patch('index.get_orchard_genre_mapping') def test_set_product_fails( mock_get_orchard_genre_mapping, mock_update_product, mock_create_product, mock_check_for_product, test_check_return_value, test_method, expected_exception, test_get_genre_mapping_result, test_model, test_event, request): """Tests the handler function with different parameters.""" # Set the return value for check_for_product() test_check_return_value = request.getfixturevalue(test_check_return_value) mock_check_for_product.return_value = test_check_return_value # Genre mapping mock mock_get_orchard_genre_mapping.return_value = test_get_genre_mapping_result # Change conditions based on parameter if test_method == 'check': mock_check_for_product.side_effect = GraphQLError( MagicMock( return_value={ 'body': { 'message': 'Check Failed' } } ) ) if test_method == 'create': mock_create_product.side_effect = GraphQLError( MagicMock( return_value={ 'body': { 'message': 'Create Failed' } } ) ) if test_method == 'update': mock_update_product.side_effect = GraphQLError( MagicMock( return_value={ 'body': { 'message': 'Update Failed' } } ) ) # Check the exception with pytest.raises(expected_exception): index.set_product(test_event, test_model, MagicMock()) @patch('utils.product_utils.graphql_gateway.execute') def test_create_product_payload( mock_graphql_execute, test_model, test_event ): """Test that create product calls graphql with correct payload.""" payload = [ 'productName', 'productCode', 'productHighlights', 'projectId', 'accountId', 'subaccountId', 'upc', 'metaLanguage', 'version', 'pLine', 'format', 'imprint', 'notForDistribution', 'participations', 'genreId', 'subgenreId', 'manufacturerUpc', 'cLine', ] # Patch here with context. Modified for python ~3.8.14 with patch('utils.product_utils.save_catalog_ingestion_action'), \ patch('utils.product_utils.get_participations'): product_utils.create_product(test_event, test_model, MagicMock()) call_dict = mock_graphql_execute.mock_calls[0][1][1]['data'] call_keys = list(call_dict) mock_graphql_execute.assert_called_once() assert payload == call_keys def test_create_product_payload_fails_on_format(test_model, test_event): """Test that create product calls graphql with correct payload.""" # Create a new data object with an invalid release type data = test_model._product._replace(release_type='Broken') # Patch the graphql_gateway.execute method with patch('utils.product_utils.graphql_gateway.execute'): # Assert that a ValueError is raised when calling with invalid data with pytest.raises(ValueError): # Call the create_product function product_utils.create_product(test_event, data, MagicMock()) @patch('utils.product_utils.graphql_gateway.execute') def test_update_product_payload( mock_graphql_execute, test_model, test_event): """Test that create product calls graphql with correct payload.""" # Define the expected keys in the payload payload = [ 'productName', 'productId', 'accountId', 'productHighlights', 'metaLanguage', 'participations', 'pLine', 'genreId', 'subgenreId', 'imprint', 'notForDistribution', 'format', 'manufacturerUpc', 'cLine', 'version' ] # Set genre_id and subgenre_id test_model.genre_id = 1 test_model.subgenre_id = 10 # Patch here with context. Modified for python ~3.8.14 with patch('utils.product_utils.save_catalog_ingestion_action'), \ patch('utils.product_utils.get_participations'): # Call the update_product function product_utils.update_product(test_event, test_model, MagicMock()) # Get the dictionary from the mock graphql execute call and get the keys call_dict = mock_graphql_execute.mock_calls[0][1][1]['data'] call_keys = list(call_dict) # Assert that the mock graphql execute method was called once and that the # expected payload keys match the actual keys mock_graphql_execute.assert_called_once() assert payload == call_keys def test_update_product_payload_fails_on_format(test_model, test_event): """Test that update product calls graphql with correct payload.""" # Create a new data object with an invalid release type data = test_model._product._replace(release_type='Broken') # Patch the graphql_gateway.execute method with patch('utils.product_utils.graphql_gateway.execute'): # Assert that a ValueError is raised when calling with the invalid data with pytest.raises(ValueError): product_utils.update_product(test_event, data, MagicMock())