"""Test for the Chart Handler.""" from unittest.mock import patch from charts.logic import charts import pytest import datetime admin_permissions = { 'artist_ids': ['*'], 'vendor_ids': ['*'], 'subaccount_ids': ['*'], 'label_participant_ids': ['*'], } def test_get_charts_by_definition_with_type(): """Test get_charts_by_definition_with_type.""" fetchall_mock_response = [ ( 'chart_id_1', ), ( 'chart_id_2', ) ] with patch.object(charts.snowflake, 'fetchall', return_value=fetchall_mock_response): response = charts.get_charts_by_definition(platform='youtube', target='video', frequency='weekly', type='music videos') assert response == [ { 'chartId': 'chart_id_1', }, { 'chartId': 'chart_id_2' } ] def test_get_charts_by_definition_with_genre(): """Test get_charts_by_definition_with_genre.""" fetchall_mock_response = [ ( 'chart_id_1', ), ( 'chart_id_2', ) ] with patch.object(charts.snowflake, 'fetchall', return_value=fetchall_mock_response): response = charts.get_charts_by_definition(platform='amazon', target='track', frequency='weekly', genre='Blues_&_Country') assert response == [ { 'chartId': 'chart_id_1', }, { 'chartId': 'chart_id_2' } ] def test_get_charts_by_definition_with_type_and_country(): """Test get_charts_by_definition_with_type_and_country.""" fetchall_mock_response = [ ( 'chart_id_1', ), ( 'chart_id_2', ) ] with patch.object(charts.snowflake, 'fetchall', return_value=fetchall_mock_response): response = charts.get_charts_by_definition(platform='youtube', target='video', frequency='weekly', type='music videos', country_code='global') assert response == [ { 'chartId': 'chart_id_1', }, { 'chartId': 'chart_id_2' } ] def test_get_charts_by_definition_with_missing_key_value(): """Test get_charts_by_definition_with_missing_key_value.""" with pytest.raises(TypeError, match=r"get_charts_by_definition\(\) missing 1 required positional argument: 'platform'"): charts.get_charts_by_definition(target='video', frequency='weekly', type='music videos') def test_get_charts_by_definition_with_missing_type_and_genre(): """Test get_charts_by_definition_with_missing_type_and_genre.""" with pytest.raises(ValueError, match=r"Either type or genre must be defined"): charts.get_charts_by_definition(platform='youtube', target='video', frequency='weekly') def test_get_chart_rankings_aggregated_values_on_date_dataloaded(): """Test get_chart_rankings_aggregated_values_on_date_dataloaded.""" fetchall_mock_response = [ [ '9745468d-9dbd-40a7-af10-3a0066fd808b', "2022-07-01", "038f3079-31ad-4335-aa4e-3ad89cdc1cc7", ], [ None, None, None, ] ] with patch.object(charts.snowflake, 'fetchall_nocache', return_value=fetchall_mock_response): response = charts.get_chart_rankings_aggregated_values_on_date_dataloaded([ {"chart_id": "9745468d-9dbd-40a7-af10-3a0066fd808b", "chart_date": "2022-07-01", "public_sound_recording_id": "psr-id", "chartmetric_track_id": None, "public_product_id": None}, {"chart_id": "038f3079-31ad-4335-aa4e-3ad89cdc1cc7", "chart_date": "2022-07-01", "public_sound_recording_id": "psr-id", "chartmetric_track_id": None, "public_product_id": None}, ]) assert response == [ { "data": { "id": "9745468d-9dbd-40a7-af10-3a0066fd808b", "chartDate": "2022-07-01", "uuid": "038f3079-31ad-4335-aa4e-3ad89cdc1cc7", } }, { "data": None, } ] @pytest.mark.parametrize( 'order_by, filter, expected_sql, expected_params', [ ( 'FEATURING', {}, ['ORDER BY count(chartid)'], {'chart_date': '2024-12-11', 'chart_ids': ['9745468d-9dbd-40a7-af10-3a0066fd808b']} ), ( 'MIN_POSITION', {}, ['ORDER BY min(position)'], {'chart_date': '2024-12-11', 'chart_ids': ['9745468d-9dbd-40a7-af10-3a0066fd808b']} ), ( 'FEATURING', {'brand_uuid_in': ['brand-uuid']}, ['ORDER BY count(chartid)', 'LEFT JOIN vendor_company_brand_parent_company'], {'brand_uuid_in': ['brand-uuid'], 'chart_date': '2024-12-11', 'chart_ids': ['9745468d-9dbd-40a7-af10-3a0066fd808b'], 'nfd_filter_admin_brands': [["'N', 'EditoriallySuspectContent', " "'SMEAnalyticsDummy', 'SwitchboardDummy'"]]} ), ] ) def test_get_global_sound_recordings_for_charts_date(order_by, filter, expected_sql, expected_params): """Test get_global_sound_recordings_for_charts_date.""" fetchall_mock_response = [ [ "isrc1", True, ], [ "isrc2", True, ], [ "isrc3", False, ], ] with patch.object(charts.snowflake, 'fetchall_nocache', return_value=fetchall_mock_response) as snowflake: response = charts.get_global_sound_recordings_for_charts_date( [ "9745468d-9dbd-40a7-af10-3a0066fd808b", ], "2024-12-11", filter, order_by, "DESC", { 'vendor_ids': '*' }, ) assert response == { "globalSoundRecordingIsrcs": [ "isrc1", "isrc2", ] } sql, params = snowflake.call_args[0] for exp_sql in expected_sql: assert exp_sql in sql assert expected_params == params