"""Test graphql_requests helper.""" from unittest.mock import MagicMock, patch import config from constants import general, graphql_queries from ddex_ingester_common.schemas.s3_schema import S3Schema from helpers.graphql_requests import ( create_new_subaccount, create_new_vendor, get_product_by_upc, get_subaccount_for_vendor, get_vendor_for_rep_owner_code, update_external_identifier_1) @patch('index.logger') @patch('config.ows_client.graphql_query') def test_get_vendor_for_rep_owner_code( mock_graphql, mock_logger, s3_ddex): """Test get_vendor_for_rep_owner_code.""" s3_context = S3Schema().load(s3_ddex) s3_context.correlation_id = 'e2d336b3-fbf2-4e51-95fc-c434bc92a2d2' # Mock the return value of graphql_query mock_response = MagicMock() mock_response.json.return_value = { 'data': {'vendorsByExternalIdentifier': [{'vendorId': '123'}]} } mock_graphql.return_value = mock_response # Call the function result = get_vendor_for_rep_owner_code(mock_logger, s3_context) # Assertions assert result == [{'vendorId': '123'}] mock_graphql.assert_called_once_with( service_name=config.GRAPHQL_SERVICE, query=graphql_queries.get_vendor, variables={ 'externalIdentifier': 'A123', 'owner': config.OWNER }, correlation_id='e2d336b3-fbf2-4e51-95fc-c434bc92a2d2', **config.GRAPHQL_HEADERS, ) @patch('index.logger') @patch('config.ows_client.graphql_query') def test_get_subaccount_for_vendor( mock_graphql, mock_logger, s3_ddex): """Test get_subaccount_for_vendor.""" s3_context = S3Schema().load(s3_ddex) s3_context.correlation_id = 'e2d336b3-fbf2-4e51-95fc-c434bc92a2d2' # Mock the return value of graphql_query mock_response = MagicMock() mock_response.json.return_value = { 'data': { 'vendor': { 'subaccounts': { 'subaccounts': [ { 'subaccountId': 1212, 'name': 'Subaccount Name' }, { 'subaccountId': 1213, 'name': 'Subaccount Name2' }, ] } } } } mock_graphql.return_value = mock_response # Call the function result = get_subaccount_for_vendor(mock_logger, s3_context, 123) # Assertions assert result == [{'name': 'Subaccount Name', 'subaccountId': 1212}] mock_graphql.assert_called_once_with( service_name='graphql-router', query=graphql_queries.get_subaccounts, variables={ 'vendorId': 123, 'limit': config.LIMIT_SUBACCOUNTS }, correlation_id='e2d336b3-fbf2-4e51-95fc-c434bc92a2d2', **config.GRAPHQL_HEADERS, ) @patch('index.logger') @patch('config.ows_client.graphql_query') def test_get_product_by_upc( mock_graphql, mock_logger, s3_ddex): """Test get_product_by_upc.""" s3_context = S3Schema().load(s3_ddex) s3_context.correlation_id = 'e2d336b3-fbf2-4e51-95fc-c434bc92a2d2' # Mock the return value of graphql_query mock_response = MagicMock() mock_response.json.return_value = { 'data': {'productByUpc': {'vendorId': 123, 'subaccountId': 456, 'notForDistribution': 'N'}} } mock_graphql.return_value = mock_response # Call the function result = get_product_by_upc(mock_logger, s3_context) # Assertions assert result == {'vendorId': 123, 'subaccountId': 456, 'notForDistribution': 'N'} mock_graphql.assert_called_once_with( service_name=config.GRAPHQL_SERVICE, query=graphql_queries.get_product, variables={ 'upc': '886448369561' }, correlation_id='e2d336b3-fbf2-4e51-95fc-c434bc92a2d2', **config.GRAPHQL_HEADERS, ) @patch('index.logger') @patch('config.ows_client.graphql_query') def test_get_product_by_upc_product_does_not_exist( mock_graphql, mock_logger, s3_ddex): """Test get_product_by_upc.""" s3_context = S3Schema().load(s3_ddex) s3_context.correlation_id = 'e2d336b3-fbf2-4e51-95fc-c434bc92a2d2' # Mock the return value of graphql_query mock_response = MagicMock() mock_response.json.return_value = { 'data': {'productByUpc': None} } mock_graphql.return_value = mock_response # Call the function result = get_product_by_upc(mock_logger, s3_context) # Assertions assert result is None mock_graphql.assert_called_once_with( service_name=config.GRAPHQL_SERVICE, query=graphql_queries.get_product, variables={ 'upc': '886448369561' }, correlation_id='e2d336b3-fbf2-4e51-95fc-c434bc92a2d2', **config.GRAPHQL_HEADERS, ) @patch('index.logger') @patch('config.ows_client.graphql_query') def test_create_new_vendor(mock_graphql_query, mock_logger, s3_ddex): """Test create_new_vendor function.""" # Mock logger s3_context = S3Schema().load(s3_ddex) s3_context.correlation_id = 'e2d336b3-fbf2-4e51-95fc-c434bc92a2d2' # Mock parent_repertoire_owner_name s3_context.product.parent_repertoire_owner_name = 'Test Owner' # Mock GraphQL response mock_response = MagicMock() mock_response.json.return_value = { 'data': { 'createVendor': { 'vendorId': '123', 'name': 'Test Owner', 'uuid': '6821fb18-1b66-11f0-a464-023d593e8e61', } } } mock_graphql_query.return_value = mock_response # Call the function result = create_new_vendor(mock_logger, s3_context) # Assertions assert result == { 'vendorId': '123', 'name': 'Test Owner', 'uuid': '6821fb18-1b66-11f0-a464-023d593e8e61', } mock_graphql_query.assert_called_once_with( service_name=config.GRAPHQL_SERVICE, query=graphql_queries.create_vendor, variables={ 'input': { 'name': 'Test Owner', 'companyBrandName': general.COMPANY_BRAND_NAME, 'currency': general.CURRENCY, 'owner': general.OWNER, 'serviceTierUuid': general.SERVICE_TIER_UUID, 'isDistributor': general.IS_DISTRIBUTOR, } }, **config.GRAPHQL_HEADERS, correlation_id='e2d336b3-fbf2-4e51-95fc-c434bc92a2d2' ) @patch('index.logger') @patch('config.ows_client.graphql_query') def test_update_external_identifier_1(mock_graphql_query, mock_logger, s3_ddex): """Test update_external_identifier_1 function.""" s3_context = S3Schema().load(s3_ddex) s3_context.correlation_id = 'e2d336b3-fbf2-4e51-95fc-c434bc92a2d2' s3_context.product.parent_repertoire_owner_code = 'TEST_CODE' # Mock GraphQL response mock_response = MagicMock() mock_response.json.return_value = { 'data': { 'updateVendorExternalIdentifier1': { 'vendorId': '123', } } } mock_graphql_query.return_value = mock_response # Call the function result = update_external_identifier_1(mock_logger, s3_context, 'vendor_uuid') # Assertions assert result == {'vendorId': '123'} mock_graphql_query.assert_called_once_with( service_name=config.GRAPHQL_SERVICE, query=graphql_queries.update_external_identifier_1, variables={ 'uuid': 'vendor_uuid', 'externalIdentifier1': 'TEST_CODE' }, **config.GRAPHQL_HEADERS, correlation_id='e2d336b3-fbf2-4e51-95fc-c434bc92a2d2' ) @patch('index.logger') @patch('config.ows_client.graphql_query') def test_create_new_subaccount(mock_graphql_query, mock_logger, s3_ddex): """Test create_new_subaccount function.""" # Mock logger s3_context = S3Schema().load(s3_ddex) s3_context.correlation_id = 'e2d336b3-fbf2-4e51-95fc-c434bc92a2d2' # Mock parent_repertoire_owner_name s3_context.product.repertoire_owner_name = 'Test Subaccount' s3_context.product.repertoire_owner_code = 'TEST_CODE' # Mock GraphQL response mock_response = MagicMock() mock_response.json.return_value = { 'data': { 'createSubaccount': { 'subaccountId': 123, } } } mock_graphql_query.return_value = mock_response # Call the function result = create_new_subaccount(mock_logger, s3_context, 456) # Assertions assert result == {'subaccountId': 123} mock_graphql_query.assert_called_once_with( service_name=config.GRAPHQL_SERVICE, query=graphql_queries.create_subaccount, variables={ 'input': { 'subaccountName': 'Test Subaccount', 'vendorId': 456, 'description': 'TEST_CODE', } }, **config.GRAPHQL_HEADERS, correlation_id='e2d336b3-fbf2-4e51-95fc-c434bc92a2d2' )