ETA444 / datasafari

DataSafari simplifies complex data science tasks into straightforward, powerful one-liners.
https://datasafari.dev
GNU General Public License v3.0
2 stars 0 forks source link

Construct tests for calculate_composite_score() #105

Closed ETA444 closed 5 months ago

ETA444 commented 5 months ago

Summary of Unit Tests for calculate_composite_score()

The calculate_composite_score() function is used within the model_recommendation_core() for combining different performance metrics into a single composite score, often used for model selection and evaluation in machine learning. It calculates a weighted average based on the given scores and their respective weights. The unit tests are structured to ensure that the function handles various data types correctly, validates the input effectively, and computes the expected output accurately.

Detailed Test Descriptions

  1. Type Validation Tests:

    • Invalid Types for Inputs: Checks if a TypeError is raised when the inputs for scores and metric_weights are not dictionaries.
    • Empty Dictionary Inputs: Verifies that a ValueError is raised when either scores or metric_weights is an empty dictionary, as it cannot perform a calculation without these inputs.
  2. Data Integrity Checks:

    • Missing Weights for Metrics: Ensures that a ValueError is raised if some metrics included in scores do not have corresponding weights in metric_weights. This ensures all scores are appropriately weighted.
    • Zero Weights: Tests that a ValueError is raised if all metric weights are zero, as this would lead to a division by zero error during the calculation.

Example Test Code

Here's an example of how an error-handling test is implemented:

def test_calculate_composite_score_missing_weights(sample_data_ccs):
    """ Test calculate_composite_score with missing weights for some metrics. """
    scores, metric_weights = sample_data_ccs
    del metric_weights['Recall']
    with pytest.raises(ValueError, match="Missing metric weights for: Recall"):
        calculate_composite_score(scores, metric_weights)

This test ensures that the function correctly identifies missing weights for the provided scores, preventing calculations that would otherwise ignore certain metrics.

Full Test Suite

You can access the complete suite here: Composite Score Calculation Test Suite.