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

Commit 88f64a4

Browse files
committed
Add tests for data profile scan
1 parent 7f80a97 commit 88f64a4

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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", "dataplex-dp-published-project", "dataplex-dp-published-location"
75+
]
76+
labels_to_be_created = profile_scan_labels + list(ORIGINAL_LABELS.keys())
77+
assert set(table.labels.keys()) == set(labels_to_be_created)
78+
79+
80+
class TestDataProfileScanWithoutProfileScanSetting:
81+
@pytest.fixture(scope="class")
82+
def models(self):
83+
return {
84+
f"{MODEL_NAME}.sql": SQL_CONTENT,
85+
f"{MODEL_NAME}.yml": YAML_CONTENT,
86+
}
87+
88+
def test_create_data_profile_scan(self, project):
89+
with patch(
90+
"dbt.adapters.bigquery.impl.dataplex_v1.DataScanServiceClient"
91+
) as MockDataScanClient:
92+
mock_data_scan_client = MockDataScanClient.return_value
93+
94+
results = run_dbt()
95+
assert len(results) == 1
96+
97+
mock_data_scan_client.create_data_scan.assert_not_called()
98+
mock_data_scan_client.run_data_scan.assert_not_called()
99+
100+
relation: BigQueryRelation = relation_from_name(project.adapter, MODEL_NAME)
101+
adapter = project.adapter
102+
with get_connection(project.adapter) as conn:
103+
table = conn.handle.get_table(
104+
adapter.connections.get_bq_table(
105+
relation.database, relation.schema, relation.table
106+
)
107+
)
108+
labels_to_be_created = []
109+
assert set(table.labels.keys()) == set(labels_to_be_created)
110+
111+
112+
class TestDataProfileScanDisabledProfileScanSetting:
113+
@pytest.fixture(scope="class")
114+
def project_config_update(self):
115+
return {
116+
"models": {
117+
"+data_profile_scan": {
118+
"location": SCAN_LOCATION,
119+
"scan_id": SCAN_ID,
120+
"enabled": False
121+
},
122+
},
123+
}
124+
125+
@pytest.fixture(scope="class")
126+
def models(self):
127+
return {
128+
f"{MODEL_NAME}.sql": SQL_CONTENT,
129+
f"{MODEL_NAME}.yml": YAML_CONTENT,
130+
}
131+
132+
def test_create_data_profile_scan(self, project):
133+
with patch(
134+
"dbt.adapters.bigquery.impl.dataplex_v1.DataScanServiceClient"
135+
) as MockDataScanClient:
136+
mock_data_scan_client = MockDataScanClient.return_value
137+
138+
results = run_dbt()
139+
assert len(results) == 1
140+
141+
mock_data_scan_client.create_data_scan.assert_not_called()
142+
mock_data_scan_client.run_data_scan.assert_not_called()
143+
144+
relation: BigQueryRelation = relation_from_name(project.adapter, MODEL_NAME)
145+
adapter = project.adapter
146+
with get_connection(project.adapter) as conn:
147+
table = conn.handle.get_table(
148+
adapter.connections.get_bq_table(
149+
relation.database, relation.schema, relation.table
150+
)
151+
)
152+
labels_to_be_created = []
153+
assert set(table.labels.keys()) == set(labels_to_be_created)

0 commit comments

Comments
 (0)