_sort and _sort_desc parameters for table views

Allows for paginated sorted results based on a specified column.

Refs #189
This commit is contained in:
Simon Willison 2018-04-08 17:06:10 -07:00
commit 3704db669f
No known key found for this signature in database
GPG key ID: 17E2DEA2588B7F52
5 changed files with 146 additions and 24 deletions

View file

@ -1,6 +1,7 @@
from .fixtures import (
app_client,
generate_compound_rows,
generate_sortable_rows,
)
import pytest
@ -13,7 +14,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'] == 9
assert d['tables_count'] == 10
def test_database_page(app_client):
@ -106,6 +107,16 @@ def test_database_page(app_client):
'outgoing': [],
},
'label_column': None,
}, {
'columns': [
'pk1', 'pk2', 'content', 'sortable', 'sortable_with_nulls',
'sortable_with_nulls_2'
],
'name': 'sortable',
'count': 201,
'hidden': False,
'foreign_keys': {'incoming': [], 'outgoing': []},
'label_column': None,
}, {
'columns': ['pk', 'content'],
'name': 'table/with/slashes.csv',
@ -345,6 +356,30 @@ def test_paginate_compound_keys_with_extra_filters(app_client):
assert expected == [f['content'] for f in fetched]
@pytest.mark.parametrize('query_string,sort_key', [
('_sort=sortable', lambda row: row['sortable']),
('_sort_desc=sortable', lambda row: -row['sortable']),
])
def test_sortable(app_client, query_string, sort_key):
path = '/test_tables/sortable.jsono?{}'.format(query_string)
fetched = []
page = 0
while path:
page += 1
assert page < 100
response = app_client.get(path, gather_request=False)
fetched.extend(response.json['rows'])
path = response.json['next_url']
assert 5 == page
expected = list(generate_sortable_rows(201))
expected.sort(key=sort_key)
assert [
r['content'] for r in expected
] == [
r['content'] for r in fetched
]
@pytest.mark.parametrize('path,expected_rows', [
('/test_tables/simple_primary_key.json?content=hello', [
['1', 'hello'],