Commit 05b34098 by Will Daly

Refactored ModuleStoreTestCase to use modulestore interface

for clearing _MODULESTORES
parent 06977c19
...@@ -53,3 +53,12 @@ def modulestore(name='default'): ...@@ -53,3 +53,12 @@ def modulestore(name='default'):
settings.MODULESTORE[name]['OPTIONS']) settings.MODULESTORE[name]['OPTIONS'])
return _MODULESTORES[name] 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()
...@@ -3,7 +3,7 @@ from uuid import uuid4 ...@@ -3,7 +3,7 @@ from uuid import uuid4
from django.test import TestCase from django.test import TestCase
from django.conf import settings from django.conf import settings
import xmodule.modulestore.django from xmodule.modulestore.django import modulestore, clear_existing_modulestores
from unittest.util import safe_repr from unittest.util import safe_repr
...@@ -126,7 +126,7 @@ class ModuleStoreTestCase(TestCase): ...@@ -126,7 +126,7 @@ class ModuleStoreTestCase(TestCase):
'data' is a dictionary with an entry for each CourseField we want to update. '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) store.update_metadata(course.location, data)
updated_course = store.get_instance(course.id, course.location) updated_course = store.get_instance(course.id, course.location)
return updated_course return updated_course
...@@ -136,15 +136,15 @@ class ModuleStoreTestCase(TestCase): ...@@ -136,15 +136,15 @@ class ModuleStoreTestCase(TestCase):
""" """
Delete everything in the module store except templates. Delete everything in the module store except templates.
""" """
modulestore = xmodule.modulestore.django.modulestore() store = modulestore()
# This query means: every item in the collection # This query means: every item in the collection
# that is not a template # that is not a template
query = {"_id.course": {"$ne": "templates"}} query = {"_id.course": {"$ne": "templates"}}
# Remove everything except templates # Remove everything except templates
modulestore.collection.remove(query) store.collection.remove(query)
modulestore.collection.drop() store.collection.drop()
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
...@@ -160,7 +160,7 @@ class ModuleStoreTestCase(TestCase): ...@@ -160,7 +160,7 @@ class ModuleStoreTestCase(TestCase):
settings.MODULESTORE['default']['OPTIONS']['collection'] = 'modulestore_%s' % uuid4().hex settings.MODULESTORE['default']['OPTIONS']['collection'] = 'modulestore_%s' % uuid4().hex
settings.MODULESTORE['direct']['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 print settings.MODULESTORE
...@@ -173,10 +173,10 @@ class ModuleStoreTestCase(TestCase): ...@@ -173,10 +173,10 @@ class ModuleStoreTestCase(TestCase):
""" """
# Clean up by dropping the collection # Clean up by dropping the collection
modulestore = xmodule.modulestore.django.modulestore() store = modulestore()
modulestore.collection.drop() store.collection.drop()
xmodule.modulestore.django._MODULESTORES.clear() clear_existing_modulestores()
# Restore the original modulestore settings # Restore the original modulestore settings
settings.MODULESTORE = cls.orig_modulestore settings.MODULESTORE = cls.orig_modulestore
......
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