Skip to content

armgit / burr

Public Imported from GitHub
0

Build applications that make decisions (chatbots, agents, simulations, etc...). Monitor, trace, persist, and execute on your own infrastructure.

Updates tests for async sqlite persister

elijahbenizzyec448fda2bc6
diff --git a/burr/integrations/persisters/b_aiosqlite.py b/burr/integrations/persisters/b_aiosqlite.py
index 68730b5d..26508e6b 100644
--- a/burr/integrations/persisters/b_aiosqlite.py
+++ b/burr/integrations/persisters/b_aiosqlite.py
@@ -29,10 +29,7 @@ class AsyncSQLitePersister(AsyncBaseStatePersister, BaseCopyable):
def copy(self) -> "Self":
return AsyncSQLitePersister(
- db_path=self.db_path,
- table_name=self.table_name,
- serde_kwargs=self.serde_kwargs,
- connect_kwargs=self._connect_kwargs,
+ connection=self.connection, table_name=self.table_name, serde_kwargs=self.serde_kwargs
)
PARTITION_KEY_DEFAULT = ""
diff --git a/pyproject.toml b/pyproject.toml
index 63c7c2c4..558aa292 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -40,7 +40,7 @@ graphviz = [
"graphviz"
]
-sqlite = [
+aiosqlite = [
"aiosqlite"
]
diff --git a/tests/core/test_persistence.py b/tests/core/test_persistence.py
index 592074fd..f8370fba 100644
--- a/tests/core/test_persistence.py
+++ b/tests/core/test_persistence.py
@@ -1,16 +1,7 @@
-import asyncio
-from typing import Tuple
-
-import aiosqlite
import pytest
-from burr.core import ApplicationBuilder, State, action
-from burr.core.persistence import (
- AsyncInMemoryPersister,
- AsyncSQLLitePersister,
- InMemoryPersister,
- SQLLitePersister,
-)
+from burr.core import State
+from burr.core.persistence import InMemoryPersister, SQLLitePersister
@pytest.fixture(
@@ -109,7 +100,20 @@ def test_persister_methods_none_partition_key(persistence, method_name: str, kwa
# these operations are stateful (i.e., read/write to a db)
-class AsyncSQLLiteContextManager:
+import asyncio
+from typing import Tuple
+
+import aiosqlite
+import pytest
+
+from burr.core import ApplicationBuilder, State, action
+from burr.core.persistence import AsyncInMemoryPersister
+from burr.integrations.persisters.b_aiosqlite import AsyncSQLitePersister
+
+"""Asyncio integration for sqlite persister + """
+
+
+class AsyncSQLiteContextManager:
def __init__(self, sqlite_object):
self.client = sqlite_object
@@ -120,23 +124,9 @@ class AsyncSQLLiteContextManager:
await self.client.close()
- params=[
- {"which": "sqlite"},
- {"which": "memory"},
- ]
-)
async def async_persistence(request):
- which = request.param["which"]
- if which == "sqlite":
- sqlite_persister = await AsyncSQLLitePersister.from_values(
- db_path=":memory:", table_name="test_table"
- )
- async_context_manager = AsyncSQLLiteContextManager(sqlite_persister)
- async with async_context_manager as client:
- yield client
- elif which == "memory":
- yield AsyncInMemoryPersister()
+ yield AsyncInMemoryPersister()
async def test_async_persistence_saves_and_loads_state(async_persistence):
@@ -204,11 +194,11 @@ async def test_async_persister_methods_none_partition_key(
# these operations are stateful (i.e., read/write to a db)
-async def test_AsyncSQLLitePersister_from_values():
+async def test_AsyncSQLitePersister_from_values():
await asyncio.sleep(0.00001)
connection = await aiosqlite.connect(":memory:")
- sqlite_persister_init = AsyncSQLLitePersister(connection=connection, table_name="test_table")
- sqlite_persister_from_values = await AsyncSQLLitePersister.from_values(
+ sqlite_persister_init = AsyncSQLitePersister(connection=connection, table_name="test_table")
+ sqlite_persister_from_values = await AsyncSQLitePersister.from_values(
db_path=":memory:", table_name="test_table"
)
@@ -221,9 +211,9 @@ async def test_AsyncSQLLitePersister_from_values():
await sqlite_persister_from_values.close()
-async def test_AsyncSQLLitePersister_connection_shutdown():
+async def test_AsyncSQLitePersister_connection_shutdown():
await asyncio.sleep(0.00001)
- sqlite_persister = await AsyncSQLLitePersister.from_values(
+ sqlite_persister = await AsyncSQLitePersister.from_values(
db_path=":memory:", table_name="test_table"
)
await sqlite_persister.close()
@@ -231,10 +221,10 @@ async def test_AsyncSQLLitePersister_connection_shutdown():
@pytest.fixture()
async def initializing_async_persistence():
- sqlite_persister = await AsyncSQLLitePersister.from_values(
+ sqlite_persister = await AsyncSQLitePersister.from_values(
db_path=":memory:", table_name="test_table"
)
- async_context_manager = AsyncSQLLiteContextManager(sqlite_persister)
+ async_context_manager = AsyncSQLiteContextManager(sqlite_persister)
async with async_context_manager as client:
yield client
@@ -259,9 +249,9 @@ async def test_async_persistence_is_initialized_true(initializing_async_persiste
async def test_asyncsqlite_persistence_is_initialized_true_new_connection(tmp_path):
await asyncio.sleep(0.00001)
db_path = tmp_path / "test.db"
- p = await AsyncSQLLitePersister.from_values(db_path=db_path, table_name="test_table")
+ p = await AsyncSQLitePersister.from_values(db_path=db_path, table_name="test_table")
await p.initialize()
- p2 = await AsyncSQLLitePersister.from_values(db_path=db_path, table_name="test_table")
+ p2 = await AsyncSQLitePersister.from_values(db_path=db_path, table_name="test_table")
try:
assert await p.is_initialized()
assert await p2.is_initialized()
@@ -300,7 +290,7 @@ async def test_async_save_and_load_from_sqlite_persister_end_to_end(tmp_path):
)
db_path = tmp_path / "test.db"
- sqlite_persister = await AsyncSQLLitePersister.from_values(
+ sqlite_persister = await AsyncSQLitePersister.from_values(
db_path=db_path, table_name="test_table"
)
await sqlite_persister.initialize()
@@ -330,7 +320,7 @@ async def test_async_save_and_load_from_sqlite_persister_end_to_end(tmp_path):
await sqlite_persister.close()
del sqlite_persister
- sqlite_persister_2 = await AsyncSQLLitePersister.from_values(
+ sqlite_persister_2 = await AsyncSQLitePersister.from_values(
db_path=db_path, table_name="test_table"
)
await sqlite_persister_2.initialize()
diff --git a/tests/integrations/persisters/test_b_aiosqlite.py b/tests/integrations/persisters/test_b_aiosqlite.py
new file mode 100644
index 00000000..889d54ef
--- /dev/null
+++ b/tests/integrations/persisters/test_b_aiosqlite.py
@@ -0,0 +1,259 @@
+import asyncio
+from typing import Tuple
+
+import aiosqlite
+import pytest
+
+from burr.core import ApplicationBuilder, State, action
+from burr.integrations.persisters.b_aiosqlite import AsyncSQLitePersister
+
+
+class AsyncSQLiteContextManager:
+ def __init__(self, sqlite_object):
+ self.client = sqlite_object
+
+ async def __aenter__(self):
+ return self.client
+
+ async def __aexit__(self, exc_type, exc, tb):
+ await self.client.close()
+
+
+async def test_copy_persister(async_persistence: AsyncSQLitePersister):
+ copy = async_persistence.copy()
+ assert copy.table_name == async_persistence.table_name
+ assert copy.serde_kwargs == async_persistence.serde_kwargs
+ assert copy.connection is not None
+
+
+async def async_persistence(request):
+ sqlite_persister = await AsyncSQLitePersister.from_values(
+ db_path=":memory:", table_name="test_table"
+ )
+ async_context_manager = AsyncSQLiteContextManager(sqlite_persister)
+ async with async_context_manager as client:
+ yield client
+
+
+async def test_async_persistence_saves_and_loads_state(async_persistence):
+ await asyncio.sleep(0.00001)
+ if hasattr(async_persistence, "initialize"):
+ await async_persistence.initialize()
+ await async_persistence.save(
+ "partition_key", "app_id", 1, "position", State({"key": "value"}), "status"
+ )
+ loaded_state = await async_persistence.load("partition_key", "app_id")
+ assert loaded_state["state"] == State({"key": "value"})
+
+
+async def test_async_persistence_returns_none_when_no_state(async_persistence):
+ await asyncio.sleep(0.00001)
+ if hasattr(async_persistence, "initialize"):
+ await async_persistence.initialize()
+ loaded_state = await async_persistence.load("partition_key", "app_id")
+ assert loaded_state is None
+
+
+async def test_async_persistence_lists_app_ids(async_persistence):
+ await asyncio.sleep(0.00001)
+ if hasattr(async_persistence, "initialize"):
+ await async_persistence.initialize()
+ await async_persistence.save(
+ "partition_key", "app_id1", 1, "position", State({"key": "value"}), "status"
+ )
+ await async_persistence.save(
+ "partition_key", "app_id2", 1, "position", State({"key": "value"}), "status"
+ )
+ app_ids = await async_persistence.list_app_ids("partition_key")
+ assert set(app_ids) == set(["app_id1", "app_id2"])
+
+
+ "method_name,kwargs",
+ [
+ ("list_app_ids", {"partition_key": None}),
+ ("load", {"partition_key": None, "app_id": "foo"}),
+ (
+ "save",
+ {
+ "partition_key": None,
+ "app_id": "foo",
+ "sequence_id": 1,
+ "position": "position",
+ "state": State({"key": "value"}),
+ "status": "status",
+ },
+ ),
+ ],
+)
+async def test_async_persister_methods_none_partition_key(
+ async_persistence, method_name: str, kwargs: dict
+):
+ await asyncio.sleep(0.00001)
+ if hasattr(async_persistence, "initialize"):
+ await async_persistence.initialize()
+ method = getattr(async_persistence, method_name)
+ # method can be executed with `partition_key=None`
+ await method(**kwargs)
+ # this doesn't guarantee that the results of `partition_key=None` and
+ # `partition_key=persistence.PARTITION_KEY_DEFAULT`. This is hard to test because
+ # these operations are stateful (i.e., read/write to a db)
+
+
+async def test_AsyncSQLitePersister_from_values():
+ await asyncio.sleep(0.00001)
+ connection = await aiosqlite.connect(":memory:")
+ sqlite_persister_init = AsyncSQLitePersister(connection=connection, table_name="test_table")
+ sqlite_persister_from_values = await AsyncSQLitePersister.from_values(
+ db_path=":memory:", table_name="test_table"
+ )
+
+ try:
+ sqlite_persister_init.connection == sqlite_persister_from_values.connection
+ except Exception as e:
+ raise e
+ finally:
+ await sqlite_persister_init.close()
+ await sqlite_persister_from_values.close()
+
+
+async def test_AsyncSQLitePersister_connection_shutdown():
+ await asyncio.sleep(0.00001)
+ sqlite_persister = await AsyncSQLitePersister.from_values(
+ db_path=":memory:", table_name="test_table"
+ )
+ await sqlite_persister.close()
+
+
+async def initializing_async_persistence():
+ sqlite_persister = await AsyncSQLitePersister.from_values(
+ db_path=":memory:", table_name="test_table"
+ )
+ async_context_manager = AsyncSQLiteContextManager(sqlite_persister)
+ async with async_context_manager as client:
+ yield client
+
+
+async def test_async_persistence_initialization_creates_table(initializing_async_persistence):
+ await asyncio.sleep(0.00001)
+ await initializing_async_persistence.initialize()
+ assert await initializing_async_persistence.list_app_ids("partition_key") == []
+
+
+async def test_async_persistence_is_initialized_false(initializing_async_persistence):
+ await asyncio.sleep(0.00001)
+ assert not await initializing_async_persistence.is_initialized()
+
+
+async def test_async_persistence_is_initialized_true(initializing_async_persistence):
+ await asyncio.sleep(0.00001)
+ await initializing_async_persistence.initialize()
+ assert await initializing_async_persistence.is_initialized()
+
+
+async def test_asyncsqlite_persistence_is_initialized_true_new_connection(tmp_path):
+ await asyncio.sleep(0.00001)
+ db_path = tmp_path / "test.db"
+ p = await AsyncSQLitePersister.from_values(db_path=db_path, table_name="test_table")
+ await p.initialize()
+ p2 = await AsyncSQLitePersister.from_values(db_path=db_path, table_name="test_table")
+ try:
+ assert await p.is_initialized()
+ assert await p2.is_initialized()
+ except Exception as e:
+ raise e
+ finally:
+ await p.close()
+ await p2.close()
+
+
+async def test_async_save_and_load_from_sqlite_persister_end_to_end(tmp_path):
+ await asyncio.sleep(0.00001)
+
+ @action(reads=[], writes=["prompt", "chat_history"])
+ async def dummy_input(state: State) -> Tuple[dict, State]:
+ await asyncio.sleep(0.0001)
+ if state["chat_history"]:
+ new = state["chat_history"][-1] + 1
+ else:
+ new = 1
+ return (
+ {"prompt": "PROMPT"},
+ state.update(prompt="PROMPT").append(chat_history=new),
+ )
+
+ @action(reads=["chat_history"], writes=["response", "chat_history"])
+ async def dummy_response(state: State) -> Tuple[dict, State]:
+ await asyncio.sleep(0.0001)
+ if state["chat_history"]:
+ new = state["chat_history"][-1] + 1
+ else:
+ new = 1
+ return (
+ {"response": "RESPONSE"},
+ state.update(response="RESPONSE").append(chat_history=new),
+ )
+
+ db_path = tmp_path / "test.db"
+ sqlite_persister = await AsyncSQLitePersister.from_values(
+ db_path=db_path, table_name="test_table"
+ )
+ await sqlite_persister.initialize()
+ app = await (
+ ApplicationBuilder()
+ .with_actions(dummy_input, dummy_response)
+ .with_transitions(("dummy_input", "dummy_response"), ("dummy_response", "dummy_input"))
+ .initialize_from(
+ initializer=sqlite_persister,
+ resume_at_next_action=True,
+ default_state={"chat_history": []},
+ default_entrypoint="dummy_input",
+ )
+ .with_state_persister(sqlite_persister)
+ .with_identifiers(app_id="test_1", partition_key="sqlite")
+ .abuild()
+ )
+
+ try:
+ *_, state = await app.arun(halt_after=["dummy_response"])
+ assert state["chat_history"][0] == 1
+ assert state["chat_history"][1] == 2
+ del app
+ except Exception as e:
+ raise e
+ finally:
+ await sqlite_persister.close()
+ del sqlite_persister
+
+ sqlite_persister_2 = await AsyncSQLitePersister.from_values(
+ db_path=db_path, table_name="test_table"
+ )
+ await sqlite_persister_2.initialize()
+ new_app = await (
+ ApplicationBuilder()
+ .with_actions(dummy_input, dummy_response)
+ .with_transitions(("dummy_input", "dummy_response"), ("dummy_response", "dummy_input"))
+ .initialize_from(
+ initializer=sqlite_persister_2,
+ resume_at_next_action=True,
+ default_state={"chat_history": []},
+ default_entrypoint="dummy_input",
+ )
+ .with_state_persister(sqlite_persister_2)
+ .with_identifiers(app_id="test_1", partition_key="sqlite")
+ .abuild()
+ )
+
+ try:
+ assert new_app.state["chat_history"][0] == 1
+ assert new_app.state["chat_history"][1] == 2
+
+ *_, state = await new_app.arun(halt_after=["dummy_response"])
+ assert state["chat_history"][2] == 3
+ assert state["chat_history"][3] == 4
+ except Exception as e:
+ raise e
+ finally:
+ await sqlite_persister_2.close()