"""Tests for Artist Social Profile logic.""" from unittest.mock import MagicMock from oto import response from oto import status import pytest from social_analytics.constants import error from social_analytics.logic import artist_social_profile from social_analytics.logic import artist_search from social_analytics.models import ( artist_social_profile as artist_social_profile_model) from social_analytics.models import metric @pytest.fixture def fixture_artist_social_profiles(): """Fixture with Artist Social Profiles.""" return response.Response({'items': [{ 'social_profile_id': 3, 'artist_id': 3, 'label_id': 3}]}) @pytest.fixture def search_artist_params(): """Fixture for artist search params.""" return { 'context': 'test', 'token': 'test', 'term': 'test' } @pytest.fixture def search_artist_headers(): """Fixture for artist search headers.""" return { 'Grass-Account-Type': 'vendor', 'Grass-Account-Id': 1 } @pytest.fixture def ows_search_response(): """Fixture for search artist response.""" return response.Response({'items': [ { 'artist_name': 'Test artist 1', 'artist_id': '1', 'highlight_fields': { 'artist_name': 'A Test' } }, { 'artist_name': 'Test artist 2', 'artist_id': '2', 'highlight_fields': { 'artist_name': 'Another Test' } }, { 'artist_name': 'Test artist 3', 'artist_id': '3', 'highlight_fields': { 'artist_name': 'Another Test 2' } } ]}) @pytest.fixture def search_artist_response(): """Fixture for ows-search artist response.""" return [ { 'artist_name': 'Test artist 1', 'artist_id': '1', 'is_linked': True }, { 'artist_name': 'Test artist 2', 'artist_id': '2', 'is_linked': True }, { 'artist_name': 'Test artist 3', 'artist_id': '3', 'is_linked': False } ] @pytest.fixture def fixture_artist_ids_and_social_profiles(): """Fixture with Social Profile Ids.""" return response.Response( {'items': [ { 'artist_id': 1, 'social_profiles': [ { 'social_profile_id': 3, 'platform': 'instagram', 'platform_id': 8910, 'platform_name': 'taylor_swift' }, { 'social_profile_id': 4, 'platform': 'facebook', 'platform_id': 678, 'platform_name': 'Taylor Swift' } ] }, { 'artist_id': 2, 'social_profiles': [ { 'social_profile_id': 5, 'platform': 'instagram', 'platform_id': 8910, 'platform_name': 'Angry Joe' } ] } ]} ) @pytest.fixture def latest_metrics(): """Fixture for latest metrics.""" return response.Response({ 'items': [ { 'social_profile_id': 3, 'metric': 'fan_count', 'platform': 'facebook', 'metric_value': 4598087.0 }, { 'social_profile_id': 4, 'metric': 'followers', 'platform': 'instagram', 'metric_value': 3898087.0 }, { 'social_profile_id': 5, 'metric': 'followed_by', 'platform': 'spotify', 'metric_value': 3898087.0 } ]}) @pytest.fixture def fixture_artist_social_profile(): """Fixture with Artist Social Profiles.""" return response.Response( {'social_profile_id': 1, 'artist_id': 2, 'label_id': 3}) @pytest.fixture def fixture_artist_social_profiles_not_found(): """Fixture with artist social profiles not found.""" return response.create_not_found_response() @pytest.fixture def fixture_error_response(): """Fixture with social profiles not found.""" return response.create_error_response( status.BAD_REQUEST, error.ERROR_MESSAGE_EMPTY_BODY) @pytest.fixture def fixture_error_bad_params(): """Fixture for error bad params.""" return response.create_error_response( status.BAD_REQUEST, error.ERROR_MESSAGE_BAD_PARAMS) def test_get_artist_social_profiles( monkeypatch, fixture_artist_social_profiles): """Test get artist social profiles.""" monkeypatch.setattr( artist_social_profile_model, 'get_artist_social_profiles', MagicMock( return_value=fixture_artist_social_profiles)) result = artist_social_profile.get_artist_social_profiles() assert result def test_get_artist_social_profiles_not_found( monkeypatch, fixture_artist_social_profiles_not_found): """Test get artist social profiles with not found response.""" monkeypatch.setattr( artist_social_profile_model, 'get_artist_social_profiles', MagicMock( return_value=fixture_artist_social_profiles_not_found)) result = artist_social_profile.get_artist_social_profiles() assert not result def test_get_linked_artists( monkeypatch, fixture_artist_ids_and_social_profiles, latest_metrics): """Test get linked artists.""" monkeypatch.setattr( artist_social_profile_model, 'get_social_profile_ids_by_label_id', MagicMock(return_value=fixture_artist_ids_and_social_profiles)) monkeypatch.setattr( metric, 'get_latest_metrics_by_social_profile_ids', MagicMock( return_value=latest_metrics)) label_id = 3 result = artist_social_profile.get_linked_artists(label_id) (artist_social_profile_model.get_social_profile_ids_by_label_id .assert_called_once_with(label_id)) metric.get_latest_metrics_by_social_profile_ids.assert_called_once_with( [3, 4, 5]) assert result items = result.message['items'] assert items assert len(items) == 2 def test_get_linked_artists_bad_params(monkeypatch, fixture_error_bad_params): """Test get linked artists bad params.""" monkeypatch.setattr( artist_social_profile_model, 'get_social_profile_ids_by_label_id', MagicMock(return_value=fixture_error_bad_params)) label_id = None result = artist_social_profile.get_linked_artists(label_id) (artist_social_profile_model .get_social_profile_ids_by_label_id.assert_called_once_with(label_id)) assert not result def test_get_linked_artists_not_found( monkeypatch, fixture_artist_social_profiles_not_found): """Test get linked artists not found.""" monkeypatch.setattr( artist_social_profile_model, 'get_social_profile_ids_by_label_id', MagicMock(return_value=fixture_artist_social_profiles_not_found)) label_id = 3 result = artist_social_profile.get_linked_artists(label_id) (artist_social_profile_model .get_social_profile_ids_by_label_id.assert_called_once_with(label_id)) assert not result def test_create_artist_social_profile( monkeypatch, fixture_artist_social_profile): """Test create an artist social profile.""" data = {'profile_id': 1, 'artist_id': 2, 'label_id': 3} monkeypatch.setattr( artist_social_profile_model, 'create_artist_social_profile', MagicMock( return_value=fixture_artist_social_profile)) result = artist_social_profile.create_artist_social_profile(data) assert result.status is status.OK assert result.message['social_profile_id'] is 1 def test_create_artist_social_profile_error_response( monkeypatch, fixture_error_response): """Test create an artist social profile with error response.""" data = {} monkeypatch.setattr( artist_social_profile_model, 'create_artist_social_profile', MagicMock( return_value=fixture_error_response)) result = artist_social_profile.create_artist_social_profile(data) assert not result assert result.errors['code'] == status.BAD_REQUEST def test_update_artist_social_profile( monkeypatch, fixture_artist_social_profile): """Test update an artist social profile.""" data = {'profile_id': 1, 'artist_id': 2, 'label_id': 3} monkeypatch.setattr( artist_social_profile_model, 'update_artist_social_profile', MagicMock( return_value=fixture_artist_social_profile)) result = artist_social_profile.update_artist_social_profile(data) assert result.status is status.OK assert result.message['social_profile_id'] is 1 def test_update_artist_social_profile_error_response( monkeypatch, fixture_error_response): """Test update an artist social profile with error response.""" data = {} monkeypatch.setattr( artist_social_profile_model, 'update_artist_social_profile', MagicMock( return_value=fixture_error_response)) result = artist_social_profile.create_artist_social_profile(data) assert not result assert result.errors['code'] == status.BAD_REQUEST def test_delete_artist_social_profile( monkeypatch, fixture_artist_social_profiles): """Test delete artist social profile.""" monkeypatch.setattr( artist_social_profile, 'delete_artist_social_profile', MagicMock( return_value=fixture_artist_social_profiles)) social_profile_id = 3 artist_id = 3 result = artist_social_profile.delete_artist_social_profile( social_profile_id, artist_id) assert result assert result.status == status.OK assert len(result.message['items']) == 1 def test_delete_artist_social_profile_bad_params( monkeypatch, fixture_error_bad_params): """Test delete artist social profile bad params.""" monkeypatch.setattr( artist_social_profile, 'delete_artist_social_profile', MagicMock( return_value=fixture_error_bad_params)) social_profile_id = 2 artist_id = None result = artist_social_profile.delete_artist_social_profile( social_profile_id, artist_id) assert not result assert result.status == status.BAD_REQUEST def test_delete_artist_social_profile_not_found( monkeypatch, fixture_artist_social_profiles_not_found): """Test delete artist social profile not found.""" monkeypatch.setattr( artist_social_profile, 'delete_artist_social_profile', MagicMock( return_value=fixture_artist_social_profiles_not_found)) social_profile_id = 2 artist_id = 2 result = artist_social_profile.delete_artist_social_profile( social_profile_id, artist_id) assert not result assert result.status == status.NOT_FOUND def test_search_artists_not_found( monkeypatch, search_artist_params, search_artist_headers, ows_search_response): """Test search artists.""" monkeypatch.setattr(artist_search, 'search_artist', MagicMock( return_value=ows_search_response)) monkeypatch.setattr( artist_social_profile_model, 'get_linked_artist_social_profile_ids', MagicMock(return_value=response.create_not_found_response())) result = artist_social_profile.search_artists( search_artist_params, search_artist_headers) assert not result assert result.status == status.NOT_FOUND def test_search_artists_ows_search_no_results( monkeypatch, search_artist_params, search_artist_headers): """Test search artists no ows search results.""" monkeypatch.setattr(artist_search, 'search_artist', MagicMock( return_value=response.create_not_found_response())) monkeypatch.setattr( artist_social_profile_model, 'get_linked_artist_social_profile_ids', MagicMock(return_value=response.create_not_found_response())) result = artist_social_profile.search_artists( search_artist_params, search_artist_headers) assert not result assert result.status == status.NOT_FOUND def test_search_artists( monkeypatch, search_artist_params, search_artist_headers, fixture_artist_social_profiles_not_found): """Test search artists.""" monkeypatch.setattr(artist_search, 'search_artist', MagicMock( return_value=response.create_not_found_response())) monkeypatch.setattr( artist_social_profile_model, 'get_linked_artist_social_profile_ids', MagicMock(return_value=fixture_artist_social_profiles_not_found)) result = artist_social_profile.search_artists( search_artist_params, search_artist_headers) assert not result assert result.status == status.NOT_FOUND