1
1
from pathlib import Path
2
+ from jsonschema import ValidationError
2
3
3
4
import pytest
4
5
from dpytools .validation .json .validation import validate_json_schema
@@ -12,9 +13,9 @@ def test_validate_json_schema_data_path():
12
13
pipeline_config = "tests/test_cases/pipeline_config.json"
13
14
assert (
14
15
validate_json_schema (
15
- pipeline_config_schema ,
16
- pipeline_config ,
17
- "Validating pipeline_config.json" ,
16
+ schema_path = pipeline_config_schema ,
17
+ data_path = pipeline_config ,
18
+ error_msg = "Validating pipeline_config.json" ,
18
19
)
19
20
is None
20
21
)
@@ -35,22 +36,74 @@ def test_validate_json_schema_data_dict():
35
36
}
36
37
assert (
37
38
validate_json_schema (
38
- pipeline_config_schema ,
39
- pipeline_config ,
40
- "Validating pipeline_config dict" ,
39
+ schema_path = pipeline_config_schema ,
40
+ data_dict = pipeline_config ,
41
+ error_msg = "Validating pipeline_config dict" ,
41
42
)
42
43
is None
43
44
)
44
45
45
46
46
- def test_validate_json_schema_invalid_data_format ():
47
+ def test_validate_json_schema_data_dict_and_data_path ():
48
+ pipeline_config_schema = "tests/test_cases/pipeline_config_schema.json"
49
+ pipeline_config_path = "tests/test_cases/pipeline_config.json"
50
+ pipeline_config_dict = {
51
+ "schema" : "airflow.schemas.ingress.sdmx.v1.schema.json" ,
52
+ "required_files" : [{"matches" : "*.sdmx" , "count" : 1 }],
53
+ "supplementary_distributions" : [{"matches" : "*.sdmx" , "count" : 1 }],
54
+ "priority" : 1 ,
55
+
56
+ "pipeline" : "default" ,
57
+ }
58
+ with pytest .raises (ValueError ) as err :
59
+ validate_json_schema (
60
+ schema_path = pipeline_config_schema ,
61
+ data_dict = pipeline_config_dict ,
62
+ data_path = pipeline_config_path ,
63
+ )
64
+ assert (
65
+ "Both a dictionary and file path of data have been provided - please specify either one or the other, not both."
66
+ in str (err .value )
67
+ )
68
+
69
+
70
+ def test_validate_json_schema_no_data_dict_or_data_path ():
71
+ pipeline_config_schema = "tests/test_cases/pipeline_config_schema.json"
72
+
73
+ with pytest .raises (ValueError ) as err :
74
+ validate_json_schema (
75
+ schema_path = pipeline_config_schema ,
76
+ )
77
+ assert (
78
+ "Please provide either a dictionary or a file path of the data to be validated against the schema."
79
+ in str (err .value )
80
+ )
81
+
82
+
83
+ def test_validate_json_schema_invalid_data_path_format ():
84
+ """
85
+ Raise ValueError if data_path is not a file path
86
+ """
87
+ pipeline_config_schema = "tests/test_cases/pipeline_config_schema.json"
88
+ pipeline_config = ["Invalid" , "data" , "format" ]
89
+ with pytest .raises (ValueError ) as err :
90
+ validate_json_schema (
91
+ schema_path = pipeline_config_schema , data_path = pipeline_config
92
+ )
93
+ assert "Invalid data format" in str (err .value )
94
+
95
+
96
+ def test_validate_json_schema_invalid_data_dict_format ():
47
97
"""
48
- Raise ValueError if data is not a file path or dictionary
98
+ Raise ValueError if data_dict is not a dictionary
49
99
"""
50
100
pipeline_config_schema = "tests/test_cases/pipeline_config_schema.json"
51
101
pipeline_config = ["Invalid" , "data" , "format" ]
52
- with pytest .raises (ValueError ):
53
- validate_json_schema (pipeline_config_schema , pipeline_config )
102
+ with pytest .raises (ValueError ) as err :
103
+ validate_json_schema (
104
+ schema_path = pipeline_config_schema , data_dict = pipeline_config
105
+ )
106
+ assert "Invalid data format" in str (err .value )
54
107
55
108
56
109
def test_validate_json_schema_url ():
@@ -59,8 +112,33 @@ def test_validate_json_schema_url():
59
112
"""
60
113
pipeline_config_schema = "http://example.org"
61
114
pipeline_config = "tests/test_cases/pipeline_config.json"
62
- with pytest .raises (NotImplementedError ):
63
- validate_json_schema (pipeline_config_schema , pipeline_config )
115
+ with pytest .raises (NotImplementedError ) as err :
116
+ validate_json_schema (
117
+ schema_path = pipeline_config_schema , data_path = pipeline_config
118
+ )
119
+ assert "Validation from remote schema not yet supported" in str (err .value )
120
+
121
+
122
+ def test_validate_json_schema_invalid_schema_path ():
123
+ pipeline_config_schema = "tests/test_cases/does_not_exist.json"
124
+ pipeline_config = "tests/test_cases/pipeline_config.json"
125
+ schema_path = Path (pipeline_config_schema ).absolute ()
126
+ with pytest .raises (ValueError ) as err :
127
+ validate_json_schema (
128
+ schema_path = pipeline_config_schema , data_path = pipeline_config
129
+ )
130
+ assert f"Schema path '{ schema_path } ' does not exist" in str (err .value )
131
+
132
+
133
+ def test_validate_json_schema_invalid_data_path ():
134
+ pipeline_config_schema = "tests/test_cases/pipeline_config_schema.json"
135
+ pipeline_config = "tests/test_cases/does_not_exist.json"
136
+ data_path = Path (pipeline_config ).absolute ()
137
+ with pytest .raises (ValueError ) as err :
138
+ validate_json_schema (
139
+ schema_path = pipeline_config_schema , data_path = pipeline_config
140
+ )
141
+ assert f"Data path '{ data_path } ' does not exist" in str (err .value )
64
142
65
143
66
144
def test_validate_json_schema_data_path_required_field_missing ():
@@ -69,12 +147,13 @@ def test_validate_json_schema_data_path_required_field_missing():
69
147
"""
70
148
pipeline_config_schema = "tests/test_cases/pipeline_config_schema.json"
71
149
pipeline_config = "tests/test_cases/pipeline_config_missing_required_field.json"
72
- err = validate_json_schema (
73
- pipeline_config_schema ,
74
- pipeline_config ,
75
- "Validating pipeline_config_missing_required_field.json" ,
76
- )
77
- assert err .message == "'priority' is a required property"
150
+ with pytest .raises (ValidationError ) as err :
151
+ validate_json_schema (
152
+ schema_path = pipeline_config_schema ,
153
+ data_path = pipeline_config ,
154
+ error_msg = "Error validating pipeline_config_missing_required_field.json" ,
155
+ )
156
+ assert "'priority' is a required property" in str (err .value )
78
157
79
158
80
159
def test_validate_json_schema_data_path_invalid_data_type ():
@@ -83,12 +162,13 @@ def test_validate_json_schema_data_path_invalid_data_type():
83
162
"""
84
163
pipeline_config_schema = "tests/test_cases/pipeline_config_schema.json"
85
164
pipeline_config = "tests/test_cases/pipeline_config_invalid_data_type.json"
86
- err = validate_json_schema (
87
- pipeline_config_schema ,
88
- pipeline_config ,
89
- "Validating pipeline_config_invalid_data_type.json" ,
90
- )
91
- assert err .message == "'1' is not of type 'integer'"
165
+ with pytest .raises (ValidationError ) as err :
166
+ validate_json_schema (
167
+ schema_path = pipeline_config_schema ,
168
+ data_path = pipeline_config ,
169
+ error_msg = "Error validating pipeline_config_invalid_data_type.json" ,
170
+ )
171
+ assert "'1' is not of type 'integer'" in str (err .value )
92
172
93
173
94
174
def test_validate_json_schema_data_dict_required_field_missing ():
@@ -103,12 +183,13 @@ def test_validate_json_schema_data_dict_required_field_missing():
103
183
104
184
"pipeline" : "default" ,
105
185
}
106
- err = validate_json_schema (
107
- pipeline_config_schema ,
108
- pipeline_config ,
109
- "Validating pipeline_config with required field missing" ,
110
- )
111
- assert err .message == "'priority' is a required property"
186
+ with pytest .raises (ValidationError ) as err :
187
+ validate_json_schema (
188
+ schema_path = pipeline_config_schema ,
189
+ data_dict = pipeline_config ,
190
+ error_msg = "Error validating pipeline_config with required field missing" ,
191
+ )
192
+ assert "'priority' is a required property" in str (err .value )
112
193
113
194
114
195
def test_validate_json_schema_data_dict_invalid_data_type ():
@@ -124,9 +205,10 @@ def test_validate_json_schema_data_dict_invalid_data_type():
124
205
125
206
"pipeline" : "default" ,
126
207
}
127
- err = validate_json_schema (
128
- pipeline_config_schema ,
129
- pipeline_config ,
130
- f"Validating pipeline_config dict with invalid data type" ,
131
- )
132
- assert err .message == "'1' is not of type 'integer'"
208
+ with pytest .raises (ValidationError ) as err :
209
+ validate_json_schema (
210
+ schema_path = pipeline_config_schema ,
211
+ data_dict = pipeline_config ,
212
+ error_msg = "Error validating pipeline_config dict with invalid data type" ,
213
+ )
214
+ assert "'1' is not of type 'integer'" in str (err .value )
0 commit comments