-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
contrib/alchemy: Use sort_attribute as default sort for instances, support DESC sorting #152
Merged
lyschoening
merged 2 commits into
biosustain:master
from
albertodonato:sort-attribute-desc
Nov 30, 2018
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -448,3 +448,78 @@ class Meta: | |
"$id": 1, | ||
"username": "foo" | ||
}, response.json) | ||
|
||
|
||
class SQLAlchemySortTestCase(BaseTestCase): | ||
|
||
def setUp(self): | ||
super(SQLAlchemySortTestCase, self).setUp() | ||
self.app.config['SQLALCHEMY_ENGINE'] = 'sqlite://' | ||
self.api = Api(self.app) | ||
self.sa = sa = SQLAlchemy( | ||
self.app, session_options={"autoflush": False}) | ||
|
||
class Type(sa.Model): | ||
id = sa.Column(sa.Integer, primary_key=True) | ||
name = sa.Column(sa.String(60), nullable=False) | ||
|
||
class Machine(sa.Model): | ||
id = sa.Column(sa.Integer, primary_key=True) | ||
name = sa.Column(sa.String(60), nullable=False) | ||
|
||
type_id = sa.Column(sa.Integer, sa.ForeignKey(Type.id)) | ||
type = sa.relationship(Type, foreign_keys=[type_id]) | ||
|
||
sa.create_all() | ||
|
||
class MachineResource(ModelResource): | ||
class Meta: | ||
model = Machine | ||
|
||
class Schema: | ||
type = fields.ToOne('type') | ||
|
||
class TypeResource(ModelResource): | ||
class Meta: | ||
model = Type | ||
sort_attribute = ('name', True) | ||
|
||
self.MachineResource = MachineResource | ||
self.TypeResource = TypeResource | ||
|
||
self.api.add_resource(MachineResource) | ||
self.api.add_resource(TypeResource) | ||
|
||
def test_default_sorting_with_desc(self): | ||
self.client.post('/type', data={"name": "aaa"}) | ||
self.client.post('/type', data={"name": "ccc"}) | ||
self.client.post('/type', data={"name": "bbb"}) | ||
response = self.client.get('/type') | ||
self.assert200(response) | ||
self.assertJSONEqual( | ||
[{'$uri': '/type/2', 'name': 'ccc'}, | ||
{'$uri': '/type/3', 'name': 'bbb'}, | ||
{'$uri': '/type/1', 'name': 'aaa'}], | ||
response.json) | ||
|
||
def test_sort_by_related_field(self): | ||
response = self.client.post('/type', data={"name": "aaa"}) | ||
self.assert200(response) | ||
aaa_uri = response.json["$uri"] | ||
response = self.client.post('/type', data={"name": "bbb"}) | ||
self.assert200(response) | ||
bbb_uri = response.json["$uri"] | ||
self.client.post( | ||
'/machine', data={"name": "foo", "type": {"$ref": aaa_uri}}) | ||
self.assert200(response) | ||
self.client.post( | ||
'/machine', data={"name": "bar", "type": {"$ref": bbb_uri}}) | ||
self.assert200(response) | ||
response = self.client.get('/machine?sort={"type": true}') | ||
self.assert200(response) | ||
type_uris = [entry['type']['$ref'] for entry in response.json] | ||
self.assertTrue(type_uris, [bbb_uri, aaa_uri]) | ||
response = self.client.get('/machine?sort={"type": false}') | ||
self.assert200(response) | ||
type_uris = [entry['type']['$ref'] for entry in response.json] | ||
self.assertTrue(type_uris, [bbb_uri, aaa_uri]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if we pass true of false the test check the same thing |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it sort everything by default on primary key column ?
What if we do not want to have this behaviour ?
Sorting can have substantial impact on performance for instance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That has been the behavior for a while. Without an intrinsic sort order, it is not possible to do reliable pagination. Since the primary key is indexed, it is a good default. Do you see a practical scenario where no sort is preferred? We could support
sort_attribute=False
to mean no sorting.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback, that's a good point.
I'd take inspiration from django. If Pagination is activated and if the query is not sorted, we could raise a warning. But having a default on primary key is not that interesting for our use case (we would prefer on last_updated for instance). Though, not a big deal either. Feel free to ignore.
reference:
https://github.com/django/django/blob/ed4bfacb3c942d0d32795e1a733b1b9367afd2e9/django/core/paginator.py#L110