2022-12-01 05:33:20 +00:00
|
|
|
from pydantic import BaseModel, Field
|
|
|
|
|
2023-08-18 14:57:18 +00:00
|
|
|
from invokeai.app.services.sqlite import SqliteItemStorage, sqlite_memory
|
|
|
|
|
2022-12-01 05:33:20 +00:00
|
|
|
|
|
|
|
class TestModel(BaseModel):
|
2023-07-27 14:54:01 +00:00
|
|
|
id: str = Field(description="ID")
|
|
|
|
name: str = Field(description="Name")
|
2022-12-01 05:33:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_sqlite_service_can_create_and_get():
|
2023-07-27 14:54:01 +00:00
|
|
|
db = SqliteItemStorage[TestModel](sqlite_memory, "test", "id")
|
|
|
|
db.set(TestModel(id="1", name="Test"))
|
|
|
|
assert db.get("1") == TestModel(id="1", name="Test")
|
|
|
|
|
2022-12-01 05:33:20 +00:00
|
|
|
|
|
|
|
def test_sqlite_service_can_list():
|
2023-07-27 14:54:01 +00:00
|
|
|
db = SqliteItemStorage[TestModel](sqlite_memory, "test", "id")
|
|
|
|
db.set(TestModel(id="1", name="Test"))
|
|
|
|
db.set(TestModel(id="2", name="Test"))
|
|
|
|
db.set(TestModel(id="3", name="Test"))
|
2022-12-01 05:33:20 +00:00
|
|
|
results = db.list()
|
|
|
|
assert results.page == 0
|
|
|
|
assert results.pages == 1
|
|
|
|
assert results.per_page == 10
|
|
|
|
assert results.total == 3
|
2023-07-27 14:54:01 +00:00
|
|
|
assert results.items == [
|
|
|
|
TestModel(id="1", name="Test"),
|
|
|
|
TestModel(id="2", name="Test"),
|
|
|
|
TestModel(id="3", name="Test"),
|
|
|
|
]
|
|
|
|
|
2022-12-01 05:33:20 +00:00
|
|
|
|
|
|
|
def test_sqlite_service_can_delete():
|
2023-07-27 14:54:01 +00:00
|
|
|
db = SqliteItemStorage[TestModel](sqlite_memory, "test", "id")
|
|
|
|
db.set(TestModel(id="1", name="Test"))
|
|
|
|
db.delete("1")
|
|
|
|
assert db.get("1") is None
|
|
|
|
|
2022-12-01 05:33:20 +00:00
|
|
|
|
|
|
|
def test_sqlite_service_calls_set_callback():
|
2023-07-27 14:54:01 +00:00
|
|
|
db = SqliteItemStorage[TestModel](sqlite_memory, "test", "id")
|
2022-12-01 05:33:20 +00:00
|
|
|
called = False
|
2023-07-27 14:54:01 +00:00
|
|
|
|
2022-12-01 05:33:20 +00:00
|
|
|
def on_changed(item: TestModel):
|
|
|
|
nonlocal called
|
|
|
|
called = True
|
2023-07-27 14:54:01 +00:00
|
|
|
|
2022-12-01 05:33:20 +00:00
|
|
|
db.on_changed(on_changed)
|
2023-07-27 14:54:01 +00:00
|
|
|
db.set(TestModel(id="1", name="Test"))
|
2022-12-01 05:33:20 +00:00
|
|
|
assert called
|
|
|
|
|
2023-07-27 14:54:01 +00:00
|
|
|
|
2022-12-01 05:33:20 +00:00
|
|
|
def test_sqlite_service_calls_delete_callback():
|
2023-07-27 14:54:01 +00:00
|
|
|
db = SqliteItemStorage[TestModel](sqlite_memory, "test", "id")
|
2022-12-01 05:33:20 +00:00
|
|
|
called = False
|
2023-07-27 14:54:01 +00:00
|
|
|
|
2022-12-01 05:33:20 +00:00
|
|
|
def on_deleted(item_id: str):
|
|
|
|
nonlocal called
|
|
|
|
called = True
|
2023-07-27 14:54:01 +00:00
|
|
|
|
2022-12-01 05:33:20 +00:00
|
|
|
db.on_deleted(on_deleted)
|
2023-07-27 14:54:01 +00:00
|
|
|
db.set(TestModel(id="1", name="Test"))
|
|
|
|
db.delete("1")
|
2022-12-01 05:33:20 +00:00
|
|
|
assert called
|
|
|
|
|
2023-07-27 14:54:01 +00:00
|
|
|
|
2022-12-01 05:33:20 +00:00
|
|
|
def test_sqlite_service_can_list_with_pagination():
|
2023-07-27 14:54:01 +00:00
|
|
|
db = SqliteItemStorage[TestModel](sqlite_memory, "test", "id")
|
|
|
|
db.set(TestModel(id="1", name="Test"))
|
|
|
|
db.set(TestModel(id="2", name="Test"))
|
|
|
|
db.set(TestModel(id="3", name="Test"))
|
|
|
|
results = db.list(page=0, per_page=2)
|
2022-12-01 05:33:20 +00:00
|
|
|
assert results.page == 0
|
|
|
|
assert results.pages == 2
|
|
|
|
assert results.per_page == 2
|
|
|
|
assert results.total == 3
|
2023-07-27 14:54:01 +00:00
|
|
|
assert results.items == [TestModel(id="1", name="Test"), TestModel(id="2", name="Test")]
|
|
|
|
|
2022-12-01 05:33:20 +00:00
|
|
|
|
|
|
|
def test_sqlite_service_can_list_with_pagination_and_offset():
|
2023-07-27 14:54:01 +00:00
|
|
|
db = SqliteItemStorage[TestModel](sqlite_memory, "test", "id")
|
|
|
|
db.set(TestModel(id="1", name="Test"))
|
|
|
|
db.set(TestModel(id="2", name="Test"))
|
|
|
|
db.set(TestModel(id="3", name="Test"))
|
|
|
|
results = db.list(page=1, per_page=2)
|
2022-12-01 05:33:20 +00:00
|
|
|
assert results.page == 1
|
|
|
|
assert results.pages == 2
|
|
|
|
assert results.per_page == 2
|
|
|
|
assert results.total == 3
|
2023-07-27 14:54:01 +00:00
|
|
|
assert results.items == [TestModel(id="3", name="Test")]
|
|
|
|
|
2022-12-01 05:33:20 +00:00
|
|
|
|
|
|
|
def test_sqlite_service_can_search():
|
2023-07-27 14:54:01 +00:00
|
|
|
db = SqliteItemStorage[TestModel](sqlite_memory, "test", "id")
|
|
|
|
db.set(TestModel(id="1", name="Test"))
|
|
|
|
db.set(TestModel(id="2", name="Test"))
|
|
|
|
db.set(TestModel(id="3", name="Test"))
|
|
|
|
results = db.search(query="Test")
|
2022-12-01 05:33:20 +00:00
|
|
|
assert results.page == 0
|
|
|
|
assert results.pages == 1
|
|
|
|
assert results.per_page == 10
|
|
|
|
assert results.total == 3
|
2023-07-27 14:54:01 +00:00
|
|
|
assert results.items == [
|
|
|
|
TestModel(id="1", name="Test"),
|
|
|
|
TestModel(id="2", name="Test"),
|
|
|
|
TestModel(id="3", name="Test"),
|
|
|
|
]
|
|
|
|
|
2022-12-01 05:33:20 +00:00
|
|
|
|
|
|
|
def test_sqlite_service_can_search_with_pagination():
|
2023-07-27 14:54:01 +00:00
|
|
|
db = SqliteItemStorage[TestModel](sqlite_memory, "test", "id")
|
|
|
|
db.set(TestModel(id="1", name="Test"))
|
|
|
|
db.set(TestModel(id="2", name="Test"))
|
|
|
|
db.set(TestModel(id="3", name="Test"))
|
|
|
|
results = db.search(query="Test", page=0, per_page=2)
|
2022-12-01 05:33:20 +00:00
|
|
|
assert results.page == 0
|
|
|
|
assert results.pages == 2
|
|
|
|
assert results.per_page == 2
|
|
|
|
assert results.total == 3
|
2023-07-27 14:54:01 +00:00
|
|
|
assert results.items == [TestModel(id="1", name="Test"), TestModel(id="2", name="Test")]
|
|
|
|
|
2022-12-01 05:33:20 +00:00
|
|
|
|
|
|
|
def test_sqlite_service_can_search_with_pagination_and_offset():
|
2023-07-27 14:54:01 +00:00
|
|
|
db = SqliteItemStorage[TestModel](sqlite_memory, "test", "id")
|
|
|
|
db.set(TestModel(id="1", name="Test"))
|
|
|
|
db.set(TestModel(id="2", name="Test"))
|
|
|
|
db.set(TestModel(id="3", name="Test"))
|
|
|
|
results = db.search(query="Test", page=1, per_page=2)
|
2022-12-01 05:33:20 +00:00
|
|
|
assert results.page == 1
|
|
|
|
assert results.pages == 2
|
|
|
|
assert results.per_page == 2
|
|
|
|
assert results.total == 3
|
2023-07-27 14:54:01 +00:00
|
|
|
assert results.items == [TestModel(id="3", name="Test")]
|