This commit is contained in:
Roli Bosch 2026-10-12 14:16:49 -07:00 • committed by GitHub
commit 4379cf5578
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 67 additions and 9 deletions

View file

@ -980,6 +980,65 @@ async def test_hook_startup(ds_client):
assert 2 == ds_client.ds._startup_hook_calculation
@pytest.mark.asyncio
@pytest.mark.parametrize("return_style", ["async_def", "async_function", "coroutine"])
async def test_hook_startup_async(return_style, tmp_path):
db_path = tmp_path / "startup.db"
sqlite3.connect(db_path).close()
ds = Datasette([db_path])
async def initialize(datasette):
await datasette.get_database().execute_write(
"create table startup_test (id integer)"
)
class AsyncStartupPlugin:
__name__ = "AsyncStartupPlugin"
if return_style == "async_def":
@hookimpl
async def startup(self, datasette):
await initialize(datasette)
else:
@hookimpl
def startup(self, datasette):
async def inner():
await initialize(datasette)
return inner if return_style == "async_function" else inner()
ds.pm.register(AsyncStartupPlugin(), name="async_startup_plugin")
try:
await ds.invoke_startup()
result = await ds.get_database().execute("select count(*) from startup_test")
assert result.first()[0] == 0
finally:
ds.pm.unregister(name="async_startup_plugin")
@pytest.mark.asyncio
async def test_hook_startup_async_error():
ds = Datasette(memory=True)
class FailingStartupPlugin:
__name__ = "FailingStartupPlugin"
@hookimpl
async def startup(self, datasette):
await datasette.get_database().execute("select 1")
raise StartupError("Invalid plugin configuration")
ds.pm.register(FailingStartupPlugin(), name="failing_startup_plugin")
try:
with pytest.raises(StartupError, match="Invalid plugin configuration"):
await ds.invoke_startup()
finally:
ds.pm.unregister(name="failing_startup_plugin")
@pytest.mark.asyncio
async def test_hook_startup_metadata_available(ds_client):
# Metadata from metadata.yaml should be populated before startup() fires