"""Unit tests for track playlist model layer.""" from unittest.mock import patch import pytest from sound_recordings.models import playlists as playlists from tests.unit.models.conftest import snowflake_fetch_assert # This mock SQL is to test that filtering is correct MOCK_SQL = "{filter_clause}" @pytest.fixture def mock_load_query(): """Mock load query.""" with patch("sound_recordings.models.playlists.SQLLoader") as sql_loader: load_query = sql_loader.load_query load_query.return_value = MOCK_SQL yield load_query @pytest.fixture def mock_store_availability(): """Mock store_availability.""" with patch( "sound_recordings.models.playlists.store_availability" ) as store_availability: store_availability.get_playlist_store_ids.return_value = [1, 187, 286] yield store_availability @pytest.fixture def db_result(): """Mock db result.""" return [ ( "http://www.example.org", 10, "Apple Playlist", "http://www.example.org/{w}x{h}picture.jpg", "asd", 1, 2340, ), ( "http://www.example.org", None, "Spotify Playlist", "http://www.example.org/picture.jpg", "asd", 286, 821, ), ( "http://www.example.org", 204, "Spotify Playlist 2", "http://www.example.org/picture.jpg", "asd", 286, 234, ), ( "http://www.example.org", None, "Amazon Playlist", "http://www.example.org/picture.jpg", "asd", 187, 2, ), ("http://www.example.org", 20, None, None, "some_playlist_type", 187, 2), ( "spotify-personalized-playlist:dailyMix", 50, "Spotify Personalized Playlist", None, "asd", 286, 23, ), ] @pytest.fixture def expected_response(): """Expect formatted response.""" return [ { "playlist_name": "Apple Playlist", "playlist_url": "http://www.example.org", "playlist_image": "http://www.example.org/300x300picture.jpg", "playlist_type_name": "asd", "store_id": 1, "streams": 2340, "followers": 10, }, { "playlist_name": "Spotify Playlist", "playlist_url": "http://www.example.org", "playlist_image": "http://www.example.org/picture.jpg", "playlist_type_name": "asd", "store_id": 286, "streams": 821, "followers": None, }, { "playlist_name": "Spotify Playlist 2", "playlist_url": "http://www.example.org", "playlist_image": "http://www.example.org/picture.jpg", "playlist_type_name": "asd", "store_id": 286, "streams": 234, "followers": 204, }, { "playlist_name": "Amazon Playlist", "playlist_url": "http://www.example.org", "playlist_image": "http://www.example.org/picture.jpg", "playlist_type_name": "asd", "store_id": 187, "streams": 2, "followers": None, }, { "playlist_name": "Some Playlist Type", "playlist_url": "http://www.example.org", "playlist_image": None, "playlist_type_name": "some_playlist_type", "store_id": 187, "streams": 2, "followers": 20, }, { "playlist_name": "Daily Mix", "playlist_url": "spotify-personalized-playlist:dailyMix", "playlist_image": "https://cdn.theorchard.io/web-icons/dailyMix.png", "playlist_type_name": "asd", "store_id": 286, "streams": 23, "followers": 50, }, ] class TestGetPlaylistsForPeriodWithLabel: """Test get_playlists_for_period with label.""" permissions_filter = { "label_ids": [7123], "artist_ids": None, "subaccount_ids": None, } args = { "isrc": "isrc", "distributors": ["theorchard"], "countries": ["DE"], "start_date": "2019-12-01", "end_date": "2019-12-01", "limit": "$$$$", "offset": 0, } expected_sql = ( "product_id IN (SELECT product_id FROM dim_release " "WHERE labelid IN (:label_ids)) " "AND (download_activity_date " "BETWEEN :start_date AND :end_date) " " AND feed_id IN (:feed_ids) " ) @pytest.fixture def mock_snowflake_fetchall(self, db_result): """Mock Snowflake fetchall.""" with patch("sound_recordings.models.playlists.snowflake.fetchall") as fetchall: fetchall.return_value = db_result yield fetchall @pytest.fixture(autouse=True) def no_cache(self): """Mock out caching of response.""" with patch("sound_recordings.connectors.redis.client.get") as get: get.return_value = None yield get @pytest.fixture def response( self, mock_snowflake_fetchall, mock_load_query, mock_store_availability, expected_response, ): """Return playlists for track.""" return playlists.get_playlists(self.permissions_filter, **self.args) def test_succeeds(self, response, expected_response): """Test successful response.""" assert response.message == expected_response def test_query_is_loaded(self, response, mock_load_query): """Test correct query is loaded.""" mock_load_query.assert_called_once_with("playlists_by_country") def test_loaded_query_is_run(self, response, mock_snowflake_fetchall): """Test loaded query is run.""" expected_args = self.args del expected_args["countries"] expected_args["country_codes"] = ["DE"] expected_args["store_ids"] = [1, 187, 286] snowflake_fetch_assert( mock_snowflake_fetchall, self.expected_sql, {**self.permissions_filter, **expected_args}, ) class TestGetPlaylistsForPeriodWithSubaccount: """Test get_playlists_for_period with subaccount.""" permissions_filter = { "label_ids": None, "artist_ids": None, "subaccount_ids": [345], } args = { "isrc": "isrc", "distributors": ["theorchard"], "store_ids": [1, 187, 286], "start_date": "2019-12-01", "end_date": "2019-12-01", "limit": 0, "offset": "$$$$", } expected_sql = ( "product_id IN (SELECT product_id FROM dim_release " "WHERE subaccountid IN (:subaccount_ids)) " "AND (download_activity_date BETWEEN :start_date AND " ":end_date) AND feed_id IN (:feed_ids) " ) @pytest.fixture def mock_snowflake_fetchall(self, db_result): """Mock Snowflake fetchall.""" with patch("sound_recordings.models.playlists.snowflake.fetchall") as fetchall: fetchall.return_value = db_result yield fetchall @pytest.fixture(autouse=True) def no_cache(self): """Mock out caching of response.""" with patch("sound_recordings.connectors.redis.client.get") as get: get.return_value = None yield get @pytest.fixture def response( self, mock_snowflake_fetchall, mock_load_query, mock_store_availability ): """Return playlists for track.""" return playlists.get_playlists(self.permissions_filter, **self.args) def test_succeeds(self, response, expected_response): """Test successful response.""" assert response.message == expected_response def test_query_is_loaded(self, response, mock_load_query): """Test correct query is loaded.""" mock_load_query.assert_called_once_with("playlists") def test_loaded_query_is_run(self, response, mock_snowflake_fetchall): """Test loaded query is run.""" snowflake_fetch_assert( mock_snowflake_fetchall, self.expected_sql, {**self.permissions_filter, **self.args, "store_ids": [1, 187, 286]}, )