import datetime import logging import pytest import tadas.domain.constants import tadas.snowflake.snowflake_publish from tests.integration.models import common_test_models from tadas.models.t2511_noamazon import model_config logger = logging.getLogger(__name__) REPORT_DATES_TO_TEST = [ '2025-11-23', ] # these trending flags are missing for this model MISSING_TRENDING_FLAGS = { 'shazam_flag', 'tiktok_creations_global_flag', 'tiktok_views_global_flag', 'tiktok_likes_global_flag', 'insta_reels_views_country_flag', 'insta_reels_views_global_flag', 'insta_reels_creations_country_flag', 'insta_reels_creations_global_flag', } _TADAS_RESULTS = {} @pytest.fixture(params=REPORT_DATES_TO_TEST) def report_to_df(request, capsys): """ Fixture which runs the model for a given report_date and returns cached results. :param request: extract report_date from fixture's params :return: mappings from report to cache table name, report_date """ global _TADAS_RESULTS report_date = request.param model_version = model_config.MODEL_VERSION cached_result = _TADAS_RESULTS.get(report_date) if cached_result is not None: return cached_result with common_test_models.lock_final_tables(), \ capsys.disabled(): # disable capsys to let run_model print results to stdout common_test_models.clear_final_tables() common_test_models.run_model( model_version=model_version, report_date=report_date, ) reports_as_df = tadas.snowflake.snowflake_publish.load_final_tables_as_df() reports_as_df['report_date'] = report_date _TADAS_RESULTS[report_date] = reports_as_df return _TADAS_RESULTS[report_date] @pytest.mark.parametrize( 'report', tadas.domain.constants.REPORTS ) def test_number_of_rows(report_to_df, report): df = report_to_df[report] assert len(df) > 200 assert len(df) < 150_000 @pytest.mark.parametrize( 'report', tadas.domain.constants.REPORTS ) def test_report_date(report_to_df, report): df = report_to_df[report] # - report_date should be only one - and exactly the same as we requested report_date = report_to_df['report_date'] date_obj = datetime.datetime.strptime(report_date, '%Y-%m-%d').date() report_dates = df['report_date'].unique().tolist() assert report_dates == [date_obj], f'Unexpected report_date: {report_dates}' @pytest.mark.parametrize( 'report', tadas.domain.constants.REPORTS ) @pytest.mark.parametrize( 'column', common_test_models.EXPECTED_COLUMNS ) def test_expected_columns(report_to_df, report, column): df = report_to_df[report] # - availability of required columns assert column in df.columns assert not df[column].isna().any() @pytest.mark.parametrize( 'report', tadas.domain.constants.REPORTS ) @pytest.mark.parametrize( 'column', [ pytest.param( column, marks=[pytest.mark.xfail] if column in MISSING_TRENDING_FLAGS or column.replace('_lift', '_flag') in MISSING_TRENDING_FLAGS else [] ) for column in common_test_models.COLUMNS_WITH_VALUES ]) def test_not_single_value_columns(report_to_df, report, column): df = report_to_df[report] # - availability of required columns assert df[column].nunique() > 1, \ f"Column '{column}' should have more than one unique value, but has {df[column].nunique()}" @pytest.mark.parametrize( 'report', tadas.domain.constants.REPORTS ) def test_tadas_score_values(report_to_df, report): df = report_to_df[report] # values for predict_proba between 0 and 1 assert (df['tadas_30day_score'] >= 0).all() and (df['tadas_30day_score'] <= 1).all() @pytest.mark.parametrize( 'report', tadas.domain.constants.REPORTS ) @pytest.mark.parametrize( 'flag_column', common_test_models.EXPECTED_TRENDING_FLAGS ) def test_trending_flags_values(report_to_df, report, flag_column): df = report_to_df[report] # make sure no negative values for trending columns with positive flag lift_column = flag_column.replace('_flag', '_lift') mask = df[flag_column] == 1 assert (df.loc[mask, lift_column] > 0).all(), \ f"With {flag_column} == 1 all {lift_column} > 0" @pytest.mark.parametrize( 'report', tadas.domain.constants.REPORTS ) @pytest.mark.parametrize( 'flag_column', [ pytest.param( flag_column, marks=[pytest.mark.xfail] if flag_column in MISSING_TRENDING_FLAGS else [] ) for flag_column in common_test_models.EXPECTED_TRENDING_FLAGS ] ) def test_expected_trending_flags(report_to_df, report, flag_column): df = report_to_df[report] # make sure there are some values for trending flag mask = df[flag_column] == 1 assert len(df.loc[mask]) > 0 @pytest.mark.parametrize( 'country', [ pytest.param( country, marks=[pytest.mark.xfail] if country in common_test_models.COUNTRIES_OK_IF_EMPTY else [] ) for country in common_test_models.EXPECTED_COUNTRIES ] ) def test_data_per_country(report_to_df, country): if country == 'XX': report = tadas.domain.constants.REPORT_GLOBAL else: report = tadas.domain.constants.REPORT_COUNTRIES df = report_to_df[report] mask = df['geo_country'] == country # check entries in expected report assert len(df[mask]) > 0, f"Missing rows for {country} in {report} report" @pytest.mark.parametrize( 'country', [ pytest.param( country, ) for country in common_test_models.EXPECTED_COUNTRIES ] ) def test_unexpected_data_per_country(report_to_df, country): if country == 'XX': report = tadas.domain.constants.REPORT_GLOBAL else: report = tadas.domain.constants.REPORT_COUNTRIES wrong_reports_dfs = [df for report, df in report_to_df.items() if report != report] for df in wrong_reports_dfs: mask = df['geo_country'] == country # check no unexpected entries in report's df assert len(df[mask]) == 0, f"Unexpected rows for {country} in {report} report"