Commit 9ae0dada by David Ormsbee

Merge pull request #11445 from edx/ormsbee/clear_cache_in_tests

Clear caches between tests for ModuleStoreTestCase/SharedModuleStoreTestCase
parents 07c542d8 8fa8b209
"""
THE TESTS IN THIS MODULE SHOULD BE RUN ON THE SAME PROCESS TO BE MEANINGFUL!!!
The tests in this module look kind of goofy, but the idea is to make sure that
cache values can't leak between different TestCase classes and methods. The need
for this will go away whenever Django merges the fix to reset the caches between
tests (https://code.djangoproject.com/ticket/11505).
"""
from django.core.cache import caches
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase, SharedModuleStoreTestCase
class CacheCheckMixin(object):
"""Base mixin that does our cache check."""
def check_caches(self, key):
"""Check that caches are empty, and add values."""
for cache in caches.all():
self.assertIsNone(cache.get(key))
cache.set(key, "Not None")
class CacheModuleStoreTestCaseParent(ModuleStoreTestCase, CacheCheckMixin):
"""Make sure that we're clearing cache values between tests."""
def test_cache_reset_1(self):
"""Check to make sure cache is empty, and add values to it."""
self.check_caches("mstc_cache_test_key")
def test_cache_reset_2(self):
"""Check to make sure cache is empty, and add values to it."""
self.check_caches("mstc_cache_test_key")
class CacheModuleStoreTestCaseChild(CacheModuleStoreTestCaseParent): # pylint: disable=test-inherits-tests
"""Make sure that we're clearing cache values between classes."""
class CacheSharedModuleStoreTestCaseParent(SharedModuleStoreTestCase, CacheCheckMixin):
"""Make sure that we're clearing cache values between tests."""
def test_cache_reset_1(self):
"""Check to make sure cache is empty, and add values to it."""
self.check_caches("smstc_cache_test_key")
def test_cache_reset_2(self):
"""Check to make sure cache is empty, and add values to it."""
self.check_caches("smstc_cache_test_key")
class CacheSharedModuleStoreTestCaseChild(CacheSharedModuleStoreTestCaseParent): # pylint: disable=test-inherits-tests
"""Make sure that we're clearing cache values between classes."""
......@@ -7,6 +7,7 @@ from uuid import uuid4
from mock import patch
import django.core.cache
from django.conf import settings
from django.contrib.auth.models import User
from django.test import TestCase
......@@ -215,6 +216,16 @@ TEST_DATA_SPLIT_MODULESTORE = mixed_store_config(
)
def clear_all_caches():
"""Clear all caches so that cache info doesn't leak across test cases."""
# This will no longer be necessary when Django adds (in Django 1.10?):
# https://code.djangoproject.com/ticket/11505
for cache in django.core.cache.caches.all():
cache.clear()
RequestCache().clear_request_cache()
class SharedModuleStoreTestCase(TestCase):
"""
Subclass for any test case that uses a ModuleStore that can be shared
......@@ -268,7 +279,7 @@ class SharedModuleStoreTestCase(TestCase):
@classmethod
def tearDownClass(cls):
drop_mongo_collections() # pylint: disable=no-value-for-parameter
RequestCache().clear_request_cache()
clear_all_caches()
XMODULE_FACTORY_LOCK.disable()
cls._settings_override.__exit__(None, None, None)
......@@ -280,6 +291,11 @@ class SharedModuleStoreTestCase(TestCase):
OverrideFieldData.provider_classes = None
super(SharedModuleStoreTestCase, self).setUp()
def tearDown(self):
"""Reset caches."""
clear_all_caches()
super(SharedModuleStoreTestCase, self).tearDown()
def reset(self):
"""
Manually run tearDownClass/setUpClass again.
......@@ -394,7 +410,7 @@ class ModuleStoreTestCase(TestCase):
clear_existing_modulestores()
self.addCleanup(drop_mongo_collections)
self.addCleanup(RequestCache().clear_request_cache)
self.addCleanup(clear_all_caches)
# Enable XModuleFactories for the space of this test (and its setUp).
self.addCleanup(XMODULE_FACTORY_LOCK.disable)
......
......@@ -520,7 +520,7 @@ class CourseBlocksOrNavigationTestMixin(CourseDetailTestMixin, CourseViewTestsMi
return 'course_structure_api:v0:' + self.block_navigation_view_type
def test_get(self):
with check_mongo_calls(3):
with check_mongo_calls(4):
response = super(CourseBlocksOrNavigationTestMixin, self).test_get()
# verify root element
......
......@@ -950,7 +950,7 @@ class ProgressPageTests(ModuleStoreTestCase):
self.assertContains(resp, u"Download Your Certificate")
@ddt.data(
*itertools.product(((38, 4, True), (38, 4, False)), (True, False))
*itertools.product(((40, 4, True), (40, 4, False)), (True, False))
)
@ddt.unpack
def test_query_counts(self, (sql_calls, mongo_calls, self_paced), self_paced_enabled):
......
......@@ -1628,7 +1628,7 @@ class TestCertificateGeneration(InstructorTaskModuleTestCase):
current_task.update_state = Mock()
instructor_task = Mock()
instructor_task.task_input = json.dumps({'students': None})
with self.assertNumQueries(213):
with self.assertNumQueries(214):
with patch('instructor_task.tasks_helper._get_current_task') as mock_current_task:
mock_current_task.return_value = current_task
with patch('capa.xqueue_interface.XQueueInterface.send_to_queue') as mock_queue:
......
......@@ -348,7 +348,7 @@ class CourseOverviewTestCase(ModuleStoreTestCase):
course_overview = CourseOverview._create_from_course(course) # pylint: disable=protected-access
self.assertEqual(course_overview.lowest_passing_grade, None)
@ddt.data((ModuleStoreEnum.Type.mongo, 5, 5), (ModuleStoreEnum.Type.split, 3, 4))
@ddt.data((ModuleStoreEnum.Type.mongo, 4, 4), (ModuleStoreEnum.Type.split, 3, 4))
@ddt.unpack
def test_versioning(self, modulestore_type, min_mongo_calls, max_mongo_calls):
"""
......
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