This repository was archived by the owner on May 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 281
/
Copy pathtest_data_source.py
407 lines (375 loc) · 15.8 KB
/
test_data_source.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
from io import StringIO
import json
from pathlib import Path
from parameterized import parameterized
import unittest
from unittest.mock import Mock, patch
from data_diff.cloud.datafold_api import (
TCloudApiDataSourceConfigSchema,
TCloudApiDataSourceSchema,
TCloudApiDataSource,
TCloudApiDataSourceTestResult,
TCloudDataSourceTestResult,
TDsConfig,
)
from data_diff.cloud.data_source import (
TDataSourceTestStage,
TestDataSourceStatus,
create_ds_config,
_check_data_source_exists,
_get_temp_schema,
_test_data_source,
)
from data_diff.dbt_parser import TDatadiffConfig
from tests.common import ansi_stdout_cleanup
DATA_SOURCE_CONFIGS = {
"snowflake": TDsConfig(
name="ds_name",
type="snowflake",
options={
"account": "account",
"user": "user",
"password": "password",
"warehouse": "warehouse",
"role": "role",
"default_db": "database",
},
float_tolerance=0.000001,
temp_schema="database.temp_schema",
),
"pg": TDsConfig(
name="ds_name",
type="pg",
options={
"host": "host",
"port": 5432,
"user": "user",
"password": "password",
"dbname": "database",
},
float_tolerance=0.000001,
temp_schema="database.temp_schema",
),
"bigquery": TDsConfig(
name="ds_name",
type="bigquery",
options={
"projectId": "project_id",
"jsonKeyFile": '{"key1": "value1"}',
"location": "US",
},
float_tolerance=0.000001,
temp_schema="database.temp_schema",
),
"databricks": TDsConfig(
name="ds_name",
type="databricks",
options={
"host": "host",
"http_path": "some_http_path",
"http_password": "password",
"database": "database",
},
float_tolerance=0.000001,
temp_schema="database.temp_schema",
),
"redshift": TDsConfig(
name="ds_name",
type="redshift",
options={
"host": "host",
"port": 5432,
"user": "user",
"password": "password",
"dbname": "database",
},
float_tolerance=0.000001,
temp_schema="database.temp_schema",
),
"postgres_aurora": TDsConfig(
name="ds_name",
type="postgres_aurora",
options={
"host": "host",
"port": 5432,
"user": "user",
"password": "password",
"dbname": "database",
},
float_tolerance=0.000001,
temp_schema="database.temp_schema",
),
"postgres_aws_rds": TDsConfig(
name="ds_name",
type="postgres_aws_rds",
options={
"host": "host",
"port": 5432,
"user": "user",
"password": "password",
"dbname": "database",
},
float_tolerance=0.000001,
temp_schema="database.temp_schema",
),
}
def format_data_source_config_test(testcase_func, param_num, param):
(config,) = param.args
return f"{testcase_func.__name__}_{config.type}"
class TestDataSource(unittest.TestCase):
def setUp(self) -> None:
with open(Path(__file__).parent / "files/data_source_schema_config_response.json", "r") as file:
self.data_source_schema = [
TCloudApiDataSourceConfigSchema(
name=item["name"],
db_type=item["type"],
config_schema=TCloudApiDataSourceSchema.from_orm(item),
)
for item in json.load(file)
]
self.db_type_data_source_schemas = {ds_schema.db_type: ds_schema for ds_schema in self.data_source_schema}
with open(Path(__file__).parent / "files/data_source_list_response.json", "r") as file:
self.data_sources = [TCloudApiDataSource(**item) for item in json.load(file)]
self.api = Mock()
self.api.get_data_source_schema_config.return_value = self.data_source_schema
self.api.get_data_sources.return_value = self.data_sources
@parameterized.expand([(c,) for c in DATA_SOURCE_CONFIGS.values()], name_func=format_data_source_config_test)
@patch("data_diff.dbt_parser.DbtParser.__new__")
def test_get_temp_schema(self, config: TDsConfig, mock_dbt_parser):
datadiff_config = TDatadiffConfig(prod_database="db", prod_schema="schema")
mock_dbt_parser.get_datadiff_config.return_value = datadiff_config
temp_schema = f"{datadiff_config.prod_database}.{datadiff_config.prod_schema}"
if config.type == "snowflake":
temp_schema = temp_schema.upper()
elif config.type in {"pg", "postgres_aurora", "postgres_aws_rds", "redshift"}:
temp_schema = temp_schema.lower()
assert _get_temp_schema(dbt_parser=mock_dbt_parser, db_type=config.type) == temp_schema
@parameterized.expand([(c,) for c in DATA_SOURCE_CONFIGS.values()], name_func=format_data_source_config_test)
def test_create_ds_config(self, config: TDsConfig):
inputs = list(config.options.values()) + [config.temp_schema, config.float_tolerance]
with patch("rich.prompt.Console.input", side_effect=map(str, inputs)):
actual_config = create_ds_config(
ds_config=self.db_type_data_source_schemas[config.type],
data_source_name=config.name,
)
self.assertEqual(actual_config, config)
@patch("data_diff.dbt_parser.DbtParser.__new__")
def test_create_snowflake_ds_config_from_dbt_profiles(self, mock_dbt_parser):
config = DATA_SOURCE_CONFIGS["snowflake"]
mock_dbt_parser.get_connection_creds.return_value = (config.options,)
with patch("rich.prompt.Console.input", side_effect=["y", config.temp_schema, str(config.float_tolerance)]):
actual_config = create_ds_config(
ds_config=self.db_type_data_source_schemas[config.type],
data_source_name=config.name,
dbt_parser=mock_dbt_parser,
)
self.assertEqual(actual_config, config)
@patch("data_diff.dbt_parser.DbtParser.__new__")
def test_create_bigquery_ds_config_dbt_oauth(self, mock_dbt_parser):
config = DATA_SOURCE_CONFIGS["bigquery"]
mock_dbt_parser.get_connection_creds.return_value = (config.options,)
with patch("rich.prompt.Console.input", side_effect=["y", config.temp_schema, str(config.float_tolerance)]):
actual_config = create_ds_config(
ds_config=self.db_type_data_source_schemas[config.type],
data_source_name=config.name,
dbt_parser=mock_dbt_parser,
)
self.assertEqual(actual_config, config)
@patch("data_diff.dbt_parser.DbtParser.__new__")
@patch("data_diff.cloud.data_source._get_data_from_bigquery_json")
def test_create_bigquery_ds_config_dbt_service_account(self, mock_get_data_from_bigquery_json, mock_dbt_parser):
config = DATA_SOURCE_CONFIGS["bigquery"]
mock_get_data_from_bigquery_json.return_value = json.loads(config.options["jsonKeyFile"])
mock_dbt_parser.get_connection_creds.return_value = (
{
"type": "bigquery",
"method": "service-account",
"project": config.options["projectId"],
"threads": 1,
"keyfile": "/some/path",
},
)
with patch(
"rich.prompt.Console.input",
side_effect=["y", config.options["location"], config.temp_schema, str(config.float_tolerance)],
):
actual_config = create_ds_config(
ds_config=self.db_type_data_source_schemas[config.type],
data_source_name=config.name,
dbt_parser=mock_dbt_parser,
)
self.assertEqual(actual_config, config)
@patch("data_diff.dbt_parser.DbtParser.__new__")
def test_create_bigquery_ds_config_dbt_service_account_json(self, mock_dbt_parser):
config = DATA_SOURCE_CONFIGS["bigquery"]
mock_dbt_parser.get_connection_creds.return_value = (
{
"type": "bigquery",
"method": "service-account-json",
"project": config.options["projectId"],
"threads": 1,
"keyfile_json": json.loads(config.options["jsonKeyFile"]),
},
)
with patch(
"rich.prompt.Console.input",
side_effect=["y", config.options["location"], config.temp_schema, str(config.float_tolerance)],
):
actual_config = create_ds_config(
ds_config=self.db_type_data_source_schemas[config.type],
data_source_name=config.name,
dbt_parser=mock_dbt_parser,
)
self.assertEqual(actual_config, config)
@patch("sys.stdout", new_callable=StringIO)
@patch("data_diff.dbt_parser.DbtParser.__new__")
def test_create_ds_snowflake_config_from_dbt_profiles_one_param_passed_through_input(
self, mock_dbt_parser, mock_stdout
):
config = DATA_SOURCE_CONFIGS["snowflake"]
options = {**config.options, "type": "snowflake"}
options["database"] = options.pop("default_db")
account = options.pop("account")
mock_dbt_parser.get_connection_creds.return_value = (options,)
with patch(
"rich.prompt.Console.input", side_effect=["y", account, config.temp_schema, str(config.float_tolerance)]
):
actual_config = create_ds_config(
ds_config=self.db_type_data_source_schemas[config.type],
data_source_name=config.name,
dbt_parser=mock_dbt_parser,
)
self.assertEqual(actual_config, config)
self.assertEqual(
ansi_stdout_cleanup(mock_stdout.getvalue().strip()),
'Cannot extract "account" from dbt profiles.yml. Please, type it manually',
)
@patch("sys.stdout", new_callable=StringIO)
def test_create_ds_config_validate_required_parameter(self, mock_stdout):
"""
Here we validate "host" as an example of a required parameter,
but it might be any parameter without a default value
"""
config = TDsConfig(
name="ds_name",
type="pg",
options={
"host": "host",
"port": 5432,
"user": "user",
"password": "password",
"dbname": "database",
},
float_tolerance=0.000001,
temp_schema="database.temp_schema",
)
inputs = ["", "host", 5432, "user", "password", "database", config.temp_schema, config.float_tolerance]
with patch("rich.prompt.Console.input", side_effect=map(str, inputs)):
actual_config = create_ds_config(
ds_config=self.db_type_data_source_schemas[config.type],
data_source_name=config.name,
)
self.assertEqual(actual_config, config)
self.assertEqual(ansi_stdout_cleanup(mock_stdout.getvalue().strip()), "Parameter must not be empty")
def test_check_data_source_exists(self):
self.assertEqual(_check_data_source_exists(self.data_sources, self.data_sources[0].name), self.data_sources[0])
def test_check_data_source_not_exists(self):
self.assertEqual(_check_data_source_exists(self.data_sources, "ds_with_this_name_does_not_exist"), None)
@patch("data_diff.cloud.data_source.DatafoldAPI")
def test_data_source_all_tests_ok(self, mock_api: Mock):
mock_api.test_data_source.return_value = 1
mock_api.check_data_source_test_results.return_value = [
TCloudApiDataSourceTestResult(
name="lineage_download",
status="done",
result=TCloudDataSourceTestResult(
status=TestDataSourceStatus.SUCCESS,
message="No lineage downloader for this data source",
outcome="skipped",
),
),
TCloudApiDataSourceTestResult(
name="schema_download",
status="done",
result=TCloudDataSourceTestResult(
status=TestDataSourceStatus.SUCCESS, message="Discovered 6 tables", outcome="success"
),
),
TCloudApiDataSourceTestResult(
name="temp_schema",
status="done",
result=TCloudDataSourceTestResult(
status=TestDataSourceStatus.FAILED, message='Created table "database"."schema"', outcome="failed"
),
),
TCloudApiDataSourceTestResult(
name="connection",
status="done",
result=TCloudDataSourceTestResult(
status=TestDataSourceStatus.SUCCESS, message="Connected to the database", outcome="success"
),
),
]
expected_results = [
TDataSourceTestStage(
name="schema_download", status=TestDataSourceStatus.SUCCESS, description="Discovered 6 tables"
),
TDataSourceTestStage(
name="temp_schema", status=TestDataSourceStatus.FAILED, description='Created table "database"."schema"'
),
TDataSourceTestStage(
name="connection", status=TestDataSourceStatus.SUCCESS, description="Connected to the database"
),
]
self.assertEqual(_test_data_source(api=mock_api, data_source_id=1), expected_results)
@patch("data_diff.cloud.data_source.DatafoldAPI")
def test_data_source_one_test_failed(self, mock_api: Mock):
mock_api.test_data_source.return_value = 1
mock_api.check_data_source_test_results.return_value = [
TCloudApiDataSourceTestResult(
name="lineage_download",
status="done",
result=TCloudDataSourceTestResult(
status=TestDataSourceStatus.SUCCESS,
message="No lineage downloader for this data source",
outcome="skipped",
),
),
TCloudApiDataSourceTestResult(
name="schema_download",
status="done",
result=TCloudDataSourceTestResult(
status=TestDataSourceStatus.SUCCESS, message="Discovered 6 tables", outcome="success"
),
),
TCloudApiDataSourceTestResult(
name="temp_schema",
status="done",
result=TCloudDataSourceTestResult(
status=TestDataSourceStatus.FAILED,
message='Unable to create table "database"."schema"',
outcome="failed",
),
),
TCloudApiDataSourceTestResult(
name="connection",
status="done",
result=TCloudDataSourceTestResult(
status=TestDataSourceStatus.SUCCESS, message="Connected to the database", outcome="success"
),
),
]
expected_results = [
TDataSourceTestStage(
name="schema_download", status=TestDataSourceStatus.SUCCESS, description="Discovered 6 tables"
),
TDataSourceTestStage(
name="temp_schema",
status=TestDataSourceStatus.FAILED,
description='Unable to create table "database"."schema"',
),
TDataSourceTestStage(
name="connection", status=TestDataSourceStatus.SUCCESS, description="Connected to the database"
),
]
self.assertEqual(_test_data_source(api=mock_api, data_source_id=1), expected_results)