示例#1
0
def test_derive_typeddict_dataclass_simple():
    DC = make_datacls("A", (("a", str), ("b", int), ("c", float)))
    TD = derive_typeddict("TD", DC)
    annotations = TD.__annotations__
    assert annotations["a"] is str
    assert annotations["b"] is int
    assert annotations["c"] is float
示例#2
0
def test_subset_dataclass_optional_subset():
    Foo = make_datacls("Foo", (("a", int), ("b", str), ("c", float)))
    optional = {"a", "b"}
    Bar = derive_datacls("Bar", Foo, optional=optional)
    for f in fields(Bar):
        assert (f.name in optional
                and is_optional(f.type)) or not is_optional(f.type)
示例#3
0
async def test_table_patch(database):
    DC = make_datacls("DC", (("id", str), ("s", str)))
    table = sql.Table("dc", database, DC, "id")
    async with database.transaction():
        with contextlib.suppress(Exception):
            await table.drop()
        await table.create()
        await table.insert(DC(id="a", s="aaa"))
        await table.insert(DC(id="b", s="aaa"))
        await table.insert(DC(id="c", s="aaa"))
        resource = sql.table_resource_class(table)()
        await resource.patch([
            {
                "id": "a",
                "s": "bbb"
            },
            {
                "id": "b",
                "s": "bbb"
            },
            {
                "id": "z",
                "s": "zzz"
            },
        ])
        assert await table.read("a") == DC(id="a", s="bbb")
        assert await table.read("b") == DC(id="b", s="bbb")
        assert await table.read("c") == DC(id="c", s="aaa")
        assert await table.read("z") == DC(id="z", s="zzz")
        with pytest.raises(fondat.error.BadRequestError):
            await resource.patch([{"id": "a", "s": 123}])
        await table.drop()
async def test_foo(database):
    DC = make_datacls("DC", (("id", str), ("val", list[str])))
    table = sql.Table(name="fun", database=database, schema=DC, pk="id")
    index = fondat.postgresql.Index(name="fun_ix", table=table, keys=("val",), method="GIN")
    async with database.transaction():
        await table.create()
        await index.create()
        await table.drop()
示例#5
0
def test_derive_typeddict_annotated_dataclass():
    DC = make_datacls("A", (("a", str), ))
    TD = derive_typeddict("TD", Annotated[DC, "annotated"])
    assert TD.__annotations__.keys() == DC.__annotations__.keys()
示例#6
0
def test_derive_datacls_optional_true():
    Foo = make_datacls("Foo", (("a", int), ("b", str), ("c", float)))
    Bar = derive_datacls("Bar", Foo, optional=True)
    for field in fields(Bar):
        assert is_optional(field.type)
示例#7
0
 async def select(n: int):
     stmt = sql.Statement(f"SELECT {n} AS foo;",
                          result=make_datacls("DC", (("foo", int), )))
     result = await (await database.execute(stmt)).__anext__()
     assert result.foo == n
示例#8
0
def test_derive_datacls_exclude():
    Foo = make_datacls("Foo", (("a", int), ("b", str), ("c", float)))
    Bar = derive_datacls("Bar", Foo, exclude={"c"})
    assert Bar.__annotations__.keys() == {"a", "b"}
示例#9
0
def test_derive_dataclass_append():
    Foo = make_datacls("Foo", (("a", int), ("b", str), ("c", float)))
    Bar = derive_datacls("Bar", Foo, append=(("d", str), ))
    assert Bar.__annotations__.keys() == {"a", "b", "c", "d"}
    assert Bar.__annotations__["d"] is str
示例#10
0
def test_make_datacls_field_default_factory():
    Foo = make_datacls("Foo", (("x", dict, field(default_factory=dict)), ))
    foo = Foo()
    assert foo.x == {}
示例#11
0
def test_derive_datacls():
    Foo = make_datacls("Foo", (("a", int), ("b", str), ("c", float)))
    Bar = derive_datacls("Bar", Foo)
    assert Bar.__annotations__.keys() == Foo.__annotations__.keys()
示例#12
0
def test_make_datacls_optional():
    Foo = make_datacls("Foo", {"x": Optional[int]}.items())
    foo = Foo()
    assert foo.x == None
示例#13
0
def test_make_datacls_field_default():
    Foo = make_datacls("Foo", (("x", int, field(default=1)), ))
    foo = Foo()
    assert foo.x == 1
示例#14
0
def test_derive_typeddict_dataclass_exclude():
    DC = make_datacls("A", (("a", str), ("b", int), ("c", float)))
    TD = derive_typeddict("TD", DC, exclude={"a"})
    assert TD.__annotations__.keys() == {"b", "c"}
示例#15
0
def test_datacls_json_decode_missing_field():
    DC = make_datacls("DC", [("x", str)])
    with pytest.raises(DecodeError):
        get_codec(JSON, DC).decode({})
示例#16
0
def test_copy_all():
    Foo = make_datacls("Foo", (("a", Optional[int]), ("b", Optional[str]),
                               ("c", Optional[float])))
    foo = Foo(a=1, b="a", c=2.0)
    bar = copy_data(foo, Foo)
    assert foo == bar
示例#17
0
def test_copy_dc_to_td_exclude():
    fields = (("a", Optional[str]), ("b", Optional[int]))
    DC = make_datacls("DC", fields)
    TD = TypedDict("TD", fields)
    dc = DC(a="a", b=1)
    assert copy_data(dc, TD, exclude={"a"}) == {"b": 1}
示例#18
0
def test_copy_dc_to_td():
    fields = (("a", str), ("b", int))
    DC = make_datacls("DC", fields)
    TD = TypedDict("TD", fields)
    dc = DC(a="a", b=1)
    assert copy_data(dc, TD) == asdict(dc)
示例#19
0
def test_copy_td_to_dc_exclude():
    fields = (("a", Optional[str]), ("b", Optional[int]))
    DC = make_datacls("DC", fields)
    TD = TypedDict("TD", fields)
    td = TD(a="a", b=1)
    assert copy_data(td, DC, exclude={"a"}) == DC(b=1)
示例#20
0
def test_copy_td_to_dc():
    fields = (("a", str), ("b", int))
    DC = make_datacls("DC", fields)
    TD = TypedDict("TD", fields)
    td = TD(a="a", b=1)
    assert copy_data(td, DC) == DC(**td)