-
Notifications
You must be signed in to change notification settings - Fork 4.2k
feat: Add sort order to cohorts endpoint #38149
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -583,6 +583,48 @@ def test_patch_cohort_with_group_id_missing_partition_id(self): | |||||||||
| assert data['developer_message'] == 'If group_id is specified, user_partition_id must also be specified.' | ||||||||||
| assert data['error_code'] == 'missing-user-partition-id' | ||||||||||
|
|
||||||||||
| def test_get_cohorts_default_ordering(self): | ||||||||||
| """ | ||||||||||
| Test that cohorts are returned in ascending alphabetical order by default. | ||||||||||
| """ | ||||||||||
| cohorts.add_cohort(self.course_key, "Zebra", "manual") | ||||||||||
| cohorts.add_cohort(self.course_key, "Alpha", "manual") | ||||||||||
| cohorts.add_cohort(self.course_key, "Mango", "manual") | ||||||||||
|
|
||||||||||
| path = reverse('api_cohorts:cohort_handler', kwargs={'course_key_string': self.course_str}) | ||||||||||
| self.client.login(username=self.staff_user.username, password=self.password) | ||||||||||
| response = self.client.get(path=path) | ||||||||||
|
|
||||||||||
| assert response.status_code == 200 | ||||||||||
| names = [c['name'] for c in response.json()] | ||||||||||
| assert names == ['Alpha', 'Mango', 'Zebra'] | ||||||||||
|
|
||||||||||
| def test_get_cohorts_desc_ordering(self): | ||||||||||
| """ | ||||||||||
| Test that cohorts are returned in descending alphabetical order when ordering=desc. | ||||||||||
| """ | ||||||||||
| cohorts.add_cohort(self.course_key, "Zebra", "manual") | ||||||||||
| cohorts.add_cohort(self.course_key, "Alpha", "manual") | ||||||||||
| cohorts.add_cohort(self.course_key, "Mango", "manual") | ||||||||||
|
|
||||||||||
| path = reverse('api_cohorts:cohort_handler', kwargs={'course_key_string': self.course_str}) | ||||||||||
| self.client.login(username=self.staff_user.username, password=self.password) | ||||||||||
| response = self.client.get(path=path, data={'ordering': 'desc'}) | ||||||||||
|
|
||||||||||
| assert response.status_code == 200 | ||||||||||
| names = [c['name'] for c in response.json()] | ||||||||||
| assert names == ['Zebra', 'Mango', 'Alpha'] | ||||||||||
|
|
||||||||||
| def test_get_cohorts_invalid_ordering(self): | ||||||||||
| """ | ||||||||||
| Test that an invalid ordering value returns a 400 error. | ||||||||||
| """ | ||||||||||
| path = reverse('api_cohorts:cohort_handler', kwargs={'course_key_string': self.course_str}) | ||||||||||
| self.client.login(username=self.staff_user.username, password=self.password) | ||||||||||
| response = self.client.get(path=path, data={'ordering': 'invalid'}) | ||||||||||
|
|
||||||||||
| assert response.status_code == 400 | ||||||||||
|
||||||||||
| assert response.status_code == 400 | |
| assert response.status_code == 400 | |
| data = response.json() | |
| assert data.get('error_code') == 'invalid-ordering-value' |
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.
The docstring says
orderingmust be 'asc' or 'desc', but the implementation silently treats any other value as ascending. To avoid surprising callers, either validate/normalizeorderinginget_course_cohorts(e.g., raise ValueError for invalid values) or adjust the docstring to reflect the fallback behavior.