tests package

Submodules

tests.conftest module

This file contains unit tests and fixtures used with pytest to test data analysis, cleaning, and manipulation features. The tests cover several cases, such as data cleaning, outlier handling, and merging DataFrames.

tests.conftest.column_merge_data()

Provides sample DataFrames for testing the add_columns function.

This fixture creates two DataFrames: - A target DataFrame with keys and values. - A source DataFrame with keys and additional columns to merge.

Returns:
tuple: A tuple containing:
  • df_target (pd.DataFrame): The target DataFrame with the following columns:
    • ‘key’ (int): Keys for merging.

    • ‘value’ (str): Values associated with each key.

  • df_source (pd.DataFrame): The source DataFrame with the following columns:
    • ‘key’ (int): Keys for merging.

    • ‘extra’ (str): Additional column to merge with the target DataFrame.

tests.conftest.df_high_count()
tests.conftest.df_low_count()
tests.conftest.ingr_map()
tests.conftest.merged_sample()

Fixture that generates two DataFrames to test the merge.

These DataFrames contain common and different columns, allowing to validate join operations under several configurations (‘left’, ‘right’, ‘inner’, ‘outer’).

Returns: tuple: Two DataFrames with a common column ‘A’ and a specific column (‘B’ or ‘C’).

tests.conftest.normalisation_data()

Provides a sample DataFrame for testing the normalisation function.

This fixture creates a DataFrame with a single numeric column to validate the normalization process.

Returns:
pd.DataFrame: A DataFrame with the following column:
  • ‘value’ (int): A numeric column with sample values to normalize.

tests.conftest.nutriments_data()

Provides a sample of a row of a DataFrame for testing the calcul of the nutri-socre

This fixture creates a dictionnary representing the nutrients of 1 recipe - Calories - Sugar - Saturated Fat - Sodium - Protein

Args:

None

Returns:
A Dictionnary containing the nutrients of 1 recipe.
  • “Calories” (float)

  • “Sugar” (float)

  • “Saturated_Fat” (float)

  • “Sodium” (flaoa)

  • “Protein” (float)

tests.conftest.outliers_sample()

Fixture that generates a sample of data to test outlier handling.

The generated DataFrame contains two numeric columns with incremental values to simulate different data ranges.

Returns: df (DataFrame): A DataFrame containing two columns ‘A’ and ‘B’ with incremental values.

tests.conftest.recipes_table()

Provides a sample DataFrame for testing recipe data by user.

This fixture creates a DataFrame with recipe data that includes: - contributor_id - name: name of the recipe. - rating: rating given to each recipe.

Args:

None

Returns:
pd.DataFrame: A DataFrame containing recipe data with the following columns:
  • ‘contributor_id’ (int)

  • ‘name’ (str)

  • ‘rating’ (int)

tests.conftest.sample_date_avgrating()

summary

tests.conftest.sample_date_data()

Fixture qui crée un DataFrame contenant une colonne ‘submitted’ avec des dates pour tester la fonction ‘date_separated’.

tests.conftest.sample_raw_recipes()

Fixture that loads a sample of raw recipe data.

This DataFrame contains the columns needed to test data transformations such as splitting columns and adding new time information.

Returns: df (DataFrame): A DataFrame containing the raw recipe data.

tests.conftest.visu_data()

Fixture to test the visualisation season function

tests.test_preprocess module

tests.test_preprocess.test_add_columns(column_merge_data)

Tests the add_columns function to ensure it correctly merges additional columns from a source DataFrame into a target DataFrame based on a common key.

The test verifies that: - The new column is successfully added to the target DataFrame. - Missing keys in the source DataFrame result in NaN values in the merged column.

Args:
column_merge_data (tuple): A fixture providing two DataFrames:
  • Target DataFrame with keys and values.

  • Source DataFrame with keys and additional columns to merge.

tests.test_preprocess.test_add_columns_mixed_keys(sample_raw_recipes)

Tests add_columns with partial key matches.

Verifies: - Matching keys add the correct data. - Non-matching keys result in NaN values.

tests.test_preprocess.test_dataframe_concat_exceptions_and_edge_cases(merged_sample)

Tests edge cases and exceptions for the dataframe_concat function.

Verifies: - Errors when the key is missing in one or both DataFrames. - Handling of mismatched join types. - Joining on columns that exist only in one DataFrame.

tests.test_preprocess.test_date_separated(sample_date_data)

Teste que ‘date_separated’ ajoute les colonnes ‘year’, ‘month’, et ‘day’, et supprime la colonne ‘submitted’.

tests.test_preprocess.test_df_merged(merged_sample)

Test the ‘dataframe_concat’ function for merging DataFrames.

Checks that each join type (‘left’, ‘right’, ‘outer’, ‘inner’) returns a DataFrame with the correct size.

Args: merged_sample (tuple): A tuple containing two DataFrames, sharing a common column A and specific columns (‘B’ or ‘C’), to test different merge configurations.

tests.test_preprocess.test_drop_columns_existing(sample_raw_recipes)

Tests drop_columns removes existing columns.

tests.test_preprocess.test_drop_columns_nonexistent(sample_raw_recipes)

Tests drop_columns raises KeyError for nonexistent columns.

tests.test_preprocess.test_drop_columns_valid(sample_raw_recipes)

Tests drop_columns removes specified columns.

Verifies: - Specified columns are removed. - Remaining columns are unchanged.

tests.test_preprocess.test_outliers_df(outliers_sample)

Test the outliers_df function to detect and extract outliers based on specified thresholds.

tests.test_utils module

tests.test_utils.get_top_ingredients2(season_df, ingr_map, excluded_ingredients, top_n)
tests.test_utils.test_assign_grade()
tests.test_utils.test_average_and_total_comments_per_contributor(sample_raw_recipes)

Test that the average and total comments per contributor are calculated correctly.

tests.test_utils.test_count_contributors_by_recipe_range_with_bins(sample_raw_recipes)

Test that contributors are correctly categorized into recipe count bins.

tests.test_utils.test_get_top_ingredients2(sample_raw_recipes)

Test that the top ingredients are correctly extracted, excluding specified ingredients.

tests.test_utils.test_get_top_tags(sample_raw_recipes)

Test that the top N tags are correctly extracted from the dataset.

tests.test_utils.test_group_by_season_and_count(sample_data)

Test if the grouping by season and count calculation works correctly.

tests.test_utils.test_metrics_main_contributor(sample_raw_recipes)

Test that metrics_main_contributor returns the correct number of unique contributors and recipes.

tests.test_utils.test_nutri_score(nutriments_data)
tests.test_utils.test_top_commented_recipes(sample_raw_recipes)

Test that the top N commented recipes are correctly extracted.

tests.test_utils.test_top_commented_recipes_by_contributors(sample_raw_recipes)

Test that the top commented recipes are correctly extracted for top contributors.

tests.test_utils.test_top_recipes_top_three(sample_date_avgrating)

Tests that top_recipes works correctly and returns the top 3 recipes when there are exactly 3 recipes.

tests.test_utils.test_top_recipes_user(sample_raw_recipes)

Test the top_recipes_user function with the sample_raw_recipes dataset.

tests.test_utils.test_trendy_ingredients_by_seasons()
tests.test_utils.test_unique_ingr(sample_raw_recipes, ingr_map)
tests.test_utils.test_user_recipes(recipes_table)
tests.test_utils.test_visualise_low_rank_insight(df_low_count)

Test the visualisation low rank function

tests.test_utils.test_visualise_recipe_season(sample_data)

Module contents