From 2f0e3d35a02074802f82feb97225f18b7f5c11fd Mon Sep 17 00:00:00 2001
From: Sanford Student <sstudent@edx.org>
Date: Wed, 25 Apr 2018 13:11:49 -0400
Subject: [PATCH] EDUCATOR-2618 request cache some more stuff

---
 lms/djangoapps/discussion/tests/test_views.py          | 10 ++++++----
 .../django_comment_client/tests/test_utils.py          |  4 +++-
 lms/djangoapps/django_comment_client/utils.py          | 10 ++++++++--
 3 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/lms/djangoapps/discussion/tests/test_views.py b/lms/djangoapps/discussion/tests/test_views.py
index a3448bacb2c..34cd79391fa 100644
--- a/lms/djangoapps/discussion/tests/test_views.py
+++ b/lms/djangoapps/discussion/tests/test_views.py
@@ -44,6 +44,7 @@ from lms.lib.comment_client.utils import CommentClientPaginatedResult
 from openedx.core.djangoapps.course_groups.models import CourseUserGroup
 from openedx.core.djangoapps.course_groups.tests.helpers import config_course_cohorts
 from openedx.core.djangoapps.course_groups.tests.test_views import CohortViewsTestCase
+from openedx.core.djangoapps.request_cache.middleware import RequestCache
 from openedx.core.djangoapps.util.testing import ContentGroupTestCase
 from openedx.core.djangoapps.waffle_utils.testutils import WAFFLE_TABLES
 from openedx.features.enterprise_support.tests.mixins.enterprise import EnterpriseTestConsentRequired
@@ -404,15 +405,15 @@ class SingleThreadQueryCountTestCase(ForumsEnableMixin, ModuleStoreTestCase):
         # course is outside the context manager that is verifying the number of queries,
         # and with split mongo, that method ends up querying disabled_xblocks (which is then
         # cached and hence not queried as part of call_single_thread).
-        (ModuleStoreEnum.Type.mongo, False, 1, 6, 4, 16, 4),
-        (ModuleStoreEnum.Type.mongo, False, 50, 6, 4, 16, 4),
+        (ModuleStoreEnum.Type.mongo, False, 1, 5, 2, 16, 4),
+        (ModuleStoreEnum.Type.mongo, False, 50, 5, 2, 16, 4),
         # split mongo: 3 queries, regardless of thread response size.
         (ModuleStoreEnum.Type.split, False, 1, 3, 3, 16, 4),
         (ModuleStoreEnum.Type.split, False, 50, 3, 3, 16, 4),
 
         # Enabling Enterprise integration should have no effect on the number of mongo queries made.
-        (ModuleStoreEnum.Type.mongo, True, 1, 6, 4, 16, 4),
-        (ModuleStoreEnum.Type.mongo, True, 50, 6, 4, 16, 4),
+        (ModuleStoreEnum.Type.mongo, True, 1, 5, 2, 16, 4),
+        (ModuleStoreEnum.Type.mongo, True, 50, 5, 2, 16, 4),
         # split mongo: 3 queries, regardless of thread response size.
         (ModuleStoreEnum.Type.split, True, 1, 3, 3, 16, 4),
         (ModuleStoreEnum.Type.split, True, 50, 3, 3, 16, 4),
@@ -1854,6 +1855,7 @@ class CourseDiscussionsHandlerTestCase(DividedDiscussionsTestCase):
         expected_response = self.get_expected_response()
         self.assertEqual(response, expected_response)
 
+        RequestCache.clear_request_cache()
         now = datetime.now()
         # inline discussion
         ItemFactory.create(
diff --git a/lms/djangoapps/django_comment_client/tests/test_utils.py b/lms/djangoapps/django_comment_client/tests/test_utils.py
index 3a0b68b4fbb..7700b0769d1 100644
--- a/lms/djangoapps/django_comment_client/tests/test_utils.py
+++ b/lms/djangoapps/django_comment_client/tests/test_utils.py
@@ -38,6 +38,7 @@ from openedx.core.djangoapps.content.course_structures.models import CourseStruc
 from openedx.core.djangoapps.course_groups import cohorts
 from openedx.core.djangoapps.course_groups.cohorts import set_course_cohorted
 from openedx.core.djangoapps.course_groups.tests.helpers import CohortFactory, config_course_cohorts
+from openedx.core.djangoapps.request_cache.middleware import RequestCache
 from openedx.core.djangoapps.util.testing import ContentGroupTestCase
 from student.roles import CourseStaffRole
 from student.tests.factories import AdminFactory, CourseEnrollmentFactory, UserFactory
@@ -137,7 +138,6 @@ class CoursewareContextTestCase(ModuleStoreTestCase):
     """
     def setUp(self):
         super(CoursewareContextTestCase, self).setUp()
-
         self.course = CourseFactory.create(org="TestX", number="101", display_name="Test Course")
         self.discussion1 = ItemFactory.create(
             parent_location=self.course.location,
@@ -224,6 +224,8 @@ class CoursewareContextTestCase(ModuleStoreTestCase):
         # Assert that there is only one discussion xblock in the course at the moment.
         self.assertEqual(len(utils.get_accessible_discussion_xblocks(course, self.user)), 1)
 
+        # The above call is request cached, so we need to clear it for this test.
+        RequestCache.clear_request_cache()
         # Add an orphan discussion xblock to that course
         orphan = course.id.make_usage_key('discussion', 'orphan_discussion')
         self.store.create_item(self.user.id, orphan.course_key, orphan.block_type, block_id=orphan.block_id)
diff --git a/lms/djangoapps/django_comment_client/utils.py b/lms/djangoapps/django_comment_client/utils.py
index cd197ac2324..d7e3fecd00a 100644
--- a/lms/djangoapps/django_comment_client/utils.py
+++ b/lms/djangoapps/django_comment_client/utils.py
@@ -131,6 +131,7 @@ def get_accessible_discussion_xblocks(course, user, include_all=False):  # pylin
     return get_accessible_discussion_xblocks_by_course_id(course.id, user, include_all=include_all)
 
 
+@request_cached
 def get_accessible_discussion_xblocks_by_course_id(course_id, user, include_all=False):  # pylint: disable=invalid-name
     """
     Return a list of all valid discussion xblocks in this course that
@@ -198,7 +199,7 @@ def get_cached_discussion_id_map_by_course_id(course_id, discussion_ids, user):
             key = get_cached_discussion_key(course_id, discussion_id)
             if not key:
                 continue
-            xblock = modulestore().get_item(key)
+            xblock = _get_item_from_modulestore(key)
             if not (has_required_keys(xblock) and has_access(user, 'load', xblock, course_id)):
                 continue
             entries.append(get_discussion_id_map_entry(xblock))
@@ -224,6 +225,11 @@ def get_discussion_id_map_by_course_id(course_id, user):  # pylint: disable=inva
     return dict(map(get_discussion_id_map_entry, xblocks))
 
 
+@request_cached
+def _get_item_from_modulestore(key):
+    return modulestore().get_item(key)
+
+
 def _filter_unstarted_categories(category_map, course):
     """
     Returns a subset of categories from the provided map which have not yet met the start date
@@ -434,7 +440,7 @@ def discussion_category_id_access(course, user, discussion_id, xblock=None):
             key = get_cached_discussion_key(course.id, discussion_id)
             if not key:
                 return False
-            xblock = modulestore().get_item(key)
+            xblock = _get_item_from_modulestore(key)
         return has_required_keys(xblock) and has_access(user, 'load', xblock, course.id)
     except DiscussionIdMapIsNotCached:
         return discussion_id in get_discussion_categories_ids(course, user)
-- 
GitLab