"""Unit tests for the top sound_recordings model layer.""" from unittest.mock import MagicMock, patch import pytest from sound_recordings.models import top_sound_recordings MOCK_SQL = "{filter_clause}" @pytest.fixture() def mock_snowflake(monkeypatch): """Mock for Snowflake database client.""" snowflake_mock = MagicMock() monkeypatch.setattr( "sound_recordings.models.top_sound_recordings.snowflake", snowflake_mock ) return snowflake_mock @pytest.fixture def mock_db_result(): """Mock database result.""" return [("isrc", 1, 0.5)] @pytest.fixture def mock_db_subaccount_result(): """Mock database result.""" return [("isrc", 1, 0.5)] @pytest.fixture def mock_load_query(): """Mock load query.""" with patch("sound_recordings.models.top_sound_recordings.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.top_sound_recordings.store_availability" ) as store_availability: store_availability.get_store_ids.return_value = [1, 286] yield store_availability class TestGetTopSoundRecordingsWithLabel: """Test get_top_sound_recordings with label.""" expected_sql = "AND subaccountid IS NULL" expected_response = [ { "isrc": "isrc", "streams": 1, "growth_percentage": 0.5, } ] @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 top_sound_recordings_response( self, mock_snowflake, mock_load_query, mock_db_result, mock_store_availability ): """Return top sound_recordings.""" permissions_filter = { "label_ids": [7123], "artist_ids": None, "subaccount_ids": None, } limit = 10 offset = 0 mock_snowflake.fetchall.return_value = mock_db_result return top_sound_recordings.get_top_sound_recordings( permissions_filter, ["theorchard"], limit, offset ) @pytest.fixture def top_sound_recordings_response_with_countries( self, mock_snowflake, mock_load_query, mock_db_result, mock_store_availability ): """Return response from top_sound_recordings.""" permissions_filter = { "label_ids": [7123], "artist_ids": None, "subaccount_ids": None, } limit = 10 offset = 0 countries = ["DE", "NO", "US"] mock_snowflake.fetchall.return_value = mock_db_result return top_sound_recordings.get_top_sound_recordings( permissions_filter, ["theorchard"], limit, offset, countries ) def test_succeeds(self, top_sound_recordings_response): """Test successful response.""" assert top_sound_recordings_response == self.expected_response def test_succeeds_with_countries( self, top_sound_recordings_response_with_countries ): """Test successful response.""" assert top_sound_recordings_response_with_countries == self.expected_response def test_top_sound_recordings_query_is_loaded( self, top_sound_recordings_response, mock_load_query ): """Test top sound_recordings query is loaded.""" mock_load_query.assert_called_once_with("get_top_sound_recordings") def test_top_sound_recordings_with_countries_query_is_loaded( self, top_sound_recordings_response_with_countries, mock_load_query ): """Test top_sound_recordings_by_country query is loaded.""" mock_load_query.assert_called_once_with("get_top_sound_recordings_by_country") class TestGetTopSoundRecordingsWithSubaccount: """Test get_top_sound_recordings with subaccount.""" expected_sql = "AND subaccountid = :subaccountid" expected_response = [{"isrc": "isrc", "streams": 1, "growth_percentage": 0.5}] @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 top_sound_recordings_response( self, mock_snowflake, mock_load_query, mock_db_subaccount_result, mock_store_availability, ): """Return top sound_recordings.""" permissions_filter = { "label_ids": [7123], "artist_ids": None, "subaccount_ids": [345], } limit = 10 offset = 0 mock_snowflake.fetchall.return_value = mock_db_subaccount_result return top_sound_recordings.get_top_sound_recordings( permissions_filter, ["theorchard"], limit, offset ) @pytest.fixture def top_sound_recordings_all_time_response( self, mock_snowflake, mock_load_query, mock_db_subaccount_result, mock_store_availability, ): """Return top sound_recordings.""" permissions_filter = { "label_ids": [7123], "artist_ids": None, "subaccount_ids": [345], } limit = 10 offset = 0 order_by = "streams_all_time" mock_snowflake.fetchall.return_value = mock_db_subaccount_result return top_sound_recordings.get_top_sound_recordings( permissions_filter, ["theorchard"], limit, offset, [], order_by ) @pytest.fixture def top_sound_recordings_with_countries_response( self, mock_snowflake, mock_load_query, mock_db_subaccount_result, mock_store_availability, ): """Return response from top_sound_recordings.""" permissions_filter = { "label_ids": [7123], "artist_ids": None, "subaccount_ids": [345], } limit = 10 offset = 0 countries = ["DE", "NO", "US"] order_by = "streams_28_days" mock_snowflake.fetchall.return_value = mock_db_subaccount_result return top_sound_recordings.get_top_sound_recordings( permissions_filter, ["theorchard"], limit, offset, countries, order_by ) @pytest.fixture def top_sound_recordings_with_countries_all_time_response( self, mock_snowflake, mock_load_query, mock_db_subaccount_result, mock_store_availability, ): """Return response from top_sound_recordings.""" permissions_filter = { "label_ids": [7123], "artist_ids": None, "subaccount_ids": [345], } limit = 10 offset = 0 countries = ["DE", "NO", "US"] order_by = "streams_all_time" mock_snowflake.fetchall.return_value = mock_db_subaccount_result return top_sound_recordings.get_top_sound_recordings( permissions_filter, ["theorchard"], limit, offset, countries, order_by ) def test_succeeds(self, top_sound_recordings_response): """Test successful response.""" assert top_sound_recordings_response == self.expected_response def test_top_sound_recordings_query_is_loaded( self, top_sound_recordings_response, mock_load_query ): """Test top sound_recordings query is loaded.""" mock_load_query.assert_called_once_with("get_top_sound_recordings") def test_top_sound_recordings_all_time_query_is_loaded( self, top_sound_recordings_all_time_response, mock_load_query ): """Test top sound_recordings query is loaded.""" mock_load_query.assert_called_once_with("get_top_sound_recordings_all_time") def test_top_sound_recordings_with_countries_query_is_loaded( self, top_sound_recordings_with_countries_response, mock_load_query ): """Test top_sound_recordings_by_country query is loaded.""" mock_load_query.assert_called_once_with("get_top_sound_recordings_by_country") def test_top_sound_recordings_with_countries_all_time_query_is_loaded( self, top_sound_recordings_with_countries_all_time_response, mock_load_query ): """Test top_sound_recordings_by_country query is loaded.""" mock_load_query.assert_called_once_with( "get_top_sound_recordings_by_country_all_time" ) class TestGetTopSoundRecordingsWithOrderBy: """Test get_top_soundrecording with order_by.""" @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 def get_top_sound_recordings(self, order_by): """Return top metrics.""" permissions_filter = { "label_ids": [7123], "artist_ids": None, "subaccount_ids": [345], } limit = 10 offset = 10 return top_sound_recordings.get_top_sound_recordings( permissions_filter, ["theorchard"], limit, offset, [], order_by ) def test_raises_error_on_invalid_order_by_field(self): """Test exception thrown.""" with pytest.raises(Exception, match=r"Invalid order_by field"): self.get_top_sound_recordings("rubbish")