Skip to content

Commit

Permalink
Create saved_queries table, refs #1
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jun 20, 2020
1 parent 90a349d commit 9b396ee
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
18 changes: 18 additions & 0 deletions datasette_saved_queries/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
from datasette import hookimpl
import sqlite_utils


def create_tables(conn):
db = sqlite_utils.Database(conn)
if not db["saved_queries"].exists():
db["saved_queries"].create(
{"name": str, "sql": str, "author_id": str,}, pk="name"
)


@hookimpl
def startup(datasette):
async def inner():
db = datasette.get_database()
await db.execute_write_fn(create_tables, block=True)

return inner
6 changes: 2 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ def get_long_description():
version=VERSION,
packages=["datasette_saved_queries"],
entry_points={"datasette": ["saved_queries = datasette_saved_queries"]},
install_requires=["datasette"],
extras_require={
"test": ["pytest", "pytest-asyncio", "httpx"]
},
install_requires=["datasette>=0.45a1", "sqlite-utils"],
extras_require={"test": ["pytest", "pytest-asyncio", "httpx"]},
tests_require=["datasette-saved-queries[test]"],
)
14 changes: 13 additions & 1 deletion tests/test_saved_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,21 @@

@pytest.mark.asyncio
async def test_plugin_is_installed():
app = Datasette([], memory=True,).app()
app = Datasette([], memory=True).app()
async with httpx.AsyncClient(app=app) as client:
response = await client.get("http://localhost/-/plugins.json")
assert 200 == response.status_code
installed_plugins = {p["name"] for p in response.json()}
assert "datasette-saved-queries" in installed_plugins


@pytest.mark.asyncio
async def test_table_created(tmpdir):
data = tmpdir / "data.db"
ds = Datasette([data])
await ds.invoke_startup()
app = ds.app()
async with httpx.AsyncClient(app=app) as client:
response = await client.get("http://localhost/data/saved_queries.json")
assert 200 == response.status_code
assert ["name", "sql", "author_id"] == response.json()["columns"]

0 comments on commit 9b396ee

Please sign in to comment.