|
| 1 | +import json |
| 2 | + |
| 3 | +import pytest |
| 4 | +from dbt_project import DbtProject |
| 5 | + |
| 6 | +COLUMN_NAME = "some_data" |
| 7 | + |
| 8 | +SAMPLES_QUERY = """ |
| 9 | + with latest_elementary_test_result as ( |
| 10 | + select id |
| 11 | + from {{{{ ref("elementary_test_results") }}}} |
| 12 | + where lower(table_name) = lower('{test_id}') |
| 13 | + order by created_at desc |
| 14 | + limit 1 |
| 15 | + ) |
| 16 | +
|
| 17 | + select result_row |
| 18 | + from {{{{ ref("test_result_rows") }}}} |
| 19 | + where elementary_test_results_id in (select * from latest_elementary_test_result) |
| 20 | +""" |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.skip_targets(["clickhouse"]) |
| 24 | +def test_sample_count_unlimited( |
| 25 | + test_id: str, dbt_project: DbtProject |
| 26 | +): |
| 27 | + null_count = 20 |
| 28 | + data = [{COLUMN_NAME: None} for _ in range(null_count)] |
| 29 | + |
| 30 | + test_result = dbt_project.test( |
| 31 | + test_id, |
| 32 | + "not_null", |
| 33 | + dict(column_name=COLUMN_NAME), |
| 34 | + data=data, |
| 35 | + as_model=True, |
| 36 | + test_vars={ |
| 37 | + "enable_elementary_test_materialization": True, |
| 38 | + "test_sample_row_count": 5, |
| 39 | + }, |
| 40 | + test_config={"meta": {"test_sample_row_count": None}}, |
| 41 | + ) |
| 42 | + assert test_result["status"] == "fail" |
| 43 | + |
| 44 | + samples = [ |
| 45 | + json.loads(row["result_row"]) |
| 46 | + for row in dbt_project.run_query(SAMPLES_QUERY.format(test_id=test_id)) |
| 47 | + ] |
| 48 | + assert len(samples) == 20 |
| 49 | + for sample in samples: |
| 50 | + assert COLUMN_NAME in sample |
| 51 | + assert sample[COLUMN_NAME] is None |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.skip_targets(["clickhouse"]) |
| 55 | +def test_sample_count_small(test_id: str, dbt_project: DbtProject): |
| 56 | + null_count = 20 |
| 57 | + data = [{COLUMN_NAME: None} for _ in range(null_count)] |
| 58 | + |
| 59 | + test_result = dbt_project.test( |
| 60 | + test_id, |
| 61 | + "not_null", |
| 62 | + dict(column_name=COLUMN_NAME), |
| 63 | + data=data, |
| 64 | + as_model=True, |
| 65 | + test_vars={ |
| 66 | + "enable_elementary_test_materialization": True, |
| 67 | + "test_sample_row_count": 5, |
| 68 | + }, |
| 69 | + test_config={"meta": {"test_sample_row_count": 3}}, |
| 70 | + ) |
| 71 | + assert test_result["status"] == "fail" |
| 72 | + |
| 73 | + samples = [ |
| 74 | + json.loads(row["result_row"]) |
| 75 | + for row in dbt_project.run_query(SAMPLES_QUERY.format(test_id=test_id)) |
| 76 | + ] |
| 77 | + assert len(samples) == 3 |
| 78 | + for sample in samples: |
| 79 | + assert COLUMN_NAME in sample |
| 80 | + assert sample[COLUMN_NAME] is None |
0 commit comments