Merge branch 'main' into playwright-tests

This commit is contained in:
Simon Willison 2026-06-16 13:29:22 -07:00
commit 2a887e5853
9 changed files with 140 additions and 20 deletions

View file

@ -1,7 +1,7 @@
import pytest
from datasette.app import Datasette
import datasette.views.table as table_views
from datasette.database import QueryInterrupted
@pytest.mark.asyncio
@ -192,7 +192,6 @@ async def test_autocomplete_primary_key_called_label():
@pytest.mark.asyncio
async def test_autocomplete_timeout_uses_prefix_fallback(monkeypatch):
monkeypatch.setattr(table_views, "AUTOCOMPLETE_TIME_LIMIT_MS", 1)
ds = Datasette(
memory=True,
config={
@ -217,16 +216,34 @@ async def test_autocomplete_timeout_uses_prefix_fallback(monkeypatch):
def insert_rows(conn):
conn.executemany(
"insert into things (id, name) values (?, ?)",
((f"item-{i:06d}", f"name {i:06d}") for i in range(200_000)),
((f"item-1999{i:02d}", f"name 1999{i:02d}") for i in range(12)),
)
await db.execute_write_fn(insert_rows)
original_execute = db.execute
timeout_was_simulated = False
async def execute_with_simulated_timeout(sql, params=None, *args, **kwargs):
nonlocal timeout_was_simulated
if (
not timeout_was_simulated
and isinstance(params, dict)
and params.get("q") == "item-1999"
and "prefix_end" not in params
):
timeout_was_simulated = True
raise QueryInterrupted(Exception("interrupted"), sql, params)
return await original_execute(sql, params, *args, **kwargs)
monkeypatch.setattr(db, "execute", execute_with_simulated_timeout)
response = await ds.client.get(
"/autocomplete_timeout/things/-/autocomplete?q=item-1999"
)
assert response.status_code == 200
assert timeout_was_simulated
data = response.json()
assert data == {
"rows": [

View file

@ -55,6 +55,57 @@ async def test_request_post_body():
assert data == json.loads(body)
@pytest.mark.asyncio
async def test_request_json():
scope = {
"http_version": "1.1",
"method": "POST",
"path": "/",
"raw_path": b"/",
"query_string": b"",
"scheme": "http",
"type": "http",
"headers": [[b"content-type", b"application/json"]],
}
data = {"hello": "world", "items": [1, 2, 3]}
async def receive():
return {
"type": "http.request",
"body": json.dumps(data).encode("utf-8"),
"more_body": False,
}
request = Request(scope, receive)
assert data == await request.json()
@pytest.mark.asyncio
async def test_request_json_invalid():
scope = {
"http_version": "1.1",
"method": "POST",
"path": "/",
"raw_path": b"/",
"query_string": b"",
"scheme": "http",
"type": "http",
"headers": [[b"content-type", b"application/json"]],
}
async def receive():
return {
"type": "http.request",
"body": b"this is not JSON",
"more_body": False,
}
request = Request(scope, receive)
with pytest.raises(json.JSONDecodeError):
await request.json()
def test_request_args():
request = Request.fake("/foo?multi=1&multi=2&single=3")
assert "1" == request.args.get("multi")