forked from core-api/python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_array.py
More file actions
94 lines (71 loc) · 3.01 KB
/
Copy pathtest_array.py
File metadata and controls
94 lines (71 loc) · 3.01 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
from coreapi.typesys import Array, String, Integer, ValidationError
import pytest
def test_array_type():
schema = Array()
assert schema.validate([]) == []
assert schema.validate(['a', 1]) == ['a', 1]
with pytest.raises(ValidationError) as exc:
schema.validate(1)
assert exc.value.detail == 'Must be an array.'
def test_array_items():
schema = Array(items=String())
assert schema.validate([]) == []
assert schema.validate(['a', 'b', 'c']) == ['a', 'b', 'c']
with pytest.raises(ValidationError) as exc:
schema.validate(['a', 'b', 123])
assert exc.value.detail == {2: 'Must be a string.'}
def test_array_items_as_list():
schema = Array(items=[String(), Integer()])
assert schema.validate([]) == []
assert schema.validate(['a', 123]) == ['a', 123]
with pytest.raises(ValidationError) as exc:
schema.validate(['a', 'b'])
assert exc.value.detail == {1: 'Must be a number.'}
def test_array_max_items():
schema = Array(max_items=2)
assert schema.validate([1, 2]) == [1, 2]
with pytest.raises(ValidationError) as exc:
schema.validate([1, 2, 3])
assert exc.value.detail == 'Must have no more than 2 items.'
def test_array_min_items():
schema = Array(min_items=2)
assert schema.validate([1, 2]) == [1, 2]
with pytest.raises(ValidationError) as exc:
schema.validate([1])
assert exc.value.detail == 'Must have at least 2 items.'
def test_array_empty():
schema = Array(min_items=1)
assert schema.validate([1]) == [1]
with pytest.raises(ValidationError) as exc:
schema.validate([])
assert exc.value.detail == 'Must not be empty.'
def test_array_null():
schema = Array(allow_null=True)
assert schema.validate(None) is None
schema = Array()
with pytest.raises(ValidationError) as exc:
schema.validate(None)
assert exc.value.detail == 'May not be null.'
def test_array_exact_count():
schema = Array(min_items=3, max_items=3)
assert schema.validate([1, 2, 3]) == [1, 2, 3]
with pytest.raises(ValidationError) as exc:
schema.validate([1, 2, 3, 4])
assert exc.value.detail == 'Must have 3 items.'
def test_array_unique_items():
schema = Array(unique_items=True)
assert schema.validate([1, 2, 3]) == [1, 2, 3]
with pytest.raises(ValidationError) as exc:
schema.validate([1, 2, 1])
assert exc.value.detail == {2: 'This item is not unique.'}
def test_array_additional_items_disallowed():
schema = Array(items=[String(), Integer()])
assert schema.validate(['a', 123, True]) == ['a', 123, True]
schema = Array(items=[String(), Integer()], additional_items=False)
with pytest.raises(ValidationError) as exc:
schema.validate(['a', 123, True])
assert exc.value.detail == 'May not contain additional items.'
schema = Array(items=[String(), Integer()], additional_items=Integer())
with pytest.raises(ValidationError) as exc:
schema.validate(['a', 123, 'c'])
assert exc.value.detail == {2: 'Must be a number.'}