-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_validation_input.py
367 lines (304 loc) · 11.5 KB
/
test_validation_input.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
import json
from unittest.mock import patch
import pytest
from tests import TEST_DATA_FOLDER
from src.dq_suite import validation_input
from src.dq_suite.validation_input import (
filter_validation_dict_by_table_name,
get_data_quality_rules_dict,
read_data_quality_rules_from_json,
validate_data_quality_rules_dict,
validate_dataset,
validate_rule,
validate_rules_dict,
validate_table_schema,
validate_tables,
)
@pytest.fixture
def real_json_file_path():
return f"{TEST_DATA_FOLDER}/dq_rules.json"
@pytest.fixture
def real_non_json_file_path():
return f"{TEST_DATA_FOLDER}/test_schema.py"
@pytest.fixture
def real_json_file_invalid_formatting_path():
return f"{TEST_DATA_FOLDER}/dq_rules_invalid_formatting.json"
@pytest.fixture
def data_quality_rules_json_string(real_json_file_path):
return read_data_quality_rules_from_json(file_path=real_json_file_path)
@pytest.fixture
def data_quality_rules_dict(data_quality_rules_json_string):
return json.loads(data_quality_rules_json_string)
@pytest.fixture
def validated_data_quality_rules_dict(data_quality_rules_dict):
"""
Note: the normal flow validates the data quality rules dict before
using it - hence this fixture.
"""
validate_data_quality_rules_dict(
data_quality_rules_dict=data_quality_rules_dict
)
return data_quality_rules_dict
@pytest.fixture
def rules_dict(data_quality_rules_dict):
return data_quality_rules_dict["tables"][0]
@pytest.mark.usefixtures("real_json_file_path")
@pytest.mark.usefixtures("real_non_json_file_path")
class TestReadDataQualityRulesFromJson:
def test_read_data_quality_rules_from_json_raises_type_error(
self,
):
with pytest.raises(TypeError):
read_data_quality_rules_from_json(file_path=123)
def test_read_data_quality_rules_from_json_raises_file_not_found_error(
self,
):
with pytest.raises(FileNotFoundError):
read_data_quality_rules_from_json(file_path="nonexistent_file_path")
def test_read_data_quality_rules_from_json_raises_value_error_for_non_json_file(
self, real_non_json_file_path
):
non_json_string = read_data_quality_rules_from_json(
file_path=real_non_json_file_path
)
assert isinstance(non_json_string, str) # It's a string...
with pytest.raises(ValueError): # ... but not valid JSON
json.loads(non_json_string)
def test_read_data_quality_rules_from_json_returns_json_string(
self, real_json_file_path
):
data_quality_rules_json_string = read_data_quality_rules_from_json(
file_path=real_json_file_path
)
assert isinstance(data_quality_rules_json_string, str)
@pytest.mark.usefixtures("data_quality_rules_dict")
class TestValidateDataQualityRulesDict:
def test_validate_data_quality_rules_dict_raises_type_error(
self,
):
with pytest.raises(TypeError):
validate_data_quality_rules_dict(
data_quality_rules_dict="wrong_type"
)
def test_rules_dict_without_rules_field_results_in_table_schema_validation(
self, data_quality_rules_dict
):
with patch.object(
target=validation_input,
attribute="validate_table_schema",
) as validate_table_schema_mock:
validate_data_quality_rules_dict(
data_quality_rules_dict=data_quality_rules_dict
)
validate_table_schema_mock.assert_called_once()
def test_validate_data_quality_rules_dict(self, data_quality_rules_dict):
validate_data_quality_rules_dict(
data_quality_rules_dict=data_quality_rules_dict
)
@pytest.mark.usefixtures("data_quality_rules_dict")
class TestValidateDataSet:
def test_validate_dataset_without_dataset_key_raises_key_error(
self,
):
with pytest.raises(KeyError):
validate_dataset(
data_quality_rules_dict={"some_other_thing": "with_some_value"}
)
def test_validate_dataset_without_dict_typed_value_raises_type_error(
self,
):
with pytest.raises(TypeError):
validate_dataset(
data_quality_rules_dict={"dataset": "with_some_value"}
)
def test_validate_dataset_without_name_key_raises_key_error(
self,
):
with pytest.raises(KeyError):
validate_dataset(data_quality_rules_dict={"dataset": dict()})
def test_validate_dataset_without_layer_key_raises_key_error(
self,
):
with pytest.raises(KeyError):
validate_dataset(data_quality_rules_dict={"dataset": {"name": 123}})
def test_validate_dataset_without_str_typed_name_raises_type_error(
self,
):
with pytest.raises(TypeError):
validate_dataset(
data_quality_rules_dict={"dataset": {"name": 123, "layer": 456}}
)
def test_validate_dataset_without_str_typed_layer_raises_type_error(
self,
):
with pytest.raises(TypeError):
validate_dataset(
data_quality_rules_dict={
"dataset": {"name": "the_dataset_name", "layer": 456}
}
)
def test_validate_dataset_works_as_expected(self, data_quality_rules_dict):
validate_dataset(data_quality_rules_dict=data_quality_rules_dict)
@pytest.mark.usefixtures("data_quality_rules_dict")
class TestValidateTables:
def test_validate_tables_without_tables_key_raises_key_error(
self,
):
with pytest.raises(KeyError):
validate_tables(
data_quality_rules_dict={"some_other_thing": "with_some_value"}
)
def test_validate_tables_without_list_typed_value_raises_type_error(
self,
):
with pytest.raises(TypeError):
validate_tables(
data_quality_rules_dict={"tables": "with_some_value"}
)
def test_validate_tables_works_as_expected(self, data_quality_rules_dict):
validate_tables(data_quality_rules_dict=data_quality_rules_dict)
@pytest.mark.usefixtures("rules_dict")
class TestValidateRulesDict:
def test_validate_rules_dict_without_dict_typed_value_raises_type_error(
self,
):
with pytest.raises(TypeError):
validate_rules_dict(rules_dict="not_a_dict")
def test_validate_rules_dict_without_unique_identifier_key_raises_key_error(
self,
):
with pytest.raises(KeyError):
validate_rules_dict(rules_dict=dict())
def test_validate_rules_dict_without_table_name_key_raises_key_error(
self,
):
with pytest.raises(KeyError):
validate_rules_dict(rules_dict={"unique_identifier": 123})
def test_validate_rules_dict_without_rules_key_raises_key_error(
self,
):
with pytest.raises(KeyError):
validate_rules_dict(
rules_dict={"unique_identifier": 123, "table_name": 456}
)
def test_validate_rules_dict_without_list_typed_rules_raises_type_error(
self,
):
with pytest.raises(TypeError):
validate_rules_dict(
rules_dict={
"unique_identifier": 123,
"table_name": 456,
"rules": 789,
}
)
def test_validate_rules_dict_works_as_expected(self, rules_dict):
validate_rules_dict(rules_dict=rules_dict)
class TestValidateTableSchema:
def test_validate_table_schema_without_validate_table_schema_key_raises_key_error(
self,
):
with pytest.raises(KeyError):
validate_table_schema(rules_dict=dict())
def test_validate_table_schema_without_validate_table_schema_url_key_raises_key_error(
self,
):
with pytest.raises(KeyError):
validate_table_schema(
rules_dict={"validate_table_schema": "some_table_name"}
)
def test_validate_table_schema_with_invalid_url_raises_value_error(self):
with pytest.raises(ValueError):
validate_table_schema(
rules_dict={
"validate_table_schema": "some_table_name",
"validate_table_schema_url": "some_invalid_url",
}
)
def test_validate_table_schema_works_as_expected(
self,
):
validate_table_schema(
rules_dict={
"validate_table_schema": "some_table_name",
"validate_table_schema_url": "https://www.someurl.nl",
}
)
class TestValidateRule:
def test_validate_rule_without_dict_typed_rule_raises_type_error(
self,
):
with pytest.raises(TypeError):
validate_rule(rule="not_a_dict")
def test_validate_rule_without_rule_name_key_raises_key_error(
self,
):
with pytest.raises(KeyError):
validate_rule(rule=dict())
def test_validate_rule_without_parameters_key_raises_key_error(
self,
):
with pytest.raises(KeyError):
validate_rule(rule={"rule_name": 123})
def test_validate_rule_without_string_typed_rule_name_raises_type_error(
self,
):
with pytest.raises(TypeError):
validate_rule(rule={"rule_name": 123, "parameters": 456})
def test_validate_rule_without_pascal_case_rule_name_raises_value_error(
self,
):
with pytest.raises(ValueError):
validate_rule(rule={"rule_name": "the_rule", "parameters": 456})
def test_validate_rule_without_dict_typed_parameters_raises_type_error(
self,
):
with pytest.raises(TypeError):
validate_rule(rule={"rule_name": "TheRule", "parameters": 456})
def test_validate_rule_works_as_expected(
self,
):
validate_rule(
rule={
"rule_name": "TheRule",
"parameters": {"some_key": "some_value"},
}
)
@pytest.mark.usefixtures("real_json_file_invalid_formatting_path")
@pytest.mark.usefixtures("real_json_file_path")
class TestGetDataQualityRulesDict:
def test_get_data_quality_rules_dict_raises_json_decode_error(
self, real_json_file_invalid_formatting_path
):
with pytest.raises(json.JSONDecodeError):
get_data_quality_rules_dict(
file_path=real_json_file_invalid_formatting_path
)
def test_get_data_quality_rules_dict_returns_dict(
self, real_json_file_path
):
data_quality_rules_dict = get_data_quality_rules_dict(
file_path=real_json_file_path
)
assert isinstance(data_quality_rules_dict, dict)
@pytest.mark.usefixtures("validated_data_quality_rules_dict")
class TestFilterValidationDictByTableName:
def test_filter_validation_dict_by_table_name_returns_none_for_nonexistent_table(
self, validated_data_quality_rules_dict
):
the_nonexistent_table_dict = filter_validation_dict_by_table_name(
validation_dict=validated_data_quality_rules_dict,
table_name="the_nonexistent_table",
)
assert the_nonexistent_table_dict is None
def test_filter_validation_dict_by_table_name(
self, validated_data_quality_rules_dict
):
the_table_dict = filter_validation_dict_by_table_name(
validation_dict=validated_data_quality_rules_dict,
table_name="the_table",
)
assert the_table_dict is not None
assert "unique_identifier" in the_table_dict
assert "table_name" in the_table_dict
assert "rules" in the_table_dict