From 79303163b249829182f2e8d8c2d45e4526aa174f Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Wed, 15 May 2019 17:40:10 -0700 Subject: [PATCH] Row count fix + sort index page databases alphabetically Sorting alphabetically should fix a test failure in Python 3.5 Refs #460 --- datasette/views/index.py | 5 ++++- tests/test_html.py | 10 +++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/datasette/views/index.py b/datasette/views/index.py index 31116baf..d383b730 100644 --- a/datasette/views/index.py +++ b/datasette/views/index.py @@ -73,7 +73,7 @@ class IndexView(RenderMixin): "tables_and_views_more": (len(visible_tables) + len(views)) > TRUNCATE_AT, "tables_count": len(visible_tables), - "table_rows_sum": sum((t["count"] or 0) for t in tables.values()), + "table_rows_sum": sum((t["count"] or 0) for t in visible_tables), "hidden_table_rows_sum": sum( t["count"] for t in hidden_tables if t["count"] is not None ), @@ -81,6 +81,9 @@ class IndexView(RenderMixin): "views_count": len(views), } ) + + databases.sort(key=lambda database: database["name"]) + if as_format: headers = {} if self.ds.cors: diff --git a/tests/test_html.py b/tests/test_html.py index 07644c27..0b9b9178 100644 --- a/tests/test_html.py +++ b/tests/test_html.py @@ -25,15 +25,15 @@ def test_homepage(app_client_two_attached_databases): ) # Should be two attached databases assert [ - {"href": "/fixtures", "text": "fixtures"}, {"href": "/extra_database", "text": "extra_database"}, + {"href": "/fixtures", "text": "fixtures"}, ] == [{"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] + # The first attached database should show count text and attached tables + h2 = soup.select("h2")[0] assert "extra_database" == h2.text.strip() - counts_p, links_p = h2.find_all_next("p") + counts_p, links_p = h2.find_all_next("p")[:2] assert ( - "7 rows in 1 table, 5 rows in 4 hidden tables, 1 view" == counts_p.text.strip() + "2 rows in 1 table, 5 rows in 4 hidden tables, 1 view" == counts_p.text.strip() ) # We should only show visible, not hidden tables here: table_links = [