Esempio n. 1
0
async def test_resource():
    info = fondat.openapi.Info(title="title", version="version")
    root = Root()
    resource = openapi_resource(resource=root, info=info)
    result = await resource.get()
    validate(result, fondat.openapi.OpenAPI)
    assert generate_openapi(resource=root, info=info) == result
Esempio n. 2
0
def test_generic_range():
    Range = Annotated[list[Optional[T]], MinLen(2), MaxLen(2)]
    IntRange = Range[int]
    validate([1, 2], IntRange)
    validate([None, 2], IntRange)
    validate([1, None], IntRange)
    with pytest.raises(ValidationError):
        validate(["1", 2], IntRange)
    with pytest.raises(ValidationError):
        validate([1, 2, 3], IntRange)
Esempio n. 3
0
def test_literal_bool_int():
    l = Literal[2, 3, True]
    validate(2, l)
    validate(3, l)
    validate(True, l)
    with pytest.raises(ValidationError):
        validate(1, l)
Esempio n. 4
0
async def test_nested_containers():
    @resource
    class R1:
        @operation
        async def get(self) -> str:
            return "str"

    c1 = container_resource({"r1": R1()})
    c2 = container_resource({"c1": c1})
    info = fondat.openapi.Info(title="title", version="version")
    doc = generate_openapi(resource=c2, info=info)
    validate(doc, fondat.openapi.OpenAPI)
    c1_r1 = doc.paths.get("/c1/r1")
    assert c1_r1 is not None
    assert c1_r1.get is not None
    js = get_codec(JSON, fondat.openapi.OpenAPI).encode(doc)
Esempio n. 5
0
def test_float_min_success():
    validate(1.1, Annotated[float, MinValue(1.0)])
Esempio n. 6
0
def test_float_type_success():
    validate(123.45, float)
Esempio n. 7
0
def test_int_max_error():
    with pytest.raises(ValidationError):
        validate(5, Annotated[int, MaxValue(4)])
Esempio n. 8
0
def test_int_minerror():
    with pytest.raises(ValidationError):
        validate(1, Annotated[int, MinValue(2)])
Esempio n. 9
0
def test_int_type_error():
    with pytest.raises(ValidationError):
        validate(123.45, int)
Esempio n. 10
0
def test_typeddict_optional():
    TD = TypedDict("TD", {"a": Optional[str]})
    validate(dict(a=None), TD)
Esempio n. 11
0
def test_literal_int_float():
    l = Literal[1, 2, 3]
    with pytest.raises(ValidationError):
        validate(1.0, l)
Esempio n. 12
0
def test_literal_int_bool():
    l = Literal[1, 2, 3]
    with pytest.raises(ValidationError):
        validate(True, l)
Esempio n. 13
0
def test_literal_not_match():
    l = Literal["a", "b", "c"]
    with pytest.raises(ValidationError):
        validate("d", l)
Esempio n. 14
0
def test_literal_match():
    l = Literal["a", "b", "c"]
    validate("a", l)
    validate("b", l)
    validate("c", l)
Esempio n. 15
0
def test_str_max_length_error():
    with pytest.raises(ValidationError):
        validate("1234567", Annotated[str, MaxLen(6)])
Esempio n. 16
0
def test_typeddict_total_false():
    class TD(TypedDict, total=False):
        l: str

    validate(dict(), TD)
Esempio n. 17
0
def test_str_pattern_error():
    with pytest.raises(ValidationError):
        validate("ghi", Annotated[str, Pattern(r"^def$")])
Esempio n. 18
0
def test_str_pattern_success():
    validate("abc", Annotated[str, Pattern(r"^abc$")])
Esempio n. 19
0
def test_int_type_success():
    validate(123, int)
Esempio n. 20
0
def test_dataclass_success():
    DC = make_dataclass("DC", [("a", str)])
    validate(DC(a="b"), DC)
Esempio n. 21
0
def test_int_min_success():
    validate(2, Annotated[int, MinValue(1)])
Esempio n. 22
0
def test_dataclass_error():
    DC = make_dataclass("DC", [("c", int)])
    with pytest.raises(ValidationError):
        validate(DC(c="str"), DC)
Esempio n. 23
0
def test_int_max_success():
    validate(2, Annotated[int, MaxValue(3)])
Esempio n. 24
0
def test_typeddict_success():
    TD = TypedDict("TD", a=str)
    validate(dict(a="b"), TD)
Esempio n. 25
0
def test_int_reject_bool():
    with pytest.raises(ValidationError):
        validate(True, int)
Esempio n. 26
0
def test_typeddict_error():
    TD = TypedDict("TD", c=int)
    with pytest.raises(ValidationError):
        validate(dict(c="str"), TD)
Esempio n. 27
0
def test_float_type_error():
    with pytest.raises(ValidationError):
        validate("123.45", float)
Esempio n. 28
0
def test_typeddict_total_success():
    TD = TypedDict("TD", e=float)
    validate(dict(e=1.2), TD)
Esempio n. 29
0
 async def _validate(self, value: table.schema):
     """Validate value; raise ValidationError if invalid."""
     validate(value, table.schema)
Esempio n. 30
0
def test_typeddict_total_error():
    TD = TypedDict("TD", f=str)
    with pytest.raises(ValidationError):
        validate(dict(), TD)