Skip to content
This repository was archived by the owner on Sep 2, 2025. It is now read-only.

Commit 38f1e8c

Browse files
committed
Add tests for data profile scan
1 parent 7f80a97 commit 38f1e8c

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
import json
2+
import pytest
3+
from unittest.mock import patch
4+
from dbt.adapters.bigquery.relation import BigQueryRelation
5+
from dbt.tests.util import run_dbt, get_connection, relation_from_name
6+
7+
SCAN_LOCATION = "us-central1"
8+
SCAN_ID = "bigquery_data_profile_scan_test"
9+
MODEL_NAME = "test_model"
10+
11+
ORIGINAL_LABELS = {
12+
"my_label_key": "my_label_value",
13+
}
14+
15+
SQL_CONTENT = """
16+
{{
17+
config(
18+
materialized="table"
19+
)
20+
}}
21+
select 20 as id, cast('2020-01-01 01:00:00' as datetime) as date_hour union all
22+
select 40 as id, cast('2020-01-01 02:00:00' as datetime) as date_hour
23+
"""
24+
25+
YAML_CONTENT = f"""version: 2
26+
models:
27+
- name: {MODEL_NAME}
28+
"""
29+
30+
31+
class TestDataProfileScan:
32+
@pytest.fixture(scope="class")
33+
def project_config_update(self):
34+
return {
35+
"models": {
36+
"+labels": ORIGINAL_LABELS,
37+
"+data_profile_scan": {
38+
"location": SCAN_LOCATION,
39+
"scan_id": SCAN_ID,
40+
"sampling_percent": 10,
41+
# "enabled": True
42+
},
43+
},
44+
}
45+
46+
@pytest.fixture(scope="class")
47+
def models(self):
48+
return {
49+
f"{MODEL_NAME}.sql": SQL_CONTENT,
50+
f"{MODEL_NAME}.yml": YAML_CONTENT,
51+
}
52+
53+
def test_create_data_profile_scan(self, project):
54+
with patch(
55+
"dbt.adapters.bigquery.impl.dataplex_v1.DataScanServiceClient"
56+
) as MockDataScanClient:
57+
mock_data_scan_client = MockDataScanClient.return_value
58+
59+
results = run_dbt()
60+
assert len(results) == 1
61+
62+
mock_data_scan_client.create_data_scan.assert_called_once()
63+
mock_data_scan_client.run_data_scan.assert_called_once()
64+
65+
relation: BigQueryRelation = relation_from_name(project.adapter, MODEL_NAME)
66+
adapter = project.adapter
67+
with get_connection(project.adapter) as conn:
68+
table = conn.handle.get_table(
69+
adapter.connections.get_bq_table(
70+
relation.database, relation.schema, relation.table
71+
)
72+
)
73+
profile_scan_labels = [
74+
"dataplex-dp-published-scan",
75+
"dataplex-dp-published-project",
76+
"dataplex-dp-published-location",
77+
]
78+
labels_to_be_created = profile_scan_labels + list(ORIGINAL_LABELS.keys())
79+
assert set(table.labels.keys()) == set(labels_to_be_created)
80+
81+
82+
class TestDataProfileScanWithoutProfileScanSetting:
83+
@pytest.fixture(scope="class")
84+
def models(self):
85+
return {
86+
f"{MODEL_NAME}.sql": SQL_CONTENT,
87+
f"{MODEL_NAME}.yml": YAML_CONTENT,
88+
}
89+
90+
def test_create_data_profile_scan(self, project):
91+
with patch(
92+
"dbt.adapters.bigquery.impl.dataplex_v1.DataScanServiceClient"
93+
) as MockDataScanClient:
94+
mock_data_scan_client = MockDataScanClient.return_value
95+
96+
results = run_dbt()
97+
assert len(results) == 1
98+
99+
mock_data_scan_client.create_data_scan.assert_not_called()
100+
mock_data_scan_client.run_data_scan.assert_not_called()
101+
102+
relation: BigQueryRelation = relation_from_name(project.adapter, MODEL_NAME)
103+
adapter = project.adapter
104+
with get_connection(project.adapter) as conn:
105+
table = conn.handle.get_table(
106+
adapter.connections.get_bq_table(
107+
relation.database, relation.schema, relation.table
108+
)
109+
)
110+
labels_to_be_created = []
111+
assert set(table.labels.keys()) == set(labels_to_be_created)
112+
113+
114+
class TestDataProfileScanDisabledProfileScanSetting:
115+
@pytest.fixture(scope="class")
116+
def project_config_update(self):
117+
return {
118+
"models": {
119+
"+data_profile_scan": {
120+
"location": SCAN_LOCATION,
121+
"scan_id": SCAN_ID,
122+
"enabled": False,
123+
},
124+
},
125+
}
126+
127+
@pytest.fixture(scope="class")
128+
def models(self):
129+
return {
130+
f"{MODEL_NAME}.sql": SQL_CONTENT,
131+
f"{MODEL_NAME}.yml": YAML_CONTENT,
132+
}
133+
134+
def test_create_data_profile_scan(self, project):
135+
with patch(
136+
"dbt.adapters.bigquery.impl.dataplex_v1.DataScanServiceClient"
137+
) as MockDataScanClient:
138+
mock_data_scan_client = MockDataScanClient.return_value
139+
140+
results = run_dbt()
141+
assert len(results) == 1
142+
143+
mock_data_scan_client.create_data_scan.assert_not_called()
144+
mock_data_scan_client.run_data_scan.assert_not_called()
145+
146+
relation: BigQueryRelation = relation_from_name(project.adapter, MODEL_NAME)
147+
adapter = project.adapter
148+
with get_connection(project.adapter) as conn:
149+
table = conn.handle.get_table(
150+
adapter.connections.get_bq_table(
151+
relation.database, relation.schema, relation.table
152+
)
153+
)
154+
labels_to_be_created = []
155+
assert set(table.labels.keys()) == set(labels_to_be_created)

0 commit comments

Comments
 (0)