Skip to content

Commit

Permalink
Fix for bug on table with no related tables, closes #9
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Apr 16, 2024
1 parent a2510d1 commit 3462b7e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
1 change: 1 addition & 0 deletions datasette_query_assistant/__init__.py
Expand Up @@ -31,6 +31,7 @@ def _related(conn):
return get_related_tables(conn, table)

tables = await db.execute_fn(_related)
tables.add(table)
sql = SCHEMA_SQL_SPECIFIC.replace("PARAMS", ",".join("?" for _ in tables))
return (await db.execute(sql, tuple(tables))).first()[0]
else:
Expand Down
3 changes: 2 additions & 1 deletion datasette_query_assistant/templates/query_assistant.html
Expand Up @@ -20,13 +20,14 @@ <h1>Query assistant for {% if table %}{{ table }}{% else %}{{ database }}{% endi
<input type="hidden" name="table" value="{{ table }}">
<input type="submit" value="Submit">
</p>
</form>

<details><summary>Schema that will be passed to the model</summary>
<pre>{{ schema }}</pre>
</details>

<script>
document.querySelector('textarea[name=question]').focus();
document.querySelector('#id_question').focus();
</script>

{% endblock %}
38 changes: 31 additions & 7 deletions tests/test_query_assistant.py
@@ -1,16 +1,18 @@
from datasette.app import Datasette
from datasette_query_assistant import get_related_tables
import pytest_asyncio
import pytest
import sqlite_utils


@pytest.mark.asyncio
async def test_plugin_is_installed():
datasette = Datasette(memory=True)
response = await datasette.client.get("/-/plugins.json")
assert response.status_code == 200
installed_plugins = {p["name"] for p in response.json()}
assert "datasette-query-assistant" in installed_plugins
@pytest_asyncio.fixture
async def datasette():
ds = Datasette()
db = ds.add_memory_database("test")
await db.execute_write(
"create table if not exists foo (id integer primary key, name text)"
)
return ds


def test_get_related_tables():
Expand All @@ -25,3 +27,25 @@ def test_get_related_tables():
assert get_related_tables(db.conn, "foo.bar.baz") == set()
assert get_related_tables(db.conn, "species") == {"species", "animals"}
assert get_related_tables(db.conn, "animals") == {"species", "animals"}


@pytest.mark.asyncio
async def test_database_assistant_page(datasette):
response = await datasette.client.get("/test/-/assistant")
assert response.status_code == 200
assert "Query assistant for test" in response.text
assert (
"<pre>CREATE TABLE foo (id integer primary key, name text)</pre>"
in response.text
)


@pytest.mark.asyncio
async def test_table_assistant_page(datasette):
response = await datasette.client.get("/test/-/assistant?table=foo")
assert response.status_code == 200
assert "Query assistant for foo" in response.text
assert (
"<pre>CREATE TABLE foo (id integer primary key, name text)</pre>"
in response.text
)

0 comments on commit 3462b7e

Please sign in to comment.