"""Unit tests for artist_social_profile model.""" from oto import status import pytest from social_analytics.models import artist_social_profile from tests.utils import db_operations @pytest.fixture def db_fixture(): """Drop and re-create all the SQLite tables and seed them.""" db_operations.create_tables() db_operations.seed_data() @pytest.fixture(params=[ {'social_profile_id': 1, 'artist_id': None}, {'social_profile_id': None, 'artist_id': 1} ]) def delete_params(request): """Fixture for delete params.""" return request.param def test_get_artist_social_profiles(db_fixture): """Test artist social profile model returns data when seeded.""" result = artist_social_profile.get_artist_social_profiles() assert result result_artist_social_profiles = result.message assert len(result_artist_social_profiles) == 1 result_artist_social_profile = result_artist_social_profiles['items'][0] assert (result_artist_social_profile['social_profile_id'] == db_operations.artist_social_profile_data[0]['social_profile_id']) assert (result_artist_social_profile['artist_id'] == db_operations.artist_social_profile_data[0]['artist_id']) assert (result_artist_social_profile['label_id'] == db_operations.artist_social_profile_data[0]['label_id']) def test_get_artist_social_profiles_empty(): """Test not found response when there is no artist social profile data.""" db_operations.create_tables() result = artist_social_profile.get_artist_social_profiles() assert not result def test_get_artist_social_profiles_by_artist_id(db_fixture): """Test get artist social profiles by artist id.""" artist_id = 1 result = artist_social_profile.get_artist_social_profiles_by_artist_id( artist_id) assert result result_artist_social_profiles = result.message assert len(result_artist_social_profiles) == 1 result_artist_social_profile = result_artist_social_profiles['items'][0] assert (result_artist_social_profile['artist_id'] == artist_id) def test_get_artist_social_profiles_by_artist_id_no_id(): """Test get artist social profiles by artist id with no artist id given.""" result = artist_social_profile.get_artist_social_profiles_by_artist_id( None) assert result.status == status.BAD_REQUEST def test_get_artist_social_profiles_by_artist_id_not_found(db_fixture): """Test get artist social profiles by artist id not found.""" artist_id = 15 result = artist_social_profile.get_artist_social_profiles_by_artist_id( artist_id) assert not result def test_create_artist_social_profile(db_fixture): """Test that a artist social profile can be created.""" data = { 'artist_id': 4, 'social_profile_id': 4, 'label_id': 4 } result = artist_social_profile.create_artist_social_profile(data) created_artist_social_profile = result.message assert created_artist_social_profile['artist_id'] == 4 assert created_artist_social_profile['social_profile_id'] == 4 assert created_artist_social_profile['label_id'] == 4 def test_create_artist_social_profile_empty_body(): """Test that a artist social profile requires fields to be created.""" data = {} result = artist_social_profile.create_artist_social_profile(data) assert result.status == status.BAD_REQUEST def test_get_social_profile_ids_by_label_id_no_id(): """Test get social profile ids error when no id is given.""" result = artist_social_profile.get_social_profile_ids_by_label_id(None) assert result.status == status.BAD_REQUEST def test_get_social_profile_ids_by_label_id(db_fixture): """Test get social profile ids with a given label id.""" label_id = 2 result = artist_social_profile.get_social_profile_ids_by_label_id(label_id) assert result items = result.message['items'] assert items assert len(items) == 2 for item in items: assert 'artist_id' in item assert 'social_profiles' in item def test_get_social_profile_ids_by_label_id_not_found(db_fixture): """Test get social profile ids with a given label id not found.""" label_id = 15 result = artist_social_profile.get_social_profile_ids_by_label_id( label_id) assert not result def test_update_artist_social_profile(db_fixture): """Test that a artist social profile can be updated.""" data = { 'artist_id': 4, 'social_profile_id': 3, 'label_id': 5 } result = artist_social_profile.update_artist_social_profile(data) assert result updated_artist_social_profile = result.message assert updated_artist_social_profile['updated'] == 1 def test_update_artist_social_profile_empty_body(): """Test that a artist social profile requires fields to be updated.""" data = {} result = artist_social_profile.update_artist_social_profile(data) assert result.status == status.BAD_REQUEST def test_get_artist_social_profile_with_artist_id_and_social_profile_id( db_fixture): """Test get artist social profile with artist_id and social_profile_id.""" artist_id = 1 social_profile_id = 1 result = ( artist_social_profile .get_artist_social_profile_by_artist_id_and_social_profile_id( artist_id, social_profile_id)) assert result.message def test_get_artist_social_profile_with_artist_id_social_profile_id_no_params( db_fixture): """Test get artist social profile with artist_id and social_profile_id. This case exams the result when there are empty params """ artist_id = None social_profile_id = None result = ( artist_social_profile .get_artist_social_profile_by_artist_id_and_social_profile_id( artist_id, social_profile_id)) assert not result def test_get_artist_social_profile_with_artist_id_social_profile_id_not_found( db_fixture): """Test get artist social profile with artist_id and social_profile_id. This case exams the result when artist social profile is not found. """ artist_id = 5 social_profile_id = 5 result = ( artist_social_profile .get_artist_social_profile_by_artist_id_and_social_profile_id( artist_id, social_profile_id)) assert not result assert result.status == status.NOT_FOUND def test_delete_artist_social_profile_by_social_profile_id(db_fixture): """Test deleted artist social profile by social profile id.""" social_profile_id = 3 result = ( artist_social_profile .delete_artist_social_profiles_by_social_profile_id( social_profile_id)) assert result assert len(result.message['items']) == 2 def test_delete_artist_social_profile_by_social_profile_id_no_id(db_fixture): """Test deleted artist social profile by social profile id.""" social_profile_id = None result = ( artist_social_profile .delete_artist_social_profiles_by_social_profile_id( social_profile_id)) assert not result assert result.status == status.BAD_REQUEST def test_delete_artist_social_profile_by_social_profile_id_not_found( db_fixture): """Test deleted artist social profile by social profile id.""" social_profile_id = 53 result = ( artist_social_profile .delete_artist_social_profiles_by_social_profile_id( social_profile_id)) assert not result assert result.status == status.NOT_FOUND def test_delete_artist_social_profile(db_fixture): """Test delete artist social profile.""" social_profile_id = 2 artist_id = 2 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(db_fixture, delete_params): """Test delete artist social profile bad params.""" social_profile_id = delete_params['social_profile_id'] artist_id = delete_params['artist_id'] 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(db_fixture): """Test delete artist social profile not found.""" social_profile_id = 15 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_get_linked_artist_social_profile_ids_bad_params(): """Test get linked artist social profile ids.""" artist_ids = [] result = artist_social_profile.get_linked_artist_social_profile_ids( artist_ids) assert not result assert result.status == status.BAD_REQUEST def test_get_linked_artist_social_profile_ids_not_found(db_fixture): """Test get linked artist social profile ids not found.""" artist_ids = [1234, 12] result = artist_social_profile.get_linked_artist_social_profile_ids( artist_ids) assert not result assert result.status == status.NOT_FOUND def test_get_linked_artist_social_profile_ids(db_fixture): """Test get linked artist social profile ids not found.""" artist_ids = [2, 3, 4] result = artist_social_profile.get_linked_artist_social_profile_ids( artist_ids) assert result assert all(item in artist_ids for item in result.message['items'])