Beispiel #1
0
def test_validate_union_nested():
    schema = Union[int, str, Dict[str, Union[int, str]]]
    d = validate(schema, "3")
    assert d.data == "3"
    d = validate(schema, 3)
    assert d.data == 3
    d = validate(schema, dict(foo=3, bla="ciao"))
    assert d.data == dict(foo=3, bla="ciao")
Beispiel #2
0
def test_json_list(db):
    Randoms = dataclass_from_table("Randoms", db.randoms)
    fields = field_dict(Randoms)
    assert fields["jsonlist"].type is typing.List
    d = validate(Randoms, dict(jsonlist="jhgjg"))
    assert d.errors["jsonlist"] == "expected a sequence"
    d = validate(Randoms, dict(jsonlist=["bla", "foo"]))
    assert not d.errors
Beispiel #3
0
def test_validate_union():
    schema = Union[int, str]
    d = validate(schema, "3")
    assert d.data == "3"
    d = validate(schema, 3)
    assert d.data == 3
    d = validate(schema, 3.3)
    assert d.errors
Beispiel #4
0
def test_convert_random(db):
    Randoms = dataclass_from_table("Randoms", db.randoms)
    assert Randoms
    fields = field_dict(Randoms)
    assert isinstance(fields["id"].metadata[VALIDATOR], UUIDValidator)
    d = validate(Randoms, dict(info="jhgjg"))
    assert d.errors["info"] == "expected an object"
    d = validate(Randoms, dict(info=dict(a="3", b="test")))
    assert not d.errors
Beispiel #5
0
def test_validate(db):
    Tasks = dataclass_from_table("Tasks", db.tasks, exclude=("id",))
    d = validate(Tasks, dict(title="test"))
    assert not d.errors
    d = validate(Tasks, dict(title="te"))
    assert d.errors["title"] == "Too short"
    d = validate(Tasks, dict(title="t" * 100))
    assert d.errors["title"] == "Too long"
    d = validate(Tasks, dict(title=40))
    assert d.errors["title"] == "Must be a string"
Beispiel #6
0
def test_foo():
    assert validate(Foo, {}).errors
    assert validate(Foo, dict(text="ciao")).errors
    assert validate(Foo, dict(text="ciao"),
                    strict=False).data == dict(text="ciao")
    valid = dict(text="ciao", param=3)
    assert validate(Foo, valid).data == dict(text="ciao", param=3, done=False)
    d = validated_schema(List[Foo], [valid])
    assert len(d) == 1
    assert isinstance(d[0], Foo)
Beispiel #7
0
def test_date(db):
    Randoms = dataclass_from_table("Randoms", db.randoms)
    d = validate(Randoms, dict(randomdate="jhgjg"))
    assert d.errors["randomdate"] == "jhgjg not valid format"
    d = validate(Randoms, dict(randomdate=date.today()))
    assert not d.errors
    v = dump(Randoms, d.data)
    assert v["randomdate"] == date.today().isoformat()
    v = dump(Randoms, {"randomdate": datetime.now()})
    assert v["randomdate"] == date.today().isoformat()
    v = dump(Randoms, {"randomdate": date.today().isoformat()})
    assert v["randomdate"] == date.today().isoformat()
Beispiel #8
0
def test_validate_list():
    data = [dict(paths=["bla"], methods=["get"], body=dict(a="test"))]
    d = validate(List[Permission], data)
    assert not d.errors
    assert isinstance(d.data, list)
Beispiel #9
0
def test_post_process():
    d = validate(Moon, {})
    assert d.data == {}
    d = validate(Moon, {"names": "luca, max"})
    assert d.data == {"names": ["luca", "max"]}