import json import unittest from argparse import Namespace import allure import pytest from assertpy import assert_that from ui_automation_framework.core import TestStatus from ui_automation_framework.utils import AppConfig, CoreConfig from app.api_client import ApiClient from app.mysql_db_client_custom import MySqlClient from ...pages.add_track_popup import AddTrackPopup from ...pages.audience_graph import AudienceGraphChart from ...pages.comparison_page import TrackComparisonPage from ...pages.left_navigation_panel import LeftNavigationPanel from ...pages.login_page import LoginPage from ...pages.track_page import TrackPage @pytest.mark.usefixtures("set_up", "one_time_set_up") class ComparisonPlaylistSupportTest(unittest.TestCase): track6 = json.loads(json.dumps(AppConfig.get("track6")), object_hook=lambda d: Namespace(**d)) track24 = json.loads(json.dumps(AppConfig.get("track24")), object_hook=lambda d: Namespace(**d)) track15 = json.loads(json.dumps(AppConfig.get("track15")), object_hook=lambda d: Namespace(**d)) track13 = json.loads(json.dumps(AppConfig.get("track13")), object_hook=lambda d: Namespace(**d)) track22 = json.loads(json.dumps(AppConfig.get("track22")), object_hook=lambda d: Namespace(**d)) tracks = [track6, track24, track15, track13, track22] source = json.loads(json.dumps(AppConfig.get("source")), object_hook=lambda d: Namespace(**d)) us = json.loads(json.dumps(AppConfig.get("market")["us"]), object_hook=lambda d: Namespace(**d)) @pytest.fixture(autouse=True) def classSetup(self): self.loginPage = LoginPage(self.driver) self.trackPage = TrackPage(self.driver) self.add_track_popup = AddTrackPopup(self.driver) self.comparison_page = TrackComparisonPage(self.driver) self.left_panel = LeftNavigationPanel(self.driver) self.audience_graph = AudienceGraphChart(self.driver) self.api_client = ApiClient() self.sql_client = MySqlClient() self.ts = TestStatus(self.driver) @allure.story("AP-2938") @allure.severity(allure.severity_level.NORMAL) @allure.title("""Streams by Countries table Data calculation""") @allure.description("""TC Verifies correctness of data calculation in Streams by Countries table.""") def test_by_countries_table_elements(self): self.comparison_page.remove_tracks_if_present_with_worker(self.us.market) user = self.loginPage.get_user_email_with_worker() token = self.api_client.authorize(user=user) self.api_client.add_track_to_comparison(token=token, market=self.us.market, isrc=self.track6.isrc, id=self.track6.id) self.loginPage.login_with_worker() self.loginPage.is_logged_in() self.left_panel.open_comparison_page() self.trackPage.select_market(self.us.name) self.comparison_page.select_market_from_dd(self.us.name) self.comparison_page.open_new_tab() url_open = "{base}/spotify/track/{id}?market={market}&str_gr_tab=2&str_gr_dsp=1&str_gr_view=4&str_gr_start_date={start}&str_gr_end_date={end}" for period in [AppConfig.get("periods")["14d"]]: self.comparison_page.switch_to_tab(2) end_date = self.comparison_page.get_end_date_for_track_period(period=period["count"], track=self.track6) self.comparison_page.open_url( url_open.format( base=CoreConfig.ENV_BASE_URL, id=self.track6.id, market=4, start=self.track6.dates.f_date, end=end_date, ) ) for dsp in [AppConfig.get("dsp")["spotify"]]: sources = [AppConfig.get("source")["all"], AppConfig.get("source")["album_page"]] for s in sources: self.comparison_page.switch_to_tab(1) self.comparison_page.select_period_from_dd(period["name"]) self.comparison_page.country_select_dsp_from_dd(dsp) self.comparison_page.country_select_source(s) percentage = self.comparison_page.country_get_values_by_track_index_percentage(1) countries = self.comparison_page.country_get_values_by_track_index_countries(1) streams_val = self.comparison_page.country_get_values_by_track_index_count(1) streams = [float(s) for s in streams_val] self.comparison_page.switch_to_tab(2) self.trackPage.select_dsp(dsp) self.trackPage.change_source_value(s) tooltip_countries = self.trackPage.get_track_performance_countries_tooltips(10) # steps 1-10 assert_that(countries).is_equal_to(tooltip_countries["countries"]) self.ts.markFinal( streams == tooltip_countries["streams"], "streams are correct: {}, track: {}".format(streams, tooltip_countries["streams"]), ) for p, t in zip(percentage, tooltip_countries["percentage"]): assert_that([-1, 0, 1]).described_as(f"{p}-{t}").contains(p - t) self.comparison_page.switch_to_tab(1) self.trackPage.select_market(AppConfig.get("market")["australia"]["name"]) self.comparison_page.switch_to_tab(2) end_date = self.comparison_page.get_end_date_for_track_period(period=AppConfig.get("periods")["28d"]["count"], track=self.track6) self.comparison_page.open_url(url_open.format(base=CoreConfig.ENV_BASE_URL, id=self.track6.id, market=14, start=self.track6.dates.f_date, end=end_date)) for dsp in [AppConfig.get("dsp")["apple"]]: sources = [AppConfig.get("source")["all"], AppConfig.get("source")["discovery"]] for s in sources: self.comparison_page.switch_to_tab(1) self.comparison_page.select_period_from_dd(AppConfig.get("periods")["28d"]["name"]) self.comparison_page.country_select_dsp_from_dd(dsp) self.comparison_page.country_select_source(s) percentage = self.comparison_page.country_get_values_by_track_index_percentage(1) countries = self.comparison_page.country_get_values_by_track_index_countries(1) streams_val = self.comparison_page.country_get_values_by_track_index_count(1) streams = [float(s) for s in streams_val] self.comparison_page.switch_to_tab(2) self.trackPage.select_dsp(dsp) self.trackPage.choose_graph_view_bar_chart() if s != AppConfig.get("source")["all"]: self.trackPage.change_source_value(s) tooltip_countries = self.trackPage.get_track_performance_countries_tooltips(10) self.ts.markFinal( countries == tooltip_countries["countries"], "STEP 1-10: countries are correct, source: {}".format(s), ) self.ts.markFinal( streams == tooltip_countries["streams"], "streams are correct: {}, track: {}".format(streams, tooltip_countries["streams"]), ) self.ts.markFinal( percentage == tooltip_countries["percentage"], "percentage are correct period: {}," "dsp: {}, source: {}, comp: {}, tooltip: {}".format(AppConfig.get("periods")["28d"], dsp, s, percentage, tooltip_countries["percentage"]), ) @allure.story("AP-2938") @allure.severity(allure.severity_level.NORMAL) @allure.title("""Streams by Countries table Data calculation - Export 14d""") @allure.description("""TC Verifies correctness of data calculation in Streams by Countries table. - Export 14d""") def test_by_countries_table_elements_export_compare_14d(self): self.comparison_page.login_and_open_comparison(market=AppConfig.get("market")["us"], track=self.track6) self.comparison_page.compare_countries_in_track_csv_and_comparison( dsp=AppConfig.get("dsp")["spotify"], periods=[AppConfig.get("periods")["14d"]], track=self.track6, market=AppConfig.get("market")["us"], ) @allure.story("AP-2938") @allure.severity(allure.severity_level.NORMAL) @allure.title("""Streams by Countries table Data calculation - Export 28d""") @allure.description("""TC Verifies correctness of data calculation in Streams by Countries table. - Export 28d""") def test_by_countries_table_elements_export_compare_28d(self): self.comparison_page.login_and_open_comparison(market=AppConfig.get("market")["us"], track=self.track24) self.comparison_page.compare_countries_in_track_csv_and_comparison( dsp=AppConfig.get("dsp")["spotify"], periods=[AppConfig.get("periods")["28d"]], track=self.track24, market=AppConfig.get("market")["us"], source=AppConfig.get("source")["album_page"], ) @allure.story("AP-2938") @allure.severity(allure.severity_level.NORMAL) @allure.title("""Streams by Countries table Data calculation - Export 8w""") @allure.description("""TC Verifies correctness of data calculation in Streams by Countries table. - Export 8w""") def test_by_countries_table_elements_export_compare_8w(self): self.comparison_page.login_and_open_comparison(market=AppConfig.get("market")["us"], track=self.track15) self.comparison_page.compare_countries_in_track_csv_and_comparison( dsp=AppConfig.get("dsp")["apple"], periods=[AppConfig.get("periods")["8w"]], track=self.track15, market=AppConfig.get("market")["us"], ) @allure.story("AP-2938") @allure.severity(allure.severity_level.NORMAL) @allure.title("""Streams by Countries table Data calculation - Export 12w""") @allure.description("""TC Verifies correctness of data calculation in Streams by Countries table. - Export 12w""") def test_by_countries_table_elements_export_compare_12w(self): self.comparison_page.login_and_open_comparison(market=AppConfig.get("market")["us"], track=self.track13) self.comparison_page.compare_countries_in_track_csv_and_comparison( dsp=AppConfig.get("dsp")["apple"], periods=[AppConfig.get("periods")["12w"]], track=self.track13, market=AppConfig.get("market")["us"], source=AppConfig.get("source")["discovery"], ) @allure.story("AP-2938") @allure.severity(allure.severity_level.NORMAL) @allure.title("""Streams by Countries table Data calculation - Export 6m""") @allure.description("""TC Verifies correctness of data calculation in Streams by Countries table. - Export 6m""") def test_by_countries_table_elements_export_compare_6m(self): self.comparison_page.login_and_open_comparison(market=AppConfig.get("market")["us"], track=self.track24) self.comparison_page.compare_countries_in_track_csv_and_comparison( dsp=AppConfig.get("dsp")["amazon"], periods=[AppConfig.get("periods")["6m"]], track=self.track24, market=AppConfig.get("market")["us"], ) @allure.story("AP-2938") @allure.severity(allure.severity_level.NORMAL) @allure.title("""Streams by Countries table Data calculation - Export 1y""") @allure.description("""TC Verifies correctness of data calculation in Streams by Countries table. - Export 1y""") def test_by_countries_table_elements_export_compare_1y(self): self.comparison_page.login_and_open_comparison(market=AppConfig.get("market")["us"], track=self.track24) self.comparison_page.compare_countries_in_track_csv_and_comparison( dsp=AppConfig.get("dsp")["total_streams"], periods=[AppConfig.get("periods")["1y"]], track=self.track24, market=AppConfig.get("market")["us"], )