Commit c303d2cc by Renzo Lucioni Committed by GitHub

Merge pull request #250 from edx/renzo/uuid-list-filter

Allow programs to be filtered by comma-separated UUIDs
parents a9c5877c 61a3e903
...@@ -93,7 +93,8 @@ class CourseRunFilter(django_filters.FilterSet): ...@@ -93,7 +93,8 @@ class CourseRunFilter(django_filters.FilterSet):
class ProgramFilter(django_filters.FilterSet): class ProgramFilter(django_filters.FilterSet):
type = django_filters.CharFilter(name='type__name', lookup_expr='iexact') type = django_filters.CharFilter(name='type__name', lookup_expr='iexact')
uuids = CharListFilter(name='uuid', lookup_type='in')
class Meta: class Meta:
model = Program model = Program
fields = ['type'] fields = ['type', 'uuids']
...@@ -8,6 +8,8 @@ from course_discovery.apps.course_metadata.tests.factories import ProgramFactory ...@@ -8,6 +8,8 @@ from course_discovery.apps.course_metadata.tests.factories import ProgramFactory
class ProgramViewSetTests(APITestCase): class ProgramViewSetTests(APITestCase):
list_path = reverse('api:v1:program-list')
def setUp(self): def setUp(self):
super(ProgramViewSetTests, self).setUp() super(ProgramViewSetTests, self).setUp()
self.user = UserFactory(is_staff=True, is_superuser=True) self.user = UserFactory(is_staff=True, is_superuser=True)
...@@ -16,13 +18,11 @@ class ProgramViewSetTests(APITestCase): ...@@ -16,13 +18,11 @@ class ProgramViewSetTests(APITestCase):
def test_authentication(self): def test_authentication(self):
""" Verify the endpoint requires the user to be authenticated. """ """ Verify the endpoint requires the user to be authenticated. """
url = reverse('api:v1:program-list') response = self.client.get(self.list_path)
response = self.client.get(url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.client.logout() self.client.logout()
response = self.client.get(url) response = self.client.get(self.list_path)
self.assertEqual(response.status_code, 403) self.assertEqual(response.status_code, 403)
def test_get(self): def test_get(self):
...@@ -35,16 +35,15 @@ class ProgramViewSetTests(APITestCase): ...@@ -35,16 +35,15 @@ class ProgramViewSetTests(APITestCase):
def test_list(self): def test_list(self):
""" Verify the endpoint returns a list of all programs. """ """ Verify the endpoint returns a list of all programs. """
url = reverse('api:v1:program-list')
ProgramFactory.create_batch(3) ProgramFactory.create_batch(3)
response = self.client.get(url) response = self.client.get(self.list_path)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['results'], ProgramSerializer(Program.objects.all(), many=True).data) self.assertEqual(response.data['results'], ProgramSerializer(Program.objects.all(), many=True).data)
def test_filter_by_type(self): def test_filter_by_type(self):
""" Verify that the endpoint filters programs to those of a given type. """ """ Verify that the endpoint filters programs to those of a given type. """
url = reverse('api:v1:program-list') + '?type=' url = self.list_path + '?type='
self.program.type = ProgramTypeFactory(name='Foo') self.program.type = ProgramTypeFactory(name='Foo')
self.program.save() # pylint: disable=no-member self.program.save() # pylint: disable=no-member
...@@ -54,3 +53,16 @@ class ProgramViewSetTests(APITestCase): ...@@ -54,3 +53,16 @@ class ProgramViewSetTests(APITestCase):
response = self.client.get(url + 'bar') response = self.client.get(url + 'bar')
self.assertEqual(response.data['results'], []) self.assertEqual(response.data['results'], [])
def test_filter_by_uuids(self):
""" Verify that the endpoint filters programs to those matching the provided UUIDs. """
url = self.list_path + '?uuids='
programs = [ProgramFactory(), self.program]
uuids = [str(p.uuid) for p in programs]
# Create a third program, which should be filtered out.
ProgramFactory()
response = self.client.get(url + ','.join(uuids))
self.assertEqual(response.data['results'], ProgramSerializer(programs, many=True).data)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment