"""Graphql-gateway tests.""" from unittest.mock import patch from gql import gql as gql_query import pytest from requests import HTTPError from src import constants from src import graphql_gateway from src import mock_graphql_gateway MOCK_EMPTY_RESPONSE = { 'globalParticipantBySpotifyId': [] } SPOTIFY_ID = '3Nrfpe0tUJi4K4DXYWgMUX' MOCK_RESPONSE_BY_SPOTIFY_ID = \ mock_graphql_gateway.MOCK_GLOBAL_PARTICIPANT_DATA_GQL_RESPONSE_BY_SPOTIFY_ID MOCK_RESPONSE_FOR_MERGE_AND_DEDUP = { 'globalParticipantBySpotifyId': [ { 'id': 'f67b0892-f0bc-4575-b23e-59566ccb44bc', 'labelParticipants': { 'labelParticipants': [ {'uuid': '3115496a-b516-46b8-92dc-7faf1a722889'}, {'uuid': 'e36f2773-e483-41f6-a621-1cdda31ae196'}, ] }, 'publicParticipantChartmetric': { 'accountStatsV2': [ { 'platform': 'spotify', 'url': 'https://open.spotify.com/artist/46SqMLtuMApmIOfROPpWSz', 'followers': 1, 'monthlyListeners': 2 }, { 'platform': 'deezer', 'url': 'https://deezer.com/artist/118011532', 'followers': 3, 'monthlyListeners': None }, { 'platform': 'spotify', 'url': 'https://open.spotify.com/artist/wdwdf32ft234g', 'followers': 100, 'monthlyListeners': 1000 } ] } }, { 'id': 'fewf-32rf-asfas-a23rsf-asfasf', 'labelParticipants': { 'labelParticipants': [ {'uuid': 'fqwe-qwer-qwef-asce-asvscc32r'}, ] }, 'publicParticipantChartmetric': { 'accountStatsV2': [ { 'platform': 'spotify', 'url': 'https://open.spotify.com/artist/3Nrfpe0tUJi4K4DXYWgMUX', 'followers': 500, 'monthlyListeners': 2000 } ] } } ] } @patch('src.graphql_gateway.Client') @patch('src.graphql_gateway.RequestsHTTPTransport') def test_get_global_participant_data_success( mock_transport, mock_client, mock_global_participant_data, mock_event_spotify): """Test successful execution of graphql query.""" mock_client_instance = mock_client.return_value mock_client_instance.execute.return_value = MOCK_RESPONSE_BY_SPOTIFY_ID result = graphql_gateway.get_global_participant_data(mock_event_spotify) expected_query = gql_query(constants.QUERIES.get( 'get_globalParticipantBySpotifyId_query')) expected_vars = {'spotifyId': SPOTIFY_ID} mock_client_instance.execute.assert_called_with( expected_query, variable_values=expected_vars) assert result == mock_global_participant_data @patch('src.graphql_gateway.Client') @patch('src.graphql_gateway.RequestsHTTPTransport') def test_get_global_participant_data_error( mock_transport, mock_client, mock_event_spotify): """Test http error.""" mock_client_instance = mock_client.return_value mock_client_instance.execute.side_effect = HTTPError() with pytest.raises(HTTPError): graphql_gateway.get_global_participant_data(mock_event_spotify) @patch('src.graphql_gateway.config') @patch('src.graphql_gateway.Client') @patch('src.graphql_gateway.RequestsHTTPTransport') def test_get_mocked_data( mock_transport, mock_client, mock_config, mock_global_participant_data, mock_event_spotify): """Test returning mocked result.""" mock_config.USE_MOCK_GRAPHQL_RESPONSE = 1 mock_client_instance = mock_client.return_value result = graphql_gateway.get_global_participant_data(mock_event_spotify) mock_client_instance.execute.assert_not_called() assert result == mock_global_participant_data @patch('src.graphql_gateway.config') @patch('src.graphql_gateway.Client') @patch('src.graphql_gateway.RequestsHTTPTransport') def test_get_mocked_data_instagram( mock_transport, mock_client, mock_config, mock_global_participant_data, mock_event_instagram): """Test returning mocked result.""" mock_config.USE_MOCK_GRAPHQL_RESPONSE = 1 mock_client_instance = mock_client.return_value result = graphql_gateway.get_global_participant_data(mock_event_instagram) mock_client_instance.execute.assert_not_called() # we don't provide overriding values in the mock_event_instagram mock_global_participant_data['soundcloud_followers'] = 3746486 mock_global_participant_data['soundcloud_url'] = 'https://soundcloud.com/bangtan' assert result == mock_global_participant_data @patch('src.graphql_gateway.config') @patch('src.graphql_gateway.Client') @patch('src.graphql_gateway.RequestsHTTPTransport') def test_get_mocked_data_empty_result( mock_transport, mock_client, mock_config, mock_event_spotify): """Test returning mocked empty result.""" del mock_event_spotify['spotify_url'] del mock_event_spotify['soundcloud_url'] mock_config.USE_MOCK_GRAPHQL_RESPONSE = 1 mock_client_instance = mock_client.return_value result = graphql_gateway.get_global_participant_data(mock_event_spotify) mock_client_instance.execute.assert_not_called() # only values from user input assert result == { 'spotify_id': None, 'label_participants': None, 'deezer_url': None, 'deezer_followers': None, 'facebook_url': None, 'facebook_followers': None, 'instagram_url': None, 'instagram_followers': None, 'soundcloud_url': None, 'soundcloud_followers': None, 'spotify_url': None, 'spotify_followers': None, 'spotify_monthly_listeners': None, 'tiktok_url': None, 'tiktok_followers': None, 'twitter_url': None, 'twitter_followers': None, 'youtube_url': None, 'youtube_followers': None } def test__merge_and_dedup(): """Test merging and deduplication of a GQL response.""" merged_result = graphql_gateway._merge_and_dedup( MOCK_RESPONSE_FOR_MERGE_AND_DEDUP, 'globalParticipantBySpotifyId') assert merged_result == { 'deezer': {'followers': 3, 'url': 'https://deezer.com/artist/118011532'}, 'facebook': {}, 'instagram': {}, 'soundcloud': {}, 'spotify': { 'followers': 500, 'monthly_listeners': 2000, 'url': 'https://open.spotify.com/artist/3Nrfpe0tUJi4K4DXYWgMUX'}, 'tiktok': {}, 'twitter': {}, 'youtube': {} } def test__replace_handles_from_graph_with_user_input_match( mock_event_spotify, mock_global_participant_data): """Test _replace_handles_from_graph_with_user_input. If data in knowledge graph is the same as in user input. """ assert graphql_gateway._replace_handles_from_graph_with_user_input( mock_event_spotify, mock_global_participant_data) == mock_global_participant_data def test__replace_handles_from_graph_with_user_input_no_match( mock_event_spotify, mock_global_participant_data): """Test _replace_handles_from_graph_with_user_input. If data in knowledge graph is NOT the same as in user input. """ mock_event_spotify['soundcloud_url'] = 'https://soundcloud.com/BTS_1' assert graphql_gateway._replace_handles_from_graph_with_user_input( mock_event_spotify, mock_global_participant_data)['soundcloud_url'] == \ mock_event_spotify['soundcloud_url']