async def test_resource_patch_small(table): pk = uuid4() resource = sql.table_resource_class(table, sql.row_resource_class(table))()[pk] row = DC(key=pk, str_="string") await resource.put(row) patch = {"str_": "strung"} await resource.patch(patch) resource = sql.table_resource_class(table, sql.row_resource_class(table))()[pk] row = await resource.get() assert row.str_ == "strung"
async def test_find_pks(table): resource = sql.table_resource_class(table, sql.row_resource_class(table))() keys = {uuid4() for _ in range(10)} async with table.database.transaction(): for key in keys: await table.insert(DC(key=key)) assert len(await resource.find_pks(keys)) == 10
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_resource_put_invalid_pk(table): pk = uuid4() resource = sql.table_resource_class(table, sql.row_resource_class(table))()[pk] row = DC(key=uuid4(), str_="string") # different pk with pytest.raises(fondat.error.BadRequestError): await resource.put(row)
async def test_resource_crud(table): pk = uuid4() resource = sql.table_resource_class(table, sql.row_resource_class(table))()[pk] row = DC( key=pk, str_="string", dict_={"a": 1}, list_=[1, 2, 3], set_={"foo", "bar"}, int_=1, float_=2.3, bool_=True, bytes_=b"12345", date_=date.fromisoformat("2019-01-01"), datetime_=datetime.fromisoformat("2019-01-01T01:01:01+00:00"), ) await resource.put(row) assert await resource.get() == row row.dict_ = {"a": 2} row.list_ = [2, 3, 4] row.set_ = None row.int_ = 2 row.float_ = 1.0 row.bool_ = False row.bytes_ = None row.date_ = None row.datetime_ = None await resource.put(row) assert await resource.get() == row await resource.delete() with pytest.raises(fondat.error.NotFoundError): await resource.get()
async def test_resource_list(table): resource = sql.table_resource_class(table, sql.row_resource_class(table))() count = 5 for n in range(0, count): key = uuid4() await resource[key].put(DC(key=key, int_=n)) results = await resource.get() assert len(results.items) == count
async def test_resource_patch_pk(table): pk = uuid4() resource = sql.table_resource_class(table, sql.row_resource_class(table))()[pk] row = DC(key=pk, str_="string") await resource.put(row) patch = {"key": str(uuid4())} # modify pk with pytest.raises(fondat.error.BadRequestError): await resource.patch(patch)
async def test_resource_patch(table): pk = uuid4() resource = sql.table_resource_class(table, sql.row_resource_class(table))()[pk] row = DC( key=pk, str_="string", dict_={"a": 1}, list_=[1, 2, 3], set_={"foo", "bar"}, int_=1, float_=2.3, bool_=True, bytes_=b"12345", date_=date.fromisoformat("2019-01-01"), datetime_=datetime.fromisoformat("2019-01-01T01:01:01+00:00"), ) await resource.put(row) patch = {"str_": "new_string", "dict_": {"a": None, "b": 2}} await resource.patch(patch) row = fondat.patch.json_merge_patch(value=row, type=DC, patch=patch) assert await resource.get() == row