"""Test for the Chart Handler.""" import datetime from unittest.mock import patch import pytest from charts.logic import chart def test_get_charts(mock_get_all_feature_flags): """Test get_charts.""" fetchall_mock_response = [ [ '7038104f-8845-4a36-b274-ade706d627e3', 'Amazon Daily Folk', 'amazon', 'track', 'GLOBAL', 'default', 'daily', 'Folk', '2021-01-19', datetime.datetime(2021, 2, 11, 11, 19, 16), datetime.datetime(2021, 2, 11, 17, 41, 55), ], ] with patch.object(chart.snowflake, 'fetchall', return_value=fetchall_mock_response): response = chart.get_charts() assert response == [ { 'chartId': '7038104f-8845-4a36-b274-ade706d627e3', 'name': 'Amazon Daily Folk', 'platform': 'amazon', 'target': 'track', 'country': 'GLOBAL', 'type': 'default', 'frequency': 'daily', 'genre': 'Folk', 'lastAvailableTimestamp': '2021-01-19', 'createdAt': datetime.datetime(2021, 2, 11, 11, 19, 16), 'modifiedAt': datetime.datetime(2021, 2, 11, 17, 41, 55) } ] def test_get_chart(): """Test get_chart.""" mock_return_value = [ '7038104f-8845-4a36-b274-ade706d627e3', 'Amazon Daily Folk', 'amazon', 'track', 'GLOBAL', 'default', 'daily', 'Folk', '2021-01-19', datetime.datetime(2021, 2, 11, 11, 19, 16), datetime.datetime(2021, 2, 11, 17, 41, 55), ] with patch.object(chart.snowflake, 'fetchone', return_value=mock_return_value): response = chart.get_chart('7038104f-8845-4a36-b274-ade706d627e3') assert response == { 'chartId': '7038104f-8845-4a36-b274-ade706d627e3', 'name': 'Amazon Daily Folk', 'platform': 'amazon', 'target': 'track', 'country': 'GLOBAL', 'type': 'default', 'frequency': 'daily', 'genre': 'Folk', 'lastAvailableTimestamp': '2021-01-19', 'createdAt': datetime.datetime(2021, 2, 11, 11, 19, 16), 'modifiedAt': datetime.datetime(2021, 2, 11, 17, 41, 55) } def test_get_chart_available_dates(): """Test get_chart_available_dates.""" fetchall_mock_response = [ ('2020-01-01',), ('2020-01-02',), ] with patch.object(chart.snowflake, 'fetchall', return_value=fetchall_mock_response): response = chart.get_chart_available_dates('7038104f-8845-4a36-b274-ade706d627e3') assert response == [ '2020-01-01', '2020-01-02', ] @pytest.mark.parametrize( 'limit_offset, expected_limit_offset', [ [(1, 2), (1, 2)], [(10, 0), (10, 0)], [(10, 11), (10, 11)], [(None, None), Exception], [("None", "None"), Exception], [(-1, 0), Exception], [(0, -1), Exception], ]) def test_get_chart_rankings_limit_offset(limit_offset, expected_limit_offset): limit, offset = limit_offset with patch.object(chart.snowflake, 'fetchall') as fetchall_mock: params = dict( chart_id='a8eee5c8-f871-48b1-a18d-9c1df7f8ebae', chart_date='2021-04-23', limit=limit, offset=offset, ) if expected_limit_offset == Exception: with pytest.raises(Exception): chart.get_chart_rankings(**params) else: chart.get_chart_rankings(**params) fetchall_mock.assert_called() expected_limit, expected_offset = expected_limit_offset query, params = fetchall_mock.call_args[0] assert params == { 'chart_id': 'a8eee5c8-f871-48b1-a18d-9c1df7f8ebae', 'chart_date': '2021-04-23', 'limit': expected_limit, 'offset': expected_offset, } def test_get_chart_rankings(): """Test get_chart_rankings.""" fetchall_mock_response = [ ( 'aa0ad2aa-cee1-49b6-9758-9186c7c166e0', 'a8eee5c8-f871-48b1-a18d-9c1df7f8ebae', 1, 0, None, None, None, None, None, None, 'B0135P786K', 'USAT21502602', 'a3025e65-fdeb-4808-8c83-fbfdb978728c', None, None, None, None, None, '2021-04-18', '2020-07-06', 1, 507), ( '19222187-cc83-47b7-8ba4-8aecc3fbd326', 'a8eee5c8-f871-48b1-a18d-9c1df7f8ebae', 2, 1, None, None, None, None, None, None, 'B0135P6SS4', 'USAT21502596', 'eefec74d-ce0f-497e-b29b-3a60d0a99517', None, None, None, None, None, '2021-04-18', '2020-01-01', 1, 507)] with patch.object(chart.snowflake, 'fetchall', return_value=fetchall_mock_response): response = chart.get_chart_rankings( chart_id='a8eee5c8-f871-48b1-a18d-9c1df7f8ebae', chart_date='2021-04-23', limit=2, offset=0, ) assert response == [ { 'uuid': 'aa0ad2aa-cee1-49b6-9758-9186c7c166e0', 'chartId': 'a8eee5c8-f871-48b1-a18d-9c1df7f8ebae', 'position': 1, 'positionChange': 0, 'streams': None, 'totalStreams': None, 'totalShazams': None, 'views': None, 'numTracks': None, 'totalUnits': None, 'trackId': 'B0135P786K', 'isrc': 'USAT21502602', 'publicSoundRecordingId': 'a3025e65-fdeb-4808-8c83-fbfdb978728c', 'upc': None, 'publicProductId': None, 'videoId': None, 'channelId': None, 'channelName': None, 'chartDate': '2021-04-18', 'peakTimestamp': '2020-07-06', 'peakPosition': 1, 'daysOnChart': 507 }, { 'uuid': '19222187-cc83-47b7-8ba4-8aecc3fbd326', 'chartId': 'a8eee5c8-f871-48b1-a18d-9c1df7f8ebae', 'position': 2, 'positionChange': 1, 'streams': None, 'totalStreams': None, 'totalShazams': None, 'views': None, 'numTracks': None, 'totalUnits': None, 'trackId': 'B0135P6SS4', 'isrc': 'USAT21502596', 'publicSoundRecordingId': 'eefec74d-ce0f-497e-b29b-3a60d0a99517', 'upc': None, 'publicProductId': None, 'videoId': None, 'channelId': None, 'channelName': None, 'chartDate': '2021-04-18', 'peakTimestamp': '2020-01-01', 'peakPosition': 1, 'daysOnChart': 507 }, ] @pytest.mark.parametrize( 'limit_offset, expected_limit_offset', [ [(1, 2), (1, 2)], [(10, 0), (10, 0)], [(10, 11), (10, 11)], [(None, None), Exception], [("None", "None"), Exception], [(-1, 0), Exception], [(0, -1), Exception], ]) def test_get_chart_rankings_in_catalog_limit_offset(limit_offset, expected_limit_offset): limit, offset = limit_offset test_header = { 'Orchard-Profile-Id': '1213400', 'Orchard-Profile-Type': 'InsightsProfile' } get_permissions_mock_return_value = { 'artist_ids': None, 'vendor_ids': ['*'], 'subaccount_ids': ['*'], 'label_participant_ids': ['*'] } with patch.object(chart, 'get_permissions', return_value=get_permissions_mock_return_value), \ patch.object(chart.snowflake, 'fetchall') as fetchall_mock: params = dict( request_context=test_header, chart_id='a8eee5c8-f871-48b1-a18d-9c1df7f8ebae', chart_date='2021-04-23', limit=limit, offset=offset, ) if expected_limit_offset == Exception: with pytest.raises(Exception): chart.get_chart_rankings_in_catalog(**params) else: chart.get_chart_rankings_in_catalog(**params) fetchall_mock.assert_called() expected_limit, expected_offset = expected_limit_offset query, params = fetchall_mock.call_args[0] assert params == { 'artist_ids': None, 'vendor_ids': ['*'], 'subaccount_ids': ['*'], 'label_participant_ids': ['*'], 'chart_id': 'a8eee5c8-f871-48b1-a18d-9c1df7f8ebae', 'chart_date': '2021-04-23', 'limit': expected_limit, 'offset': expected_offset, 'position_limit': chart.IN_CATALOG_POSITION_LIMIT } def test_get_chart_rankings_in_catalog(): """Test get_chart_rankings_in_catalog.""" fetchall_mock_response = [ ( 'c9bf8339-302f-4361-8842-e061bec6fb35', 'a8eee5c8-f871-48b1-a18d-9c1df7f8ebae', 1, 0, None, None, None, None, None, None, 'B0135P786K', 'USAT21502602', 'a3025e65-fdeb-4808-8c83-fbfdb978728c', None, None, None, None, None, '2021-04-18', '2020-07-06', 1, 507), ( 'efcf8ac9-2960-4bd1-8d2f-3e58ffe66996', 'a8eee5c8-f871-48b1-a18d-9c1df7f8ebae', 2, 1, None, None, None, None, None, None, 'B0135P6SS4', 'USAT21502596', 'eefec74d-ce0f-497e-b29b-3a60d0a99517', None, None, None, None, None, '2021-04-18', '2020-01-01', 1, 507) ] test_header = { 'Orchard-Profile-Id': '1213400', 'Orchard-Profile-Type': 'InsightsProfile' } get_permissions_mock_return_value = { 'artist_ids': None, 'vendor_ids': ['*'], 'subaccount_ids': ['*'], 'label_participant_ids': ['*'] } with patch.object(chart, 'get_permissions', return_value=get_permissions_mock_return_value), \ patch.object(chart.snowflake, 'fetchall', return_value=fetchall_mock_response): response = chart.get_chart_rankings_in_catalog( request_context=test_header, chart_id='a8eee5c8-f871-48b1-a18d-9c1df7f8ebae', chart_date='2021-04-23', limit=2, offset=0 ) assert response == [ { 'uuid': 'c9bf8339-302f-4361-8842-e061bec6fb35', 'chartId': 'a8eee5c8-f871-48b1-a18d-9c1df7f8ebae', 'position': 1, 'positionChange': 0, 'streams': None, 'totalStreams': None, 'totalShazams': None, 'views': None, 'numTracks': None, 'totalUnits': None, 'trackId': 'B0135P786K', 'isrc': 'USAT21502602', 'publicSoundRecordingId': 'a3025e65-fdeb-4808-8c83-fbfdb978728c', 'upc': None, 'publicProductId': None, 'videoId': None, 'channelId': None, 'channelName': None, 'chartDate': '2021-04-18', 'peakTimestamp': '2020-07-06', 'peakPosition': 1, 'daysOnChart': 507 }, { 'uuid': 'efcf8ac9-2960-4bd1-8d2f-3e58ffe66996', 'chartId': 'a8eee5c8-f871-48b1-a18d-9c1df7f8ebae', 'position': 2, 'positionChange': 1, 'streams': None, 'totalStreams': None, 'totalShazams': None, 'views': None, 'numTracks': None, 'totalUnits': None, 'trackId': 'B0135P6SS4', 'isrc': 'USAT21502596', 'publicSoundRecordingId': 'eefec74d-ce0f-497e-b29b-3a60d0a99517', 'upc': None, 'publicProductId': None, 'videoId': None, 'channelId': None, 'channelName': None, 'chartDate': '2021-04-18', 'peakTimestamp': '2020-01-01', 'peakPosition': 1, 'daysOnChart': 507 }, ] def test_get_chart_song_history(): """Test get_chart_rankings.""" fetchall_mock_response = [ ('0356b93d-169d-41ea-abf5-c87e4d82f4c8', 5, None, None, None, None, None, None, 'B08GR81W92', 'KRA402000131', '050236c6-062c-4728-8279-98ae7eb5dd19', None, None, datetime.date(2021, 2, 13)), ('5521d07a-9050-41bc-a075-3d7efd0f6a83', 6, None, None, None, None, None, None, 'B08GR81W92', 'KRA402000131', '050236c6-062c-4728-8279-98ae7eb5dd19', None, None, datetime.date(2021, 2, 14)), ] with patch.object(chart.snowflake, 'fetchall', return_value=fetchall_mock_response): response = chart.get_chart_song_history( chart_id='9b5cfccc-27a7-4969-965a-e17d9a0e20d4', public_sound_recording_id='050236c6-062c-4728-8279-98ae7eb5dd19', ) assert response == [ {'uuid': '0356b93d-169d-41ea-abf5-c87e4d82f4c8', 'chartDate': datetime.date(2021, 2, 13), 'isrc': 'KRA402000131', 'position': 5, 'positionChange': None, 'publicSoundRecordingId': '050236c6-062c-4728-8279-98ae7eb5dd19', 'streams': None, 'totalShazams': None, 'totalStreams': None, 'views': None, 'totalUnits': None, 'trackId': 'B08GR81W92', 'videoId': None, 'upc': None}, {'uuid': '5521d07a-9050-41bc-a075-3d7efd0f6a83', 'chartDate': datetime.date(2021, 2, 14), 'isrc': 'KRA402000131', 'position': 6, 'positionChange': None, 'publicSoundRecordingId': '050236c6-062c-4728-8279-98ae7eb5dd19', 'streams': None, 'totalShazams': None, 'totalStreams': None, 'views': None, 'totalUnits': None, 'trackId': 'B08GR81W92', 'videoId': None, 'upc': None} ] def test_get_chart_product_history(): """Test get_chart_product_history.""" fetchall_mock_response = [ ('ecc6f55b-ddb3-4cc1-bc95-f0f72c47e7d9', 5, None, None, None, None, None, None, None, None, '06ca6732-e943-4eab-a06b-a2da8c897dfe', '094636797854', None, datetime.date(2021, 2, 13)), ('f22d05d6-f7a6-4fe8-bee2-16561cf0d736', 6, None, None, None, None, None, None, None, None, '06ca6732-e943-4eab-a06b-a2da8c897dfe', '094636797854', None, datetime.date(2021, 2, 14)), ] with patch.object(chart.snowflake, 'fetchall', return_value=fetchall_mock_response): response = chart.get_chart_product_history( chart_id='0e80f8fe-38e1-444f-a38f-f4f015915170', public_product_id='06ca6732-e943-4eab-a06b-a2da8c897dfe', ) assert response == [ {'uuid': 'ecc6f55b-ddb3-4cc1-bc95-f0f72c47e7d9', 'chartDate': datetime.date(2021, 2, 13), 'isrc': None, 'position': 5, 'positionChange': None, 'publicProductId': '06ca6732-e943-4eab-a06b-a2da8c897dfe', 'streams': None, 'totalShazams': None, 'totalStreams': None, 'views': None, 'totalUnits': None, 'trackId': None, 'videoId': None, 'upc': '094636797854'}, {'uuid': 'f22d05d6-f7a6-4fe8-bee2-16561cf0d736', 'chartDate': datetime.date(2021, 2, 14), 'isrc': None, 'position': 6, 'positionChange': None, 'publicProductId': '06ca6732-e943-4eab-a06b-a2da8c897dfe', 'streams': None, 'totalShazams': None, 'totalStreams': None, 'views': None, 'totalUnits': None, 'trackId': None, 'videoId': None, 'upc': '094636797854'} ]