import datetime import json import math import unittest from argparse import Namespace import allure import pytest from parameterized import parameterized from ui_automation_framework.core import TestStatus from ui_automation_framework.utils import AppConfig from app.api_client import ApiClient from app.src.pages.apple_playlist_page import ApplePlaylistPage from app.src.pages.login_page import LoginPage from app.src.pages.track_page import TrackPage @pytest.mark.usefixtures("set_up", "one_time_set_up") class ApplePlaylistPageTest(unittest.TestCase): playlist5 = json.loads(json.dumps(AppConfig.get("playlist5")), object_hook=lambda d: Namespace(**d)) playlist14 = json.loads(json.dumps(AppConfig.get("playlist14")), object_hook=lambda d: Namespace(**d)) us = json.loads(json.dumps(AppConfig.get("market")["us"]), object_hook=lambda d: Namespace(**d)) canada = json.loads(json.dumps(AppConfig.get("market")["canada"]), object_hook=lambda d: Namespace(**d)) @pytest.fixture(autouse=True) def classSetup(self): self.loginPage = LoginPage(self.driver) self.ap = ApplePlaylistPage(self.driver) self.tp = TrackPage(self.driver) self.api_client = ApiClient() self.ts = TestStatus(self.driver) @parameterized.expand( [ (AppConfig.get("market")["us"], "current"), (AppConfig.get("market")["us"], "11/14/20"), (AppConfig.get("market")["canada"], "current"), (AppConfig.get("market")["canada"], "11/14/20"), ] ) @allure.story("AP-3875") @allure.severity(allure.severity_level.NORMAL) @allure.title("Apple Playlist Avg Streams and Index calculation logic verification") @allure.description("""TC Verifies that data shown in the table is accurate.""") def test_avg_streams_and_index_calculation(self, market, date): user = self.loginPage.login_with_worker() self.loginPage.is_logged_in() self.ap.open_apple_playlist_page(self.playlist14.apple_id) self.ap.change_market(market["name"]) # step 1-4 if date != "current": self.ap.tl_choose_date(date) self.ap.tl_sort_by_column(self.ap.TL_COLUMN_AVG_STREAMS) self.verify_stream_indexes(user=user, playlist=self.playlist14.apple_id, market=market["market"], date=date) def verify_stream_indexes(self, user, playlist, market, date="current"): if date == "current": end_date = self.api_client.get_response(endpoint="/spotify-consumer-analytics-compat/latest-date", user=user)["body"]["date"] start_date = (datetime.datetime.strptime(end_date, "%Y-%m-%d") - datetime.timedelta(days=6)).date() apple_playlists_endpoint = f"/applemusic/playlists/{playlist}?country={market}" else: chosen_date = datetime.datetime.strptime(date, "%m/%d/%y").date() end_date = chosen_date start_date = chosen_date apple_playlists_endpoint = f"/applemusic/playlists/{playlist}/tracklisthistory/{market}/{chosen_date}" tracklist = self.api_client.get_response(endpoint=apple_playlists_endpoint, user=user)["body"] if date == "current": tracklist = tracklist["tracklist"] isrcs = [t["isrc"] for t in tracklist] isrcs_filtered = [i for i in isrcs if i is not None] streams_avg_api = [] for i in isrcs_filtered: items = self.api_client.get_response( endpoint=f"/apple-consumer-analytics/track-in-container?containerId={playlist}&startDate={start_date}&endDate={end_date}&isrc={i}&countryCode={market}", user=user, )[ "body" ]["items"] streams = 0 if len(items) > 0: streams = round(sum(i["localStreams"] for i in items) / len(items)) streams_avg_api.append(streams) streams_avg_api = sorted(streams_avg_api, reverse=True)[:5] avg_streams = self.ap.tl_get_column_values(self.ap.TL_COLUMN_AVG_STREAMS) streams_indexes = self.ap.tl_get_column_values(self.ap.TL_COLUMN_STREAMS_INDEX) streams_indexes = [math.ceil(i) for i in streams_indexes] calculated_indexes = [math.ceil(a / avg_streams[0] * 100) for a in avg_streams] ran = list(range(-1, 2)) for fu, fa in zip(calculated_indexes, streams_indexes): self.ts.markFinal( (fu - fa) in ran, f"avg from calculated: {calculated_indexes}, \nui: {streams_indexes}\ndiff: {(fu - fa)}", ) for u, a in zip(avg_streams, streams_avg_api): self.ts.markFinal((u - a) in ran, f"avg from api: {streams_avg_api}, \nui: {avg_streams}\ndiff: {(u - a)}")