-
Notifications
You must be signed in to change notification settings - Fork 4
Feature: Add order_by method to filter containers #61
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
Merged
cortadocodes
merged 8 commits into
release/0.1.7
from
feature/add-extra-methods-to-filter-containers
Jan 5, 2021
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0dd78cd
IMP: Add order_by method to FilterSet and FilterList
cortadocodes 9bcb629
IMP: Provide better error message for non-existent attribute when fil…
cortadocodes ac1098d
DOC: Clarify that a new FilterList is returned by order_by
cortadocodes f3e45f8
TST: Test ordering by list attributes
cortadocodes e14d0e4
IMP: Add ability to reverse ordering direction
cortadocodes fb68d06
TST: Change test order
cortadocodes c12ab59
TST: Change test name; simplify test object
cortadocodes 6545ff1
REF: Use AttributError in Filterable
cortadocodes 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| from tests.base import BaseTestCase | ||
|
|
||
| from octue import exceptions | ||
| from octue.mixins import Filterable | ||
| from octue.resources.filter_containers import FilterList, FilterSet | ||
|
|
||
|
|
||
| class Cat(Filterable): | ||
| def __init__(self, name=None, previous_names=None, age=None): | ||
| self.name = name | ||
| self.previous_names = previous_names | ||
| self.age = age | ||
|
|
||
|
|
||
| class TestFilterSet(BaseTestCase): | ||
| def test_ordering_by_a_non_existent_attribute(self): | ||
| """ Ensure an error is raised if ordering is attempted by a non-existent attribute. """ | ||
| filter_set = FilterSet([Cat(age=5), Cat(age=4), Cat(age=3)]) | ||
| with self.assertRaises(exceptions.InvalidInputException): | ||
| filter_set.order_by("dog-likeness") | ||
|
|
||
| def test_order_by_with_string_attribute(self): | ||
| """ Test ordering a FilterSet by a string attribute returns an appropriately ordered FilterList. """ | ||
| cats = [Cat(name="Zorg"), Cat(name="James"), Cat(name="Princess Carolyn")] | ||
| sorted_filter_set = FilterSet(cats).order_by("name") | ||
| self.assertEqual(sorted_filter_set, FilterList([cats[1], cats[2], cats[0]])) | ||
|
|
||
| def test_order_by_with_int_attribute(self): | ||
| """ Test ordering a FilterSet by an integer attribute returns an appropriately ordered FilterList. """ | ||
| cats = [Cat(age=5), Cat(age=4), Cat(age=3)] | ||
| sorted_filter_set = FilterSet(cats).order_by("age") | ||
| self.assertEqual(sorted_filter_set, FilterList(reversed(cats))) | ||
|
|
||
| def test_order_by_list_attribute(self): | ||
| """ Test that ordering by list attributes orders by the size of the list. """ | ||
| cats = [Cat(previous_names=["Scatta", "Catta"]), Cat(previous_names=["Kitty"]), Cat(previous_names=[])] | ||
| sorted_filter_set = FilterSet(cats).order_by("previous_names") | ||
| self.assertEqual(sorted_filter_set, FilterList(reversed(cats))) | ||
|
|
||
| def test_order_by_in_reverse(self): | ||
| """ Test ordering in reverse works correctly. """ | ||
| cats = [Cat(age=5), Cat(age=3), Cat(age=4)] | ||
| sorted_filter_set = FilterSet(cats).order_by("age", reverse=True) | ||
| self.assertEqual(sorted_filter_set, FilterList([cats[0], cats[2], cats[1]])) |
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.
Uh oh!
There was an error while loading. Please reload this page.