In-memory _schemas database tracking schemas of attached tables, closes #1150

This commit is contained in:
Simon Willison 2020-12-18 14:34:05 -08:00
commit ebc7aa287c
8 changed files with 279 additions and 6 deletions

View file

@ -293,7 +293,7 @@ def test_hook_extra_body_script(app_client, path, expected_extra_body_script):
def test_hook_asgi_wrapper(app_client):
response = app_client.get("/fixtures")
assert "fixtures" == response.headers["x-databases"]
assert "_schemas, fixtures" == response.headers["x-databases"]
def test_hook_extra_template_vars(restore_working_directory):

68
tests/test_schemas.py Normal file
View file

@ -0,0 +1,68 @@
from .fixtures import app_client
import pytest
def test_schemas_only_available_to_root(app_client):
cookie = app_client.actor_cookie({"id": "root"})
assert app_client.get("/_schemas").status == 403
assert app_client.get("/_schemas", cookies={"ds_actor": cookie}).status == 200
def test_schemas_databases(app_client):
cookie = app_client.actor_cookie({"id": "root"})
databases = app_client.get(
"/_schemas/databases.json?_shape=array", cookies={"ds_actor": cookie}
).json
assert len(databases) == 2
assert databases[0]["database_name"] == "_schemas"
assert databases[1]["database_name"] == "fixtures"
def test_schemas_tables(app_client):
cookie = app_client.actor_cookie({"id": "root"})
tables = app_client.get(
"/_schemas/tables.json?_shape=array", cookies={"ds_actor": cookie}
).json
assert len(tables) > 5
table = tables[0]
assert set(table.keys()) == {"rootpage", "table_name", "database_name", "sql"}
def test_schemas_indexes(app_client):
cookie = app_client.actor_cookie({"id": "root"})
indexes = app_client.get(
"/_schemas/indexes.json?_shape=array", cookies={"ds_actor": cookie}
).json
assert len(indexes) > 5
index = indexes[0]
assert set(index.keys()) == {
"partial",
"name",
"table_name",
"unique",
"seq",
"database_name",
"origin",
}
def test_schemas_foreign_keys(app_client):
cookie = app_client.actor_cookie({"id": "root"})
foreign_keys = app_client.get(
"/_schemas/foreign_keys.json?_shape=array", cookies={"ds_actor": cookie}
).json
assert len(foreign_keys) > 5
foreign_key = foreign_keys[0]
assert set(foreign_key.keys()) == {
"table",
"seq",
"on_update",
"on_delete",
"to",
"rowid",
"id",
"match",
"database_name",
"table_name",
"from",
}