"""Unit tests for channels top-countries logic layer.""" import datetime from unittest.mock import patch import pytest from analytics.api import app from analytics.logic import top_countries_channels @pytest.fixture(autouse=True) def mock_context(): """Mock context.""" with app.test_request_context(): yield {} MOCK_SNOWFLAKE_ROWS = [ {"country_code": "US", "views": 9977}, {"country_code": "MX", "views": 1428}, {"country_code": "BR", "views": 1358}, ] @pytest.fixture def mock_store_availability(): """Mock store_availability.""" with patch( "analytics.logic.top_countries_channels.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 @pytest.fixture def mock_data_availability(): """Mock data_availability.get_date_range for HIGHWATERMARK fallback.""" with patch( "analytics.logic.top_countries_channels.data_availability" ) as data_availability: data_availability.get_date_range.return_value = ( datetime.date(2021, 1, 1), datetime.date(2021, 1, 28), ) yield data_availability def test_get_top_countries_channels_mocked_db_result( mock_context, mock_store_availability, mock_data_availability ): """Employee (full access) returns the expected payload.""" with patch( "analytics.logic.top_countries_channels.TopCountriesChannels.execute", return_value=MOCK_SNOWFLAKE_ROWS, ): result = top_countries_channels.get_top_countries_channels( { "channel_id": "channel_id", "store_ids": [], "start_date": None, "end_date": None, "distributors": ["theorchard", "sme", "awal"], }, { "permission_label_ids": [], "permission_artist_ids": [], "permission_subaccount_ids": [], "permission_label_participant_ids": [], }, ) assert result.message == { "channel_id": "channel_id", "sources": [{"id": 453, "name": "YouTube"}], "top_countries_channels": [ {"country_code": "US", "views": 9977}, {"country_code": "MX", "views": 1428}, {"country_code": "BR", "views": 1358}, ], } def test_get_top_countries_channels_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.top_countries_channels.TopCountriesChannels.execute" ) as mock_execute: result = top_countries_channels.get_top_countries_channels( { "channel_id": "channel_id", "store_ids": [], "start_date": None, "end_date": None, "distributors": ["theorchard"], }, { "permission_label_ids": [], "permission_artist_ids": [42], "permission_subaccount_ids": [], "permission_label_participant_ids": [], }, ) mock_execute.assert_not_called() assert result.message["top_countries_channels"] == []