Skip to content

Commit

Permalink
Don't explain an explain even in the demo, refs simonw#2293
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Mar 6, 2024
1 parent 5de6797 commit 4d24bf6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
6 changes: 4 additions & 2 deletions docs/plugin_hooks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1557,7 +1557,10 @@ This example adds a new query action linking to a page for explaining a query:
@hookimpl
def query_actions(datasette, database, sql):
def query_actions(datasette, database, query_name, sql):
# Don't explain an explain
if sql.lower().startswith("explain"):
return
return [
{
"href": datasette.urls.database(database)
Expand All @@ -1571,7 +1574,6 @@ This example adds a new query action linking to a page for explaining a query:
},
]
.. _plugin_hook_database_actions:

database_actions(datasette, actor, database, request)
Expand Down
8 changes: 3 additions & 5 deletions tests/plugins/my_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,11 +393,9 @@ def table_actions(datasette, database, table, actor):

@hookimpl
def query_actions(datasette, database, query_name, sql):
args = {
"sql": sql,
}
if query_name:
args["query_name"] = query_name
# Don't explain an explain
if sql.lower().startswith("explain"):
return
return [
{
"href": datasette.urls.database(database)
Expand Down
7 changes: 6 additions & 1 deletion tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,8 @@ def get_table_actions_links(html):
"/fixtures/pragma_cache_size",
"/fixtures?sql=explain+PRAGMA+cache_size%3B",
),
# Don't attempt to explain an explain
("/fixtures?sql=explain+select+1", None),
),
)
async def test_hook_query_actions(ds_client, path, expected_url):
Expand All @@ -967,7 +969,10 @@ def get_table_actions_links(html):
response = await ds_client.get(path)
assert response.status_code == 200
links = get_table_actions_links(response.text)
assert links == [{"label": "Explain this query", "href": expected_url}]
if expected_url is None:
assert links == []
else:
assert links == [{"label": "Explain this query", "href": expected_url}]


@pytest.mark.asyncio
Expand Down

0 comments on commit 4d24bf6

Please sign in to comment.