def test_shared_dict_addition(value, expect): sp = SchemaParser(shared={"user": {"userid?int(0,9)": "UserID"}}) f = sp.parse({"group": { "$self@user": "******", "age?int(0,100)&default=18": "Age" }}) value = {"group": value} expect = {"group": expect} assert f(value) == expect
def test_shared_dict_addition(value, expect): sp = SchemaParser(shared={"user": {"userid?int(0,9)": "UserID"}}) f = sp.parse( {"group": { "$self@user": "******", "age?int(0,100)&default=18": "Age" }}) value = {"group": value} expect = {"group": expect} assert f(value) == expect
def test_shared_2(value, expect): shared = { "userid": "int(0,9)", "age": "int(10,99)" } sp = SchemaParser(shared=shared) f = sp.parse({ "userid@userid": "UserID", "age@age": "Age" }) assert f(value) == expect
def test_shared_list(value, expect): sp = SchemaParser(shared={"user": {"userid?int(0,9)": "UserID"}}) f = sp.parse(["@user"]) assert f(value) == expect
def test_shared_dict(value, expect): sp = SchemaParser(shared={"user": {"userid?int(0,9)": "UserID"}}) f = sp.parse({"group@user": "******"}) value = {"group": value} expect = {"group": expect} assert f(value) == expect
def test_shared_scalar(value, expect): sp = SchemaParser(shared={"userid": "int(0,9)"}) f = sp.parse({"userid@userid": "UserID"}) assert f(value) == expect
import json import sys from profile import run from timeit import timeit from validater import SchemaParser sp = SchemaParser() value = { "user": {"userid": 5}, "tags": [1, 2, 5] } schema = { "user": {"userid?int(0,9)": "UserID"}, "tags": ["int(0,9)"] } text = json.dumps(value) f = sp.parse(schema) def benchmark(): print("json.loads: %.3f" % timeit("json.loads(text)", globals={"json": json, "text": text})) print("validater: %.3f" % timeit("f(value)", globals={"f": f, "value": value})) def profile(): run("for i in range(10000):\tf(value)") if __name__ == "__main__": if len(sys.argv) > 1 and sys.argv[1] == "-p":
def test_list_shared_fail(value): sp = SchemaParser(shared={"numbers": ["(1,3)&unique", "int(0,9)"]}) f = sp.parse("@numbers") with pytest.raises(Invalid): f(value)
def test_list_shared(value, expect): sp = SchemaParser(shared={"numbers": ["(1,3)&unique", "int(0,9)"]}) f = sp.parse("@numbers") assert f(value) == expect
def test_shared_2(value, expect): shared = {"userid": "int(0,9)", "age": "int(10,99)"} sp = SchemaParser(shared=shared) f = sp.parse({"userid@userid": "UserID", "age@age": "Age"}) assert f(value) == expect
def test_shared_schema_error_position(): shared = {"name": [{"key?xxx": "value"}]} with pytest.raises(SchemaError) as exinfo: SchemaParser(shared=shared) assert exinfo.value.position == "name[].key"
from validater import SchemaError, Invalid, SchemaParser from validater.schema import ValidaterString import pytest import sys sp = SchemaParser() class User: def __init__(self, userid): self.userid = userid validater_string = { "int": { "name": "int" }, "int()": { "name": "int" }, "int(0)": { "name": "int", "args": tuple([0]) }, "int(0,10)": { "name": "int", "args": tuple([0, 10]) }, "int(0, 10)": { "name": "int", "args": tuple([0, 10])