forked from cwacek/python-jsonschema-objects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_array_validation.py
More file actions
106 lines (81 loc) · 2.64 KB
/
Copy pathtest_array_validation.py
File metadata and controls
106 lines (81 loc) · 2.64 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
105
106
import pytest
import python_jsonschema_objects as pjo
@pytest.fixture
def arrayClass():
schema = {
"title": "ArrayVal",
"type": "object",
"properties": {
"min": {
"type": "array",
"items": {"type": "string"},
"default": [],
"minItems": 1,
},
"max": {
"type": "array",
"items": {"type": "string"},
"default": [],
"maxItems": 1,
},
"both": {
"type": "array",
"items": {"type": "string"},
"default": [],
"maxItems": 2,
"minItems": 1,
},
"unique": {
"type": "array",
"items": {"type": "string"},
"default": [],
"uniqueItems": True,
},
"reffed": {
"type": "array",
"items": {"$ref": "#/definitions/myref"},
"minItems": 1,
},
},
"definitions": {"myref": {"type": "string"}},
}
ns = pjo.ObjectBuilder(schema).build_classes()
return ns["Arrayval"](min=["1"], both=["1"])
def test_validators_work_with_reference(arrayClass):
arrayClass.reffed = ["foo"]
with pytest.raises(pjo.ValidationError):
arrayClass.reffed = []
def test_array_length_validates(markdown_examples):
builder = pjo.ObjectBuilder(
markdown_examples["Example Schema"], resolved=markdown_examples
)
ns = builder.build_classes()
with pytest.raises(pjo.ValidationError):
ns.ExampleSchema(
firstName="Fred",
lastName="Huckstable",
dogs=["Fido", "Spot", "Jasper", "Lady", "Tramp"],
)
def test_minitems(arrayClass):
arrayClass.min = ["1"]
arrayClass.min.append("2")
with pytest.raises(pjo.ValidationError):
arrayClass.min = []
def test_maxitems(arrayClass):
arrayClass.max = []
arrayClass.max.append("2")
assert arrayClass.max == ["2"]
with pytest.raises(pjo.ValidationError):
arrayClass.max.append("3")
# You have to explicitly validate with append
arrayClass.validate()
with pytest.raises(pjo.ValidationError):
arrayClass.max = ["45", "42"]
def test_unique(arrayClass):
arrayClass.unique = ["hi", "there"]
with pytest.raises(pjo.ValidationError):
arrayClass.unique.append("hi")
# You have to explicitly validate with append
arrayClass.validate()
with pytest.raises(pjo.ValidationError):
arrayClass.unique = ["Fred", "Fred"]