-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathtest_export_great_expectations.py
604 lines (540 loc) · 18.9 KB
/
test_export_great_expectations.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
import json
from typing import Any, Dict
import pytest
from typer.testing import CliRunner
from datacontract.cli import app
from datacontract.export.great_expectations_converter import to_great_expectations
from datacontract.lint import resolve
from datacontract.model.data_contract_specification import DataContractSpecification
from datacontract.model.exceptions import DataContractException
# logging.basicConfig(level=logging.DEBUG, force=True)
def test_cli():
runner = CliRunner()
result = runner.invoke(
app,
[
"export",
"./fixtures/export/datacontract.yaml",
"--format",
"great-expectations",
],
)
assert result.exit_code == 0
def test_cli_multi_models():
"""
Test with 2 model definitions in the contract with the model parameter
"""
runner = CliRunner()
result = runner.invoke(
app,
[
"export",
"./fixtures/export/rdf/datacontract-complex.yaml",
"--format",
"great-expectations",
"--model",
"orders",
],
)
assert result.exit_code == 0
def test_cli_multi_models_failed():
"""
Test with 2 model definitions in the contract without the model parameter
"""
runner = CliRunner()
result = runner.invoke(
app,
[
"export",
"./fixtures/export/rdf/datacontract-complex.yaml",
"--format",
"great-expectations",
],
)
assert result.exit_code == 1
@pytest.fixture
def data_contract_basic() -> DataContractSpecification:
return DataContractSpecification.from_file("fixtures/export/datacontract.yaml")
@pytest.fixture
def data_contract_complex() -> DataContractSpecification:
return DataContractSpecification.from_file("fixtures/export/rdf/datacontract-complex.yaml")
@pytest.fixture
def data_contract_great_expectations() -> DataContractSpecification:
return resolve.resolve_data_contract_from_location(
"./fixtures/great-expectations/datacontract.yaml", inline_quality=True
)
@pytest.fixture
def data_contract_great_expectations_quality_file() -> DataContractSpecification:
return resolve.resolve_data_contract_from_location(
"./fixtures/great-expectations/datacontract_quality_file.yaml",
inline_quality=True,
)
@pytest.fixture
def expected_json_suite() -> Dict[str, Any]:
return {
"data_asset_type": "null",
"expectation_suite_name": "orders.1.0.0",
"expectations": [
{
"expectation_type": "expect_table_columns_to_match_ordered_list",
"kwargs": {"column_list": ["order_id", "processed_timestamp"]},
"meta": {},
},
{
"expectation_type": "expect_column_values_to_be_of_type",
"kwargs": {"column": "order_id", "type_": "string"},
"meta": {},
},
{
"expectation_type": "expect_column_values_to_be_of_type",
"kwargs": {"column": "processed_timestamp", "type_": "timestamp"},
"meta": {},
},
{
"expectation_type": "expect_table_row_count_to_be_between",
"kwargs": {"min_value": 10},
"meta": {},
},
],
"meta": {},
}
@pytest.fixture
def expected_spark_engine() -> Dict[str, Any]:
return {
"data_asset_type": "null",
"expectation_suite_name": "orders.1.0.0",
"expectations": [
{
"expectation_type": "expect_table_columns_to_match_ordered_list",
"kwargs": {"column_list": ["order_id", "processed_timestamp"]},
"meta": {},
},
{
"expectation_type": "expect_column_values_to_be_of_type",
"kwargs": {"column": "order_id", "type_": "StringType"},
"meta": {},
},
{
"expectation_type": "expect_column_values_to_be_of_type",
"kwargs": {"column": "processed_timestamp", "type_": "TimestampType"},
"meta": {},
},
{
"expectation_type": "expect_table_row_count_to_be_between",
"kwargs": {"min_value": 10},
"meta": {},
},
],
"meta": {},
}
@pytest.fixture
def expected_pandas_engine() -> Dict[str, Any]:
return {
"data_asset_type": "null",
"expectation_suite_name": "orders.1.0.0",
"expectations": [
{
"expectation_type": "expect_table_columns_to_match_ordered_list",
"kwargs": {"column_list": ["order_id", "processed_timestamp"]},
"meta": {},
},
{
"expectation_type": "expect_column_values_to_be_of_type",
"kwargs": {"column": "order_id", "type_": "str"},
"meta": {},
},
{
"expectation_type": "expect_column_values_to_be_of_type",
"kwargs": {"column": "processed_timestamp", "type_": "datetime64[ns]"},
"meta": {},
},
{
"expectation_type": "expect_table_row_count_to_be_between",
"kwargs": {"min_value": 10},
"meta": {},
},
],
"meta": {},
}
@pytest.fixture
def expected_sql_engine() -> Dict[str, Any]:
return {
"data_asset_type": "null",
"expectation_suite_name": "orders.1.0.0",
"expectations": [
{
"expectation_type": "expect_table_columns_to_match_ordered_list",
"kwargs": {"column_list": ["order_id", "processed_timestamp"]},
"meta": {},
},
{
"expectation_type": "expect_column_values_to_be_of_type",
"kwargs": {"column": "order_id", "type_": "STRING"},
"meta": {},
},
{
"expectation_type": "expect_column_values_to_be_of_type",
"kwargs": {"column": "processed_timestamp", "type_": "TIMESTAMP_TZ"},
"meta": {},
},
{
"expectation_type": "expect_table_row_count_to_be_between",
"kwargs": {"min_value": 10},
"meta": {},
},
],
"meta": {},
}
@pytest.fixture
def expected_sql_trino_engine() -> Dict[str, Any]:
return {
"data_asset_type": "null",
"expectation_suite_name": "orders.1.0.0",
"expectations": [
{
"expectation_type": "expect_table_columns_to_match_ordered_list",
"kwargs": {"column_list": ["order_id", "processed_timestamp"]},
"meta": {},
},
{
"expectation_type": "expect_column_values_to_be_of_type",
"kwargs": {"column": "order_id", "type_": "varchar"},
"meta": {},
},
{
"expectation_type": "expect_column_values_to_be_of_type",
"kwargs": {
"column": "processed_timestamp",
"type_": "timestamp(3) with time zone",
},
"meta": {},
},
{
"expectation_type": "expect_table_row_count_to_be_between",
"kwargs": {"min_value": 10},
"meta": {},
},
],
"meta": {},
}
def test_to_great_expectation(data_contract_basic: DataContractSpecification):
expected_json_suite = {
"data_asset_type": "null",
"expectation_suite_name": "orders.1.0.0",
"expectations": [
{
"expectation_type": "expect_table_columns_to_match_ordered_list",
"kwargs": {"column_list": ["order_id", "order_total", "order_status"]},
"meta": {},
},
{
"expectation_type": "expect_column_values_to_be_of_type",
"kwargs": {"column": "order_id", "type_": "varchar"},
"meta": {},
},
{
"expectation_type": "expect_column_values_to_be_unique",
"kwargs": {"column": "order_id"},
"meta": {},
},
{
"expectation_type": "expect_column_value_lengths_to_be_between",
"kwargs": {"column": "order_id", "min_value": 8, "max_value": 10},
"meta": {},
},
{
"expectation_type": "expect_column_values_to_be_of_type",
"kwargs": {"column": "order_total", "type_": "bigint"},
"meta": {},
},
{
"expectation_type": "expect_column_values_to_be_between",
"kwargs": {
"column": "order_total",
"min_value": 0,
"max_value": 1000000,
},
"meta": {},
},
{
"expectation_type": "expect_column_values_to_be_of_type",
"kwargs": {"column": "order_status", "type_": "text"},
"meta": {},
},
],
"meta": {},
}
result_orders = to_great_expectations(data_contract_basic, "orders")
assert result_orders == json.dumps(expected_json_suite, indent=2)
def test_to_great_expectation_complex(data_contract_complex: DataContractSpecification):
"""
Test with 2 model definitions in the contract
"""
expected_orders = {
"data_asset_type": "null",
"expectation_suite_name": "orders.1.0.0",
"expectations": [
{
"expectation_type": "expect_table_columns_to_match_ordered_list",
"kwargs": {
"column_list": [
"order_id",
"order_timestamp",
"order_total",
"customer_id",
"customer_email_address",
]
},
"meta": {},
},
{
"expectation_type": "expect_column_values_to_be_unique",
"kwargs": {"column": "order_id"},
"meta": {},
},
{
"expectation_type": "expect_column_values_to_be_of_type",
"kwargs": {"column": "order_timestamp", "type_": "timestamp"},
"meta": {},
},
{
"expectation_type": "expect_column_values_to_be_of_type",
"kwargs": {"column": "order_total", "type_": "long"},
"meta": {},
},
{
"expectation_type": "expect_column_values_to_be_of_type",
"kwargs": {"column": "customer_id", "type_": "text"},
"meta": {},
},
{
"expectation_type": "expect_column_value_lengths_to_be_between",
"kwargs": {"column": "customer_id", "min_value": 10, "max_value": 20},
"meta": {},
},
{
"expectation_type": "expect_column_values_to_be_of_type",
"kwargs": {"column": "customer_email_address", "type_": "text"},
"meta": {},
},
],
"meta": {},
}
expected_line_items = {
"data_asset_type": "null",
"expectation_suite_name": "line_items.1.0.0",
"expectations": [
{
"expectation_type": "expect_table_columns_to_match_ordered_list",
"kwargs": {"column_list": ["lines_item_id", "order_id", "sku"]},
"meta": {},
},
{
"expectation_type": "expect_column_values_to_be_of_type",
"kwargs": {"column": "lines_item_id", "type_": "text"},
"meta": {},
},
{
"expectation_type": "expect_column_values_to_be_unique",
"kwargs": {"column": "lines_item_id"},
"meta": {},
},
],
"meta": {},
}
result_orders = to_great_expectations(data_contract_complex, "orders")
assert result_orders == json.dumps(expected_orders, indent=2)
result_line_items = to_great_expectations(data_contract_complex, "line_items")
assert result_line_items == json.dumps(expected_line_items, indent=2)
def test_to_great_expectation_quality(
data_contract_great_expectations: DataContractSpecification,
expected_json_suite: Dict[str, Any],
):
"""
Test with Quality definition in the contract
"""
result = to_great_expectations(data_contract_great_expectations, "orders")
assert result == json.dumps(expected_json_suite, indent=2)
def test_to_great_expectation_custom_name(
data_contract_great_expectations: DataContractSpecification,
):
"""
Test with Quality definition in the contract
"""
expected = {
"data_asset_type": "null",
"expectation_suite_name": "my_expectation_suite_name",
"expectations": [
{
"expectation_type": "expect_table_columns_to_match_ordered_list",
"kwargs": {"column_list": ["order_id", "processed_timestamp"]},
"meta": {},
},
{
"expectation_type": "expect_column_values_to_be_of_type",
"kwargs": {"column": "order_id", "type_": "string"},
"meta": {},
},
{
"expectation_type": "expect_column_values_to_be_of_type",
"kwargs": {"column": "processed_timestamp", "type_": "timestamp"},
"meta": {},
},
{
"expectation_type": "expect_table_row_count_to_be_between",
"kwargs": {"min_value": 10},
"meta": {},
},
],
"meta": {},
}
result = to_great_expectations(
data_contract_great_expectations,
model_key="orders",
expectation_suite_name="my_expectation_suite_name",
)
assert result == json.dumps(expected, indent=2)
def test_to_great_expectation_engine_spark(
data_contract_great_expectations: DataContractSpecification,
expected_spark_engine: Dict[str, Any],
):
"""
Test with Spark engine
"""
result = to_great_expectations(
data_contract_great_expectations,
model_key="orders",
engine="spark",
)
assert result == json.dumps(expected_spark_engine, indent=2)
def test_to_great_expectation_engine_pandas(
data_contract_great_expectations: DataContractSpecification,
expected_pandas_engine: Dict[str, Any],
):
"""
Test with pandas engine
"""
result = to_great_expectations(
data_contract_great_expectations,
model_key="orders",
engine="pandas",
)
assert result == json.dumps(expected_pandas_engine, indent=2)
def test_to_great_expectation_engine_sql(
data_contract_great_expectations: DataContractSpecification,
expected_sql_engine: Dict[str, Any],
):
"""
Test with sql engine
"""
result = to_great_expectations(
data_contract_great_expectations,
model_key="orders",
engine="sql",
)
assert result == json.dumps(expected_sql_engine, indent=2)
def test_to_great_expectation_engine_sql_trino(
data_contract_great_expectations: DataContractSpecification,
expected_sql_trino_engine: Dict[str, Any],
):
"""
Test with sql engine and sql server trino trino
"""
result = to_great_expectations(
data_contract_great_expectations,
model_key="orders",
engine="sql",
sql_server_type="trino",
)
assert result == json.dumps(expected_sql_trino_engine, indent=2)
def test_to_great_expectation_quality_json_file(
data_contract_great_expectations_quality_file: DataContractSpecification,
expected_json_suite: Dict[str, Any],
):
"""
Test with Quality definition in a json file
"""
result = to_great_expectations(data_contract_great_expectations_quality_file, "orders")
assert result == json.dumps(expected_json_suite, indent=2)
def test_cli_with_quality_file(expected_json_suite: Dict[str, Any]):
runner = CliRunner()
result = runner.invoke(
app,
[
"export",
"./fixtures/great-expectations/datacontract_quality_file.yaml",
"--format",
"great-expectations",
],
)
assert result.output.replace("\n", "") == json.dumps(expected_json_suite, indent=2).replace("\n", "")
def test_cli_with_spark_engine(expected_spark_engine: Dict[str, Any]):
runner = CliRunner()
result = runner.invoke(
app,
[
"export",
"./fixtures/great-expectations/datacontract.yaml",
"--format",
"great-expectations",
"--engine",
"spark",
],
)
assert result.output.replace("\n", "") == json.dumps(expected_spark_engine, indent=2).replace("\n", "")
def test_cli_with_pandas_engine(expected_pandas_engine: Dict[str, Any]):
runner = CliRunner()
result = runner.invoke(
app,
[
"export",
"./fixtures/great-expectations/datacontract.yaml",
"--format",
"great-expectations",
"--engine",
"pandas",
],
)
assert result.output.replace("\n", "") == json.dumps(expected_pandas_engine, indent=2).replace("\n", "")
def test_cli_with_sql_engine(expected_sql_engine: Dict[str, Any]):
runner = CliRunner()
result = runner.invoke(
app,
[
"export",
"./fixtures/great-expectations/datacontract.yaml",
"--format",
"great-expectations",
"--engine",
"sql",
],
)
assert result.output.replace("\n", "") == json.dumps(expected_sql_engine, indent=2).replace("\n", "")
def test_cli_with_sql_trino_engine(expected_sql_trino_engine: Dict[str, Any]):
runner = CliRunner()
result = runner.invoke(
app,
[
"export",
"./fixtures/great-expectations/datacontract.yaml",
"--format",
"great-expectations",
"--engine",
"sql",
"--sql-server-type",
"trino",
],
)
assert result.output.replace("\n", "") == json.dumps(expected_sql_trino_engine, indent=2).replace("\n", "")
def test_to_great_expectation_missing_quality_json_file():
"""
Test failed with missing Quality definition in a json file
"""
try:
resolve.resolve_data_contract_from_location(
"./fixtures/great-expectations/datacontract_missing_quality_file.yaml",
inline_quality=True,
)
assert False
except DataContractException as dataContractException:
assert dataContractException.reason == "Cannot resolve reference ./fixtures/great-expectations/missing.json"