mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Refactored generated_columns test, no longer in fixtures.db - refs #1391
This commit is contained in:
parent
83f6799a96
commit
2e8d924cdc
3 changed files with 59 additions and 82 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
from datasette.app import Datasette
|
from datasette.app import Datasette
|
||||||
from datasette.utils.sqlite import sqlite3, sqlite_version, supports_generated_columns
|
from datasette.utils.sqlite import sqlite3, sqlite_version
|
||||||
from datasette.utils.testing import TestClient
|
from datasette.utils.testing import TestClient
|
||||||
import click
|
import click
|
||||||
import contextlib
|
import contextlib
|
||||||
|
|
@ -118,8 +118,6 @@ def make_app_client(
|
||||||
immutables = []
|
immutables = []
|
||||||
conn = sqlite3.connect(filepath)
|
conn = sqlite3.connect(filepath)
|
||||||
conn.executescript(TABLES)
|
conn.executescript(TABLES)
|
||||||
if supports_generated_columns():
|
|
||||||
conn.executescript(GENERATED_COLUMNS_SQL)
|
|
||||||
for sql, params in TABLE_PARAMETERIZED_SQL:
|
for sql, params in TABLE_PARAMETERIZED_SQL:
|
||||||
with conn:
|
with conn:
|
||||||
conn.execute(sql, params)
|
conn.execute(sql, params)
|
||||||
|
|
@ -720,18 +718,6 @@ INSERT INTO "searchable_fts" (rowid, text1, text2)
|
||||||
SELECT rowid, text1, text2 FROM searchable;
|
SELECT rowid, text1, text2 FROM searchable;
|
||||||
"""
|
"""
|
||||||
|
|
||||||
GENERATED_COLUMNS_SQL = """
|
|
||||||
CREATE TABLE generated_columns (
|
|
||||||
body TEXT,
|
|
||||||
id INT GENERATED ALWAYS AS (json_extract(body, '$.number')) STORED,
|
|
||||||
consideration INT GENERATED ALWAYS AS (json_extract(body, '$.string')) STORED
|
|
||||||
);
|
|
||||||
INSERT INTO generated_columns (body) VALUES ('{
|
|
||||||
"number": 1,
|
|
||||||
"string": "This is a string"
|
|
||||||
}');
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
def assert_permissions_checked(datasette, actions):
|
def assert_permissions_checked(datasette, actions):
|
||||||
# actions is a list of "action" or (action, resource) tuples
|
# actions is a list of "action" or (action, resource) tuples
|
||||||
|
|
@ -792,9 +778,6 @@ def cli(db_filename, metadata, plugins_path, recreate, extra_db_filename):
|
||||||
for sql, params in TABLE_PARAMETERIZED_SQL:
|
for sql, params in TABLE_PARAMETERIZED_SQL:
|
||||||
with conn:
|
with conn:
|
||||||
conn.execute(sql, params)
|
conn.execute(sql, params)
|
||||||
if supports_generated_columns():
|
|
||||||
with conn:
|
|
||||||
conn.executescript(GENERATED_COLUMNS_SQL)
|
|
||||||
print(f"Test tables written to {db_filename}")
|
print(f"Test tables written to {db_filename}")
|
||||||
if metadata:
|
if metadata:
|
||||||
with open(metadata, "w") as fp:
|
with open(metadata, "w") as fp:
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,6 @@ from .fixtures import ( # noqa
|
||||||
generate_compound_rows,
|
generate_compound_rows,
|
||||||
generate_sortable_rows,
|
generate_sortable_rows,
|
||||||
make_app_client,
|
make_app_client,
|
||||||
supports_generated_columns,
|
|
||||||
EXPECTED_PLUGINS,
|
EXPECTED_PLUGINS,
|
||||||
METADATA,
|
METADATA,
|
||||||
)
|
)
|
||||||
|
|
@ -38,7 +37,7 @@ def test_homepage(app_client):
|
||||||
assert response.json.keys() == {"fixtures": 0}.keys()
|
assert response.json.keys() == {"fixtures": 0}.keys()
|
||||||
d = response.json["fixtures"]
|
d = response.json["fixtures"]
|
||||||
assert d["name"] == "fixtures"
|
assert d["name"] == "fixtures"
|
||||||
assert d["tables_count"] == 25 if supports_generated_columns() else 24
|
assert d["tables_count"] == 24
|
||||||
assert len(d["tables_and_views_truncated"]) == 5
|
assert len(d["tables_and_views_truncated"]) == 5
|
||||||
assert d["tables_and_views_more"] is True
|
assert d["tables_and_views_more"] is True
|
||||||
# 4 hidden FTS tables + no_primary_key (hidden in metadata)
|
# 4 hidden FTS tables + no_primary_key (hidden in metadata)
|
||||||
|
|
@ -271,22 +270,7 @@ def test_database_page(app_client):
|
||||||
},
|
},
|
||||||
"private": False,
|
"private": False,
|
||||||
},
|
},
|
||||||
] + (
|
] + [
|
||||||
[
|
|
||||||
{
|
|
||||||
"columns": ["body", "id", "consideration"],
|
|
||||||
"count": 1,
|
|
||||||
"foreign_keys": {"incoming": [], "outgoing": []},
|
|
||||||
"fts_table": None,
|
|
||||||
"hidden": False,
|
|
||||||
"name": "generated_columns",
|
|
||||||
"primary_keys": [],
|
|
||||||
"private": False,
|
|
||||||
}
|
|
||||||
]
|
|
||||||
if supports_generated_columns()
|
|
||||||
else []
|
|
||||||
) + [
|
|
||||||
{
|
{
|
||||||
"name": "infinity",
|
"name": "infinity",
|
||||||
"columns": ["value"],
|
"columns": ["value"],
|
||||||
|
|
@ -2074,16 +2058,30 @@ def test_paginate_using_link_header(app_client, qs):
|
||||||
sqlite_version() < (3, 31, 0),
|
sqlite_version() < (3, 31, 0),
|
||||||
reason="generated columns were added in SQLite 3.31.0",
|
reason="generated columns were added in SQLite 3.31.0",
|
||||||
)
|
)
|
||||||
def test_generated_columns_are_visible_in_datasette(app_client):
|
def test_generated_columns_are_visible_in_datasette():
|
||||||
response = app_client.get("/fixtures/generated_columns.json?_shape=array")
|
with make_app_client(
|
||||||
assert response.json == [
|
extra_databases={
|
||||||
{
|
"generated.db": """
|
||||||
"rowid": 1,
|
CREATE TABLE generated_columns (
|
||||||
"body": '{\n "number": 1,\n "string": "This is a string"\n}',
|
body TEXT,
|
||||||
"id": 1,
|
id INT GENERATED ALWAYS AS (json_extract(body, '$.number')) STORED,
|
||||||
"consideration": "This is a string",
|
consideration INT GENERATED ALWAYS AS (json_extract(body, '$.string')) STORED
|
||||||
|
);
|
||||||
|
INSERT INTO generated_columns (body) VALUES ('{
|
||||||
|
"number": 1,
|
||||||
|
"string": "This is a string"
|
||||||
|
}');"""
|
||||||
}
|
}
|
||||||
]
|
) as client:
|
||||||
|
response = app_client.get("/generated/generated_columns.json?_shape=array")
|
||||||
|
assert response.json == [
|
||||||
|
{
|
||||||
|
"rowid": 1,
|
||||||
|
"body": '{\n "number": 1,\n "string": "This is a string"\n}',
|
||||||
|
"id": 1,
|
||||||
|
"consideration": "This is a string",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
def test_http_options_request(app_client):
|
def test_http_options_request(app_client):
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
Tests for the datasette.database.Database class
|
Tests for the datasette.database.Database class
|
||||||
"""
|
"""
|
||||||
from datasette.database import Database, Results, MultipleValues
|
from datasette.database import Database, Results, MultipleValues
|
||||||
from datasette.utils.sqlite import sqlite3, supports_generated_columns
|
from datasette.utils.sqlite import sqlite3
|
||||||
from datasette.utils import Column
|
from datasette.utils import Column
|
||||||
from .fixtures import app_client, app_client_two_attached_databases_crossdb_enabled
|
from .fixtures import app_client, app_client_two_attached_databases_crossdb_enabled
|
||||||
import pytest
|
import pytest
|
||||||
|
|
@ -340,42 +340,38 @@ async def test_get_all_foreign_keys(db):
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_table_names(db):
|
async def test_table_names(db):
|
||||||
table_names = await db.table_names()
|
table_names = await db.table_names()
|
||||||
assert (
|
assert table_names == [
|
||||||
table_names
|
"simple_primary_key",
|
||||||
== [
|
"primary_key_multiple_columns",
|
||||||
"simple_primary_key",
|
"primary_key_multiple_columns_explicit_label",
|
||||||
"primary_key_multiple_columns",
|
"compound_primary_key",
|
||||||
"primary_key_multiple_columns_explicit_label",
|
"compound_three_primary_keys",
|
||||||
"compound_primary_key",
|
"foreign_key_references",
|
||||||
"compound_three_primary_keys",
|
"sortable",
|
||||||
"foreign_key_references",
|
"no_primary_key",
|
||||||
"sortable",
|
"123_starts_with_digits",
|
||||||
"no_primary_key",
|
"Table With Space In Name",
|
||||||
"123_starts_with_digits",
|
"table/with/slashes.csv",
|
||||||
"Table With Space In Name",
|
"complex_foreign_keys",
|
||||||
"table/with/slashes.csv",
|
"custom_foreign_key_label",
|
||||||
"complex_foreign_keys",
|
"units",
|
||||||
"custom_foreign_key_label",
|
"tags",
|
||||||
"units",
|
"searchable",
|
||||||
"tags",
|
"searchable_tags",
|
||||||
"searchable",
|
"searchable_fts",
|
||||||
"searchable_tags",
|
"searchable_fts_segments",
|
||||||
"searchable_fts",
|
"searchable_fts_segdir",
|
||||||
"searchable_fts_segments",
|
"searchable_fts_docsize",
|
||||||
"searchable_fts_segdir",
|
"searchable_fts_stat",
|
||||||
"searchable_fts_docsize",
|
"select",
|
||||||
"searchable_fts_stat",
|
"infinity",
|
||||||
"select",
|
"facet_cities",
|
||||||
"infinity",
|
"facetable",
|
||||||
"facet_cities",
|
"binary_data",
|
||||||
"facetable",
|
"roadside_attractions",
|
||||||
"binary_data",
|
"attraction_characteristic",
|
||||||
"roadside_attractions",
|
"roadside_attraction_characteristics",
|
||||||
"attraction_characteristic",
|
]
|
||||||
"roadside_attraction_characteristics",
|
|
||||||
]
|
|
||||||
+ (["generated_columns"] if supports_generated_columns() else [])
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue