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
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)
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()
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()
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)
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
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"}
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
def test_make_datacls_field_default_factory(): Foo = make_datacls("Foo", (("x", dict, field(default_factory=dict)), )) foo = Foo() assert foo.x == {}
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()
def test_make_datacls_optional(): Foo = make_datacls("Foo", {"x": Optional[int]}.items()) foo = Foo() assert foo.x == None
def test_make_datacls_field_default(): Foo = make_datacls("Foo", (("x", int, field(default=1)), )) foo = Foo() assert foo.x == 1
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"}
def test_datacls_json_decode_missing_field(): DC = make_datacls("DC", [("x", str)]) with pytest.raises(DecodeError): get_codec(JSON, DC).decode({})
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
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}
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)
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)
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)