Commit d1b9859c by Renzo Lucioni

Respect querystring parameters when caching retrieve responses

The DefaultObjectKeyConstructor doesn't include querystring parameters in its cache key.

LEARNER-401
parent f6efcbf9
......@@ -2,7 +2,7 @@ import logging
import time
from django.core.cache import cache
from rest_framework_extensions.key_constructor.bits import KeyBitBase
from rest_framework_extensions.key_constructor.bits import KeyBitBase, QueryParamsKeyBit
from rest_framework_extensions.key_constructor.constructors import (
DefaultListKeyConstructor, DefaultObjectKeyConstructor
)
......@@ -22,6 +22,9 @@ class TimestampedListKeyConstructor(DefaultListKeyConstructor):
class TimestampedObjectKeyConstructor(DefaultObjectKeyConstructor):
timestamp = ApiTimestampKeyBit()
# The DefaultObjectKeyConstructor doesn't include querystring parameters
# in its cache key.
querystring = QueryParamsKeyBit()
def timestamped_list_key_constructor(*args, **kwargs): # pylint: disable=unused-argument
......
import urllib.parse
import ddt
from django.core.cache import cache
from django.core.urlresolvers import reverse
......@@ -48,10 +50,13 @@ class ProgramViewSetTests(SerializationMixin, APITestCase):
)
return program
def assert_retrieve_success(self, program):
def assert_retrieve_success(self, program, querystring=None):
""" Verify the retrieve endpoint succesfully returns a serialized program. """
url = reverse('api:v1:program-detail', kwargs={'uuid': program.uuid})
if querystring:
url += '?' + urllib.parse.urlencode(querystring)
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
return response
......@@ -77,6 +82,10 @@ class ProgramViewSetTests(SerializationMixin, APITestCase):
with self.assertNumQueries(2):
self.assert_retrieve_success(program)
# Verify that requests including querystring parameters are cached separately.
response = self.assert_retrieve_success(program, querystring={'use_full_course_serializer': 1})
assert response.data == self.serialize_program(program, extra_context={'use_full_course_serializer': 1})
@ddt.data(True, False)
def test_retrieve_with_sorting_flag(self, order_courses_by_start_date):
""" Verify the number of queries is the same with sorting flag set to true. """
......
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