From 789a9dc978ba5f0b0c759d71b217cf2fbf9090ce Mon Sep 17 00:00:00 2001 From: Jesse Zoldak Date: Mon, 1 Dec 2014 15:24:18 -0500 Subject: [PATCH 1/2] Change the default dir for loading xml files. This should always be overridden by the testcases if needed. --- lms/envs/test.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lms/envs/test.py b/lms/envs/test.py index de74f29c0fdb..b6a2c4807557 100644 --- a/lms/envs/test.py +++ b/lms/envs/test.py @@ -15,8 +15,9 @@ from .common import * import os from path import path -from warnings import filterwarnings, simplefilter +from tempfile import mkdtemp from uuid import uuid4 +from warnings import filterwarnings, simplefilter # mongo connection settings MONGO_PORT_NUM = int(os.environ.get('EDXAPP_TEST_MONGO_PORT', '27017')) @@ -130,7 +131,7 @@ 'fs_root': TEST_ROOT / "data", }, xml_store_options={ - 'data_dir': COMMON_TEST_DATA_ROOT, + 'data_dir': mkdtemp(), # never inadvertently load all the XML courses }, doc_store_settings={ 'host': MONGO_HOST, From 77bbd3039602a0f977f58410c1485cd9b5c9a443 Mon Sep 17 00:00:00 2001 From: Jesse Zoldak Date: Fri, 5 Dec 2014 12:19:58 -0500 Subject: [PATCH 2/2] Do not create a modulestore unnecessarily in tests --- .../contentstore/tests/test_crud.py | 19 ++++++++++++++++--- .../xmodule/modulestore/tests/django_utils.py | 9 +++++++-- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/cms/djangoapps/contentstore/tests/test_crud.py b/cms/djangoapps/contentstore/tests/test_crud.py index 70ddf7fc5908..88d66a662ead 100644 --- a/cms/djangoapps/contentstore/tests/test_crud.py +++ b/cms/djangoapps/contentstore/tests/test_crud.py @@ -1,5 +1,7 @@ import unittest +from opaque_keys.edx.locator import LocalId + from xmodule import templates from xmodule.modulestore import ModuleStoreEnum from xmodule.modulestore.tests import persistent_factories @@ -7,10 +9,9 @@ from xmodule.modulestore.django import modulestore, clear_existing_modulestores from xmodule.seq_module import SequenceDescriptor 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.html_module import HtmlDescriptor -from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase class TemplateTests(unittest.TestCase): @@ -20,10 +21,22 @@ class TemplateTests(unittest.TestCase): def setUp(self): 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.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): found = templates.all_templates() self.assertIsNotNone(found.get('course')) diff --git a/common/lib/xmodule/xmodule/modulestore/tests/django_utils.py b/common/lib/xmodule/xmodule/modulestore/tests/django_utils.py index 5c0eafc31906..264e8c2ae675 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/django_utils.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/django_utils.py @@ -7,6 +7,8 @@ from tempfile import mkdtemp from uuid import uuid4 +from mock import patch + from django.conf import settings from django.contrib.auth.models import User from django.test import TestCase @@ -276,10 +278,14 @@ def update_course(self, course, user_id): return updated_course @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. """ + # Do not create the modulestore if it does not exist. + mock_create.return_value = None + module_store = modulestore() if hasattr(module_store, '_drop_database'): module_store._drop_database() # pylint: disable=protected-access @@ -302,7 +308,6 @@ def _pre_setup(self): """ Flush the ModuleStore. """ - # Flush the Mongo modulestore self.drop_mongo_collections()