import datetime from dataclasses import asdict from apollo_utils.core.constants.dsp import DSP from faker import Faker from tests.api.playlists.v1.responses.base.common import get_track_in_playlist_nested_playlist from tests.api.playlists.v1.responses.base.objects_structure import TrackInPlaylistPreviousBase, \ TrackInPlaylistPlaylistDatesBase from tests.api.playlists.v1.responses.helpers import add_dsp_prefix, get_fake_isrc, get_playlist_id, \ get_single_fake_date fake = Faker() def get_track_in_playlist_previous( dsp: DSP, isrc: str = None, playlist_id: str = None, country_code: str = None, playlist_is_personalised: bool = False, earliest_position_date_time: str = None, with_playlist: bool = False, removed_date_time: str = None, *args, **kwargs ): """Get track in playlists with includes based on passed params partially filled with fake data """ isrc = isrc or get_fake_isrc() playlist_id = playlist_id or get_playlist_id(dsp=dsp, playlist_id=playlist_id) num_tracks = fake.random_int(min=99, max=199) country_code = country_code or fake.country_code("alpha-2").lower() is_personalised = playlist_is_personalised or fake.boolean(20) fake_removed_date_time = get_single_fake_date() fake_earliest_position_date_time = get_single_fake_date() num_days_on = fake.random_int(min=1, max=299) return TrackInPlaylistPreviousBase( dsp=dsp.value, isrc=isrc, playlist_id=add_dsp_prefix(dsp, playlist_id), num_days_on=num_days_on, earliest_position_date_time=earliest_position_date_time or fake_earliest_position_date_time, removed_date_time=removed_date_time or fake_removed_date_time, playlist=get_track_in_playlist_nested_playlist( dsp=dsp, playlist_id=playlist_id, num_tracks=num_tracks, country_code=country_code, is_personalised=is_personalised, ) if with_playlist else None, playlist_dates=TrackInPlaylistPlaylistDatesBase( dsp_sync_date_time=( datetime.date.today() - datetime.timedelta(days=fake.random_int(min=1, max=4))).isoformat() ) ) def get_delphi_public_track_positions_previous_playlists_response( dsp: DSP, items_count: int = 1, with_meta_owner_categories: bool = False, country_code=None, **kwargs, ): if country_code is None: country_code = ["us", "gb", "ca", "de", "fr"] result = [] categories = {} for _ in range(items_count): for i in country_code: tip = asdict(get_track_in_playlist_previous(dsp=dsp, country_code=i, **kwargs)) if with_meta_owner_categories: cat_id = tip["playlist"]["owner"]["owner_category"]["owner_category_id"] cat_name = tip["playlist"]["owner"]["owner_category"]["name"] tip_category = { "owner_category_id": cat_id, "name": cat_name } categories[cat_id] = tip_category result.append(tip) return { "items": result, "meta": { "owner_categories": list(categories.values()) } }