"""Unit tests for channel top videos logic layer.""" import pytest from analytics.api import app from analytics.logic import channel_top_videos @pytest.fixture def mock_get_channel_top_videos_model_payload(): """Mock channels payload for channels traffic sources model.""" return [ { "average_view_duration_percentage": 74.043686874022, "premium_watch_time_minutes": 45287.61365, "views": 180121, "premium_views": 22232, "watch_time_minutes": 401156.0339, "average_view_duration_seconds": 131.797762466199, "video_id": "1", }, { "average_view_duration_percentage": 58.637196701543, "premium_watch_time_minutes": 10606.8897, "views": 177171, "premium_views": 6197, "watch_time_minutes": 328247.12536, "average_view_duration_seconds": 104.374210106936, "video_id": "2", }, { "average_view_duration_percentage": 76.877016625891, "premium_watch_time_minutes": 8889.0533, "views": 82280, "premium_views": 4201, "watch_time_minutes": 189962.16921, "average_view_duration_seconds": 136.841089129887, "video_id": "3", }, ] def snake_to_camel(snake_str): components = snake_str.split("_") return components[0] + "".join(x.title() for x in components[1:]) def convert_keys_to_camel_case(data): return {snake_to_camel(key): value for key, value in data.items()} @pytest.fixture(autouse=True) def permissions(): """Return default employee permissions.""" yield { "permission_label_ids": [], "permission_subaccount_ids": [], "permission_artist_ids": [], "permission_label_participant_ids": [], } @pytest.fixture(autouse=True) def mock_context(): """Mock successful context.""" with app.test_request_context(): yield {} @pytest.fixture(autouse=True) def mock_video_store_ids(mocker): """Mock video store ids.""" mocker.patch( "analytics.logic.channel_top_videos.store_availability.get_video_store_ids", return_value=[1, 2, 3], ) @pytest.fixture(autouse=True) def mock_video_sources(mocker): """Mock video store ids.""" mocker.patch( "analytics.logic.channel_top_videos.store_availability.get_video_sources", return_value=[1, 2, 3], ) @pytest.fixture(autouse=True) def mock_add_outage_error_to_stores(mocker): """Mock add_outage_error_to_stores.""" mocker.patch( "analytics.logic.channel_top_videos.add_outage_error_to_stores", return_value=[{"name": "YouTube", "id": 453}], ) def test_get_channel_top_videos_empty_result(mocker, permissions): """Test channel_top_videos when no store_ids are provided.""" mocker.patch( "analytics.logic.channel_top_videos.ChannelTopVideos.execute", side_effect=[[]] ) query_params = {"channel_id": "UCFg7-w47VVA3ozhvnR6P0YQ", "store_ids": []} result = channel_top_videos.get_channel_top_videos( query_params, permissions ).message assert result["sources"] == [{"name": "YouTube", "id": 453}] assert result["top_channel_videos"] == [] def test_channel_top_videos( mocker, mock_get_channel_top_videos_model_payload, permissions ): """Test get_channel_metrics with aggregate metrics.""" mocker.patch( "analytics.logic.channel_top_videos.store_availability.get_video_store_ids", return_value=[1, 2, 3], ) mocker.patch( "analytics.logic.channel_top_videos.ChannelTopVideos.execute", side_effect=[ mock_get_channel_top_videos_model_payload, ], ) query_params = {"channel_id": "UCFg7-w47VVA3ozhvnR6P0YQ", "store_ids": [1, 2]} result = channel_top_videos.get_channel_top_videos( query_params, permissions ).message assert result["sources"] == [{"name": "YouTube", "id": 453}] assert len(result["top_channel_videos"]) == 3 # Check all fields for each channel metric for i, expected_metric in enumerate(mock_get_channel_top_videos_model_payload): expected_metric = convert_keys_to_camel_case(expected_metric) for field, expected_value in expected_metric.items(): assert result["top_channel_videos"][i][field] == expected_value