本文整理汇总了Python中valideer.parse函数的典型用法代码示例。如果您正苦于以下问题:Python parse函数的具体用法?Python parse怎么用?Python parse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_parsing_required_properties
def test_parsing_required_properties(self):
get_schema = lambda: {
"foo": V.Nullable("number"),
"?nested": [V.Nullable({
"baz": "string"
})]
}
valid = [{"foo": 3, "nested": [None]}]
missing_properties = [{}, {"foo": 3, "nested": [{}]}]
for _ in xrange(3):
with V.parsing(required_properties=False):
self._testValidation(get_schema(),
valid=valid + missing_properties)
with V.parsing(required_properties=True):
self._testValidation(get_schema(),
valid=valid, invalid=missing_properties)
# gotcha: calling parse() with required_properties=True is not
# equivalent to the above call because the V.Nullable() calls in
# get_schema have already called implicitly parse() without parameters.
if V.Object.REQUIRED_PROPERTIES:
self._testValidation(V.parse(get_schema(), required_properties=True),
invalid=[missing_properties[1]])
else:
self._testValidation(V.parse(get_schema(), required_properties=True),
valid=[missing_properties[1]])
示例2: test_ignore_optional_property_errors_parse_parameter
def test_ignore_optional_property_errors_parse_parameter(self):
schema = {
"+foo": "number",
"?bar": "boolean",
"?nested": [{
"+baz": "string",
"?zoo": "number",
}]
}
invalid_required = [
{"foo": "2", "bar": True},
]
invalid_optional = [
{"foo": 3, "bar": "nan"},
{"foo": 3.1, "nested": [{"baz": "x", "zoo": "12"}]},
{"foo": 0, "nested": [{"baz": 1, "zoo": 2}]},
]
adapted = [
{"foo": 3},
{"foo": 3.1, "nested": [{"baz": "x"}]},
{"foo": 0},
]
for _ in xrange(3):
self._testValidation(V.parse(schema, ignore_optional_property_errors=False),
invalid=invalid_required + invalid_optional)
self._testValidation(V.parse(schema, ignore_optional_property_errors=True),
invalid=invalid_required,
adapted=zip(invalid_optional, adapted))
示例3: test_where_arrangement
def test_where_arrangement(self):
q = Query()
q.select("a")
q.where("a", "a > 10")
q.where("b", "b > 20")
q.where("c", "c > 30")
q.tables("from t")
q.into(False)
# no column `d`
self.assertRaises(valideer.ValidationError, q, valideer.parse({"where": "where"}).validate({"where": "(d|b)&c"}))
self.assertEqual(q(valideer.parse({"where": "where"}).validate({"where": "(a|b)&c"})),
"select a from t where ((a > 10 or b > 20) and c > 30)")
示例4: test_required_properties_parse_parameter
def test_required_properties_parse_parameter(self):
schema = {
"foo": "number",
"?bar": "boolean",
"?nested": [{
"baz": "string"
}]
}
missing_properties = [{}, {"bar": True}, {"foo": 3, "nested": [{}]}]
for _ in xrange(3):
self._testValidation(V.parse(schema, required_properties=True),
invalid=missing_properties)
self._testValidation(V.parse(schema, required_properties=False),
valid=missing_properties)
示例5: test_where_multi_arg
def test_where_multi_arg(self):
q = Query()
q.select("a")
q.where("a", "a=1")
q.where("b", "b=2")
q.where("b", "b=3")
q.tables("from t")
q.into(False)
# no column `col_1`
self.assertRaises(valideer.ValidationError, q, valideer.parse({"where": "where"}).validate({"where": "a|b|c"}))
self.assertEqual(q(valideer.parse({"where": "where"}).validate({"where": "a|b"})),
"select a from t where (a=1 or (b=2 and b=3))")
示例6: validated
def validated(arguments=None, body=None, extra_arguments=True, extra_body=False):
if type(body) in (dict, str):
body = parse(body, additional_properties=extra_body)
elif body not in (None, False):
raise ValueError('body must be type None, False, or dict')
if type(arguments) is dict:
arguments = parse(arguments, additional_properties=extra_arguments)
elif arguments not in (None, False):
raise ValueError('arguments must be type None, False, or dict')
def wrapper(method):
@wraps(method)
def validate(self, *args, **kwargs):
# ------------------
# Validate Body Data
# ------------------
if body:
try:
_body = json_decode(self.request.body) if self.request.body else {}
except:
# ex. key1=value2&key2=value2
try:
_body = dict([(k, v[0] if len(v) == 1 else v) for k, v in parse_qs(self.request.body, strict_parsing=True).items()])
except:
raise HTTPError(400, "body was not able to be decoded")
kwargs['body'] = body.validate(_body, adapt=True)
elif body is False and self.request.body:
raise HTTPError(400, reason='No body arguments allowed')
# -------------------
# Validate URL Params
# -------------------
if arguments:
# include url arguments
if self.request.query_arguments:
_arguments = dict([(k, v[0] if len(v) == 1 else v) for k, v in self.request.query_arguments.items() if v != [''] and k[0] != '_'])
else:
_arguments = {}
kwargs["arguments"] = arguments.validate(_arguments)
elif arguments is False and self.request.query_arguments and not any(map(lambda a: a[0] == '_', self.request.query_arguments)):
raise HTTPError(400, reason='No url arguments allowed')
return method(self, *args, **kwargs)
return validate
return wrapper
示例7: test_branch
def test_branch(self):
schema = valideer.parse({"branch": "branch"})
for x in ('master', 'stable', 'something/thisoem', '-aples', '.dev', 'builds,/[email protected]#$%&*'):
assert schema.validate(dict(branch=x))
for x in (False, None):
self.assertRaises(valideer.ValidationError, schema.validate, dict(branch=x))
示例8: test_file
def test_file(self):
schema = valideer.parse({"value": "file"})
for handler in ('app/base.py', 'codecov.sh'):
assert schema.validate(dict(value=handler))
for invalid in ('not/a/path', '**#^@', '../black'):
self.assertRaises(valideer.ValidationError, schema.validate, dict(value=invalid))
示例9: test_email
def test_email(self):
schema = valideer.parse({"email": "email"})
for email in ('def post(self, @example.com', '[email protected]', '[email protected]'):
self.assertEqual(schema.validate(dict(email=email))['email'], email.lower())
for email in ('needsanat', '[email protected]'):
self.assertRaises(error, schema.validate, dict(email=email))
示例10: test_ref
def test_ref(self):
schema = valideer.parse({"value": "ref"})
self.assertEqual(schema.validate(dict(value="a"*40))['value'], "a"*40)
self.assertEqual(schema.validate(dict(value="apples"))['value'], "apples")
for x in (None, object(), int, -50):
self.assertRaises(error, schema.validate, dict(value=x))
示例11: test_id
def test_id(self):
schema = valideer.parse({"value": "id"})
self.assertEqual(schema.validate(dict(value=1))['value'], 1)
self.assertEqual(schema.validate(dict(value="50"))['value'], 50)
for x in ('not', None, 'random', object(), int, -50, "19.123"):
self.assertRaises(error, schema.validate, dict(value=x))
示例12: test_timezone
def test_timezone(self):
schema = valideer.parse({"value": "timezone"})
for key, value in timezone.timezones.items():
self.assertEqual(schema.validate(dict(value=key))['value'], value)
for x in ('not', None, 'random', object(), int):
self.assertRaises(error, schema.validate, dict(value=x))
示例13: test_handler
def test_handler(self):
schema = valideer.parse({"value": "handler"})
for handler in ('valid-handler', 'someothername', '__hello.world-ok'):
assert schema.validate(dict(value=handler))
for invalid in "[email protected]#{$%^&*(),<>?/'\";:[]{}\\|~`+":
self.assertRaises(valideer.ValidationError, schema.validate, dict(value='characters'+invalid))
self.assertRaises(valideer.ValidationError, schema.validate, dict(value=None))
示例14: test_version
def test_version(self):
schema = valideer.parse({"value": "version"})
for version in ('0.0.1', '0.0.10', '0.2.10', '1.2.1'):
assert schema.validate(dict(value=version))
self.assertRaises(valideer.ValidationError, schema.validate, dict(value='-124.12.51'))
self.assertRaises(valideer.ValidationError, schema.validate, dict(value='_something-odd'))
self.assertRaises(valideer.ValidationError, schema.validate, dict(value=None))
示例15: test_day
def test_day(self):
schema = valideer.parse({"day": "day"})
for (d, v) in ((0, "sun"), (1, "mon"), (2, "tue"), (3, "wed"), (4, "thu"), (5, "fri"), (6, "sat")):
self.assertEqual(schema.validate(dict(day=d))['day'], d)
self.assertEqual(schema.validate(dict(day=v))['day'], d)
for day in ('needsanat', 'nodom'):
self.assertRaises(error, schema.validate, dict(day=day))