mirror of
https://github.com/simonw/datasette.git
synced 2026-11-19 16:48:38 +01:00
Row JSON now returns {"row": {...}}, closes #2612
/db/table/pk.json returns the row as an object in a "row" key instead of a single-item "rows" list. Passing any ?_shape= returns the same shapes as table JSON, so ?_shape=objects gets the previous "rows" list back. This happens in the JSON renderer, so other output renderers still get the "rows" list for row pages. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GaqG8H8ajwxizKXdbGrtve
This commit is contained in:
parent
e8c7b469fa
commit
28184edc52
6 changed files with 97 additions and 16 deletions
|
|
@ -363,10 +363,43 @@ async def test_invalid_custom_sql(ds_client):
|
|||
|
||||
@pytest.mark.asyncio
|
||||
async def test_row(ds_client):
|
||||
response = await ds_client.get("/fixtures/simple_primary_key/1.json?_shape=objects")
|
||||
response = await ds_client.get("/fixtures/simple_primary_key/1.json")
|
||||
assert response.status_code == 200
|
||||
assert response.json()["ok"] is True
|
||||
assert response.json()["rows"] == [{"id": 1, "content": "hello"}]
|
||||
data = response.json()
|
||||
assert data["ok"] is True
|
||||
assert data["row"] == {"id": 1, "content": "hello"}
|
||||
assert "rows" not in data
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
"shape,expected",
|
||||
(
|
||||
("objects", [{"id": 1, "content": "hello"}]),
|
||||
("arrays", [[1, "hello"]]),
|
||||
("array", [{"id": 1, "content": "hello"}]),
|
||||
("arrayfirst", [1]),
|
||||
("object", {"1": {"id": 1, "content": "hello"}}),
|
||||
),
|
||||
)
|
||||
async def test_row_shape(ds_client, shape, expected):
|
||||
# Any ?_shape= returns the same shapes as the table JSON
|
||||
response = await ds_client.get(
|
||||
f"/fixtures/simple_primary_key/1.json?_shape={shape}"
|
||||
)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
if shape in ("objects", "arrays"):
|
||||
assert "row" not in data
|
||||
data = data["rows"]
|
||||
assert data == expected
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_row_json_cols(ds_client):
|
||||
response = await ds_client.get("/fixtures/facetable/1.json?_json=tags")
|
||||
assert response.status_code == 200
|
||||
assert response.json()["row"]["tags"] == ["tag1", "tag2"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
|
@ -615,7 +648,7 @@ async def test_row_extra_render_cell():
|
|||
|
||||
# Verify the response structure
|
||||
assert "render_cell" in data
|
||||
assert "rows" in data
|
||||
assert "row" in data
|
||||
|
||||
# render_cell should be a list with one row (since this is a row page)
|
||||
# Only columns modified by plugins are included (sparse output)
|
||||
|
|
@ -628,8 +661,8 @@ async def test_row_extra_render_cell():
|
|||
# The 'id' column is not included since no plugin modified it
|
||||
assert "id" not in render_cell[0]
|
||||
|
||||
# The regular rows should still contain raw values
|
||||
assert data["rows"] == [{"id": 1, "name": "Alice"}]
|
||||
# The regular row should still contain raw values
|
||||
assert data["row"] == {"id": 1, "name": "Alice"}
|
||||
|
||||
finally:
|
||||
ds.pm.unregister(name="TestRenderCellPlugin")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue