Commit 77bbd303 by Jesse Zoldak

Do not create a modulestore unnecessarily in tests

parent 789a9dc9
import unittest import unittest
from opaque_keys.edx.locator import LocalId
from xmodule import templates from xmodule import templates
from xmodule.modulestore import ModuleStoreEnum from xmodule.modulestore import ModuleStoreEnum
from xmodule.modulestore.tests import persistent_factories from xmodule.modulestore.tests import persistent_factories
...@@ -7,10 +9,9 @@ from xmodule.course_module import CourseDescriptor ...@@ -7,10 +9,9 @@ from xmodule.course_module import CourseDescriptor
from xmodule.modulestore.django import modulestore, clear_existing_modulestores from xmodule.modulestore.django import modulestore, clear_existing_modulestores
from xmodule.seq_module import SequenceDescriptor from xmodule.seq_module import SequenceDescriptor
from xmodule.capa_module import CapaDescriptor from xmodule.capa_module import CapaDescriptor
from opaque_keys.edx.locator import BlockUsageLocator, LocalId from xmodule.contentstore.django import _CONTENTSTORE
from xmodule.modulestore.exceptions import ItemNotFoundError, DuplicateCourseError from xmodule.modulestore.exceptions import ItemNotFoundError, DuplicateCourseError
from xmodule.html_module import HtmlDescriptor from xmodule.html_module import HtmlDescriptor
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
class TemplateTests(unittest.TestCase): class TemplateTests(unittest.TestCase):
...@@ -20,10 +21,22 @@ class TemplateTests(unittest.TestCase): ...@@ -20,10 +21,22 @@ class TemplateTests(unittest.TestCase):
def setUp(self): def setUp(self):
clear_existing_modulestores() # redundant w/ cleanup but someone was getting errors clear_existing_modulestores() # redundant w/ cleanup but someone was getting errors
self.addCleanup(ModuleStoreTestCase.drop_mongo_collections) self.addCleanup(self._drop_mongo_collections)
self.addCleanup(clear_existing_modulestores) self.addCleanup(clear_existing_modulestores)
self.split_store = modulestore()._get_modulestore_by_type(ModuleStoreEnum.Type.split) self.split_store = modulestore()._get_modulestore_by_type(ModuleStoreEnum.Type.split)
@staticmethod
def _drop_mongo_collections():
"""
If using a Mongo-backed modulestore & contentstore, drop the collections.
"""
module_store = modulestore()
if hasattr(module_store, '_drop_database'):
module_store._drop_database() # pylint: disable=protected-access
_CONTENTSTORE.clear()
if hasattr(module_store, 'close_connections'):
module_store.close_connections()
def test_get_templates(self): def test_get_templates(self):
found = templates.all_templates() found = templates.all_templates()
self.assertIsNotNone(found.get('course')) self.assertIsNotNone(found.get('course'))
......
...@@ -7,6 +7,8 @@ import pytz ...@@ -7,6 +7,8 @@ import pytz
from tempfile import mkdtemp from tempfile import mkdtemp
from uuid import uuid4 from uuid import uuid4
from mock import patch
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.test import TestCase from django.test import TestCase
...@@ -276,10 +278,14 @@ class ModuleStoreTestCase(TestCase): ...@@ -276,10 +278,14 @@ class ModuleStoreTestCase(TestCase):
return updated_course return updated_course
@staticmethod @staticmethod
def drop_mongo_collections(): @patch('xmodule.modulestore.django.create_modulestore_instance')
def drop_mongo_collections(mock_create):
""" """
If using a Mongo-backed modulestore & contentstore, drop the collections. If using a Mongo-backed modulestore & contentstore, drop the collections.
""" """
# Do not create the modulestore if it does not exist.
mock_create.return_value = None
module_store = modulestore() module_store = modulestore()
if hasattr(module_store, '_drop_database'): if hasattr(module_store, '_drop_database'):
module_store._drop_database() # pylint: disable=protected-access module_store._drop_database() # pylint: disable=protected-access
...@@ -302,7 +308,6 @@ class ModuleStoreTestCase(TestCase): ...@@ -302,7 +308,6 @@ class ModuleStoreTestCase(TestCase):
""" """
Flush the ModuleStore. Flush the ModuleStore.
""" """
# Flush the Mongo modulestore # Flush the Mongo modulestore
self.drop_mongo_collections() self.drop_mongo_collections()
......
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