"""Unit tests for channel traffic sources logic layer.""" from unittest.mock import patch import pytest from analytics.api import app from analytics.logic import channel_traffic_sources @pytest.fixture(autouse=True) def mock_context(): """Mock context.""" with app.test_request_context(): yield {} MOCK_SNOWFLAKE_ROWS = [ { "source": "Playlists", "views": 7308, "premium_views": 604, "average_view_duration_seconds": 161.699904696223, "average_view_duration_percentage": 59.232352037494, "watch_time_minutes": 19707.6213, "premium_watch_time_minutes": 1769.65209, }, { "source": "YouTube search", "views": 6681, "premium_views": 303, "average_view_duration_seconds": 230.368633715013, "average_view_duration_percentage": 57.281737578207, "watch_time_minutes": 25662.96799, "premium_watch_time_minutes": 889.26512, }, ] @pytest.fixture def mock_store_availability(): """Mock store_availability.""" with patch( "analytics.logic.channel_traffic_sources.store_availability" ) as store_availability: store_availability.get_video_sources.return_value = [ {"id": 453, "name": "YouTube"} ] store_availability.get_video_store_ids.return_value = [453] yield store_availability def test_all_time_no_country_uses_rollup(mock_context, mock_store_availability): """Default (no country, no dates) uses the all-time rollup table.""" with patch( "analytics.logic.channel_traffic_sources.TopChannelTrafficSources" ) as query_cls: query_cls.return_value.execute.return_value = MOCK_SNOWFLAKE_ROWS result = channel_traffic_sources.get_top_channel_traffic_sources( { "channel_id": "channel_id", "country_ids": [], "store_ids": [], "start_date": None, "end_date": None, "distributors": ["theorchard"], "limit": 5, }, { "permission_label_ids": [], "permission_artist_ids": [], "permission_subaccount_ids": [], "permission_label_participant_ids": [], }, ) _, init_params = query_cls.call_args.args, query_cls.call_args.args[0] assert ( init_params["table_name"] == "VIEWS_BY_CHANNEL_SOURCE_FEED_DISTRIBUTOR_ROLLUP" ) assert init_params["all_time"] is True assert result.message["channel_id"] == "channel_id" assert len(result.message["top_channel_traffic_sources"]) == 2 def test_dates_with_country_uses_country_daily_table( mock_context, mock_store_availability ): """Date range + country → country daily table, all_time=False.""" with patch( "analytics.logic.channel_traffic_sources.TopChannelTrafficSources" ) as query_cls: query_cls.return_value.execute.return_value = [] channel_traffic_sources.get_top_channel_traffic_sources( { "channel_id": "channel_id", "country_ids": ["US"], "store_ids": [], "start_date": "2020-01-01", "end_date": "2020-01-28", "distributors": ["theorchard"], "limit": 5, }, { "permission_label_ids": [], "permission_artist_ids": [], "permission_subaccount_ids": [], "permission_label_participant_ids": [], }, ) init_params = query_cls.call_args.args[0] assert ( init_params["table_name"] == "VIEWS_BY_CHANNEL_SOURCE_COUNTRY_FEED_DISTRIBUTOR_DAILY" ) assert init_params["all_time"] is False def test_non_label_profile_early_return(mock_context, mock_store_availability): """Non-label profile (e.g. artist) early-returns an empty payload.""" with patch( "analytics.logic.channel_traffic_sources.TopChannelTrafficSources" ) as query_cls: result = channel_traffic_sources.get_top_channel_traffic_sources( { "channel_id": "channel_id", "country_ids": [], "store_ids": [], "start_date": None, "end_date": None, "distributors": ["theorchard"], "limit": 5, }, { "permission_label_ids": [], "permission_artist_ids": [42], "permission_subaccount_ids": [], "permission_label_participant_ids": [], }, ) query_cls.assert_not_called() assert result.message["top_channel_traffic_sources"] == []