Skip to content

Commit a45c384

Browse files
authored
Enable test_sample_row_count config on tests (#903)
* Override test_sample_row_count by test config * Add tests * lint
1 parent cffd655 commit a45c384

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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(test_id: str, dbt_project: DbtProject):
25+
null_count = 20
26+
data = [{COLUMN_NAME: None} for _ in range(null_count)]
27+
28+
test_result = dbt_project.test(
29+
test_id,
30+
"not_null",
31+
dict(column_name=COLUMN_NAME),
32+
data=data,
33+
as_model=True,
34+
test_vars={
35+
"enable_elementary_test_materialization": True,
36+
"test_sample_row_count": 5,
37+
},
38+
test_config={"meta": {"test_sample_row_count": None}},
39+
)
40+
assert test_result["status"] == "fail"
41+
42+
samples = [
43+
json.loads(row["result_row"])
44+
for row in dbt_project.run_query(SAMPLES_QUERY.format(test_id=test_id))
45+
]
46+
assert len(samples) == 20
47+
for sample in samples:
48+
assert COLUMN_NAME in sample
49+
assert sample[COLUMN_NAME] is None
50+
51+
52+
@pytest.mark.skip_targets(["clickhouse"])
53+
def test_sample_count_small(test_id: str, dbt_project: DbtProject):
54+
null_count = 20
55+
data = [{COLUMN_NAME: None} for _ in range(null_count)]
56+
57+
test_result = dbt_project.test(
58+
test_id,
59+
"not_null",
60+
dict(column_name=COLUMN_NAME),
61+
data=data,
62+
as_model=True,
63+
test_vars={
64+
"enable_elementary_test_materialization": True,
65+
"test_sample_row_count": 5,
66+
},
67+
test_config={"meta": {"test_sample_row_count": 3}},
68+
)
69+
assert test_result["status"] == "fail"
70+
71+
samples = [
72+
json.loads(row["result_row"])
73+
for row in dbt_project.run_query(SAMPLES_QUERY.format(test_id=test_id))
74+
]
75+
assert len(samples) == 3
76+
for sample in samples:
77+
assert COLUMN_NAME in sample
78+
assert sample[COLUMN_NAME] is None

macros/edr/materializations/test/test.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
{% macro handle_dbt_test(flattened_test, materialization_macro) %}
5252
{% set result = materialization_macro() %}
5353
{% set sample_limit = elementary.get_config_var('test_sample_row_count') %}
54+
{% if "meta" in flattened_test and "test_sample_row_count" in flattened_test["meta"] %}
55+
{% set sample_limit = flattened_test["meta"]["test_sample_row_count"] %}
56+
{% endif %}
5457

5558
{% set disable_test_samples = false %}
5659
{% if "meta" in flattened_test and "disable_test_samples" in flattened_test["meta"] %}

0 commit comments

Comments
 (0)