Repository navigation
Expand file tree
/
Copy pathtest_validator.py
More file actions
104 lines (84 loc) · 3.74 KB
/
Copy pathtest_validator.py
File metadata and controls
104 lines (84 loc) · 3.74 KB
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
# -*- coding: utf-8 -*-
# :Project: python-rapidjson -- Validator class tests
# :Author: Lele Gaifax <lele@metapensiero.it>
# :License: MIT License
# :Copyright: © 2017, 2019, 2020, 2024 Lele Gaifax
#
import pytest
import rapidjson as rj
def test_invalid_schema():
pytest.raises(rj.JSONDecodeError, rj.Validator, '')
pytest.raises(rj.JSONDecodeError, rj.Validator, '"')
def test_invalid_json():
validate = rj.Validator('""')
pytest.raises(rj.JSONDecodeError, validate, '')
pytest.raises(rj.JSONDecodeError, validate, '"')
@pytest.mark.parametrize('schema,json', (
('{ "type": ["number", "string"] }', '42'),
('{ "type": ["number", "string"] }', '"Life, the universe, and everything"'),
))
def test_valid(schema, json):
for s in (schema, schema.encode('utf-8'), bytearray(schema.encode('utf-8'))):
for j in (json, json.encode('utf-8'), bytearray(json.encode('utf-8'))):
validate = rj.Validator(s)
validate(j)
@pytest.mark.parametrize('schema,json,details', (
('{ "type": ["number", "string"] }',
'["Life", "the universe", "and everything"]',
('type', '#', '#'),
),
))
def test_invalid(schema, json, details):
validate = rj.Validator(schema)
with pytest.raises(ValueError) as error:
validate(json)
assert error.value.args == details
# See: https://spacetelescope.github.io/understanding-json-schema/reference/object.html#pattern-properties
@pytest.mark.parametrize('schema', [
rj.dumps({
"type": "object",
"patternProperties": {
"^S_": { "type": "string" },
"^I_": { "type": "integer" }
},
"additionalProperties": False
}),
])
@pytest.mark.parametrize('json', [
'{"I_0": 23}',
'{"S_1": "the quick brown fox jumps over the lazy dog"}',
pytest.param('{"I_2": "A string"}', marks=pytest.mark.xfail),
pytest.param('{"keyword": "value"}', marks=pytest.mark.xfail),
])
def test_additional_and_pattern_properties_valid(schema, json):
validate = rj.Validator(schema)
validate(json)
@pytest.mark.parametrize('convert', (str, lambda s: s.encode('utf-8'),
lambda s: bytearray(s.encode('utf-8'))))
@pytest.mark.parametrize('suffix', ('\0', '\0garbage', ' \n\0{}'))
@pytest.mark.parametrize('root', ('{}', '{"type":"integer"}', '{"title":"café"}'))
def test_schema_with_embedded_nul(convert, suffix, root):
with pytest.raises(rj.JSONDecodeError, match='Invalid JSON'):
rj.Validator(convert(root + suffix))
@pytest.mark.parametrize('convert', (str, lambda s: s.encode('utf-8'),
lambda s: bytearray(s.encode('utf-8'))))
@pytest.mark.parametrize('suffix', ('\0', '\0garbage', ' \n\0{}'))
@pytest.mark.parametrize('root', ('123', '[]', '{"key":"café"}'))
def test_document_with_embedded_nul(convert, suffix, root):
validate = rj.Validator('{}')
with pytest.raises(rj.JSONDecodeError, match='Invalid JSON'):
validate(convert(root + suffix))
@pytest.mark.parametrize('convert', (str, lambda s: s.encode('utf-8'),
lambda s: bytearray(s.encode('utf-8'))))
def test_escaped_nul_remains_valid(convert):
validate = rj.Validator(convert(rj.dumps({"type": "string", "enum": ["a\0b"]})))
validate(convert(rj.dumps("a\0b")))
with pytest.raises(rj.ValidationError):
validate(rj.dumps("other"))
@pytest.mark.parametrize('convert', (str, lambda s: s.encode('utf-8'),
lambda s: bytearray(s.encode('utf-8'))))
def test_trailing_json_whitespace(convert):
validate = rj.Validator(convert('{"type":"integer"} \n\t\r'))
validate(convert('123 \n\t\r'))
with pytest.raises(rj.ValidationError):
validate(convert('"wrong type" \n\t\r'))