Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions cms/djangoapps/contentstore/tests/test_crud.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
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
from xmodule.course_module import CourseDescriptor
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):
Expand All @@ -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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a must: should these cleanup tasks also go in a tearDown method? (Or, be moved entirely to a tearDown method if state is clean enough for the first test?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need. addCleanup replaces putting stuff in the tearDown.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it.

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'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -302,7 +308,6 @@ def _pre_setup(self):
"""
Flush the ModuleStore.
"""

# Flush the Mongo modulestore
self.drop_mongo_collections()

Expand Down
5 changes: 3 additions & 2 deletions lms/envs/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down Expand Up @@ -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,
Expand Down