mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Action menu descriptions
* Refactor tests to extract get_actions_links() helper * Table, database and query action menu items now support optional descriptions Closes #2294
This commit is contained in:
parent
c6e8a4a76c
commit
090dff542b
7 changed files with 59 additions and 35 deletions
|
|
@ -406,6 +406,7 @@ def query_actions(datasette, database, query_name, sql):
|
|||
}
|
||||
),
|
||||
"label": "Explain this query",
|
||||
"description": "Runs a SQLite explain",
|
||||
},
|
||||
]
|
||||
|
||||
|
|
|
|||
|
|
@ -925,26 +925,36 @@ async def test_hook_menu_links(ds_client):
|
|||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("table_or_view", ["facetable", "simple_view"])
|
||||
async def test_hook_table_actions(ds_client, table_or_view):
|
||||
def get_table_actions_links(html):
|
||||
soup = Soup(html, "html.parser")
|
||||
details = soup.find("details", {"class": "actions-menu-links"})
|
||||
if details is None:
|
||||
return []
|
||||
return [{"label": a.text, "href": a["href"]} for a in details.select("a")]
|
||||
|
||||
response = await ds_client.get(f"/fixtures/{table_or_view}")
|
||||
assert get_table_actions_links(response.text) == []
|
||||
assert get_actions_links(response.text) == []
|
||||
|
||||
response_2 = await ds_client.get(f"/fixtures/{table_or_view}?_bot=1&_hello=BOB")
|
||||
assert sorted(
|
||||
get_table_actions_links(response_2.text), key=lambda link: link["label"]
|
||||
get_actions_links(response_2.text), key=lambda link: link["label"]
|
||||
) == [
|
||||
{"label": "Database: fixtures", "href": "/"},
|
||||
{"label": "From async BOB", "href": "/"},
|
||||
{"label": f"Table: {table_or_view}", "href": "/"},
|
||||
{"label": "Database: fixtures", "href": "/", "description": None},
|
||||
{"label": "From async BOB", "href": "/", "description": None},
|
||||
{"label": f"Table: {table_or_view}", "href": "/", "description": None},
|
||||
]
|
||||
|
||||
|
||||
def get_actions_links(html):
|
||||
soup = Soup(html, "html.parser")
|
||||
details = soup.find("details", {"class": "actions-menu-links"})
|
||||
if details is None:
|
||||
return []
|
||||
links = []
|
||||
for a_el in details.select("a"):
|
||||
description = None
|
||||
if a_el.find("p") is not None:
|
||||
description = a_el.find("p").text.strip()
|
||||
a_el.find("p").extract()
|
||||
label = a_el.text.strip()
|
||||
href = a_el["href"]
|
||||
links.append({"label": label, "href": href, "description": description})
|
||||
return links
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
"path,expected_url",
|
||||
|
|
@ -959,37 +969,29 @@ async def test_hook_table_actions(ds_client, table_or_view):
|
|||
),
|
||||
)
|
||||
async def test_hook_query_actions(ds_client, path, expected_url):
|
||||
def get_table_actions_links(html):
|
||||
soup = Soup(html, "html.parser")
|
||||
details = soup.find("details", {"class": "actions-menu-links"})
|
||||
if details is None:
|
||||
return []
|
||||
return [{"label": a.text, "href": a["href"]} for a in details.select("a")]
|
||||
|
||||
response = await ds_client.get(path)
|
||||
assert response.status_code == 200
|
||||
links = get_table_actions_links(response.text)
|
||||
links = get_actions_links(response.text)
|
||||
if expected_url is None:
|
||||
assert links == []
|
||||
else:
|
||||
assert links == [{"label": "Explain this query", "href": expected_url}]
|
||||
assert links == [
|
||||
{
|
||||
"label": "Explain this query",
|
||||
"href": expected_url,
|
||||
"description": "Runs a SQLite explain",
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_hook_database_actions(ds_client):
|
||||
def get_table_actions_links(html):
|
||||
soup = Soup(html, "html.parser")
|
||||
details = soup.find("details", {"class": "actions-menu-links"})
|
||||
if details is None:
|
||||
return []
|
||||
return [{"label": a.text, "href": a["href"]} for a in details.select("a")]
|
||||
|
||||
response = await ds_client.get("/fixtures")
|
||||
assert get_table_actions_links(response.text) == []
|
||||
assert get_actions_links(response.text) == []
|
||||
|
||||
response_2 = await ds_client.get("/fixtures?_bot=1&_hello=BOB")
|
||||
assert get_table_actions_links(response_2.text) == [
|
||||
{"label": "Database: fixtures - BOB", "href": "/"},
|
||||
assert get_actions_links(response_2.text) == [
|
||||
{"label": "Database: fixtures - BOB", "href": "/", "description": None},
|
||||
]
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue