"""Unit tests for top markets model layer.""" from decimal import Decimal from unittest.mock import patch import pytest from sound_recordings.models import top_markets from tests.unit.models.conftest import snowflake_fetch_assert MOCK_SQL = "{filter_clause}" @pytest.fixture def mock_db_result(): """Mock database result.""" return [("UA", "Kyiv", 42, Decimal(0.333333)), ("US", "NYC", 37, Decimal(0.42))] @pytest.fixture def mock_snowflake_fetchall(mock_db_result): """Mock Snowflake fetchall.""" with patch("sound_recordings.models.top_markets." "snowflake.fetchall") as fetchall: fetchall.return_value = mock_db_result yield fetchall @pytest.fixture def mock_snowflake_fetchall_empty(): """Mock Snowflake fetchall empty response.""" with patch("sound_recordings.models.top_markets." "snowflake.fetchall") as fetchall: fetchall.return_value = () yield fetchall @pytest.fixture(autouse=True) def no_cache(): """Mock out caching of response.""" with patch("sound_recordings.connectors.redis.client.get") as get: get.return_value = None yield get @pytest.fixture def mock_load_query(): """Mock load query.""" with patch("sound_recordings.models.top_markets.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_markets.store_availability" ) as store_availability: store_availability.get_store_ids.return_value = [1, 286] yield store_availability def test_get_top_markets(mock_snowflake_fetchall): """Test successful response.""" expected_response = [ { "country_code": "UA", "streams_7_days": 42, "orchard_region_name": "Kyiv", "growth_percentage": Decimal(0.333333), }, { "country_code": "US", "streams_7_days": 37, "orchard_region_name": "NYC", "growth_percentage": Decimal(0.42), }, ] permissions_filter = { "label_ids": [7123], "artist_ids": None, "subaccount_ids": None, } response = top_markets.get_top_markets(permissions_filter, "test", ["theorchard"]) assert response == expected_response def test_get_top_markets_with_countries(mock_snowflake_fetchall, mock_load_query): """Test successful response with countries.""" expected_response = [ { "country_code": "UA", "streams_7_days": 42, "orchard_region_name": "Kyiv", "growth_percentage": Decimal(0.333333), }, { "country_code": "US", "streams_7_days": 37, "orchard_region_name": "NYC", "growth_percentage": Decimal(0.42), }, ] permissions_filter = { "label_ids": [7123], "artist_ids": None, "subaccount_ids": None, } store_ids = [1, 286] countries = ["US", "GB"] distributors = ["theorchard"] isrc = "test" expected_args = { **permissions_filter, "isrc": isrc, "distributors": distributors, "store_ids": store_ids, "country_codes": countries, } response = top_markets.get_top_markets( permissions_filter, isrc, distributors, store_ids=store_ids, countries=countries ) mock_load_query.assert_called_with("top_markets_by_country") expected_sql = ( "product_id IN (SELECT product_id FROM dim_release " "WHERE labelid IN (:label_ids)) " "AND feed_id IN (:feed_ids) " ) snowflake_fetch_assert(mock_snowflake_fetchall, expected_sql, expected_args) assert response == expected_response def test_get_top_markets_fail(mock_snowflake_fetchall_empty): """Test empty response.""" permissions_filter = { "label_ids": [7123], "artist_ids": None, "subaccount_ids": None, } response = top_markets.get_top_markets(permissions_filter, "test", ["theorchard"]) assert response == [] def test_get_top_markets_with_subaccount( mock_load_query, mock_snowflake_fetchall, mock_store_availability ): """Test get_top_markets with subaccount.""" permissions_filter = { "label_ids": None, "artist_ids": None, "subaccount_ids": [345], "feed_ids": [1, 2], } isrc = "isrc" store_ids = [1, 286] expected_args = { **permissions_filter, "isrc": isrc, "store_ids": store_ids, "distributors": ["theorchard"], } top_markets.get_top_markets(permissions_filter, isrc, ["theorchard"], store_ids) expected_sql = ( "product_id IN (SELECT product_id FROM dim_release " "WHERE subaccountid IN (:subaccount_ids)) " "AND feed_id IN (:feed_ids) " ) snowflake_fetch_assert(mock_snowflake_fetchall, expected_sql, expected_args) def test_get_top_markets_with_empty_subaccount( mock_load_query, mock_snowflake_fetchall, mock_store_availability ): """Test get_top_markets with empty subaccount.""" permissions_filter = { "label_ids": None, "artist_ids": None, "subaccount_ids": [345], "feed_ids": [1, 2], } isrc = "isrc" store_ids = [1, 286] expected_args = { **permissions_filter, "isrc": isrc, "distributors": ["theorchard"], "store_ids": store_ids, } top_markets.get_top_markets(permissions_filter, isrc, ["theorchard"], store_ids) expected_sql = ( "product_id IN (SELECT product_id FROM dim_release " "WHERE subaccountid IN (:subaccount_ids)) " "AND feed_id IN (:feed_ids) " ) snowflake_fetch_assert(mock_snowflake_fetchall, expected_sql, expected_args) def test_get_top_markets_with_artist_profile( mock_load_query, mock_snowflake_fetchall, mock_store_availability ): """Test get_top_markets with empty subaccount.""" permissions_filter = { "label_ids": None, "artist_ids": [917636, 1552280], "feed_ids": [1, 2], } isrc = "isrc" store_ids = [1, 286] expected_args = { **permissions_filter, "isrc": isrc, "distributors": ["theorchard"], "store_ids": store_ids, } top_markets.get_top_markets(permissions_filter, isrc, ["theorchard"], store_ids) expected_sql = ( "product_id IN (SELECT product_id " "FROM dim_release WHERE artistid IN (:artist_ids)) AND " "feed_id IN (:feed_ids) " ) snowflake_fetch_assert(mock_snowflake_fetchall, expected_sql, expected_args)