From 05b34098abfce0c3c1e0d9d2bbacd7ca623a148e Mon Sep 17 00:00:00 2001 From: Will Daly Date: Mon, 19 Aug 2013 09:27:36 -0400 Subject: [PATCH] Refactored ModuleStoreTestCase to use modulestore interface for clearing _MODULESTORES --- .../lib/xmodule/xmodule/modulestore/django.py | 9 +++++++++ .../xmodule/modulestore/tests/django_utils.py | 18 +++++++++--------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/common/lib/xmodule/xmodule/modulestore/django.py b/common/lib/xmodule/xmodule/modulestore/django.py index 2f0cd126f9..cd0166e4b0 100644 --- a/common/lib/xmodule/xmodule/modulestore/django.py +++ b/common/lib/xmodule/xmodule/modulestore/django.py @@ -53,3 +53,12 @@ def modulestore(name='default'): settings.MODULESTORE[name]['OPTIONS']) return _MODULESTORES[name] + +def clear_existing_modulestores(): + """ + Clear the existing modulestore instances, causing + them to be re-created when accessed again. + + This is useful for flushing state between unit tests. + """ + _MODULESTORES.clear() diff --git a/common/lib/xmodule/xmodule/modulestore/tests/django_utils.py b/common/lib/xmodule/xmodule/modulestore/tests/django_utils.py index 8b8d61c85a..a38cab79f6 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/django_utils.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/django_utils.py @@ -3,7 +3,7 @@ from uuid import uuid4 from django.test import TestCase from django.conf import settings -import xmodule.modulestore.django +from xmodule.modulestore.django import modulestore, clear_existing_modulestores from unittest.util import safe_repr @@ -126,7 +126,7 @@ class ModuleStoreTestCase(TestCase): 'data' is a dictionary with an entry for each CourseField we want to update. """ - store = xmodule.modulestore.django.modulestore() + store = modulestore() store.update_metadata(course.location, data) updated_course = store.get_instance(course.id, course.location) return updated_course @@ -136,15 +136,15 @@ class ModuleStoreTestCase(TestCase): """ Delete everything in the module store except templates. """ - modulestore = xmodule.modulestore.django.modulestore() + store = modulestore() # This query means: every item in the collection # that is not a template query = {"_id.course": {"$ne": "templates"}} # Remove everything except templates - modulestore.collection.remove(query) - modulestore.collection.drop() + store.collection.remove(query) + store.collection.drop() @classmethod def setUpClass(cls): @@ -160,7 +160,7 @@ class ModuleStoreTestCase(TestCase): settings.MODULESTORE['default']['OPTIONS']['collection'] = 'modulestore_%s' % uuid4().hex settings.MODULESTORE['direct']['OPTIONS']['collection'] = 'modulestore_%s' % uuid4().hex - xmodule.modulestore.django._MODULESTORES.clear() + clear_existing_modulestores() print settings.MODULESTORE @@ -173,10 +173,10 @@ class ModuleStoreTestCase(TestCase): """ # Clean up by dropping the collection - modulestore = xmodule.modulestore.django.modulestore() - modulestore.collection.drop() + store = modulestore() + store.collection.drop() - xmodule.modulestore.django._MODULESTORES.clear() + clear_existing_modulestores() # Restore the original modulestore settings settings.MODULESTORE = cls.orig_modulestore