"""Tests for 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 social_profile from social_analytics.models import ( artist_social_profile as artist_social_profile_model) from social_analytics.models import social_profile as social_profile_model @pytest.fixture def fixture_social_profiles(): """Fixture with Social Profiles.""" return response.Response({'items': [ { 'social_profile_id': 2, 'platform_name': 'Taylor Swift', 'platform': 'facebook'}, { 'social_profile_id': 3, 'platform_name': 'Taylor Swift', 'platform': 'Instagram'}] }) @pytest.fixture def fixture_social_profile(): """Fixture with a single Social Profile.""" return response.Response({ 'social_profile_id': 123, 'platform_name': 'Taylor Swift', 'platform': 'facebook'}) @pytest.fixture def fixture_not_found(): """Fixture with not found response.""" return response.create_not_found_response() @pytest.fixture def fixture_error_empty_body(): """Fixture for error empty body.""" return response.create_error_response( 400, error.ERROR_MESSAGE_EMPTY_BODY) @pytest.fixture def fixture_error_bad_params(): """Fixture for error bad params.""" return response.create_error_response( 400, error.ERROR_MESSAGE_BAD_PARAMS) def test_get_social_profiles(monkeypatch, fixture_social_profiles): """Test get social profiles.""" monkeypatch.setattr( social_profile_model, 'get_social_profiles', MagicMock( return_value=fixture_social_profiles)) result = social_profile.get_social_profiles() assert result def test_get_social_profiles_not_found(monkeypatch, fixture_not_found): """Test get social profiles with not found response.""" monkeypatch.setattr( social_profile_model, 'get_social_profiles', MagicMock( return_value=fixture_not_found)) result = social_profile.get_social_profiles() assert not result def test_get_social_profiles_by_artist_id( monkeypatch, fixture_social_profiles): """Test get social profiles by artist id.""" monkeypatch.setattr( artist_social_profile_model, 'get_artist_social_profiles_by_artist_id', MagicMock(return_value=response.Response({ 'items': [ { 'artist_id': 1, 'social_profile_id': 2, 'label_id': 1 }, { 'artist_id': 1, 'social_profile_id': 3, 'label_id': 2 }] }))) monkeypatch.setattr( social_profile_model, 'get_social_profiles_by_ids', MagicMock( return_value=fixture_social_profiles)) artist_id = '1' result = social_profile.get_social_profiles_by_artist_id(artist_id) assert result social_profiles = result.message['items'] assert len(social_profiles) == 2 def test_get_social_profiles_by_artist_id_artist_social_profiles_not_found( monkeypatch, fixture_not_found): """Test get_social_profiles_by_artist_id no artist_social_profiles.""" monkeypatch.setattr( artist_social_profile_model, 'get_artist_social_profiles_by_artist_id', MagicMock(return_value=fixture_not_found)) artist_id = '1' result = social_profile.get_social_profiles_by_artist_id(artist_id) assert not result def test_get_social_profiles_by_artist_id_artist_social_profiles_bad_params( monkeypatch, fixture_error_bad_params): """Test get_social_profiles_by_artist_id no artist_social_profiles.""" monkeypatch.setattr( artist_social_profile_model, 'get_artist_social_profiles_by_artist_id', MagicMock(return_value=fixture_error_bad_params)) artist_id = '1' result = social_profile.get_social_profiles_by_artist_id(artist_id) assert result.status == status.BAD_REQUEST def test_get_social_profiles_by_artist_id_social_profiles_not_found( monkeypatch, fixture_not_found): """Test get social profiles by artist id.""" monkeypatch.setattr( artist_social_profile_model, 'get_artist_social_profiles_by_artist_id', MagicMock(return_value=response.Response({ 'items': [ { 'artist_id': 1, 'social_profile_id': 2, 'label_id': 1 }, { 'artist_id': 1, 'social_profile_id': 3, 'label_id': 2 }] }))) monkeypatch.setattr( social_profile_model, 'get_social_profiles_by_ids', MagicMock( return_value=fixture_not_found)) artist_id = '1' result = social_profile.get_social_profiles_by_artist_id(artist_id) assert not result def test_get_social_profiles_by_ids(monkeypatch, fixture_social_profile): """Test get social profiles by ids.""" monkeypatch.setattr( social_profile_model, 'get_social_profiles_by_ids', MagicMock( return_value=fixture_social_profile)) result = social_profile.get_social_profiles_by_ids([1]) assert result def test_get_social_profiles_by_ids_not_found( monkeypatch, fixture_not_found): """Test get social profiles by ids not found.""" monkeypatch.setattr( social_profile_model, 'get_social_profiles_by_ids', MagicMock( return_value=fixture_not_found)) result = social_profile.get_social_profiles_by_ids([1]) assert not result def test_create_or_update_social_profile_existing_profile_no_old_profile( monkeypatch): """Test create or update social profile. This is the case when a social profile already exists but its linked to a different artist_id. """ data = { 'platform': 'facebook', 'platform_name': 'Taylor Swift', 'platform_id': '987', 'artist_id': 1, 'label_id': 1 } found_social_profile = response.Response({ 'social_profile_id': 4, 'platform': 'facebook', 'platform_id': '987', 'platform_name': 'Taylor Swift'}) artist_social_profile = response.Response({ 'artist_id': 1, 'social_profile_id': 4, 'label_id': 1}) monkeypatch.setattr( social_profile_model, 'get_social_profile_with_platform_id_and_platform', MagicMock( return_value=found_social_profile)) monkeypatch.setattr( artist_social_profile_model, 'create_artist_social_profile', MagicMock( return_value=artist_social_profile)) monkeypatch.setattr( artist_social_profile_model, 'delete_artist_social_profile', MagicMock( )) expected_result = { 'artist_id': 1, 'social_profile_id': 4, 'platform': 'facebook', 'platform_id': '987', 'platform_name': 'Taylor Swift', 'label_id': 1} result = social_profile.create_or_update_social_profile(data) assert ( artist_social_profile_model.delete_artist_social_profile.call_count == 0) assert result.message == expected_result def test_create_or_update_social_profile_existing_profile_with_old_profile( monkeypatch): """Test create or update social profile. This is the case when a social profile already exists but its linked to a different artist_id. """ data = { 'platform': 'facebook', 'platform_name': 'Taylor Swift', 'platform_id': '987', 'artist_id': 1, 'label_id': 1, 'old_social_profile_id': 2 } found_social_profile = response.Response({ 'social_profile_id': 4, 'platform': 'facebook', 'platform_id': '987', 'platform_name': 'Taylor Swift'}) artist_social_profile = response.Response({ 'artist_id': 1, 'social_profile_id': 4, 'label_id': 1}) deleted_artist_social_profile = response.Response({ 'items': [ { 'artist_id': 1, 'social_profile_id': 2, 'label_id': 1 }] }) monkeypatch.setattr( social_profile_model, 'get_social_profile_with_platform_id_and_platform', MagicMock( return_value=found_social_profile)) monkeypatch.setattr( artist_social_profile_model, 'create_artist_social_profile', MagicMock( return_value=artist_social_profile)) monkeypatch.setattr( artist_social_profile_model, 'delete_artist_social_profile', MagicMock( return_value=deleted_artist_social_profile)) expected_result = { 'artist_id': 1, 'social_profile_id': 4, 'platform': 'facebook', 'platform_id': '987', 'platform_name': 'Taylor Swift', 'label_id': 1} result = social_profile.create_or_update_social_profile(data) assert ( artist_social_profile_model.delete_artist_social_profile.call_count == 1) assert result.message == expected_result def test_create_or_update_social_profile_with_non_existing_social_profile( monkeypatch): """Test create or update social profile. This is the case when a social profile does not exist and its going to be created. Additionally, an artist social profile will be created with a reference to the created social profile. """ data = { 'platform': 'facebook', 'platform_name': 'Taylor Swift', 'platform_id': '987', 'artist_id': 1, 'label_id': 1} data_with_social_platform_id = data.copy() data_with_social_platform_id['social_profile_id'] = 4 found_social_profile = response.create_not_found_response() created_social_profile = response.Response(data_with_social_platform_id) artist_social_profile = response.Response({ 'artist_id': 1, 'social_profile_id': 4, 'label_id': 1}) monkeypatch.setattr( social_profile_model, 'create_social_profile', MagicMock( return_value=created_social_profile)) monkeypatch.setattr( social_profile_model, 'get_social_profile_with_platform_id_and_platform', MagicMock( return_value=found_social_profile)) monkeypatch.setattr( artist_social_profile_model, 'create_artist_social_profile', MagicMock( return_value=artist_social_profile) ) expected_result = { 'artist_id': 1, 'social_profile_id': 4, 'platform': 'facebook', 'platform_id': '987', 'platform_name': 'Taylor Swift', 'label_id': 1} result = social_profile.create_or_update_social_profile(data) assert result.message == expected_result def test_update_social_profile_with_new_social_profile_remove_old_profile( monkeypatch): """Test create or update social profile. This is the case when a social profile does not exist and its going to be created but there is another social profile associated for this specific artist. Also, we need to remove the reference of the old social profile to the artist social profile. Finally, an artist social profile will be created with a reference to the newly created social profile. """ data = { 'platform': 'facebook', 'platform_name': 'Taylor Swift', 'platform_id': '987', 'artist_id': 1, 'label_id': 1, 'old_social_profile_id': 12} data_with_social_platform_id = data.copy() data_with_social_platform_id['social_profile_id'] = 4 data_with_social_platform_id.pop('old_social_profile_id', None) found_social_profile = response.create_not_found_response() deleted_social_profile = response.Response( {'social_profile_id': 12, 'artist_id': 1}) created_social_profile = response.Response(data_with_social_platform_id) artist_social_profile = response.Response({ 'artist_id': 1, 'social_profile_id': 4, 'label_id': 1}) monkeypatch.setattr( social_profile_model, 'create_social_profile', MagicMock( return_value=created_social_profile)) monkeypatch.setattr( social_profile_model, 'get_social_profile_with_platform_id_and_platform', MagicMock( return_value=found_social_profile)) monkeypatch.setattr( artist_social_profile_model, 'create_artist_social_profile', MagicMock( return_value=artist_social_profile) ) monkeypatch.setattr( artist_social_profile_model, 'delete_artist_social_profile', MagicMock( return_value=deleted_social_profile ) ) expected_result = { 'artist_id': 1, 'social_profile_id': 4, 'platform': 'facebook', 'platform_id': '987', 'platform_name': 'Taylor Swift', 'label_id': 1} result = social_profile.create_or_update_social_profile(data) assert result.message == expected_result def test_update_social_profile_with_new_cannot_remove_old_profile( monkeypatch): """Test create or update social profile. This is the case when a social profile does not exist and its going to be created but there is another social profile associated for this specific artist. Also, we need to remove the reference of the old social profile to the artist social profile. Finally, an artist social profile will be created with a reference to the newly created social profile. """ data = { 'platform': 'facebook', 'platform_name': 'Taylor Swift', 'platform_id': '987', 'artist_id': 1, 'label_id': 1, 'old_social_profile_id': 12} found_social_profile = response.create_not_found_response() deleted_social_profile = response.create_error_response(500, 'DB ERROR') monkeypatch.setattr( social_profile_model, 'get_social_profile_with_platform_id_and_platform', MagicMock( return_value=found_social_profile)) monkeypatch.setattr( artist_social_profile_model, 'delete_artist_social_profile', MagicMock( return_value=deleted_social_profile ) ) result = social_profile.create_or_update_social_profile(data) assert not result def test_delete_social_profile(monkeypatch): """Test delete social profile.""" expected_social_profile = { 'social_profile_id': 3, 'platform': 'facebook', 'platform_id': 456, 'platform_name': 'Anika Pyle' } expected_artist_social_profiles = [ { 'artist_id': 4, 'social_profile_id': 3, 'label_id': 3 }, { 'artist_id': 5, 'social_profile_id': 3, 'label_id': 7 } ] monkeypatch.setattr( social_profile_model, 'delete_social_profile', MagicMock( return_value=response.Response(expected_social_profile) )) monkeypatch.setattr( artist_social_profile_model, 'delete_artist_social_profiles_by_social_profile_id', MagicMock( return_value=response.Response( {'items': expected_artist_social_profiles}) )) social_profile_id = 3 result = social_profile.delete_social_profile(social_profile_id) social_profile_model.delete_social_profile.assert_called_once_with( social_profile_id) (artist_social_profile_model .delete_artist_social_profiles_by_social_profile_id .assert_called_once_with(social_profile_id)) assert result assert result.message['items']['social_profile'] assert result.message['items']['artist_social_profiles'] def test_delete_social_profile_no_artist_social_profile(monkeypatch): """Test delete social profile.""" expected_social_profile = { 'social_profile_id': 3, 'platform': 'facebook', 'platform_id': 456, 'platform_name': 'Anika Pyle' } monkeypatch.setattr( social_profile_model, 'delete_social_profile', MagicMock( return_value=response.Response(expected_social_profile) )) monkeypatch.setattr( artist_social_profile_model, 'delete_artist_social_profiles_by_social_profile_id', MagicMock( return_value=response.create_not_found_response())) social_profile_id = 3 result = social_profile.delete_social_profile(social_profile_id) assert result assert result.message['items']['social_profile'] assert 'artist_social_profiles' not in result.message['items'] def test_delete_social_profile_no_social_profile(monkeypatch): """Test delete social profile.""" monkeypatch.setattr( social_profile_model, 'delete_social_profile', MagicMock( return_value=response.create_not_found_response())) result = social_profile.delete_social_profile(53) assert not result assert result.status == status.NOT_FOUND def test_delete_social_profile_no_social_profile_id(): """Test delete social profile.""" social_profile_id = None result = social_profile.delete_social_profile(social_profile_id) assert not result assert result.status == status.BAD_REQUEST