From d73b6f169fd18e34c56ffb284e647d55df1c1b40 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Thu, 23 Oct 2025 15:21:55 -0700 Subject: [PATCH] Add register_actions hook to test plugin and improve test --- tests/fixtures.py | 1 + tests/plugins/my_plugin.py | 16 ++++++++++++++++ tests/test_plugins.py | 11 +++++++++++ 3 files changed, 28 insertions(+) diff --git a/tests/fixtures.py b/tests/fixtures.py index 5f65566f..e26789a7 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -48,6 +48,7 @@ EXPECTED_PLUGINS = [ "prepare_connection", "prepare_jinja2_environment", "query_actions", + "register_actions", "register_facet_classes", "register_magic_parameters", "register_permissions", diff --git a/tests/plugins/my_plugin.py b/tests/plugins/my_plugin.py index 2aa57e69..ba26b502 100644 --- a/tests/plugins/my_plugin.py +++ b/tests/plugins/my_plugin.py @@ -2,6 +2,8 @@ import asyncio from datasette import hookimpl, Permission from datasette.facets import Facet from datasette import tracer +from datasette.permissions import Action +from datasette.resources import DatabaseResource from datasette.utils import path_with_added_args from datasette.utils.asgi import asgi_send_json, Response import base64 @@ -498,3 +500,17 @@ def register_permissions(datasette): for p in extras["permissions"] ) return permissions + + +@hookimpl +def register_actions(datasette): + return [ + Action( + name="view-collection", + abbr="vc", + description="View a collection", + takes_parent=True, + takes_child=False, + resource_class=DatabaseResource, + ) + ] diff --git a/tests/test_plugins.py b/tests/test_plugins.py index 445891e4..9c74b432 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -1560,6 +1560,17 @@ async def test_hook_register_events(): assert any(k.__name__ == "OneEvent" for k in datasette.event_classes) +@pytest.mark.asyncio +async def test_hook_register_actions(): + datasette = Datasette(memory=True, plugins_dir=PLUGINS_DIR) + await datasette.invoke_startup() + # Check that the custom action from my_plugin.py is registered + assert "view-collection" in datasette.actions + action = datasette.actions["view-collection"] + assert action.abbr == "vc" + assert action.description == "View a collection" + + @pytest.mark.skip(reason="TODO") @pytest.mark.parametrize( "metadata,config,expected_metadata,expected_config",