From cbb59e3801868e1c284ad50b66343cd4358e516c Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Tue, 14 Nov 2017 10:13:37 -0800 Subject: [PATCH] Handle tables with names that start with digits --- datasette/utils.py | 2 +- tests/test_app.py | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/datasette/utils.py b/datasette/utils.py index aefe361a..87f95811 100644 --- a/datasette/utils.py +++ b/datasette/utils.py @@ -121,7 +121,7 @@ def path_with_ext(request, ext): _css_re = re.compile(r'''['"\n\\]''') -_boring_table_name_re = re.compile(r'^[a-zA-Z0-9_]+$') +_boring_table_name_re = re.compile(r'^[a-zA-Z_][a-zA-Z0-9_]*$') def escape_css_string(s): diff --git a/tests/test_app.py b/tests/test_app.py index 95a499a0..a4405c8e 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -26,7 +26,7 @@ def test_homepage(app_client): assert response.json.keys() == {'test_tables': 0}.keys() d = response.json['test_tables'] assert d['name'] == 'test_tables' - assert d['tables_count'] == 5 + assert d['tables_count'] == 6 def test_database_page(app_client): @@ -39,6 +39,10 @@ def test_database_page(app_client): data = response.json assert 'test_tables' == data['database'] assert [{ + 'columns': ['content'], + 'name': '123_starts_with_digits', + 'table_rows': 0, + }, { 'columns': ['pk', 'content'], 'name': 'Table With Space In Name', 'table_rows': 0, @@ -125,6 +129,7 @@ def test_table_with_slashes_in_name(app_client): @pytest.mark.parametrize('path,expected_rows,expected_pages', [ ('/test_tables/no_primary_key.jsono', 201, 5), ('/test_tables/paginated_view.jsono', 201, 5), + ('/test_tables/123_starts_with_digits.jsono', 0, 1), ]) def test_paginate_tables_and_views(app_client, path, expected_rows, expected_pages): fetched = [] @@ -198,6 +203,10 @@ CREATE TABLE no_primary_key ( content text ); +CREATE TABLE [123_starts_with_digits] ( + content text +); + CREATE VIEW paginated_view AS SELECT content,