mirror of
https://github.com/simonw/datasette.git
synced 2026-06-12 20:16:56 +02:00
Add row and query JSON extras
This commit is contained in:
parent
0fa872d438
commit
4d6daa175a
9 changed files with 861 additions and 129 deletions
|
|
@ -426,6 +426,28 @@ async def test_row_foreign_key_tables(ds_client):
|
|||
]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_row_extras(ds_client):
|
||||
response = await ds_client.get(
|
||||
"/fixtures/simple_primary_key/1.json?_extra=database,table,primary_keys,query,request,debug,foreign_key_tables"
|
||||
)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["database"] == "fixtures"
|
||||
assert data["table"] == "simple_primary_key"
|
||||
assert data["primary_keys"] == ["id"]
|
||||
assert data["query"]["sql"] == 'select * from simple_primary_key where "id"=:p0'
|
||||
assert data["query"]["params"] == {"p0": "1"}
|
||||
assert data["request"]["path"] == "/fixtures/simple_primary_key/1.json"
|
||||
assert data["debug"]["url_vars"] == {
|
||||
"database": "fixtures",
|
||||
"table": "simple_primary_key",
|
||||
"pks": "1",
|
||||
"format": "json",
|
||||
}
|
||||
assert len(data["foreign_key_tables"]) == 5
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_row_extra_render_cell():
|
||||
"""Test that _extra=render_cell returns rendered HTML from render_cell plugin hook on row pages"""
|
||||
|
|
|
|||
|
|
@ -132,7 +132,7 @@ def test_render_cell_extra_example_explains_row_and_column_mapping():
|
|||
|
||||
def test_debug_and_request_extra_examples_are_documented():
|
||||
content = (docs_path / "json_api.rst").read_text()
|
||||
section = content.split(".. _json_api_extra:")[-1].split(".. _table_arguments:")[0]
|
||||
section = content.split("Table JSON responses")[-1].split("Row JSON responses")[0]
|
||||
|
||||
debug_section = section.split("``debug``")[-1].split("``request``")[0]
|
||||
assert "GET /fixtures/facetable.json?_extra=debug" in debug_section
|
||||
|
|
@ -143,6 +143,20 @@ def test_debug_and_request_extra_examples_are_documented():
|
|||
assert '"full_path":' in request_section
|
||||
|
||||
|
||||
def test_row_and_query_extra_sections_are_documented():
|
||||
content = (docs_path / "json_api.rst").read_text()
|
||||
assert "Row JSON responses" in content
|
||||
assert (
|
||||
"``GET /fixtures/simple_primary_key/1.json?_extra=foreign_key_tables``"
|
||||
in content
|
||||
)
|
||||
assert "Query JSON responses" in content
|
||||
assert "``GET /fixtures/-/query.json?sql=select+1+as+one&_extra=query``" in content
|
||||
assert (
|
||||
"``GET /fixtures/neighborhood_search.json?text=town&_extra=query``" in content
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def documented_labels():
|
||||
labels = set()
|
||||
|
|
|
|||
|
|
@ -68,6 +68,55 @@ async def test_table_shape_arrayfirst(ds_client):
|
|||
]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_query_extras_for_arbitrary_sql(ds_client):
|
||||
response = await ds_client.get(
|
||||
"/fixtures/-/query.json?"
|
||||
+ urllib.parse.urlencode(
|
||||
{
|
||||
"sql": "select 1 as one",
|
||||
"_extra": "columns,database,query,request,debug",
|
||||
}
|
||||
)
|
||||
)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["rows"] == [{"one": 1}]
|
||||
assert data["columns"] == ["one"]
|
||||
assert data["database"] == "fixtures"
|
||||
assert data["query"]["sql"] == "select 1 as one"
|
||||
assert data["request"]["path"] == "/fixtures/-/query.json"
|
||||
assert data["debug"]["url_vars"] == {
|
||||
"database": "fixtures",
|
||||
"format": "json",
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_query_extras_for_stored_query(ds_client):
|
||||
response = await ds_client.get(
|
||||
"/fixtures/neighborhood_search.json?"
|
||||
+ urllib.parse.urlencode(
|
||||
{
|
||||
"text": "town",
|
||||
"_extra": "columns,database,query,request,debug",
|
||||
}
|
||||
)
|
||||
)
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["columns"] == ["_neighborhood", "name", "state"]
|
||||
assert data["database"] == "fixtures"
|
||||
assert data["query"]["sql"].strip().startswith("select _neighborhood")
|
||||
assert data["query"]["params"]["text"] == "town"
|
||||
assert data["request"]["path"] == "/fixtures/neighborhood_search.json"
|
||||
assert data["debug"]["url_vars"] == {
|
||||
"database": "fixtures",
|
||||
"table": "neighborhood_search",
|
||||
"format": "json",
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_table_shape_objects(ds_client):
|
||||
response = await ds_client.get("/fixtures/simple_primary_key.json?_shape=objects")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue