mirror of
https://github.com/simonw/datasette.git
synced 2026-10-27 21:57:06 +01:00
Merge 1a97c6e23d into c435a80030
This commit is contained in:
commit
4379cf5578
2 changed files with 67 additions and 9 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue