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
|
import voluptuous as vol
from voluptuous_serialize import UNSUPPORTED, convert
def test_int_schema():
for value in int, vol.Coerce(int):
assert {'type': 'integer'} == convert(vol.Schema(value))
def test_str_schema():
for value in str, vol.Coerce(str):
assert {'type': 'string'} == convert(vol.Schema(value))
def test_float_schema():
for value in float, vol.Coerce(float):
assert {'type': 'float'} == convert(vol.Schema(value))
def test_bool_schema():
for value in bool, vol.Coerce(bool):
assert {'type': 'boolean'} == convert(vol.Schema(value))
def test_integer_clamp():
assert {
'type': 'integer',
'valueMin': 100,
'valueMax': 1000,
} == convert(vol.Schema(
vol.All(vol.Coerce(int),
vol.Clamp(min=100, max=1000))))
def test_length():
assert {
'type': 'string',
'lengthMin': 100,
'lengthMax': 1000,
} == convert(vol.Schema(
vol.All(vol.Coerce(str),
vol.Length(min=100, max=1000))))
def test_datetime():
assert {
'type': 'datetime',
'format': '%Y-%m-%dT%H:%M:%S.%fZ',
} == convert(vol.Schema(vol.Datetime()))
def test_in():
assert {
'type': 'select',
'options': [
('beer', 'beer'),
('wine', 'wine'),
],
} == convert(vol.Schema(vol.In(['beer', 'wine'])))
def test_in_dict():
assert {
'type': 'select',
'options': [
('en_US', 'American English'),
('zh_CN', 'Chinese (Simplified)'),
],
} == convert(vol.Schema(vol.In(
{'en_US': 'American English', 'zh_CN': 'Chinese (Simplified)'})))
def test_dict():
assert [
{
'name': 'name',
'type': 'string',
'lengthMin': 5,
'required': True,
},
{
'name': 'age',
'type': 'integer',
'valueMin': 18,
'required': True,
},
{
'name': 'hobby',
'type': 'string',
'default': 'not specified',
'optional': True,
}
] == convert(vol.Schema({
vol.Required('name'): vol.All(str, vol.Length(min=5)),
vol.Required('age'): vol.All(vol.Coerce(int), vol.Range(min=18)),
vol.Optional('hobby', default='not specified'): str
}))
def test_marker_description():
assert [{
'name': 'name',
'type': 'string',
'description': 'Description of name',
'required': True,
}] == convert(vol.Schema({
vol.Required('name', description='Description of name'): str,
}))
def test_lower():
assert {
'type': 'string',
'lower': True,
} == convert(vol.Schema(vol.All(vol.Lower, str)))
def test_upper():
assert {
'type': 'string',
'upper': True,
} == convert(vol.Schema(vol.All(vol.Upper, str)))
def test_capitalize():
assert {
'type': 'string',
'capitalize': True,
} == convert(vol.Schema(vol.All(vol.Capitalize, str)))
def test_title():
assert {
'type': 'string',
'title': True,
} == convert(vol.Schema(vol.All(vol.Title, str)))
def test_strip():
assert {
'type': 'string',
'strip': True,
} == convert(vol.Schema(vol.All(vol.Strip, str)))
def test_email():
assert {
'type': 'string',
'format': 'email',
} == convert(vol.Schema(vol.All(vol.Email, str)))
def test_url():
assert {
'type': 'string',
'format': 'url',
} == convert(vol.Schema(vol.All(vol.Url, str)))
def test_fqdnurl():
assert {
'type': 'string',
'format': 'fqdnurl',
} == convert(vol.Schema(vol.All(vol.FqdnUrl, str)))
def test_custom_serializer():
def custem_serializer(schema):
if schema is str:
return {'type': 'a string!'}
return UNSUPPORTED
assert {
'type': 'a string!',
'upper': True,
} == convert(vol.Schema(vol.All(
vol.Upper, str)),
custom_serializer=custem_serializer
)
def test_constant():
for value in True, False, "Hello", 1:
assert {
'type': 'constant', 'value': value
} == convert(vol.Schema(value))
|