Skip to content

Commit

Permalink
If max_returned_rows==page_size, increment max_returned_rows
Browse files Browse the repository at this point in the history
Fixes #230, where if the two were equal pagination didn't work correctly.
  • Loading branch information
simonw committed Apr 26, 2018
1 parent 02ee31c commit 4504d51
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
11 changes: 7 additions & 4 deletions datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,13 @@ def sql_operation_in_thread():
try:
cursor = conn.cursor()
cursor.execute(sql, params or {})
if self.max_returned_rows and truncate:
rows = cursor.fetchmany(self.max_returned_rows + 1)
truncated = len(rows) > self.max_returned_rows
rows = rows[:self.max_returned_rows]
max_returned_rows = self.max_returned_rows
if max_returned_rows == self.page_size:
max_returned_rows += 1
if max_returned_rows and truncate:
rows = cursor.fetchmany(max_returned_rows + 1)
truncated = len(rows) > max_returned_rows
rows = rows[:max_returned_rows]
else:
rows = cursor.fetchall()
truncated = False
Expand Down
8 changes: 6 additions & 2 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import time


def app_client(sql_time_limit_ms=None):
def app_client(sql_time_limit_ms=None, max_returned_rows=None):
with tempfile.TemporaryDirectory() as tmpdir:
filepath = os.path.join(tmpdir, 'test_tables.db')
conn = sqlite3.connect(filepath)
Expand All @@ -21,7 +21,7 @@ def app_client(sql_time_limit_ms=None):
ds = Datasette(
[filepath],
page_size=50,
max_returned_rows=100,
max_returned_rows=max_returned_rows or 100,
sql_time_limit_ms=sql_time_limit_ms or 20,
metadata=METADATA,
plugins_dir=plugins_dir,
Expand All @@ -38,6 +38,10 @@ def app_client_longer_time_limit():
yield from app_client(200)


def app_client_returend_rows_matches_page_size():
yield from app_client(max_returned_rows=50)


def generate_compound_rows(num):
for a, b, c in itertools.islice(
itertools.product(string.ascii_lowercase, repeat=3), num
Expand Down
15 changes: 15 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .fixtures import (
app_client,
app_client_longer_time_limit,
app_client_returend_rows_matches_page_size,
generate_compound_rows,
generate_sortable_rows,
METADATA,
Expand All @@ -9,6 +10,7 @@

pytest.fixture(scope='module')(app_client)
pytest.fixture(scope='module')(app_client_longer_time_limit)
pytest.fixture(scope='module')(app_client_returend_rows_matches_page_size)


def test_homepage(app_client):
Expand Down Expand Up @@ -691,3 +693,16 @@ def test_plugins_json(app_client):
'static': False,
'templates': False
} in response.json


def test_page_size_matching_max_returned_rows(app_client_returend_rows_matches_page_size):
fetched = []
path = '/test_tables/no_primary_key.json'
while path:
response = app_client_returend_rows_matches_page_size.get(
path, gather_request=False
)
fetched.extend(response.json['rows'])
assert len(response.json['rows']) in (1, 50)
path = response.json['next_url']
assert 201 == len(fetched)

0 comments on commit 4504d51

Please sign in to comment.