"""Unit tests for track top_countries model layer.""" from unittest.mock import patch import pytest from sound_recordings.models import top_countries as top_countries 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.top_countries.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_countries.store_availability" ) as store_availability: store_availability.get_store_ids.return_value = [1, 4, 286, 348, 496, 708, 716] store_availability.get_download_store_ids.return_value = [1, 187, 496] yield store_availability @pytest.fixture def db_streams_result(): """Mock db result.""" return [("US", 8723), ("CA", 982), ("FR", 82), ("NO", 12), ("SE", 2)] @pytest.fixture def db_downloads_result(): """Mock db result.""" return [("US", 872), ("CA", 98), ("FR", 8), ("NO", 1)] @pytest.fixture def expected_streams_response(): """Expect formatted response.""" return [ {"country_code": "US", "streams": 8723}, {"country_code": "CA", "streams": 982}, {"country_code": "FR", "streams": 82}, {"country_code": "NO", "streams": 12}, {"country_code": "SE", "streams": 2}, ] @pytest.fixture def expected_downloads_response(): """Expect formatted response.""" return [ {"country_code": "US", "downloads": 872}, {"country_code": "CA", "downloads": 98}, {"country_code": "FR", "downloads": 8}, {"country_code": "NO", "downloads": 1}, ] class TestGetTopCountriesAggregateStreamsForPeriodWithLabel: """Test get_top_countries_aggregate_streams_for_period with label.""" permissions_filter = { "label_ids": [7123], "artist_ids": None, "subaccount_ids": None, "feed_ids": [1, 2], } args = { "isrc": "isrc", "start_date": "2019-12-01", "end_date": "2019-12-01", "distributors": ["theorchard"], } 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_streams_result): """Mock Snowflake fetchall.""" with patch( "sound_recordings.models.top_countries.snowflake.fetchall" ) as fetchall: fetchall.return_value = db_streams_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_streams_response, ): """Return track top_countries.""" return top_countries.get_top_countries_aggregate_streams( self.permissions_filter, **self.args ) def test_succeeds(self, response, expected_streams_response): """Test successful response.""" assert response == expected_streams_response def test_query_is_loaded(self, response, mock_load_query): """Test correct query is loaded.""" mock_load_query.assert_called_once_with("top_countries_aggregate_streams") def test_loaded_query_is_run(self, response, mock_snowflake_fetchall): """Test loaded query is run.""" expected_args = self.args expected_args["store_ids"] = [1, 4, 286, 348, 496, 708, 716] snowflake_fetch_assert( mock_snowflake_fetchall, self.expected_sql, {**self.permissions_filter, **expected_args}, ) class TestGetTopCountriesDownloadsForPeriodWithLabel: """Test get_top_countries_downloads_for_period with label.""" permissions_filter = { "label_ids": [7123], "artist_ids": None, "subaccount_ids": None, "feed_ids": [1, 2], } args = { "isrc": "isrc", "start_date": "2019-12-01", "end_date": "2019-12-01", "distributors": ["theorchard"], } 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_downloads_result): """Mock Snowflake fetchall.""" with patch( "sound_recordings.models.top_countries.snowflake.fetchall" ) as fetchall: fetchall.return_value = db_downloads_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_downloads_response, ): """Return track top_countries.""" return top_countries.get_top_countries_downloads( self.permissions_filter, **self.args ) def test_succeeds(self, response, expected_downloads_response): """Test successful response.""" assert response == expected_downloads_response def test_query_is_loaded(self, response, mock_load_query): """Test correct query is loaded.""" mock_load_query.assert_called_once_with("top_countries_downloads") def test_loaded_query_is_run(self, response, mock_snowflake_fetchall): """Test loaded query is run.""" expected_args = self.args expected_args["store_ids"] = [1, 187, 496] snowflake_fetch_assert( mock_snowflake_fetchall, self.expected_sql, {**self.permissions_filter, **expected_args}, ) class TestGetTopCountriesStreamsForPeriodWithSubaccount: """Test get_top_countries_streams with subaccount.""" permissions_filter = { "label_ids": None, "artist_ids": None, "subaccount_ids": [345], "feed_ids": [1, 2], } args = { "isrc": "isrc", "store_ids": [1, 496], "start_date": "2019-12-01", "end_date": "2019-12-01", "distributors": ["theorchard"], } 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_streams_result): """Mock Snowflake fetchall.""" with patch( "sound_recordings.models.top_countries.snowflake.fetchall" ) as fetchall: fetchall.return_value = db_streams_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 track top countries.""" return top_countries.get_top_countries_aggregate_streams( self.permissions_filter, **self.args ) def test_succeeds(self, response, expected_streams_response): """Test successful response.""" assert response == expected_streams_response def test_query_is_loaded(self, response, mock_load_query): """Test correct query is loaded.""" mock_load_query.assert_called_once_with("top_countries_aggregate_streams") def test_loaded_query_is_run(self, response, mock_snowflake_fetchall): """Test loaded query is run.""" expected_args = self.args expected_args["store_ids"] = [1, 496] snowflake_fetch_assert( mock_snowflake_fetchall, self.expected_sql, {**self.permissions_filter, **expected_args}, ) class TestGetTopCountriesDownloadsForPeriodWithSubaccount: """Test get_top_countries_downloads with subaccount.""" permissions_filter = { "label_ids": None, "artist_ids": None, "subaccount_ids": [345], "feed_ids": [1, 2], } args = { "isrc": "isrc", "store_ids": [1, 286], "start_date": "2019-12-01", "end_date": "2019-12-01", "distributors": ["theorchard"], } 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_downloads_result): """Mock Snowflake fetchall.""" with patch( "sound_recordings.models.top_countries.snowflake.fetchall" ) as fetchall: fetchall.return_value = db_downloads_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 track top countries.""" return top_countries.get_top_countries_downloads( self.permissions_filter, **self.args ) def test_succeeds(self, response, expected_downloads_response): """Test successful response.""" assert response == expected_downloads_response def test_query_is_loaded(self, response, mock_load_query): """Test correct query is loaded.""" mock_load_query.assert_called_once_with("top_countries_downloads") def test_loaded_query_is_run(self, response, mock_snowflake_fetchall): """Test loaded query is run.""" expected_args = self.args expected_args["store_ids"] = [1] snowflake_fetch_assert( mock_snowflake_fetchall, self.expected_sql, {**self.permissions_filter, **expected_args}, )