|
| 1 | +import json |
| 2 | + |
| 3 | +import pytest |
| 4 | +from dbt_project import DbtProject |
| 5 | + |
| 6 | +COLUMN_NAME = "value" |
| 7 | + |
| 8 | + |
| 9 | +SAMPLES_QUERY = """ |
| 10 | + with latest_elementary_test_result as ( |
| 11 | + select id |
| 12 | + from {{{{ ref("elementary_test_results") }}}} |
| 13 | + where lower(table_name) = lower('{test_id}') |
| 14 | + order by created_at desc |
| 15 | + limit 1 |
| 16 | + ) |
| 17 | +
|
| 18 | + select result_row |
| 19 | + from {{{{ ref("test_result_rows") }}}} |
| 20 | + where elementary_test_results_id in (select * from latest_elementary_test_result) |
| 21 | +""" |
| 22 | + |
| 23 | +TEST_SAMPLE_ROW_COUNT = 7 |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.skip_targets(["clickhouse"]) |
| 27 | +def test_sampling_pii_disabled(test_id: str, dbt_project: DbtProject): |
| 28 | + """Test that PII-tagged tables don't upload samples even when tests fail""" |
| 29 | + null_count = 50 |
| 30 | + data = [{COLUMN_NAME: None} for _ in range(null_count)] |
| 31 | + |
| 32 | + test_result = dbt_project.test( |
| 33 | + test_id, |
| 34 | + "not_null", |
| 35 | + dict(column_name=COLUMN_NAME), |
| 36 | + data=data, |
| 37 | + as_model=True, |
| 38 | + model_config={"config": {"tags": ["pii"]}}, |
| 39 | + test_vars={ |
| 40 | + "enable_elementary_test_materialization": True, |
| 41 | + "test_sample_row_count": TEST_SAMPLE_ROW_COUNT, |
| 42 | + "disable_samples_on_pii_tables": True, |
| 43 | + "pii_table_tags": ["pii", "sensitive"], |
| 44 | + }, |
| 45 | + ) |
| 46 | + assert test_result["status"] == "fail" |
| 47 | + |
| 48 | + samples = [ |
| 49 | + json.loads(row["result_row"]) |
| 50 | + for row in dbt_project.run_query(SAMPLES_QUERY.format(test_id=test_id)) |
| 51 | + ] |
| 52 | + assert len(samples) == 0 |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.skip_targets(["clickhouse"]) |
| 56 | +def test_sampling_non_pii_enabled(test_id: str, dbt_project: DbtProject): |
| 57 | + """Test that non-PII tables still collect samples normally""" |
| 58 | + null_count = 50 |
| 59 | + data = [{COLUMN_NAME: None} for _ in range(null_count)] |
| 60 | + |
| 61 | + test_result = dbt_project.test( |
| 62 | + test_id, |
| 63 | + "not_null", |
| 64 | + dict(column_name=COLUMN_NAME), |
| 65 | + data=data, |
| 66 | + as_model=True, |
| 67 | + model_config={"config": {"tags": ["normal"]}}, |
| 68 | + test_vars={ |
| 69 | + "enable_elementary_test_materialization": True, |
| 70 | + "test_sample_row_count": TEST_SAMPLE_ROW_COUNT, |
| 71 | + "disable_samples_on_pii_tables": True, |
| 72 | + "pii_table_tags": ["pii", "sensitive"], |
| 73 | + }, |
| 74 | + ) |
| 75 | + assert test_result["status"] == "fail" |
| 76 | + |
| 77 | + samples = [ |
| 78 | + json.loads(row["result_row"]) |
| 79 | + for row in dbt_project.run_query(SAMPLES_QUERY.format(test_id=test_id)) |
| 80 | + ] |
| 81 | + assert len(samples) == TEST_SAMPLE_ROW_COUNT |
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.skip_targets(["clickhouse"]) |
| 85 | +def test_sampling_pii_feature_disabled(test_id: str, dbt_project: DbtProject): |
| 86 | + """Test that when PII feature is disabled, PII tables still collect samples""" |
| 87 | + null_count = 50 |
| 88 | + data = [{COLUMN_NAME: None} for _ in range(null_count)] |
| 89 | + |
| 90 | + test_result = dbt_project.test( |
| 91 | + test_id, |
| 92 | + "not_null", |
| 93 | + dict(column_name=COLUMN_NAME), |
| 94 | + data=data, |
| 95 | + as_model=True, |
| 96 | + model_config={"config": {"tags": ["pii"]}}, |
| 97 | + test_vars={ |
| 98 | + "enable_elementary_test_materialization": True, |
| 99 | + "test_sample_row_count": TEST_SAMPLE_ROW_COUNT, |
| 100 | + "disable_samples_on_pii_tables": False, |
| 101 | + "pii_table_tags": ["pii", "sensitive"], |
| 102 | + }, |
| 103 | + ) |
| 104 | + assert test_result["status"] == "fail" |
| 105 | + |
| 106 | + samples = [ |
| 107 | + json.loads(row["result_row"]) |
| 108 | + for row in dbt_project.run_query(SAMPLES_QUERY.format(test_id=test_id)) |
| 109 | + ] |
| 110 | + assert len(samples) == TEST_SAMPLE_ROW_COUNT |
0 commit comments