"""Tests for the graphql_router model.""" from unittest.mock import call from unittest.mock import patch from gql import gql as gql_query import pytest from config import ENVIRONMENT from src.models import graphql_router @patch('src.models.graphql_router.Client', autospec=True) @patch('src.models.graphql_router.RequestsHTTPTransport', autospec=True) # noqa:E501 def test_make_request(mock_gql_transport, mock_gql_client): """Test low level graphql_router requests.""" mock_query = """ query doSomething { func { data { id } } } """ mock_variables = {'abc': '123'} graphql_router._make_request( mock_query, mock_variables, headers={'xyz': '456'} ) test_url = f'https://{ENVIRONMENT}-graphql-router.theorchard.io/graphql' assert mock_gql_transport.call_args_list == [ call( headers={ 'xyz': '456', 'apollographql-client-name': 'fanout-event', 'apollographql-client-version': '1' }, retries=3, url=test_url, use_json=True, verify=True ) ] assert mock_gql_client.call_args_list == [ call( fetch_schema_from_transport=False, transport=mock_gql_transport(url=test_url) ) ] assert mock_gql_client.return_value.execute.call_args_list == [ call( gql_query(mock_query), variable_values=mock_variables ) ] @pytest.mark.parametrize( 'mock_graph_response, expected_result', [ ({'globalSoundRecordingByIsrc': {}}, []), ( { 'globalSoundRecordingByIsrc': { 'labelSoundRecordings': [ {'tracks': [{'tuid': '111'}, {'tuid': '222'}]}, {'tracks': [{'tuid': '333'}, {'tuid': '444'}]} ] } }, ['111', '222', '333', '444'] ), ( { 'globalSoundRecordingByIsrc': { 'labelSoundRecordings': [] } }, [] ) ] ) def test_get_available_tracks(mocker, mock_graph_response, expected_result): """Test track fetching by isrc and profile with parsed results.""" mock_call = mocker.patch.object( graphql_router, '_make_request', return_value=mock_graph_response ) result = graphql_router.get_available_tracks(123, 'ProfileType', 'uuid', 'abcd') assert result == expected_result assert mock_call.call_args_list == [ call( 'query globalSoundRecordingByIsrc($term: String!) { globalSoundRecordingByIsrc(isrc: $term) { labelSoundRecordings { tracks { tuid } } } }', # noqa:E501 { 'term': 'abcd' }, { 'Orchard-Profile-Id': '123', 'Orchard-Profile-Type': 'ProfileType', 'Orchard-Identity-Id': 'uuid' } ) ] @pytest.mark.parametrize( 'query_response, expected_result', [ (['111', '222', '333'], True), (['222', '333', '444'], False) ] ) def test_profile_can_access_track(mocker, query_response, expected_result): """Test profile access to track logic.""" mock_call = mocker.patch.object( graphql_router, 'get_available_tracks', return_value=query_response ) result = graphql_router.profile_can_access_track( 123, 'ProfileType', 'uuid', 'abcd', 111) assert result == expected_result assert mock_call.call_args_list == [call(123, 'ProfileType', 'uuid', 'abcd')] @pytest.mark.parametrize( 'mock_graph_response, expected_result', [ ({'globalSoundRecordingByIsrc': {}}, []), ( { 'globalSoundRecordingByIsrc': { 'labelSoundRecordings': [ {'id': '111'}, {'id': '222'}, {'id': '333'} ] } }, ['111', '222', '333'] ), ( { 'globalSoundRecordingByIsrc': {'labelSoundRecordings': []} }, [] ) ] ) def test_get_available_sound_recordings( mocker, mock_graph_response, expected_result): """Test sound recording fetching by isrc and profile with parsed result.""" mock_call = mocker.patch.object( graphql_router, '_make_request', return_value=mock_graph_response ) result = graphql_router.get_available_sound_recordings( 123, 'ProfileType', 'uuid', 'abcd') assert result == expected_result assert mock_call.call_args_list == [ call( 'query globalSoundRecordingByIsrc($term: String!) { globalSoundRecordingByIsrc(isrc: $term) { labelSoundRecordings { id } } }', # noqa:E501 { 'term': 'abcd' }, { 'Orchard-Profile-Id': '123', 'Orchard-Profile-Type': 'ProfileType', 'Orchard-Identity-Id': 'uuid' } ) ] @pytest.mark.parametrize( 'query_response, expected_result', [ ([], False), (['111', '222', '333'], True) ] ) def test_profile_can_access_sound_recording( mocker, query_response, expected_result): """Test profile access to sound recording logic.""" mock_call = mocker.patch.object( graphql_router, 'get_available_sound_recordings', return_value=query_response ) result = graphql_router.profile_can_access_sound_recording( 123, 'ProfileType', 'uuid', 'abcd') assert result == expected_result assert mock_call.call_args_list == [call(123, 'ProfileType', 'uuid', 'abcd')]