"""Unit tests for videos top-countries logic layer.""" import datetime from unittest.mock import patch import pytest from analytics.api import app from analytics.logic import top_countries_videos @pytest.fixture(autouse=True) def mock_context(): """Mock context.""" with app.test_request_context(): yield {} MOCK_SNOWFLAKE_ROWS = [ {"country_code": "US", "views": 43126}, {"country_code": "FR", "views": 3126}, {"country_code": "CA", "views": 126}, ] @pytest.fixture def mock_store_availability(): """Mock store_availability.""" with patch( "analytics.logic.top_countries_videos.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_videos.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_videos_mocked_db_result( mock_context, mock_store_availability, mock_data_availability ): """Employee (full access) returns the expected payload.""" with patch( "analytics.logic.top_countries_videos.TopCountriesVideos.execute", return_value=MOCK_SNOWFLAKE_ROWS, ): result = top_countries_videos.get_top_countries_videos( { "video_id": "video_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 == { "video_id": "video_id", "sources": [{"id": 453, "name": "YouTube"}], "top_countries_videos": [ {"country_code": "US", "views": 43126}, {"country_code": "FR", "views": 3126}, {"country_code": "CA", "views": 126}, ], } def test_get_top_countries_videos_empty_store_intersection( mock_context, mock_store_availability ): """Empty store intersection returns an empty payload without hitting SQL.""" with patch( "analytics.logic.top_countries_videos.TopCountriesVideos.execute" ) as mock_execute: result = top_countries_videos.get_top_countries_videos( { "video_id": "video_id", "store_ids": [999], # intersection with [453] is empty "start_date": None, "end_date": None, "distributors": ["theorchard"], }, { "permission_label_ids": [], "permission_artist_ids": [], "permission_subaccount_ids": [], "permission_label_participant_ids": [], }, ) mock_execute.assert_not_called() assert result.message["top_countries_videos"] == [] def test_get_top_countries_videos_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_videos.TopCountriesVideos.execute" ) as mock_execute: result = top_countries_videos.get_top_countries_videos( { "video_id": "video_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_videos"] == []