@@ -223,11 +223,22 @@ def test_dimension_anomalies_with_timestamp_exclude_final_results(
223223# Test for exclude_detection_period_from_training functionality
224224# This test demonstrates the use case where:
225225# 1. Detection period contains anomalous distribution data that would normally be included in training
226- # 2. With exclude_detection_period_from_training =False: anomaly is missed (test passes) because training includes the anomaly
227- # 3. With exclude_detection_period_from_training =True: anomaly is detected (test fails) because training excludes the anomaly
226+ # 2. With exclude_detection =False: anomaly is missed (test passes) because training includes the anomaly
227+ # 3. With exclude_detection =True: anomaly is detected (test fails) because training excludes the anomaly
228228@pytest .mark .skip_targets (["clickhouse" ])
229- def test_dimension_exclude_detection_from_training (
230- test_id : str , dbt_project : DbtProject
229+ @pytest .mark .parametrize (
230+ "exclude_detection,expected_status" ,
231+ [
232+ (False , "pass" ), # include detection in training → anomaly absorbed
233+ (True , "fail" ), # exclude detection from training → anomaly detected
234+ ],
235+ ids = ["include_detection_in_training" , "exclude_detection_from_training" ],
236+ )
237+ def test_anomaly_in_detection_period (
238+ test_id : str ,
239+ dbt_project : DbtProject ,
240+ exclude_detection : bool ,
241+ expected_status : str ,
231242):
232243 """
233244 Test the exclude_detection_period_from_training flag functionality for dimension anomalies.
@@ -285,42 +296,22 @@ def test_dimension_exclude_detection_from_training(
285296
286297 all_data = normal_data + anomalous_data
287298
288- # Test 1: WITHOUT exclusion (should pass - misses the anomaly because it's included in training)
289- test_args_without_exclusion = {
299+ test_args = {
290300 ** DBT_TEST_ARGS ,
291301 "training_period" : {"period" : "day" , "count" : 30 },
292302 "detection_period" : {"period" : "day" , "count" : 7 },
293303 "time_bucket" : {"period" : "day" , "count" : 1 },
294304 "sensitivity" : 5 ,
295- # exclude_detection_period_from_training is not set (defaults to False/None)
296- }
297-
298- test_result_without_exclusion = dbt_project .test (
299- test_id + "_f" ,
300- DBT_TEST_NAME ,
301- test_args_without_exclusion ,
302- data = all_data ,
303- )
304-
305- # This should PASS because the anomaly is included in training, making it part of the baseline
306- assert (
307- test_result_without_exclusion ["status" ] == "pass"
308- ), "Test should pass when anomaly is included in training"
309-
310- # Test 2: WITH exclusion (should fail - detects the anomaly because it's excluded from training)
311- test_args_with_exclusion = {
312- ** test_args_without_exclusion ,
313- "exclude_detection_period_from_training" : True ,
314305 }
306+ if exclude_detection :
307+ test_args ["exclude_detection_period_from_training" ] = True
315308
316- test_result_with_exclusion = dbt_project .test (
317- test_id + "_t" ,
309+ suffix = "_excl" if exclude_detection else "_incl"
310+ test_result = dbt_project .test (
311+ test_id + suffix ,
318312 DBT_TEST_NAME ,
319- test_args_with_exclusion ,
313+ test_args ,
320314 data = all_data ,
321315 )
322316
323- # This should FAIL because the anomaly is excluded from training, so it's detected as anomalous
324- assert (
325- test_result_with_exclusion ["status" ] == "fail"
326- ), "Test should fail when anomaly is excluded from training"
317+ assert test_result ["status" ] == expected_status
0 commit comments