mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Don't show hidden tables on index page, closes #455
Refs #460. Also bulked out HTML index page unit tests.
This commit is contained in:
parent
24bf79d2f0
commit
e04511410f
3 changed files with 68 additions and 6 deletions
|
|
@ -52,7 +52,9 @@ class IndexView(RenderMixin):
|
||||||
else hashlib.md5(name.encode("utf8")).hexdigest()[:6],
|
else hashlib.md5(name.encode("utf8")).hexdigest()[:6],
|
||||||
"path": self.database_url(name),
|
"path": self.database_url(name),
|
||||||
"tables_truncated": sorted(
|
"tables_truncated": sorted(
|
||||||
tables.values(), key=lambda t: t["count"] or 0, reverse=True
|
(t for t in tables.values() if t not in hidden_tables),
|
||||||
|
key=lambda t: t["count"] or 0,
|
||||||
|
reverse=True,
|
||||||
)[:5],
|
)[:5],
|
||||||
"tables_count": len(tables),
|
"tables_count": len(tables),
|
||||||
"tables_more": len(tables) > 5,
|
"tables_more": len(tables) > 5,
|
||||||
|
|
|
||||||
|
|
@ -30,14 +30,26 @@ def make_app_client(
|
||||||
config=None,
|
config=None,
|
||||||
filename="fixtures.db",
|
filename="fixtures.db",
|
||||||
is_immutable=False,
|
is_immutable=False,
|
||||||
|
extra_databases=None,
|
||||||
):
|
):
|
||||||
with tempfile.TemporaryDirectory() as tmpdir:
|
with tempfile.TemporaryDirectory() as tmpdir:
|
||||||
filepath = os.path.join(tmpdir, filename)
|
filepath = os.path.join(tmpdir, filename)
|
||||||
|
if is_immutable:
|
||||||
|
files = []
|
||||||
|
immutables = [filepath]
|
||||||
|
else:
|
||||||
|
files = [filepath]
|
||||||
|
immutables = []
|
||||||
conn = sqlite3.connect(filepath)
|
conn = sqlite3.connect(filepath)
|
||||||
conn.executescript(TABLES)
|
conn.executescript(TABLES)
|
||||||
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 extra_databases is not None:
|
||||||
|
for extra_filename, extra_sql in extra_databases.items():
|
||||||
|
extra_filepath = os.path.join(tmpdir, extra_filename)
|
||||||
|
sqlite3.connect(extra_filepath).executescript(extra_sql)
|
||||||
|
files.append(extra_filepath)
|
||||||
os.chdir(os.path.dirname(filepath))
|
os.chdir(os.path.dirname(filepath))
|
||||||
plugins_dir = os.path.join(tmpdir, "plugins")
|
plugins_dir = os.path.join(tmpdir, "plugins")
|
||||||
os.mkdir(plugins_dir)
|
os.mkdir(plugins_dir)
|
||||||
|
|
@ -52,8 +64,8 @@ def make_app_client(
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
ds = Datasette(
|
ds = Datasette(
|
||||||
[] if is_immutable else [filepath],
|
files,
|
||||||
immutables=[filepath] if is_immutable else [],
|
immutables=immutables,
|
||||||
memory=memory,
|
memory=memory,
|
||||||
cors=cors,
|
cors=cors,
|
||||||
metadata=METADATA,
|
metadata=METADATA,
|
||||||
|
|
@ -79,6 +91,13 @@ def app_client_no_files():
|
||||||
yield client
|
yield client
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="session")
|
||||||
|
def app_client_two_attached_databases():
|
||||||
|
yield from make_app_client(
|
||||||
|
extra_databases={"extra_database.db": EXTRA_DATABASE_SQL}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="session")
|
@pytest.fixture(scope="session")
|
||||||
def app_client_with_memory():
|
def app_client_with_memory():
|
||||||
yield from make_app_client(memory=True)
|
yield from make_app_client(memory=True)
|
||||||
|
|
@ -586,6 +605,22 @@ TABLE_PARAMETERIZED_SQL = [
|
||||||
("insert into binary_data (data) values (?);", [b"this is binary data"])
|
("insert into binary_data (data) values (?);", [b"this is binary data"])
|
||||||
]
|
]
|
||||||
|
|
||||||
|
EXTRA_DATABASE_SQL = """
|
||||||
|
CREATE TABLE searchable (
|
||||||
|
pk integer primary key,
|
||||||
|
text1 text,
|
||||||
|
text2 text
|
||||||
|
);
|
||||||
|
|
||||||
|
INSERT INTO searchable VALUES (1, 'barry cat', 'terry dog');
|
||||||
|
INSERT INTO searchable VALUES (2, 'terry dog', 'sara weasel');
|
||||||
|
|
||||||
|
CREATE VIRTUAL TABLE "searchable_fts"
|
||||||
|
USING FTS3 (text1, text2, content="searchable");
|
||||||
|
INSERT INTO "searchable_fts" (rowid, text1, text2)
|
||||||
|
SELECT rowid, text1, text2 FROM searchable;
|
||||||
|
"""
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# Can be called with data.db OR data.db metadata.json
|
# Can be called with data.db OR data.db metadata.json
|
||||||
arg_index = -1
|
arg_index = -1
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ from bs4 import BeautifulSoup as Soup
|
||||||
from .fixtures import ( # noqa
|
from .fixtures import ( # noqa
|
||||||
app_client,
|
app_client,
|
||||||
app_client_shorter_time_limit,
|
app_client_shorter_time_limit,
|
||||||
|
app_client_two_attached_databases,
|
||||||
app_client_with_hash,
|
app_client_with_hash,
|
||||||
app_client_with_memory,
|
app_client_with_memory,
|
||||||
make_app_client,
|
make_app_client,
|
||||||
|
|
@ -13,10 +14,34 @@ import re
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
|
|
||||||
|
|
||||||
def test_homepage(app_client):
|
def test_homepage(app_client_two_attached_databases):
|
||||||
response = app_client.get("/")
|
response = app_client_two_attached_databases.get("/")
|
||||||
assert response.status == 200
|
assert response.status == 200
|
||||||
assert "fixtures" in response.text
|
soup = Soup(response.body, "html.parser")
|
||||||
|
assert "Datasette Fixtures" == soup.find("h1").text
|
||||||
|
assert (
|
||||||
|
"An example SQLite database demonstrating Datasette"
|
||||||
|
== soup.select(".metadata-description")[0].text.strip()
|
||||||
|
)
|
||||||
|
# Should be two attached databases
|
||||||
|
assert [
|
||||||
|
{"href": "/fixtures", "text": "fixtures"},
|
||||||
|
{"href": "/extra_database", "text": "extra_database"},
|
||||||
|
] == [{"href": a["href"], "text": a.text.strip()} for a in soup.select("h2 a")]
|
||||||
|
# The second attached database should show count text and attached tables
|
||||||
|
h2 = soup.select("h2")[1]
|
||||||
|
assert "extra_database" == h2.text.strip()
|
||||||
|
counts_p, links_p = h2.find_all_next("p")
|
||||||
|
assert "7 rows in 5 tables, 5 rows in 4 hidden tables" == counts_p.text.strip().replace(
|
||||||
|
" ", ""
|
||||||
|
).replace(
|
||||||
|
"\n", ""
|
||||||
|
)
|
||||||
|
# We should only show visible, not hidden tables here:
|
||||||
|
table_links = [
|
||||||
|
{"href": a["href"], "text": a.text.strip()} for a in links_p.findAll("a")
|
||||||
|
]
|
||||||
|
assert [{"href": "/extra_database/searchable", "text": "searchable"}] == table_links
|
||||||
|
|
||||||
|
|
||||||
def test_memory_database_page(app_client_with_memory):
|
def test_memory_database_page(app_client_with_memory):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue