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_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_exists_no_cache(table): resource_class = sql.row_resource_class(table) key = uuid4() resource = resource_class(key) row = DC(key=key, str_=str(key)) assert not await resource.exists() await resource.put(row) assert await resource.exists()
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_delete_cache(table: sql.Table): resource_class = sql.row_resource_class(table, cache_size=10, cache_expire=10) key = UUID("38340a23-e11a-412b-b20a-22dd7fc3d316") row = DC(key=key, str_=str(key)) await resource_class(key).put(row) # caches row await resource_class(key).delete() # deletes cached row with pytest.raises(fondat.error.NotFoundError): await resource_class(key).get()
async def test_put_get_cache(table: sql.Table): resource_class = sql.row_resource_class(table, cache_size=10, cache_expire=10) key = UUID("b616303b-1278-4209-8397-4fab852c8959") row = DC(key=key, str_=str(key)) await resource_class(key).put(row) # caches row async with table.database.transaction(): await table.delete(key) assert await resource_class(key).get() == row # still cached
async def test_get_cache(table: sql.Table): resource_class = sql.row_resource_class(table, cache_size=10, cache_expire=10) key = UUID("14f6a6b0-e4d7-4f3f-bb8c-66076fd6fce9") row = DC(key=key, str_=str(key)) async with table.database.transaction(): await table.insert(row) assert await resource_class(key).get() == row # caches row async with table.database.transaction(): await table.delete(key) assert await resource_class(key).get() == row # still cached async with table.database.transaction(): await table.delete(key)
async def test_get_cache_evict(table: sql.Table): resource_class = sql.row_resource_class(table, cache_size=1, cache_expire=10) key1 = UUID("16ed1e46-a111-414c-b05c-99a8b876afd0") row1 = DC(key=key1, str_=str(key1)) async with table.database.transaction(): await table.insert(row1) key2 = UUID("6bdba737-0401-4d8b-9a22-d8b6b0f8b5b7") row2 = DC(key=key2, str_=str(key2)) async with table.database.transaction(): await table.insert(row2) assert await resource_class(key1).get() == row1 assert await resource_class(key2).get() == row2 async with table.database.transaction(): await table.delete(key1) await table.delete(key2) with pytest.raises(fondat.error.NotFoundError): await resource_class(key1).get() == row1 # evicted assert await resource_class(key2).get() == row2 # still cached
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