Support for generated columns, closes #1116

This commit is contained in:
Simon Willison 2020-11-30 12:01:15 -08:00
commit 37f87b5e52
3 changed files with 76 additions and 8 deletions

View file

@ -1,5 +1,6 @@
from datasette.app import Datasette
from datasette.plugins import DEFAULT_PLUGINS
from datasette.utils import detect_json1
from datasette.utils import detect_json1, sqlite3
from datasette.version import __version__
from .fixtures import ( # noqa
app_client,
@ -514,7 +515,14 @@ def test_database_page(app_client):
},
{
"name": "searchable_fts",
"columns": ["text1", "text2", "name with . and spaces"],
"columns": [
"text1",
"text2",
"name with . and spaces",
"searchable_fts",
"docid",
"__langid",
],
"primary_keys": [],
"count": 2,
"hidden": True,
@ -1913,3 +1921,46 @@ def test_paginate_using_link_header(app_client, qs):
else:
path = None
assert num_pages == 21
@pytest.mark.skipif(
tuple(
map(
int,
sqlite3.connect(":memory:")
.execute("select sqlite_version()")
.fetchone()[0]
.split("."),
)
)
< (3, 31, 0),
reason="generated columns were added in SQLite 3.31.0",
)
@pytest.mark.asyncio
async def test_generated_columns_are_visible_in_datasette(tmp_path_factory):
db_directory = tmp_path_factory.mktemp("dbs")
db_path = db_directory / "test.db"
conn = sqlite3.connect(str(db_path))
conn.executescript(
"""
CREATE TABLE deeds (
body TEXT,
id INT GENERATED ALWAYS AS (json_extract(body, '$.id')) STORED,
consideration INT GENERATED ALWAYS AS (json_extract(body, '$.consideration')) STORED
);
INSERT INTO deeds (body) VALUES ('{
"id": 1,
"consideration": "This is the consideration"
}');
"""
)
datasette = Datasette([db_path])
response = await datasette.client.get("/test/deeds.json?_shape=array")
assert response.json() == [
{
"rowid": 1,
"body": '{\n "id": 1,\n "consideration": "This is the consideration"\n }',
"id": 1,
"consideration": "This is the consideration",
}
]

View file

@ -120,6 +120,7 @@ async def test_table_columns(db, table, expected):
notnull=0,
default_value=None,
is_pk=1,
hidden=0,
),
Column(
cid=1,
@ -128,6 +129,7 @@ async def test_table_columns(db, table, expected):
notnull=0,
default_value=None,
is_pk=0,
hidden=0,
),
Column(
cid=2,
@ -136,6 +138,7 @@ async def test_table_columns(db, table, expected):
notnull=0,
default_value=None,
is_pk=0,
hidden=0,
),
Column(
cid=3,
@ -144,6 +147,7 @@ async def test_table_columns(db, table, expected):
notnull=0,
default_value=None,
is_pk=0,
hidden=0,
),
Column(
cid=4,
@ -152,6 +156,7 @@ async def test_table_columns(db, table, expected):
notnull=0,
default_value=None,
is_pk=0,
hidden=0,
),
Column(
cid=5,
@ -160,6 +165,7 @@ async def test_table_columns(db, table, expected):
notnull=0,
default_value=None,
is_pk=0,
hidden=0,
),
Column(
cid=6,
@ -168,6 +174,7 @@ async def test_table_columns(db, table, expected):
notnull=0,
default_value=None,
is_pk=0,
hidden=0,
),
Column(
cid=7,
@ -176,6 +183,7 @@ async def test_table_columns(db, table, expected):
notnull=0,
default_value=None,
is_pk=0,
hidden=0,
),
Column(
cid=8,
@ -184,6 +192,7 @@ async def test_table_columns(db, table, expected):
notnull=0,
default_value=None,
is_pk=0,
hidden=0,
),
Column(
cid=9,
@ -192,6 +201,7 @@ async def test_table_columns(db, table, expected):
notnull=0,
default_value=None,
is_pk=0,
hidden=0,
),
],
),
@ -205,6 +215,7 @@ async def test_table_columns(db, table, expected):
notnull=0,
default_value=None,
is_pk=1,
hidden=0,
),
Column(
cid=1,
@ -213,6 +224,7 @@ async def test_table_columns(db, table, expected):
notnull=0,
default_value=None,
is_pk=2,
hidden=0,
),
Column(
cid=2,
@ -221,6 +233,7 @@ async def test_table_columns(db, table, expected):
notnull=0,
default_value=None,
is_pk=0,
hidden=0,
),
Column(
cid=3,
@ -229,6 +242,7 @@ async def test_table_columns(db, table, expected):
notnull=0,
default_value=None,
is_pk=0,
hidden=0,
),
Column(
cid=4,
@ -237,6 +251,7 @@ async def test_table_columns(db, table, expected):
notnull=0,
default_value=None,
is_pk=0,
hidden=0,
),
Column(
cid=5,
@ -245,6 +260,7 @@ async def test_table_columns(db, table, expected):
notnull=0,
default_value=None,
is_pk=0,
hidden=0,
),
Column(
cid=6,
@ -253,6 +269,7 @@ async def test_table_columns(db, table, expected):
notnull=0,
default_value=None,
is_pk=0,
hidden=0,
),
],
),