mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Working implementation of #216 which passes the tests
Reverted commit 5364fa7f33 (where I removed the
code that didn't work).
Added primary keys to order-by clause for sorting to get tests to pass
This commit is contained in:
parent
5364fa7f33
commit
e7c769ef30
3 changed files with 66 additions and 14 deletions
|
|
@ -55,6 +55,7 @@ def generate_sortable_rows(num):
|
|||
'sortable_with_nulls_2': rand.choice([
|
||||
None, rand.random(), rand.random()
|
||||
]),
|
||||
'text': rand.choice(['$null', '$blah']),
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -78,6 +79,7 @@ METADATA = {
|
|||
'sortable',
|
||||
'sortable_with_nulls',
|
||||
'sortable_with_nulls_2',
|
||||
'text',
|
||||
]
|
||||
},
|
||||
'no_primary_key': {
|
||||
|
|
@ -153,6 +155,7 @@ CREATE TABLE sortable (
|
|||
sortable integer,
|
||||
sortable_with_nulls real,
|
||||
sortable_with_nulls_2 real,
|
||||
text text,
|
||||
PRIMARY KEY (pk1, pk2)
|
||||
);
|
||||
|
||||
|
|
@ -235,7 +238,7 @@ CREATE VIEW simple_view AS
|
|||
]) + '\n'.join([
|
||||
'''INSERT INTO sortable VALUES (
|
||||
"{pk1}", "{pk2}", "{content}", {sortable},
|
||||
{sortable_with_nulls}, {sortable_with_nulls_2});
|
||||
{sortable_with_nulls}, {sortable_with_nulls_2}, "{text}");
|
||||
'''.format(
|
||||
**row
|
||||
).replace('None', 'null') for row in generate_sortable_rows(201)
|
||||
|
|
|
|||
|
|
@ -156,7 +156,7 @@ def test_database_page(app_client):
|
|||
}, {
|
||||
'columns': [
|
||||
'pk1', 'pk2', 'content', 'sortable', 'sortable_with_nulls',
|
||||
'sortable_with_nulls_2'
|
||||
'sortable_with_nulls_2', 'text',
|
||||
],
|
||||
'name': 'sortable',
|
||||
'count': 201,
|
||||
|
|
@ -426,6 +426,25 @@ def test_paginate_compound_keys_with_extra_filters(app_client):
|
|||
@pytest.mark.parametrize('query_string,sort_key,human_description_en', [
|
||||
('_sort=sortable', lambda row: row['sortable'], 'sorted by sortable'),
|
||||
('_sort_desc=sortable', lambda row: -row['sortable'], 'sorted by sortable descending'),
|
||||
(
|
||||
'_sort=sortable_with_nulls',
|
||||
lambda row: (
|
||||
1 if row['sortable_with_nulls'] is not None else 0,
|
||||
row['sortable_with_nulls']
|
||||
),
|
||||
'sorted by sortable_with_nulls'
|
||||
),
|
||||
(
|
||||
'_sort_desc=sortable_with_nulls',
|
||||
lambda row: (
|
||||
1 if row['sortable_with_nulls'] is None else 0,
|
||||
-row['sortable_with_nulls'] if row['sortable_with_nulls'] is not None else 0,
|
||||
row['content']
|
||||
),
|
||||
'sorted by sortable_with_nulls descending'
|
||||
),
|
||||
# text column contains '$null' - ensure it doesn't confuse pagination:
|
||||
('_sort=text', lambda row: row['text'], 'sorted by text'),
|
||||
])
|
||||
def test_sortable(app_client, query_string, sort_key, human_description_en):
|
||||
path = '/test_tables/sortable.json?_shape=objects&{}'.format(query_string)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue