Esempio n. 1
0
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
Esempio n. 2
0
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
Esempio n. 3
0
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
Esempio n. 4
0
def test_shared_list(value, expect):
    sp = SchemaParser(shared={"user": {"userid?int(0,9)": "UserID"}})
    f = sp.parse(["@user"])
    assert f(value) == expect
Esempio n. 5
0
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
Esempio n. 6
0
def test_shared_scalar(value, expect):
    sp = SchemaParser(shared={"userid": "int(0,9)"})
    f = sp.parse({"userid@userid": "UserID"})
    assert f(value) == expect
Esempio n. 7
0
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":
Esempio n. 8
0
def test_shared_scalar(value, expect):
    sp = SchemaParser(shared={"userid": "int(0,9)"})
    f = sp.parse({"userid@userid": "UserID"})
    assert f(value) == expect
Esempio n. 9
0
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)
Esempio n. 10
0
def test_list_shared(value, expect):
    sp = SchemaParser(shared={"numbers": ["(1,3)&unique", "int(0,9)"]})
    f = sp.parse("@numbers")
    assert f(value) == expect
Esempio n. 11
0
def test_shared_list(value, expect):
    sp = SchemaParser(shared={"user": {"userid?int(0,9)": "UserID"}})
    f = sp.parse(["@user"])
    assert f(value) == expect
Esempio n. 12
0
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
Esempio n. 13
0
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
Esempio n. 14
0
def test_list_shared(value, expect):
    sp = SchemaParser(shared={"numbers": ["(1,3)&unique", "int(0,9)"]})
    f = sp.parse("@numbers")
    assert f(value) == expect
Esempio n. 15
0
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"
Esempio n. 16
0
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)
Esempio n. 17
0
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])