Commit b3880714 by Calen Pennington

Assert that caches are being overridden correctly (and being reset in the right order)

parent e87a8a0d
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
""" """
Modulestore configuration for test cases. Modulestore configuration for test cases.
""" """
import copy
import functools import functools
from uuid import uuid4 from uuid import uuid4
from contextlib import contextmanager from contextlib import contextmanager
...@@ -192,6 +193,8 @@ class ModuleStoreIsolationMixin(CacheIsolationMixin): ...@@ -192,6 +193,8 @@ class ModuleStoreIsolationMixin(CacheIsolationMixin):
MODULESTORE = mixed_store_config(mkdtemp_clean(), {}) MODULESTORE = mixed_store_config(mkdtemp_clean(), {})
ENABLED_CACHES = ['mongo_metadata_inheritance', 'loc_cache'] ENABLED_CACHES = ['mongo_metadata_inheritance', 'loc_cache']
__settings_overrides = []
__old_modulestores = []
@classmethod @classmethod
def start_modulestore_isolation(cls): def start_modulestore_isolation(cls):
...@@ -201,10 +204,13 @@ class ModuleStoreIsolationMixin(CacheIsolationMixin): ...@@ -201,10 +204,13 @@ class ModuleStoreIsolationMixin(CacheIsolationMixin):
be flushed (all content will be deleted). be flushed (all content will be deleted).
""" """
cls.start_cache_isolation() cls.start_cache_isolation()
cls.__settings_override = override_settings( override = override_settings(
MODULESTORE=cls.MODULESTORE, MODULESTORE=cls.MODULESTORE,
) )
cls.__settings_override.__enter__()
cls.__old_modulestores.append(copy.deepcopy(settings.MODULESTORE))
override.__enter__()
cls.__settings_overrides.append(override)
XMODULE_FACTORY_LOCK.enable() XMODULE_FACTORY_LOCK.enable()
clear_existing_modulestores() clear_existing_modulestores()
cls.store = modulestore() cls.store = modulestore()
...@@ -218,7 +224,9 @@ class ModuleStoreIsolationMixin(CacheIsolationMixin): ...@@ -218,7 +224,9 @@ class ModuleStoreIsolationMixin(CacheIsolationMixin):
""" """
drop_mongo_collections() # pylint: disable=no-value-for-parameter drop_mongo_collections() # pylint: disable=no-value-for-parameter
XMODULE_FACTORY_LOCK.disable() XMODULE_FACTORY_LOCK.disable()
cls.__settings_override.__exit__(None, None, None) cls.__settings_overrides.pop().__exit__(None, None, None)
assert settings.MODULESTORE == cls.__old_modulestores.pop()
cls.end_cache_isolation() cls.end_cache_isolation()
......
"""
Utility classes for testing django applications.
:py:class:`CacheIsolationMixin`
A mixin helping to write tests which are isolated from cached data.
:py:class:`CacheIsolationTestCase`
A TestCase baseclass that has per-test isolated caches.
"""
import copy
from django.core.cache import caches from django.core.cache import caches
from django.test import TestCase, override_settings from django.test import TestCase, override_settings
from django.conf import settings from django.conf import settings
...@@ -27,7 +39,9 @@ class CacheIsolationMixin(object): ...@@ -27,7 +39,9 @@ class CacheIsolationMixin(object):
CACHES = None CACHES = None
ENABLED_CACHES = None ENABLED_CACHES = None
__settings_override = None
__settings_overrides = []
__old_settings = []
@classmethod @classmethod
def start_cache_isolation(cls): def start_cache_isolation(cls):
...@@ -61,8 +75,12 @@ class CacheIsolationMixin(object): ...@@ -61,8 +75,12 @@ class CacheIsolationMixin(object):
if cache_settings is None: if cache_settings is None:
return return
cls.__settings_override = override_settings(CACHES=cache_settings) cls.__old_settings.append(copy.deepcopy(settings.CACHES))
cls.__settings_override.__enter__() override = override_settings(CACHES=cache_settings)
override.__enter__()
cls.__settings_overrides.append(override)
assert settings.CACHES == cache_settings
# Start with empty caches # Start with empty caches
cls.clear_caches() cls.clear_caches()
...@@ -76,9 +94,9 @@ class CacheIsolationMixin(object): ...@@ -76,9 +94,9 @@ class CacheIsolationMixin(object):
# Make sure that cache contents don't leak out after the isolation is ended # Make sure that cache contents don't leak out after the isolation is ended
cls.clear_caches() cls.clear_caches()
if cls.__settings_override is not None: if cls.__settings_overrides:
cls.__settings_override.__exit__(None, None, None) cls.__settings_overrides.pop().__exit__(None, None, None)
cls.__settings_override = None assert settings.CACHES == cls.__old_settings.pop()
@classmethod @classmethod
def clear_caches(cls): def clear_caches(cls):
......
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