Commit 3dd051a1 by Renzo Lucioni Committed by Clinton Blackburn

Roll back attempted program list optimizations

parent 54feb857
from django.test import TestCase from django.test import TestCase
from course_discovery.apps.api.fields import ImageField, StdImageSerializerField from course_discovery.apps.api.fields import ImageField
from course_discovery.apps.api.tests.test_serializers import make_request
from course_discovery.apps.core.tests.helpers import make_image_file
from course_discovery.apps.course_metadata.tests.factories import ProgramFactory
class ImageFieldTests(TestCase): class ImageFieldTests(TestCase):
...@@ -16,27 +13,3 @@ class ImageFieldTests(TestCase): ...@@ -16,27 +13,3 @@ class ImageFieldTests(TestCase):
'width': None 'width': None
} }
self.assertEqual(ImageField().to_representation(value), expected) self.assertEqual(ImageField().to_representation(value), expected)
# pylint: disable=no-member
class StdImageSerializerFieldTests(TestCase):
def test_to_representation(self):
request = make_request()
# TODO Create test-only model to avoid unnecessary dependency on Program model.
program = ProgramFactory(banner_image=make_image_file('test.jpg'))
field = StdImageSerializerField()
field._context = {'request': request} # pylint: disable=protected-access
expected = {
size_key: {
'url': '{}{}'.format(
'http://testserver',
getattr(program.banner_image, size_key).url
),
'width': program.banner_image.field.variations[size_key]['width'],
'height': program.banner_image.field.variations[size_key]['height']
}
for size_key in program.banner_image.field.variations
}
self.assertDictEqual(field.to_representation(program.banner_image), expected)
...@@ -141,7 +141,7 @@ class CatalogViewSetTests(ElasticsearchTestMixin, SerializationMixin, OAuth2Mixi ...@@ -141,7 +141,7 @@ class CatalogViewSetTests(ElasticsearchTestMixin, SerializationMixin, OAuth2Mixi
CourseRunFactory(enrollment_end=enrollment_end, course__title='ABC Test Course 2') CourseRunFactory(enrollment_end=enrollment_end, course__title='ABC Test Course 2')
CourseRunFactory(enrollment_end=enrollment_end, course=self.course) CourseRunFactory(enrollment_end=enrollment_end, course=self.course)
with self.assertNumQueries(40): with self.assertNumQueries(41):
response = self.client.get(url) response = self.client.get(url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertListEqual(response.data['results'], self.serialize_catalog_course(courses, many=True)) self.assertListEqual(response.data['results'], self.serialize_catalog_course(courses, many=True))
......
...@@ -19,7 +19,7 @@ class CourseViewSetTests(SerializationMixin, APITestCase): ...@@ -19,7 +19,7 @@ class CourseViewSetTests(SerializationMixin, APITestCase):
""" Verify the endpoint returns the details for a single course. """ """ Verify the endpoint returns the details for a single course. """
url = reverse('api:v1:course-detail', kwargs={'key': self.course.key}) url = reverse('api:v1:course-detail', kwargs={'key': self.course.key})
with self.assertNumQueries(18): with self.assertNumQueries(19):
response = self.client.get(url) response = self.client.get(url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, self.serialize_course(self.course)) self.assertEqual(response.data, self.serialize_course(self.course))
...@@ -28,7 +28,7 @@ class CourseViewSetTests(SerializationMixin, APITestCase): ...@@ -28,7 +28,7 @@ class CourseViewSetTests(SerializationMixin, APITestCase):
""" Verify the endpoint returns a list of all courses. """ """ Verify the endpoint returns a list of all courses. """
url = reverse('api:v1:course-list') url = reverse('api:v1:course-list')
with self.assertNumQueries(24): with self.assertNumQueries(25):
response = self.client.get(url) response = self.client.get(url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertListEqual( self.assertListEqual(
...@@ -55,6 +55,6 @@ class CourseViewSetTests(SerializationMixin, APITestCase): ...@@ -55,6 +55,6 @@ class CourseViewSetTests(SerializationMixin, APITestCase):
keys = ','.join([course.key for course in courses]) keys = ','.join([course.key for course in courses])
url = '{root}?keys={keys}'.format(root=reverse('api:v1:course-list'), keys=keys) url = '{root}?keys={keys}'.format(root=reverse('api:v1:course-list'), keys=keys)
with self.assertNumQueries(35): with self.assertNumQueries(38):
response = self.client.get(url) response = self.client.get(url)
self.assertListEqual(response.data['results'], self.serialize_course(courses, many=True)) self.assertListEqual(response.data['results'], self.serialize_course(courses, many=True))
...@@ -40,17 +40,15 @@ class ProgramViewSetTests(APITestCase): ...@@ -40,17 +40,15 @@ class ProgramViewSetTests(APITestCase):
def test_retrieve(self): def test_retrieve(self):
""" Verify the endpoint returns the details for a single program. """ """ Verify the endpoint returns the details for a single program. """
program = ProgramFactory() program = ProgramFactory()
with self.assertNumQueries(34): self.assert_retrieve_success(program)
self.assert_retrieve_success(program)
def test_retrieve_without_course_runs(self): def test_retrieve_without_course_runs(self):
""" Verify the endpoint returns data for a program even if the program's courses have no course runs. """ """ Verify the endpoint returns data for a program even if the program's courses have no course runs. """
course = CourseFactory() course = CourseFactory()
program = ProgramFactory(courses=[course]) program = ProgramFactory(courses=[course])
with self.assertNumQueries(49): self.assert_retrieve_success(program)
self.assert_retrieve_success(program)
def assert_list_results(self, url, expected, expected_query_count): def assert_list_results(self, url, expected):
""" """
Asserts the results serialized/returned at the URL matches those that are expected. Asserts the results serialized/returned at the URL matches those that are expected.
Args: Args:
...@@ -64,9 +62,7 @@ class ProgramViewSetTests(APITestCase): ...@@ -64,9 +62,7 @@ class ProgramViewSetTests(APITestCase):
Returns: Returns:
None None
""" """
with self.assertNumQueries(expected_query_count): response = self.client.get(url)
response = self.client.get(url)
self.assertEqual( self.assertEqual(
response.data['results'], response.data['results'],
ProgramSerializer(expected, many=True, context={'request': self.request}).data ProgramSerializer(expected, many=True, context={'request': self.request}).data
...@@ -76,17 +72,17 @@ class ProgramViewSetTests(APITestCase): ...@@ -76,17 +72,17 @@ class ProgramViewSetTests(APITestCase):
""" Verify the endpoint returns a list of all programs. """ """ Verify the endpoint returns a list of all programs. """
expected = ProgramFactory.create_batch(3) expected = ProgramFactory.create_batch(3)
expected.reverse() expected.reverse()
self.assert_list_results(self.list_path, expected, 40) self.assert_list_results(self.list_path, expected)
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. """
program_type_name = 'foo' program_type_name = 'foo'
program = ProgramFactory(type__name=program_type_name) program = ProgramFactory(type__name=program_type_name)
url = self.list_path + '?type=' + program_type_name url = self.list_path + '?type=' + program_type_name
self.assert_list_results(url, [program], 18) self.assert_list_results(url, [program])
url = self.list_path + '?type=bar' url = self.list_path + '?type=bar'
self.assert_list_results(url, [], 4) self.assert_list_results(url, [])
def test_filter_by_uuids(self): def test_filter_by_uuids(self):
""" Verify that the endpoint filters programs to those matching the provided UUIDs. """ """ Verify that the endpoint filters programs to those matching the provided UUIDs. """
...@@ -98,14 +94,14 @@ class ProgramViewSetTests(APITestCase): ...@@ -98,14 +94,14 @@ class ProgramViewSetTests(APITestCase):
# Create a third program, which should be filtered out. # Create a third program, which should be filtered out.
ProgramFactory() ProgramFactory()
self.assert_list_results(url, expected, 29) self.assert_list_results(url, expected)
@ddt.data( @ddt.data(
(ProgramStatus.Unpublished, False, 4), (ProgramStatus.Unpublished, False),
(ProgramStatus.Active, True, 40), (ProgramStatus.Active, True),
) )
@ddt.unpack @ddt.unpack
def test_filter_by_marketable(self, status, is_marketable, expected_query_count): def test_filter_by_marketable(self, status, is_marketable):
""" Verify the endpoint filters programs to those that are marketable. """ """ Verify the endpoint filters programs to those that are marketable. """
url = self.list_path + '?marketable=1' url = self.list_path + '?marketable=1'
ProgramFactory(marketing_slug='') ProgramFactory(marketing_slug='')
...@@ -114,4 +110,4 @@ class ProgramViewSetTests(APITestCase): ...@@ -114,4 +110,4 @@ class ProgramViewSetTests(APITestCase):
expected = programs if is_marketable else [] expected = programs if is_marketable else []
self.assertEqual(list(Program.objects.marketable()), expected) self.assertEqual(list(Program.objects.marketable()), expected)
self.assert_list_results(url, expected, expected_query_count) self.assert_list_results(url, expected)
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