forked from alecthomas/voluptuous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
79 lines (65 loc) · 2.56 KB
/
tests.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
from nose.tools import assert_equal
import voluptuous
from voluptuous import Schema, Required, Extra, Invalid, In, Remove
def test_required():
"""Verify that Required works."""
schema = Schema({Required('q'): 1})
# Can't use nose's raises (because we need to access the raised
# exception, nor assert_raises which fails with Python 2.6.9.
try:
schema({})
except Invalid as e:
assert_equal(str(e), "required key not provided @ data['q']")
else:
assert False, "Did not raise Invalid"
def test_extra_with_required():
"""Verify that Required does not break Extra."""
schema = Schema({Required('toaster'): str, Extra: object})
r = schema({'toaster': 'blue', 'another_valid_key': 'another_valid_value'})
assert_equal(
r, {'toaster': 'blue', 'another_valid_key': 'another_valid_value'})
def test_iterate_candidates():
"""Verify that the order for iterating over mapping candidates is right."""
schema = {
"toaster": str,
Extra: object,
}
# toaster should be first.
assert_equal(voluptuous._iterate_mapping_candidates(schema)[0][0],
'toaster')
def test_in():
"""Verify that In works."""
schema = Schema({"color": In(frozenset(["blue", "red", "yellow"]))})
schema({"color": "blue"})
def test_remove():
"""Verify that Remove works."""
# remove dict keys
schema = Schema({"weight": int,
Remove("color"): str,
Remove("amount"): int})
out_ = schema({"weight": 10, "color": "red", "amount": 1})
assert "color" not in out_ and "amount" not in out_
# remove keys by type
schema = Schema({"weight": float,
"amount": int,
# remvove str keys with int values
Remove(str): int,
# keep str keys with str values
str: str})
out_ = schema({"weight": 73.4,
"condition": "new",
"amount": 5,
"left": 2})
# amount should stay since it's defined
# other string keys with int values will be removed
assert "amount" in out_ and "left" not in out_
# string keys with string values will stay
assert "condition" in out_
# remove value from list
schema = Schema([Remove(1), int])
out_ = schema([1, 2, 3, 4, 1, 5, 6, 1, 1, 1])
assert_equal(out_, [2, 3, 4, 5, 6])
# remove values from list by type
schema = Schema([1.0, Remove(float), int])
out_ = schema([1, 2, 1.0, 2.0, 3.0, 4])
assert_equal(out_, [1, 2, 1.0, 4])