Commit 3dd051a1 by Renzo Lucioni Committed by Clinton Blackburn

Roll back attempted program list optimizations

parent 54feb857
from django.test import TestCase
from course_discovery.apps.api.fields import ImageField, StdImageSerializerField
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
from course_discovery.apps.api.fields import ImageField
class ImageFieldTests(TestCase):
......@@ -16,27 +13,3 @@ class ImageFieldTests(TestCase):
'width': None
}
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
CourseRunFactory(enrollment_end=enrollment_end, course__title='ABC Test Course 2')
CourseRunFactory(enrollment_end=enrollment_end, course=self.course)
with self.assertNumQueries(40):
with self.assertNumQueries(41):
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertListEqual(response.data['results'], self.serialize_catalog_course(courses, many=True))
......
......@@ -19,7 +19,7 @@ class CourseViewSetTests(SerializationMixin, APITestCase):
""" Verify the endpoint returns the details for a single course. """
url = reverse('api:v1:course-detail', kwargs={'key': self.course.key})
with self.assertNumQueries(18):
with self.assertNumQueries(19):
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, self.serialize_course(self.course))
......@@ -28,7 +28,7 @@ class CourseViewSetTests(SerializationMixin, APITestCase):
""" Verify the endpoint returns a list of all courses. """
url = reverse('api:v1:course-list')
with self.assertNumQueries(24):
with self.assertNumQueries(25):
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertListEqual(
......@@ -55,6 +55,6 @@ class CourseViewSetTests(SerializationMixin, APITestCase):
keys = ','.join([course.key for course in courses])
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)
self.assertListEqual(response.data['results'], self.serialize_course(courses, many=True))
......@@ -40,17 +40,15 @@ class ProgramViewSetTests(APITestCase):
def test_retrieve(self):
""" Verify the endpoint returns the details for a single program. """
program = ProgramFactory()
with self.assertNumQueries(34):
self.assert_retrieve_success(program)
self.assert_retrieve_success(program)
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. """
course = CourseFactory()
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.
Args:
......@@ -64,9 +62,7 @@ class ProgramViewSetTests(APITestCase):
Returns:
None
"""
with self.assertNumQueries(expected_query_count):
response = self.client.get(url)
response = self.client.get(url)
self.assertEqual(
response.data['results'],
ProgramSerializer(expected, many=True, context={'request': self.request}).data
......@@ -76,17 +72,17 @@ class ProgramViewSetTests(APITestCase):
""" Verify the endpoint returns a list of all programs. """
expected = ProgramFactory.create_batch(3)
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):
""" Verify that the endpoint filters programs to those of a given type. """
program_type_name = 'foo'
program = ProgramFactory(type__name=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'
self.assert_list_results(url, [], 4)
self.assert_list_results(url, [])
def test_filter_by_uuids(self):
""" Verify that the endpoint filters programs to those matching the provided UUIDs. """
......@@ -98,14 +94,14 @@ class ProgramViewSetTests(APITestCase):
# Create a third program, which should be filtered out.
ProgramFactory()
self.assert_list_results(url, expected, 29)
self.assert_list_results(url, expected)
@ddt.data(
(ProgramStatus.Unpublished, False, 4),
(ProgramStatus.Active, True, 40),
(ProgramStatus.Unpublished, False),
(ProgramStatus.Active, True),
)
@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. """
url = self.list_path + '?marketable=1'
ProgramFactory(marketing_slug='')
......@@ -114,4 +110,4 @@ class ProgramViewSetTests(APITestCase):
expected = programs if is_marketable else []
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