import json from unittest.mock import patch from tests.unit.logic.test_sound_recording_timeseries import ( MOCK_COUNTRY_CODE_SUMMARY_ROWS, MOCK_COUNTRY_CODE_TIMESERIES_ROWS, ) def test_sound_recording_time_series_handler( client, insights_request_headers, request_context ): with patch( "analytics.logic.sound_recording_timeseries" ".get_sound_recording_timeseries" ) as get_sound_recording_timeseries: get_sound_recording_timeseries.return_value = {"items": []} url = "/sound-recording/isrc/timeseries?&type=TRACK_STREAMS_BY_STORE" response = client.get(url, headers=insights_request_headers) assert response.status_code == 200 response_payload = json.loads(response.data.decode("utf-8")) assert response_payload == {"items": []} def test_sound_recording_time_series_by_country( client, insights_request_headers, request_context ): with patch( "analytics.logic.sound_recording_timeseries" ".SoundRecordingStreamsTimeSeries" ".execute", return_value=MOCK_COUNTRY_CODE_TIMESERIES_ROWS, ) as execute: url = ( "/sound-recording/isrc/timeseries?account_type=vendor" "&type=TRACK_STREAMS_BY_COUNTRY" ) response = client.get(url, headers=insights_request_headers) assert response.status_code == 200 response_payload = json.loads(response.data.decode("utf-8")) assert execute.call_count == 1 assert response_payload == { "items": [ { "id": "GB", "date": "2022-02-23", "skip_rate": 0.5, "skips": 30, "streams": 100, }, { "id": "GB", "date": "2022-02-24", "skip_rate": 0.5, "skips": 30, "streams": 100, }, { "id": "DE", "date": "2022-02-23", "skip_rate": 0.5, "skips": 30, "streams": 100, }, ] } def test_sound_recording_summary_handler( client, insights_request_headers, request_context ): with patch( "analytics.logic.sound_recording_timeseries" ".get_sound_recording_summary" ) as get_sound_recording_summary: get_sound_recording_summary.return_value = {"items": []} url = "/sound-recording/isrc/summary?&type=STORE" response = client.get(url, headers=insights_request_headers) assert response.status_code == 200 response_payload = json.loads(response.data.decode("utf-8")) assert response_payload == {"items": [], "total_count": 0} def test_sound_recording_summary_by_country( client, insights_request_headers, request_context ): with patch( "analytics.logic.sound_recording_timeseries" ".SoundRecordingSummary.execute", return_value=MOCK_COUNTRY_CODE_SUMMARY_ROWS, ) as execute: url = "/sound-recording/isrc/summary?&type=COUNTRY" response = client.get(url, headers=insights_request_headers) assert response.status_code == 200 response_payload = json.loads(response.data.decode("utf-8")) assert execute.call_count == 1 assert response_payload == { "items": [ { "streams": 100, "skips": 30, "skip_rate": 0.5, "id": "GB", "country_code": "GB", }, { "streams": 100, "skips": 30, "skip_rate": 0.5, "id": "GB", "country_code": "GB", }, { "streams": 100, "skips": 30, "skip_rate": 0.5, "id": "DE", "country_code": "DE", }, ], "total_count": 3, } def test_sound_recording_related_videos_by_isrc( client, insights_request_headers, request_context ): with patch( "analytics.logic.sound_recording_related_videos.SoundRecordingRelatedVideosByIsrc.execute", return_value=[ {"video_id": "video_id_1"}, {"video_id": "video_id_2"}, {"video_id": "video_id_3"}, ], ) as get_sound_recording_related_videos_by_isrc: url = "/sound-recording/US53Q1200099/related-videos-by-isrc" response = client.get(url, headers=insights_request_headers) assert response.status_code == 200 response_payload = json.loads(response.data.decode("utf-8")) assert response_payload == { "video_ids": ["video_id_1", "video_id_2", "video_id_3"] } get_sound_recording_related_videos_by_isrc.assert_called_once()