-
Notifications
You must be signed in to change notification settings - Fork 86
/
test_core.py
578 lines (506 loc) · 22.7 KB
/
test_core.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
# -*- coding: utf-8 -*-
""" Unit test for pyKwalify - Core """
# python std lib
import os
# pykwalify imports
import pykwalify
from pykwalify.core import Core
from pykwalify.errors import SchemaError, CoreError
# 3rd party imports
import pytest
from pykwalify.compat import yaml
from testfixtures import compare
class TestCore(object):
def setUp(self):
pykwalify.partial_schemas = {}
def f(self, *args):
return os.path.join(os.path.dirname(os.path.realpath(__file__)), "files", *args)
def test_create_empty_core_object(self, tmpdir):
"""
If createing a core object without any source or schema file an exception should be raised.
"""
with pytest.raises(CoreError) as ex:
Core()
assert "No source file/data was loaded" in str(ex.value)
# To trigger schema exception we must pass in a source file
source_f = tmpdir.join("bar.json")
source_f.write("3.14159")
with pytest.raises(CoreError) as ex:
Core(source_file=str(source_f))
assert "No schema file/data was loaded" in str(ex.value)
def test_load_non_existing_file(self):
file_to_load = "/tmp/foo/bar/barfoo"
assert not os.path.exists(file_to_load), "Following file cannot exists on your system while running these tests : {0}".format(file_to_load)
with pytest.raises(CoreError) as ex:
Core(source_file=file_to_load)
assert "Provided source_file do not exists on disk" in str(ex.value)
def test_load_non_existsing_schema_file(self):
"""
Exception should be raised if the specefied schema file do not exists on disk.
"""
file_to_load = "/tmp/foo/bar/barfoo"
assert not os.path.exists(file_to_load), "Following file cannot exists on your system while running these tests : {0}".format(file_to_load)
with pytest.raises(CoreError) as ex:
Core(schema_files=[file_to_load])
assert "Provided source_file do not exists on disk" in str(ex.value)
def test_load_wrong_schema_files_type(self):
"""
It should only be possible to send in a list type as 'schema_files' object
"""
with pytest.raises(CoreError) as ex:
Core(source_file=None, schema_files={})
assert "schema_files must be of list type" in str(ex.value)
def test_load_json_file(self, tmpdir):
"""
Load source & schema files that has json file ending.
"""
source_f = tmpdir.join("bar.json")
source_f.write("3.14159")
schema_f = tmpdir.join("foo.json")
schema_f.write('{"type": "float"}')
Core(source_file=str(source_f), schema_files=[str(schema_f)])
# TODO: Try to load a non existing json file
def test_load_yaml_files(self, tmpdir):
"""
Load source & schema files that has yaml file ending.
"""
source_f = tmpdir.join("foo.yaml")
source_f.write("3.14159")
schema_f = tmpdir.join("bar.yaml")
schema_f.write("type: float")
Core(source_file=str(source_f), schema_files=[str(schema_f)])
def test_load_unsupported_format(self, tmpdir):
"""
Try to load some fileending that is not supported.
Currently XML is not supported.
"""
source_f = tmpdir.join("foo.xml")
source_f.write("<foo>bar</foo>")
schema_f = tmpdir.join("bar.xml")
schema_f.write("<foo>bar</foo>")
with pytest.raises(CoreError) as ex:
Core(source_file=str(source_f))
assert "Unable to load source_file. Unknown file format of specified file path" in str(ex.value)
with pytest.raises(CoreError) as ex:
Core(schema_files=[str(schema_f)])
assert "Unknown file format. Supported file endings is" in str(ex.value)
def test_load_empty_json_file(self, tmpdir):
"""
Loading an empty json files should raise an exception
"""
# Load empty source file
source_f = tmpdir.join("foo.json")
source_f.write("")
schema_f = tmpdir.join("bar.json")
schema_f.write("")
with pytest.raises(ValueError) as ex:
Core(source_file=str(source_f), schema_files=[str(schema_f)])
# Python 2.7 and Python 3.5 JSON parsers return different exception
# strings for the same data file, so check for both errors strings.
assert ("No JSON object could be decoded" in str(ex.value) or
"Expecting value:" in str(ex.value))
# Load empty schema files
source_f = tmpdir.join("foo.json")
source_f.write("3.14159")
schema_f = tmpdir.join("bar.json")
schema_f.write("")
with pytest.raises(ValueError) as ex:
Core(source_file=str(source_f), schema_files=[str(schema_f)])
assert ("No JSON object could be decoded" in str(ex.value) or
"Expecting value:" in str(ex.value))
def test_load_empty_yaml_file(self, tmpdir):
"""
Loading empty yaml files should raise an exception
"""
# Load empty source file
source_f = tmpdir.join("foo.yaml")
source_f.write("")
schema_f = tmpdir.join("bar.yaml")
schema_f.write("")
# TODO: This is abit buggy because wrong exception is raised...
with pytest.raises(CoreError) as ex:
Core(source_file=str(source_f), schema_files=[str(schema_f)])
# assert "Unable to load any data from source yaml file" in str(ex.value)
# Load empty schema files
source_f = tmpdir.join("foo.yaml")
source_f.write("3.14159")
schema_f = tmpdir.join("bar.yaml")
schema_f.write("")
with pytest.raises(CoreError) as ex:
Core(source_file=str(source_f), schema_files=[str(schema_f)])
assert "No data loaded from file" in str(ex.value)
def test_validation_error_but_not_raise_exception(self):
"""
Test that if 'raise_exception=False' when validating that no exception is raised.
Currently file 2a.yaml & 2b.yaml is designed to cause exception.
"""
c = Core(source_file=self.f("cli", "2a.yaml"), schema_files=[self.f("cli", "2b.yaml")])
c.validate(raise_exception=False)
assert c.validation_errors == [
"Value '1' is not of type 'str'. Path: '/0'", "Value '2' is not of type 'str'. Path: '/1'", "Value '3' is not of type 'str'. Path: '/2'"
]
# TODO: Fix this issue...
# assert ('pykwalify.core', 'ERROR', 'Errors found but will not raise exception...') in l.actual()
def test_core_data_mode(self):
Core(source_data=3.14159, schema_data={"type": "number"}).validate()
Core(source_data="1e-06", schema_data={"type": "float"}).validate()
Core(source_data=3.14159, schema_data={"type": "float"}).validate()
Core(source_data=3, schema_data={"type": "float"}).validate()
Core(source_data=3, schema_data={"type": "int"}).validate()
Core(source_data=True, schema_data={"type": "bool"}).validate()
Core(source_data="foobar", schema_data={"type": "str"}).validate()
Core(source_data="foobar", schema_data={"type": "text"}).validate()
Core(source_data="foobar", schema_data={"type": "any"}).validate()
# Test that 'any' allows types that is not even implemented
def foo():
pass
Core(source_data=foo, schema_data={"type": "any"}).validate()
Core(source_data=lambda x: x, schema_data={"type": "any"}).validate()
with pytest.raises(SchemaError):
Core(source_data="1z-06", schema_data={"type": "float"}).validate()
with pytest.raises(SchemaError):
Core(source_data="abc", schema_data={"type": "number"}).validate()
with pytest.raises(SchemaError):
Core(source_data=3.14159, schema_data={"type": "int"}).validate()
with pytest.raises(SchemaError):
Core(source_data=1337, schema_data={"type": "bool"}).validate()
with pytest.raises(SchemaError):
Core(source_data=1, schema_data={"type": "str"}).validate()
with pytest.raises(SchemaError):
Core(source_data=True, schema_data={"type": "text"}).validate()
def test_multi_file_support(self):
"""
This should test that multiple files is supported correctly
"""
pass_tests = [
# Test that include directive can be used at top level of the schema
(
[
self.f("partial_schemas", "1s-schema.yaml"),
self.f("partial_schemas", "1s-partials.yaml"),
],
self.f("partial_schemas", "1s-data.yaml"),
{
'sequence': [{'include': 'fooone'}],
'type': 'seq',
}
),
# # This test that include directive works inside sequence
# ([self.f("33a.yaml"), self.f("33b.yaml")], self.f("33c.yaml"), {'sequence': [{'include': 'fooone'}], 'type': 'seq'}),
# This test recursive schemas
(
[
self.f("partial_schemas", "2s-schema.yaml"),
self.f("partial_schemas", "2s-partials.yaml"),
],
self.f("partial_schemas", "2s-data.yaml"),
{
'sequence': [{'include': 'fooone'}],
'type': 'seq',
}
),
# This tests that you can include a partial schema alongside other rules in a map
(
[
self.f("partial_schemas", "7s-schema.yaml"),
],
self.f("partial_schemas", "7s-data.yaml"),
{
'type': 'map',
'mapping': {
'foo': {
'type': 'str',
'required': True
},
'bar': {
'include': 'bar'
}
}
}
)
]
failing_tests = [
# Test include inside partial schema
(
[
self.f("partial_schemas", "1f-schema.yaml"),
self.f("partial_schemas", "1f-partials.yaml")
],
self.f("partial_schemas", "1f-data.yaml"),
SchemaError,
["Cannot find partial schema with name 'fooonez'. Existing partial schemas: 'bar, fooone, foothree, footwo'. Path: '/0'"]
),
(
[
self.f('partial_schemas', '2f-schema.yaml')
],
self.f('partial_schemas', '2f-data.yaml'),
SchemaError,
["Value 'True' is not of type 'str'. Path: '/0'"]
),
(
[
self.f('partial_schemas', '3f-schema.yaml')
],
self.f('partial_schemas', '3f-data.yaml'),
SchemaError,
["Value 'True' is not of type 'str'. Path: ''"]
),
(
[
self.f('partial_schemas', '4f-schema.yaml')
],
self.f('partial_schemas', '4f-data.yaml'),
SchemaError,
["Value 'True' is not of type 'str'. Path: '/0/foo/0/bar'"]
),
(
[
self.f('partial_schemas', '5f-schema.yaml')
],
self.f('partial_schemas', '5f-data.yaml'),
SchemaError,
["Value 'True' is not of type 'str'. Path: '/0/0/0/0'"]
),
(
[
self.f('partial_schemas', '6f-schema.yaml')
],
self.f('partial_schemas', '6f-data.yaml'),
SchemaError,
["Value 'True' is not of type 'str'. Path: '/foo/bar/qwe/ewq'"]
)
]
for passing_test in pass_tests:
try:
c = Core(source_file=passing_test[1], schema_files=passing_test[0])
c.validate()
compare(c.validation_errors, [], prefix="No validation errors should exist...")
except Exception as e:
print("ERROR RUNNING FILE: {0} : {1}".format(passing_test[0], passing_test[1]))
raise e
# This serve as an extra schema validation that tests more complex structures then testrule.py do
compare(c.root_rule.schema_str, passing_test[2], prefix="Parsed rules is not correct, something have changed...")
for failing_test in failing_tests:
print("Test files: {0} : {1}".format(", ".join(failing_test[0]), failing_test[1]))
with pytest.raises(failing_test[2]):
c = Core(schema_files=failing_test[0], source_file=failing_test[1])
c.validate()
if not c.validation_errors:
raise AssertionError("No validation_errors was raised...")
compare(
sorted(c.validation_errors),
sorted(failing_test[3]),
prefix="Wrong validation errors when parsing files : {0} : {1}".format(
failing_test[0],
failing_test[1],
),
)
def test_core_files(self):
# These tests should pass with no exception raised
pass_tests = [
# All tests for keyword assert
"test_assert.yaml",
# All tests for keyword default
"test_default.yaml",
# All tests for keyword desc
"test_desc.yaml",
# All tests for keyword enum
"test_enum.yaml",
# All tests for keyword example
"test_example.yaml",
# All tests for keyword extensions
"test_extensions.yaml",
# All tests for keyword func
"test_func.yaml",
# All tests for keyword ident
"test_ident.yaml",
# All tests for keyword include
"test_include.yaml",
# All tests for keyword length
"test_length.yaml",
# All tests for keyword mapping
"test_mapping.yaml",
# All tests for keyword matching
"test_matching.yaml",
# All tests for keyword name
"test_name.yaml",
# All tests for keyword nullable
"test_nullable.yaml",
# All tests for keyword pattern
"test_pattern.yaml",
# All tests for keyword range
"test_range.yaml",
# All tests for keyword required
"test_required.yaml",
# All tests for keyword schema
"test_schema.yaml",
# All tests for keyword sequence
"test_sequence.yaml",
# All tests for keyword unique
"test_unique.yaml",
# All tests for keyword version
"test_version.yaml",
# All test cases for Multiple sequence checks
"test_sequence_multi.yaml",
# All test cases for merging
"test_merge.yaml",
# All test cases for yaml anchors
"test_anchor.yaml",
# All tests for TYPE: any
"test_type_any.yaml",
# All tests for TYPE: bool
"test_type_bool.yaml",
# All tests for TYPE: date
"test_type_date.yaml",
# All tests for TYPE: enum
"test_type_enum.yaml",
# All tests for TYPE: float
"test_type_float.yaml",
# All tests for TYPE: int
"test_type_int.yaml",
# All tests for TYPE: map
"test_type_map.yaml",
# All tests for TYPE: none
"test_type_none.yaml",
# All tests for TYPE: number
"test_type_number.yaml",
# All tests for TYPE: scalar
"test_type_scalar.yaml",
# All tests for TYPE: seq
"test_type_seq.yaml",
# All tests for TYPE: str
"test_type_str.yaml",
# All tests for TYPE: symbol
"test_type_symbol.yaml",
# All tests for TYPE: text
"test_type_text.yaml",
# All tests for TYPE: timestamp
"test_type_timestamp.yaml",
# All tests for TYPE: email
"test_type_email.yaml",
]
_fail_tests = [
# All tests for keyword assert
("test_assert.yaml", SchemaError),
# All tests for keyword default
("test_default.yaml", SchemaError),
# All tests for keyword desc
("test_desc.yaml", SchemaError),
# All tests for keyword enum
("test_enum.yaml", SchemaError),
# All tests for keyword example
("test_example.yaml", SchemaError),
# All tests for keyword extensions
("test_extensions.yaml", SchemaError),
# All tests for keyword func
("test_func.yaml", SchemaError),
# All tests for keyword ident
("test_ident.yaml", SchemaError),
# All tests for keyword include
("test_include.yaml", SchemaError),
# All tests for keyword length
("test_length.yaml", SchemaError),
# All tests for keyword mapping
("test_mapping.yaml", SchemaError),
# All tests for keyword matching
("test_matching.yaml", SchemaError),
# All tests for keyword name
("test_name.yaml", SchemaError),
# All tests for keyword nullable
("test_nullable.yaml", SchemaError),
# All tests for keyword pattern
("test_pattern.yaml", SchemaError),
# All tests for keyword range
("test_range.yaml", SchemaError),
# All tests for keyword required
("test_required.yaml", SchemaError),
# All tests for keyword schema
("test_schema.yaml", SchemaError),
# All tests for keyword sequence
("test_sequence.yaml", SchemaError),
# All tests for keyword unique
("test_unique.yaml", SchemaError),
# All tests for keyword version
("test_version.yaml", SchemaError),
# All test cases for Multiple sequence checks
("test_sequence_multi.yaml", SchemaError),
# All test cases for merging
("test_merge.yaml", SchemaError),
# All test cases for yaml anchors
("test_anchor.yaml", SchemaError),
# All tests for TYPE: any
("test_type_any.yaml", SchemaError),
# All tests for TYPE: bool
("test_type_bool.yaml", SchemaError),
# All tests for TYPE: date
("test_type_date.yaml", SchemaError),
# All tests for TYPE: float
("test_type_float.yaml", SchemaError),
# All tests for TYPE: int
("test_type_int.yaml", SchemaError),
# All tests for TYPE: map
("test_type_map.yaml", SchemaError),
# All tests for TYPE: none
("test_type_none.yaml", SchemaError),
# All tests for TYPE: number
("test_type_number.yaml", SchemaError),
# All tests for TYPE: scalar
("test_type_scalar.yaml", SchemaError),
# All tests for TYPE: seq
("test_type_seq.yaml", SchemaError),
# All tests for TYPE: str
("test_type_str.yaml", SchemaError),
# All tests for TYPE: symbol
("test_type_symbol.yaml", SchemaError),
# All tests for TYPE: text
("test_type_text.yaml", SchemaError),
# All tests for TYPE: timestamp
("test_type_timestamp.yaml", SchemaError),
# All tests for TYPE: email
("test_type_email.yaml", SchemaError),
]
# Add override magic to make it easier to test a specific file
if "S" in os.environ:
pass_tests = [os.environ["S"]]
_fail_tests = []
elif "F" in os.environ:
pass_tests = []
_fail_tests = [(os.environ["F"], SchemaError)]
for passing_test_file in pass_tests:
f = self.f(os.path.join("success", passing_test_file))
with open(f, "r") as stream:
yaml_data = yaml.safe_load_all(stream)
for document_index, document in enumerate(yaml_data):
data = document["data"]
schema = document["schema"]
try:
print("Running test files: {0}".format(f))
c = Core(source_data=data, schema_data=schema, strict_rule_validation=True, allow_assertions=True)
c.validate()
compare(c.validation_errors, [], prefix="No validation errors should exist...")
except Exception as e:
print("ERROR RUNNING FILES: {0} : {1}:{2}".format(f, document_index, document.get('name', 'UNKNOWN')))
raise e
# This serve as an extra schema validation that tests more complex structures then testrule.py do
compare(c.root_rule.schema_str, schema, prefix="Parsed rules is not correct, something have changed... files : {0} : {1}".format(f, document_index))
for failing_test, exception_type in _fail_tests:
f = self.f(os.path.join("fail", failing_test))
with open(f, "r") as stream:
yaml_data = yaml.safe_load_all(stream)
for document_index, document in enumerate(yaml_data):
data = document["data"]
schema = document["schema"]
errors = document.get("errors", [])
try:
print("Running test files: {0}".format(f))
c = Core(source_data=data, schema_data=schema, strict_rule_validation=True, allow_assertions=True)
c.validate()
except exception_type as e:
pass
else:
print("ERROR RUNNING FILES: {0} : {1}:{2}".format(f, document_index, document.get('name', 'UNKNOWN')))
raise AssertionError("Exception {0} not raised as expected... FILES: {1} : {2} : {3}:{4}".format(
exception_type, exception_type, failing_test, document_index, document.get('name', 'UNKNOWN')))
compare(sorted(c.validation_errors), sorted(errors), prefix="Wrong validation errors when parsing files : {0} : {1} : {2}".format(
f, document_index, document.get('name', 'UNKNOWN')))