from datetime import date from unittest import mock import pytest from slz_downloader.config import App from slz_downloader.dsp.apollo import apple, spotify from slz_downloader.dsp.apollo.models.bigtable import AppleChartRecord, SpotifyChartRecord from slz_downloader.dsp.apollo.service.apollo_mysql import chunks from slz_downloader.entities import Job @pytest.fixture def spotify_chart_row(): return { 'date': date(2020, 1, 2), 'country_code': 'de', 'isrc': '123UWIDPG22', 'track_url': 'qwer/asdf/zxcv', 'chart_position': 1234, } @pytest.fixture def apple_chart_row(): return { 'date': date(2020, 1, 2), 'country_code': 'de', 'isrc': '123UWIDPG22', 'track_id': 'zxcv', 'chart_position': 1234, } def test_spotify_chart_record_from_dict(spotify_chart_row): expected = SpotifyChartRecord( report_date='2020-01-02', chart_id='spotify_top200_daily_de', track_id='spotify_zxcv', chart_position=b'\x00\x00\x04\xd2', isrc='123UWIDPG22', dsp='spotify', ) output = SpotifyChartRecord.from_dict(spotify_chart_row) assert output == expected def test_spotify_chart_record_get_row_keys(spotify_chart_row): record = SpotifyChartRecord.from_dict(spotify_chart_row) keys = record.get_row_keys() expected = [ b'chart_isrc_date_day~spotify_top200_daily_de~22GPDIWU321~2020-01-02', b'isrc_chart_date_day~22GPDIWU321~spotify_top200_daily_de~2020-01-02', b'isrc_date_chart_day~22GPDIWU321~2020-01-02~spotify_top200_daily_de', b'chart_date_isrc_day~spotify_top200_daily_de~2020-01-02~22GPDIWU321', b'dsp_isrc_date_chart_day~spotify~22GPDIWU321~2020-01-02~spotify_top200_daily_de', b'dsp_date_isrc_chart_day~spotify~2020-01-02~22GPDIWU321~spotify_top200_daily_de', ] assert set(keys) == set(expected) def test_spotify_chart_record_get_row_key(): assert SpotifyChartRecord.get_row_key('spotify', '123', 'qwerty') == b'spotify~123~qwerty' def test_spotify_chart_record_get_chart_id(spotify_chart_row): assert SpotifyChartRecord.get_chart_id(spotify_chart_row) == 'spotify_top200_daily_de' def test_spotify_chart_record_get_track_id(spotify_chart_row): assert SpotifyChartRecord.get_track_id(spotify_chart_row) == 'spotify_zxcv' def test_apple_chart_record_from_dict(apple_chart_row): expected = AppleChartRecord( report_date='2020-01-02', chart_id='apple_top100_daily_de', track_id='apple_zxcv', chart_position=b'\x00\x00\x04\xd2', isrc='123UWIDPG22', dsp='apple', ) output = AppleChartRecord.from_dict(apple_chart_row) assert output == expected def test_apple_chart_record_get_row_keys(apple_chart_row): record = AppleChartRecord.from_dict(apple_chart_row) keys = record.get_row_keys() expected = [ b'dsp_date_isrc_chart_day~apple~2020-01-02~22GPDIWU321~apple_top100_daily_de', b'chart_isrc_date_day~apple_top100_daily_de~22GPDIWU321~2020-01-02', b'isrc_chart_date_day~22GPDIWU321~apple_top100_daily_de~2020-01-02', b'isrc_date_chart_day~22GPDIWU321~2020-01-02~apple_top100_daily_de', b'dsp_isrc_date_chart_day~apple~22GPDIWU321~2020-01-02~apple_top100_daily_de', b'chart_date_isrc_day~apple_top100_daily_de~2020-01-02~22GPDIWU321', ] assert set(keys) == set(expected) def test_apple_chart_record_get_row_key(): assert AppleChartRecord.get_row_key('apple', '123', 'qwerty') == b'apple~123~qwerty' def test_apple_chart_record_get_chart_id(apple_chart_row): assert AppleChartRecord.get_chart_id(apple_chart_row) == 'apple_top100_daily_de' def test_apple_chart_record_get_track_id(apple_chart_row): assert AppleChartRecord.get_track_id(apple_chart_row) == 'apple_zxcv' @pytest.mark.parametrize( 'dsp_cls', [ apple.AppleMusicChartsClient, apple.AppleMusicPlaylistRankingClient, apple.AppleMusicPlaylistTrackPositionClient, apple.AppleMusicPlaylistsClient, spotify.SpotifyChartsClient, spotify.SpotifyPlaylistRankingClient, spotify.SpotifyPlaylistTrackPositionClient, spotify.SpotifyPlaylistsClient, ] ) def test_availability(dsp_cls): with mock.patch('slz_downloader.dsp.apollo.clients.mysql_client_loader') as mysql: with mock.patch('slz_downloader.dsp.apollo.clients.postgres_client_loader') as postgres: with mock.patch('slz_downloader.dsp.apollo.clients.bigtable_client_loader') as bigtable: client = dsp_cls(mock.Mock()) params = App.empty() params.environment = 'dev' client.configure(params) job = Job.empty() job.report_type = 'some_apollo_type' job.report_date = '2019-12-29' job.context = 'ctx' content_name = client.get_content_name(job) assert 'some_apollo_type_20191229' == content_name result = client.check_contexts_availability([job]) assert len(result) == 1 ctx, content_length = result[0] assert ('ctx', 0) == (ctx, content_length) def test_chunks(): playlist_ids = [1, 2, 3, 4, 5, 6, 7, 8, 9] result = [x for x in chunks(playlist_ids, 1)] assert result == [[1], [2], [3], [4], [5], [6], [7], [8], [9]] result = [x for x in chunks(playlist_ids, 2)] assert result == [[1, 2], [3, 4], [5, 6], [7, 8], [9]] result = [x for x in chunks(playlist_ids, 5)] assert result == [[1, 2, 3, 4, 5], [6, 7, 8, 9]] result = [x for x in chunks(playlist_ids, 9)] assert result == [[1, 2, 3, 4, 5, 6, 7, 8, 9]]