"""Unit tests for artist_url model.""" from unittest.mock import MagicMock from freezegun import freeze_time from oto import status import pytest from social_analytics.constants import collectors from social_analytics.models import artist_url 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': None, 'artist_id': 1}, {'social_profile_id': 1, 'artist_id': None} ]) def clear_social_profile_bad_params(request): """Fixture for clear social profile bad params.""" return request.param def test_get_artist_urls_with_artist_id(db_fixture): """Test get artist urls by artist id.""" artist_id = 1 result = artist_url.get_artist_urls_with_artist_id(artist_id) assert result artist_urls = result.message['items'] assert len(artist_urls) == 2 def test_get_artist_urls_with_artist_id_not_found(db_fixture): """Test get artist urls by artist id not found.""" artist_id = 5 result = artist_url.get_artist_urls_with_artist_id(artist_id) assert not result def test_get_artist_urls_with_artist_id_bad_params(db_fixture): """Test get artist urls by artist id bad params.""" artist_id = '' result = artist_url.get_artist_urls_with_artist_id(artist_id) assert not result assert result.status == status.BAD_REQUEST def test_create_artist_url(db_fixture): """Test create artist url.""" data = { 'artist_id': 3, 'social_profile_id': 10, 'site_id': 2, 'url': 'www.facebook.com/123'} result = artist_url.create_artist_url(data) assert result def test_create_artist_url_bad_params(db_fixture): """Test create artist url with bad params.""" data = {} result = artist_url.create_artist_url(data) assert not result assert result.status == status.BAD_REQUEST def test_get_social_profile_ids_by_artist_ids(db_fixture): """Test get social profile ids by artist ids.""" artist_id = 1 artist_ids = [1] result = artist_url.get_social_profile_ids_by_artist_ids(artist_ids) assert result artists_with_social_profiles = result.message['items'] assert len(artists_with_social_profiles) == 1 assert artists_with_social_profiles[0]['artist_id'] == artist_id assert len(artists_with_social_profiles[0]['social_profiles']) == 1 def test_get_social_profile_ids_by_artist_ids_bad_params(): """Test get social profile ids by artist ids bad params.""" artist_ids = None result = artist_url.get_social_profile_ids_by_artist_ids(artist_ids) assert not result assert result.status == status.BAD_REQUEST def test_get_social_profile_ids_by_artist_ids_not_found(db_fixture): """Test get social profile ids by artist ids bad params.""" artist_ids = [24, 46] result = artist_url.get_social_profile_ids_by_artist_ids(artist_ids) assert not result assert result.status == status.NOT_FOUND def test_clear_social_profile_id(db_fixture): """Test delete artist url.""" social_profile_id = 11 artist_id = 1 result = artist_url.clear_social_profile(social_profile_id, artist_id) assert result assert result.status == status.OK assert len(result.message['items']) == 1 def test_clear_social_profile_id_bad_params( db_fixture, clear_social_profile_bad_params): """Test delete artist url bad params.""" result = artist_url.clear_social_profile( clear_social_profile_bad_params['social_profile_id'], clear_social_profile_bad_params['artist_id']) assert not result assert result.status == status.BAD_REQUEST def test_clear_social_profile_id_not_found(db_fixture): """Test delete artist url not found.""" social_profile_id = 15 artist_id = 1 result = artist_url.clear_social_profile(social_profile_id, artist_id) assert not result assert result.status == status.NOT_FOUND def test_get_artist_ids_that_have_profiles_bad_params(): """Test get linked artist social profile ids.""" artist_ids = [] result = artist_url.get_artist_ids_that_have_profiles(artist_ids) assert not result assert result.status == status.BAD_REQUEST def test_get_artist_ids_that_have_profiles_not_found(db_fixture): """Test get linked artist social profile ids not found.""" artist_ids = [1234, 12] result = artist_url.get_artist_ids_that_have_profiles(artist_ids) assert not result assert result.status == status.NOT_FOUND def test_get_artist_ids_that_have_profiles(db_fixture): """Test get linked artist social profile ids not found.""" artist_ids = [2, 3, 4] result = artist_url.get_artist_ids_that_have_profiles(artist_ids) assert result assert all(item in artist_ids for item in result.message['items']) @freeze_time('2017-11-15 11:31:0') def test_calculate_current_time_slot(): """Test calculate current time slot.""" result = artist_url.calculate_current_time_slot() assert result == 138 @freeze_time('2017-11-15 0:0:0') def test_calculate_current_time_slot_first_slot(): """Test calculate current time slot.""" result = artist_url.calculate_current_time_slot() assert result == 0 @freeze_time('2017-11-15 23:59:59') def test_calculate_current_time_slot_last_slot(): """Test calculate current time slot.""" result = artist_url.calculate_current_time_slot() assert result == collectors.TIME_SLOTS - 1 @freeze_time('2017-11-16 10:48:59') def test_get_social_profiles_for_collection(monkeypatch, db_fixture): """Test get social profiles for collection not found.""" monkeypatch.setattr( artist_url, 'calculate_current_time_slot', MagicMock( return_value=4)) result = artist_url.get_social_profiles_for_collection() assert result.status == status.OK assert len(result.message['items']) == 3 @freeze_time('2017-11-16 10:48:59') def test_get_social_profiles_for_collection_not_found(monkeypatch, db_fixture): """Test get social profiles for collection not found.""" monkeypatch.setattr( artist_url, 'calculate_current_time_slot', MagicMock( return_value=13)) result = artist_url.get_social_profiles_for_collection() assert not result assert result.status == status.NOT_FOUND