"""Test handler.""" import json from unittest.mock import MagicMock, patch import index import pytest from utils import project_utils # from utils import participant_utils @pytest.mark.parametrize( 'project_result,handler_result,', [ pytest.param( 'test_check_for_project_exists_result', 'test_handler_output_exists', id='Project Exists'), pytest.param( 'test_check_for_project_missing_result', 'test_handler_output_missing', id='New Project') ], ) @patch('index.find_subaccount_id') @patch('index.process_participants') @patch('index.check_project_code_mismatch') @patch('index.get_release_track_list') @patch('index.check_for_project') @patch('index.create_project') @patch('index.update_project') def test_handler( mock_update_project, mock_create_project, mock_check_for_project, mock_retrieve_release, mock_check_code_mismatch, mock_process_participants, mock_find_subaccount_id, project_result, handler_result, test_check_code_mismatch_result, test_process_participants_result, test_find_subaccount_id_result, test_create_project_result, test_update_project_result, test_release_json, test_event, test_model, request ): """Test handler.""" release_idx_dict = json.loads(test_release_json)['7892920191004_____1362655'] # noqa mock_retrieve_release.return_value = release_idx_dict project_result = request.getfixturevalue(project_result) handler_result = request.getfixturevalue(handler_result) mock_check_for_project.return_value = project_result mock_create_project.return_value = test_create_project_result mock_update_project.return_value = test_update_project_result mock_check_code_mismatch.return_value = test_check_code_mismatch_result mock_find_subaccount_id.return_value = test_find_subaccount_id_result # if not test_model.subaccount_id: test_model.subaccount_id = test_find_subaccount_id_result test_model.participants = test_process_participants_result mock_process_participants.return_value = test_model event = index.handler(test_event, None) assert event assert event == handler_result @patch('utils.project_utils.graphql_gateway.execute') @patch('utils.project_utils.save_catalog_ingestion_action') def test_create_project_payload( mock_catalog_ingestion_session, mock_graphql_execute, test_find_subaccount_id_result, test_model, test_event ): """Test that create project calls graphql with correct payload.""" payload = [ 'projectCode', 'name', 'artistId', 'description', 'accountId', 'subaccountId' ] # { # 'data': { # 'projectCode', # 'name', # 'artistId', # 'description', # 'accountId', # 'subaccountId' # } # } mock_graphql_execute.reset_mock() data = test_model test_model.subaccount_id = test_find_subaccount_id_result test_model.vendor_id = 28524 test_model.project_artist_id = 1 project_utils.create_project(test_event, data, 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 # @patch('utils.project_utils.graphql_gateway.execute') # def test_create_project_payload_fails_on_format( # mock_graphql_execute, test_model, test_event): # """Test that create project calls graphql with correct payload.""" # mock_graphql_execute.reset_mock() # data = test_model._project._replace(release_type='Broken') # with pytest.raises(ValueError): # project_utils.create_project(test_event, data, '')