Skip to content

Commit

Permalink
Update tests for latest Datasette
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Sep 20, 2023
1 parent 1c6e486 commit 707bc83
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 61 deletions.
7 changes: 6 additions & 1 deletion datasette_saved_queries/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ 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"
{
"name": str,
"sql": str,
"author_id": str,
},
pk="name",
)


Expand Down
117 changes: 57 additions & 60 deletions tests/test_saved_queries.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from datasette.app import Datasette
import pytest
import sqlite3
import httpx


@pytest.fixture
Expand All @@ -12,81 +11,79 @@ def db_path(tmpdir):


@pytest.fixture
@pytest.mark.asyncio
async def ds(db_path):
ds = Datasette([db_path])
await ds.invoke_startup()
yield ds
def ds(db_path):
return Datasette([db_path])


@pytest.mark.asyncio
async def test_plugin_is_installed(ds):
async with httpx.AsyncClient(app=ds.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
response = await ds.client.get("/-/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(ds):
async with httpx.AsyncClient(app=ds.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"]
response = await ds.client.get("/data/saved_queries.json")
assert 200 == response.status_code
assert ["name", "sql", "author_id"] == response.json()["columns"]


@pytest.mark.asyncio
async def test_save_query(ds):
async with httpx.AsyncClient(app=ds.app()) as client:
# Get the csrftoken cookie
response1 = await client.get("http://localhost/data/save_query")
# Now save a new query
response2 = await client.post(
"http://localhost/data/save_query",
data={
"name": "new_query",
"sql": "select 1 + 1",
"csrftoken": response1.cookies["ds_csrftoken"],
},
allow_redirects=False,
)
assert 302 == response2.status_code
# Check the query was saved
response3 = await client.get(
"http://localhost/data/saved_queries.json?_shape=array"
)
assert [
{"name": "new_query", "sql": "select 1 + 1", "author_id": None,}
] == response3.json()
# ... and that we can run the query
response3 = await client.get(
"http://localhost/data/new_query.json?_shape=array"
)
assert [{"1 + 1": 2}] == response3.json()
# Get the csrftoken cookie
response1 = await ds.client.get("/data/save_query")
# Now save a new query
response2 = await ds.client.post(
"/data/save_query",
data={
"name": "new_query",
"sql": "select 1 + 1",
"csrftoken": response1.cookies["ds_csrftoken"],
},
)
assert 302 == response2.status_code
# Check the query was saved
response3 = await ds.client.get("/data/saved_queries.json?_shape=array")
assert [
{
"name": "new_query",
"sql": "select 1 + 1",
"author_id": None,
}
] == response3.json()
# ... and that we can run the query
response3 = await ds.client.get("/data/new_query.json?_shape=array")
assert [{"1 + 1": 2}] == response3.json()


@pytest.mark.asyncio
async def test_save_query_authenticated_actor(ds):
async with httpx.AsyncClient(app=ds.app()) as client:
response1 = await client.get("http://localhost/data/save_query")
response2 = await client.post(
"http://localhost/data/save_query",
data={
"name": "new_query",
"sql": "select 1 + 1",
"csrftoken": response1.cookies["ds_csrftoken"],
},
cookies={"ds_actor": ds.sign({"a": {"id": "root"}}, "actor")},
allow_redirects=False,
)
assert 302 == response2.status_code
response3 = await client.get(
"http://localhost/data/saved_queries.json?_shape=array"
)
assert [
{"name": "new_query", "sql": "select 1 + 1", "author_id": "root",}
] == response3.json()
ds_actor = ds.sign({"a": {"id": "root"}}, "actor")
response1 = await ds.client.get(
"/data/save_query",
cookies={"ds_actor": ds_actor},
)
csrftoken = response1.cookies["ds_csrftoken"]
response2 = await ds.client.post(
"/data/save_query",
data={
"name": "new_query",
"sql": "select 1 + 1",
"csrftoken": csrftoken,
},
cookies={"ds_actor": ds_actor, "ds_csrftoken": csrftoken},
)
assert 302 == response2.status_code
response3 = await ds.client.get("/data/saved_queries.json?_shape=array")
assert [
{
"name": "new_query",
"sql": "select 1 + 1",
"author_id": "root",
}
] == response3.json()


@pytest.mark.asyncio
Expand Down

0 comments on commit 707bc83

Please sign in to comment.