From 2bfd111d65c892e54b06c2c9498ce2b4cc3e67ff Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Wed, 23 May 2018 06:41:14 -0700 Subject: [PATCH] Faceting no longer breaks pagination, fixes #282 --- datasette/views/table.py | 2 +- tests/test_api.py | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/datasette/views/table.py b/datasette/views/table.py index 07e3cd59..cc552eb5 100644 --- a/datasette/views/table.py +++ b/datasette/views/table.py @@ -556,7 +556,7 @@ class TableView(RowTableShared): """.format( col=escape_sqlite(column), from_sql=from_sql, - and_or_where='and' if where_clause else 'where', + and_or_where='and' if from_sql_where_clauses else 'where', limit=facet_size+1, ) try: diff --git a/tests/test_api.py b/tests/test_api.py index aa77c269..ec81d520 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -7,6 +7,7 @@ from .fixtures import ( METADATA, ) import pytest +import urllib pytest.fixture(scope='module')(app_client) pytest.fixture(scope='module')(app_client_shorter_time_limit) @@ -533,6 +534,8 @@ def test_table_with_reserved_word_name(app_client): ('/test_tables/paginated_view.json?_size=25', 201, 9), ('/test_tables/paginated_view.json?_size=max', 201, 3), ('/test_tables/123_starts_with_digits.json', 0, 1), + # Ensure faceting doesn't break pagination: + ('/test_tables/compound_three_primary_keys.json?_facet=pk1', 1001, 21), ]) def test_paginate_tables_and_views(app_client, path, expected_rows, expected_pages): fetched = [] @@ -545,8 +548,10 @@ def test_paginate_tables_and_views(app_client, path, expected_rows, expected_pag path = response.json['next_url'] if path: assert response.json['next'] - assert '_next={}'.format(response.json['next']) in path - assert count < 10, 'Possible infinite loop detected' + assert urllib.parse.urlencode({ + '_next': response.json['next'] + }) in path + assert count < 30, 'Possible infinite loop detected' assert expected_rows == len(fetched) assert expected_pages == count