import pytest from datetime import date, timedelta from flask import url_for from http import HTTPStatus from tests.legacy.apollo_api.factories import ApolloKeyValueStorageFactory from tests.legacy.spotify import factories @pytest.mark.parametrize( "db_values,expected_result", ( ( { "playlist_digest_streams_date": date(2022, 2, 5), "playlist_digest_followers_date": date(2022, 2, 7), "playlist_digest_latest_date": date(2022, 2, 4), }, { "min_date": "2020-01-01", "streams_max_date": "2022-02-05", "followers_max_date": "2022-02-07", "max_date": "2022-02-04", }, ), ( { "playlist_digest_streams_date": None, "playlist_digest_followers_date": None, "playlist_digest_latest_date": None, }, { "min_date": "2020-01-01", "streams_max_date": None, "followers_max_date": None, "max_date": None, }, ), ), ) def test_charts_playlists_dates(db_values, expected_result, db_session, user_id, client, patch_auth_user): patch_auth_user(user_id) for index, (key, value) in enumerate(db_values.items()): ApolloKeyValueStorageFactory.create(id=index + 1, key=key, value=value) response = client.get(url_for("charts_playlists_spotify.get_charts_playlists_dates")) assert response.status_code == HTTPStatus.OK assert response.json == expected_result @pytest.mark.parametrize( "params,status,expected_result", ( ({}, HTTPStatus.BAD_REQUEST, {}), ({"date": "2022-02-12"}, HTTPStatus.BAD_REQUEST, {}), ( {"date": "2022-02-12", "category_id": "3,4", "metric": "streams", "type": "value", "limit": 7}, HTTPStatus.OK, { "playlists": [ { "country_code": "us", "image_url": "https://dhfsk7jl2g0km.cloudfront.net/playlist-pl_18.jpeg", "is_personalized": False, "name": "Pl18", "link": "url_18", "owner": { "account_id": "user4", "account_name": "User4", "category_id": 4, "category_name": "Cat4", "country_code": "us", }, "playlist_id": "pl_18", "position": 1, "trend": -13, "value": 297020, "change": -44996, "previous_value": 342016, }, { "country_code": "worldwide", "image_url": "https://dhfsk7jl2g0km.cloudfront.net/playlist-pl_17.jpeg", "is_personalized": False, "name": "Pl17", "link": "url_17", "owner": { "account_id": "user3", "account_name": "User3", "category_id": 3, "category_name": "Cat3", "country_code": "worldwide", }, "playlist_id": "pl_17", "position": 2, "trend": 18, "value": 280520, "change": 42496, "previous_value": 238024, }, { "country_code": "worldwide", "image_url": "https://dhfsk7jl2g0km.cloudfront.net/playlist-pl_12.jpeg", "is_personalized": False, "name": "Pl12", "link": "url_12", "owner": { "account_id": "user3", "account_name": "User3", "category_id": 3, "category_name": "Cat3", "country_code": "worldwide", }, "playlist_id": "pl_12", "position": 3, "trend": -13, "value": 198020, "change": -29996, "previous_value": 228016, }, { "country_code": "us", "image_url": "https://dhfsk7jl2g0km.cloudfront.net/playlist-pl_08.jpeg", "is_personalized": False, "name": "Pl08", "link": "url_08", "owner": { "account_id": "user4", "account_name": "User4", "category_id": 4, "category_name": "Cat4", "country_code": "us", }, "playlist_id": "pl_08", "position": 4, "trend": -11, "value": 132020, "change": -16800, "previous_value": 148820, }, { "country_code": "us", "image_url": "https://dhfsk7jl2g0km.cloudfront.net/playlist-pl_03.jpeg", "is_personalized": True, "name": "Pl03", "link": "url_03", "owner": { "account_id": "user4", "account_name": "User4", "category_id": 4, "category_name": "Cat4", "country_code": "us", }, "playlist_id": "pl_03", "position": 5, "trend": 15, "value": 49520, "change": 6300, "previous_value": 43220, }, { "country_code": "worldwide", "image_url": "https://dhfsk7jl2g0km.cloudfront.net/playlist-pl_02.jpeg", "is_personalized": False, "name": "Pl02", "link": "url_02", "owner": { "account_id": "user3", "account_name": "User3", "category_id": 3, "category_name": "Cat3", "country_code": "worldwide", }, "playlist_id": "pl_02", "position": 6, "trend": -11, "value": 33020, "change": -4200, "previous_value": 37220, }, ], }, ), ( { "date": "2022-02-12", "category_id": "2,3", "metric": "streams", "type": "value", "playlists_type": "personalized", }, HTTPStatus.OK, { "playlists": [ { "country_code": "worldwide", "image_url": "https://dhfsk7jl2g0km.cloudfront.net/playlist-pl_11.jpeg", "is_personalized": True, "name": "Pl11", "link": "url_11", "owner": { "account_id": "user2", "account_name": "User2", "category_id": 2, "category_name": "Cat2", "country_code": "us", }, "playlist_id": "pl_11", "position": 1, "trend": 18, "value": 181520, "change": 27496, "previous_value": 154024, }, ], }, ), ( { "date": "2022-02-12", "category_id": "2,3,4", "metric": "streams", "type": "value", "playlists_country_code": "worldwide", }, HTTPStatus.OK, { "playlists": [ { "country_code": "worldwide", "image_url": "https://dhfsk7jl2g0km.cloudfront.net/playlist-pl_17.jpeg", "is_personalized": False, "name": "Pl17", "link": "url_17", "owner": { "account_id": "user3", "account_name": "User3", "category_id": 3, "category_name": "Cat3", "country_code": "worldwide", }, "playlist_id": "pl_17", "position": 1, "trend": 18, "value": 280520, "change": 42496, "previous_value": 238024, }, { "country_code": "worldwide", "image_url": "https://dhfsk7jl2g0km.cloudfront.net/playlist-pl_12.jpeg", "is_personalized": False, "name": "Pl12", "link": "url_12", "owner": { "account_id": "user3", "account_name": "User3", "category_id": 3, "category_name": "Cat3", "country_code": "worldwide", }, "playlist_id": "pl_12", "position": 2, "trend": -13, "value": 198020, "change": -29996, "previous_value": 228016, }, { "country_code": "worldwide", "image_url": "https://dhfsk7jl2g0km.cloudfront.net/playlist-pl_11.jpeg", "is_personalized": True, "name": "Pl11", "link": "url_11", "owner": { "account_id": "user2", "account_name": "User2", "category_id": 2, "category_name": "Cat2", "country_code": "us", }, "playlist_id": "pl_11", "position": 3, "trend": 18, "value": 181520, "change": 27496, "previous_value": 154024, }, { "country_code": "worldwide", "image_url": "https://dhfsk7jl2g0km.cloudfront.net/playlist-pl_06.jpeg", "is_personalized": False, "name": "Pl06", "link": "url_06", "owner": { "account_id": "user2", "account_name": "User2", "category_id": 2, "category_name": "Cat2", "country_code": "us", }, "playlist_id": "pl_06", "position": 4, "trend": -13, "value": 99020, "change": -14996, "previous_value": 114016, }, { "country_code": "worldwide", "image_url": "https://dhfsk7jl2g0km.cloudfront.net/playlist-pl_02.jpeg", "is_personalized": False, "name": "Pl02", "link": "url_02", "owner": { "account_id": "user3", "account_name": "User3", "category_id": 3, "category_name": "Cat3", "country_code": "worldwide", }, "playlist_id": "pl_02", "position": 5, "trend": -11, "value": 33020, "change": -4200, "previous_value": 37220, }, ], }, ), ( { "date": "2022-02-12", "category_id": "2,3,4", "metric": "streams", "type": "value", "streams_country_code": "us,gb", "limit": 5, }, HTTPStatus.OK, { "playlists": [ { "country_code": "us", "image_url": "https://dhfsk7jl2g0km.cloudfront.net/playlist-pl_18.jpeg", "is_personalized": False, "name": "Pl18", "link": "url_18", "owner": { "account_id": "user4", "account_name": "User4", "category_id": 4, "category_name": "Cat4", "country_code": "us", }, "playlist_id": "pl_18", "position": 1, "trend": -43, "value": 162006, "change": -123305, "previous_value": 285311, }, { "country_code": "worldwide", "image_url": "https://dhfsk7jl2g0km.cloudfront.net/playlist-pl_17.jpeg", "is_personalized": False, "name": "Pl17", "link": "url_17", "owner": { "account_id": "user3", "account_name": "User3", "category_id": 3, "category_name": "Cat3", "country_code": "worldwide", }, "playlist_id": "pl_17", "position": 2, "trend": 319, "value": 153006, "change": 116455, "previous_value": 36551, }, { "country_code": "worldwide", "image_url": "https://dhfsk7jl2g0km.cloudfront.net/playlist-pl_12.jpeg", "is_personalized": False, "name": "Pl12", "link": "url_12", "owner": { "account_id": "user3", "account_name": "User3", "category_id": 3, "category_name": "Cat3", "country_code": "worldwide", }, "playlist_id": "pl_12", "position": 3, "trend": -43, "value": 108006, "change": -82205, "previous_value": 190211, }, { "country_code": "worldwide", "image_url": "https://dhfsk7jl2g0km.cloudfront.net/playlist-pl_11.jpeg", "is_personalized": True, "name": "Pl11", "link": "url_11", "owner": { "account_id": "user2", "account_name": "User2", "category_id": 2, "category_name": "Cat2", "country_code": "us", }, "playlist_id": "pl_11", "position": 4, "trend": 319, "value": 99006, "change": 75355, "previous_value": 23651, }, { "country_code": "us", "image_url": "https://dhfsk7jl2g0km.cloudfront.net/playlist-pl_08.jpeg", "is_personalized": False, "name": "Pl08", "link": "url_08", "owner": { "account_id": "user4", "account_name": "User4", "category_id": 4, "category_name": "Cat4", "country_code": "us", }, "playlist_id": "pl_08", "position": 5, "trend": -34, "value": 72006, "change": -36399, "previous_value": 108405, }, ], }, ), ( {"date": "2022-02-14", "category_id": "5", "metric": "followers", "type": "value"}, HTTPStatus.OK, { "playlists": [ { "country_code": "gb", "image_url": "https://dhfsk7jl2g0km.cloudfront.net/playlist-pl_19.jpeg", "is_personalized": False, "name": "Pl19", "link": "url_19", "owner": { "account_id": "spotify", "account_name": "Spotify", "category_id": 5, "category_name": "SpotifyC", "country_code": "worldwide", }, "playlist_id": "pl_19", "position": 1, "trend": -3, "value": 57004, "change": -1908, "previous_value": 58912, }, { "country_code": "gb", "image_url": "https://dhfsk7jl2g0km.cloudfront.net/playlist-pl_04.jpeg", "is_personalized": False, "name": "Pl04", "link": "url_04", "owner": { "account_id": "spotify", "account_name": "Spotify", "category_id": 5, "category_name": "SpotifyC", "country_code": "worldwide", }, "playlist_id": "pl_04", "position": 2, "trend": 4, "value": 12004, "change": 408, "previous_value": 11596, }, { "country_code": "gb", "image_url": "https://dhfsk7jl2g0km.cloudfront.net/playlist-pl_14.jpeg", "is_personalized": False, "name": "Pl14", "link": "url_14", "owner": { "account_id": "spotify", "account_name": "Spotify", "category_id": 5, "category_name": "SpotifyC", "country_code": "worldwide", }, "playlist_id": "pl_14", "position": 3, "trend": 3, "value": -42004, "change": 1408, "previous_value": -43412, }, ], }, ), ( {"date": "2022-02-13", "category_id": "5", "metric": "followers", "type": "move"}, HTTPStatus.OK, { "playlists": [ { "country_code": "gb", "image_url": "https://dhfsk7jl2g0km.cloudfront.net/playlist-pl_19.jpeg", "is_personalized": False, "name": "Pl19", "link": "url_19", "owner": { "account_id": "spotify", "account_name": "Spotify", "category_id": 5, "category_name": "SpotifyC", "country_code": "worldwide", }, "playlist_id": "pl_19", "position": 1, "trend": 3, "value": 57003, "change": 1906, "previous_value": 55097, }, ], }, ), ( {"date": "2022-02-12", "category_id": "2,3,4", "metric": "streams", "type": "-move", "limit": 3}, HTTPStatus.OK, { "playlists": [ { "change": -29996, "country_code": "worldwide", "image_url": "https://dhfsk7jl2g0km.cloudfront.net/playlist-pl_12.jpeg", "is_personalized": False, "link": "url_12", "name": "Pl12", "owner": { "account_id": "user3", "account_name": "User3", "category_id": 3, "category_name": "Cat3", "country_code": "worldwide", }, "playlist_id": "pl_12", "position": 1, "previous_value": 228016, "trend": -13, "value": 198020, }, { "change": -44996, "country_code": "us", "image_url": "https://dhfsk7jl2g0km.cloudfront.net/playlist-pl_18.jpeg", "is_personalized": False, "link": "url_18", "name": "Pl18", "owner": { "account_id": "user4", "account_name": "User4", "category_id": 4, "category_name": "Cat4", "country_code": "us", }, "playlist_id": "pl_18", "position": 2, "previous_value": 342016, "trend": -13, "value": 297020, }, { "change": -14996, "country_code": "worldwide", "image_url": "https://dhfsk7jl2g0km.cloudfront.net/playlist-pl_06.jpeg", "is_personalized": False, "link": "url_06", "name": "Pl06", "owner": { "account_id": "user2", "account_name": "User2", "category_id": 2, "category_name": "Cat2", "country_code": "us", }, "playlist_id": "pl_06", "position": 3, "previous_value": 114016, "trend": -13, "value": 99020, }, ], }, ), ( {"date": "2022-02-13", "category_id": "5", "metric": "followers", "type": "-move"}, HTTPStatus.OK, { "playlists": [ { "change": -1406, "country_code": "gb", "image_url": "https://dhfsk7jl2g0km.cloudfront.net/playlist-pl_14.jpeg", "is_personalized": False, "link": "url_14", "name": "Pl14", "owner": { "account_id": "spotify", "account_name": "Spotify", "category_id": 5, "category_name": "SpotifyC", "country_code": "worldwide", }, "playlist_id": "pl_14", "position": 1, "previous_value": 43409, "trend": -3, "value": 42003, }, ], }, ), ), ) def test_charts_playlists_data(params, status, expected_result, db_session, user_id, client, patch_auth_user): patch_auth_user(user_id) factories.BuzzUserFactory.create( id=1, user_name="spotify", display_name="Spotify", category_id=5, country_code="_gl" ) factories.BuzzCategoryFactory.create(id=5, name="SpotifyC") factories.BuzzUserFactory.create(id=2, user_name="sony", display_name="Sony", category_id=1, country_code="global") factories.BuzzCategoryFactory.create(id=1, name="SonyC") for index in range(3, 8): factories.BuzzUserFactory.create( id=index, user_name=f"user{index - 1}", display_name=f"User{index - 1}", category_id=index - 1 if index < 6 else None, country_code="us" if index % 2 else None, ) for index in range(2, 5): factories.BuzzCategoryFactory.create(id=index, name=f"Cat{index}") for index in range(1, 16, 2): factories.SpotifyPersonalizedPlaylistFactory.create(playlist_id=f"pl_{index:02d}") playlist_country_code_list = (None, "", "_gl", "us", "gb") fixed_playlist_country_code_list = ("worldwide", "worldwide", "worldwide", "us", "gb") playlist_owner_list = ("sony", "user2", "user3", "user4", "spotify") for index in range(1, 20): category_id = index % 5 + 1 owner = playlist_owner_list[index % 5] playlist_id = f"pl_{index:02d}" factories.SpotifyPlaylistFactory.create( id=playlist_id, name=f"Pl{index:02d}", country_code=playlist_country_code_list[index % 5], uri=f"url_{index:02d}", user_name=owner, buzz_category_id=category_id, ) if index % 3: factories.SpotifyPlaylistDigestFollowersPlaylistFactory.create( playlist_id=playlist_id, category_id=category_id, country_code=fixed_playlist_country_code_list[index % 5], ) for j in range(5): factories.SpotifyPlaylistFollowersWeekHistoryFactory.create( date=date(2022, 2, 10) + timedelta(days=j), playlist_id=playlist_id, value=(index * 3000 + j) * (1 if (index + j) % 3 else -1), change=(index * 100 + j * 2) * (-1 if (index + j) % 2 else 1), ) if index % 3 - 1: factories.SpotifyPlaylistDigestStreamsPlaylistFactory.create( playlist_id=playlist_id, category_id=category_id, country_code=fixed_playlist_country_code_list[index % 5], ) for j in range(4): change_us = (index * 5700 + j * 2) * (-1 if (index + j) % 2 else 1) change_gb = (index * 1150 + j * 3) * (-1 if (index + j) % 3 else 1) change_ca = (index * 3400 + j * 4) * (1 if (index + j) % 2 else -1) change_de = (index * 950 + j * 5) * (1 if (index + j) % 3 else -1) factories.SpotifyPlaylistStreamsWeekHistoryFactory.create( date=date(2022, 2, 11) + timedelta(days=j), playlist_id=playlist_id, value=index * 16500 + j * 20, value_us=index * 5000 + j * 2, value_gb=index * 4000 + j * 4, value_ca=index * 4500 + j * 6, value_de=index * 3000 + j * 8, change=change_us + change_gb + change_ca + change_de, change_us=change_us, change_gb=change_gb, change_ca=change_ca, change_de=change_de, ) response = client.get(url_for("charts_playlists_spotify.get_charts_playlists", **params)) assert response.status_code == status if status == HTTPStatus.OK: assert response.json == expected_result