Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
6f8c9b4a
Commit
6f8c9b4a
authored
Mar 28, 2013
by
Will Daly
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Optimized ModuleStoreTestCase to reload templates only once
over all test runs.
parent
13c01ec3
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
94 additions
and
26 deletions
+94
-26
cms/djangoapps/contentstore/tests/utils.py
+94
-26
No files found.
cms/djangoapps/contentstore/tests/utils.py
View file @
6f8c9b4a
'''
Utilities for contentstore tests
'''
#pylint: disable=W0603
import
json
import
json
import
copy
import
copy
from
uuid
import
uuid4
from
uuid
import
uuid4
...
@@ -10,6 +16,17 @@ from django.contrib.auth.models import User
...
@@ -10,6 +16,17 @@ from django.contrib.auth.models import User
import
xmodule.modulestore.django
import
xmodule.modulestore.django
from
xmodule.templates
import
update_templates
from
xmodule.templates
import
update_templates
# Share modulestore setup between classes
# We need to use global variables, because
# each ModuleStoreTestCase subclass will have its
# own class variables, and we want to re-use the
# same modulestore for all test cases.
#pylint: disable=C0103
test_modulestore
=
None
#pylint: disable=C0103
orig_modulestore
=
None
class
ModuleStoreTestCase
(
TestCase
):
class
ModuleStoreTestCase
(
TestCase
):
""" Subclass for any test case that uses the mongodb
""" Subclass for any test case that uses the mongodb
...
@@ -17,37 +34,88 @@ class ModuleStoreTestCase(TestCase):
...
@@ -17,37 +34,88 @@ class ModuleStoreTestCase(TestCase):
collection with templates before running the TestCase
collection with templates before running the TestCase
and drops it they are finished. """
and drops it they are finished. """
def
_pre_setup
(
self
):
@staticmethod
super
(
ModuleStoreTestCase
,
self
)
.
_pre_setup
()
def
flush_mongo_except_templates
():
'''
Delete everything in the module store except templates
'''
modulestore
=
xmodule
.
modulestore
.
django
.
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
)
@staticmethod
def
load_templates_if_necessary
():
'''
Load templates into the modulestore only if they do not already exist.
We need the templates, because they are copied to create
XModules such as sections and problems
'''
modulestore
=
xmodule
.
modulestore
.
django
.
modulestore
()
# Count the number of templates
query
=
{
"_id.course"
:
"templates"
}
num_templates
=
modulestore
.
collection
.
find
(
query
)
.
count
()
if
num_templates
<
1
:
update_templates
()
@classmethod
def
setUpClass
(
cls
):
'''
Flush the mongo store and set up templates
'''
global
test_modulestore
global
orig_modulestore
# Use a uuid to differentiate
# Use a uuid to differentiate
# the mongo collections on jenkins.
# the mongo collections on jenkins.
self
.
orig_MODULESTORE
=
copy
.
deepcopy
(
settings
.
MODULESTORE
)
if
test_modulestore
is
None
:
self
.
test_MODULESTORE
=
self
.
orig_MODULESTORE
orig_modulestore
=
copy
.
deepcopy
(
settings
.
MODULESTORE
)
self
.
test_MODULESTORE
[
'default'
][
'OPTIONS'
][
'collection'
]
=
'modulestore_
%
s'
%
uuid4
()
.
hex
test_modulestore
=
orig_modulestore
self
.
test_MODULESTORE
[
'direct'
][
'OPTIONS'
][
'collection'
]
=
'modulestore_
%
s'
%
uuid4
()
.
hex
test_modulestore
[
'default'
][
'OPTIONS'
][
'collection'
]
=
'modulestore_
%
s'
%
uuid4
()
.
hex
settings
.
MODULESTORE
=
self
.
test_MODULESTORE
test_modulestore
[
'direct'
][
'OPTIONS'
][
'collection'
]
=
'modulestore_
%
s'
%
uuid4
()
.
hex
xmodule
.
modulestore
.
django
.
_MODULESTORES
=
{}
# Flush and initialize the module store
# It needs the templates because it creates new records
settings
.
MODULESTORE
=
test_modulestore
# by cloning from the template.
# Note that if your test module gets in some weird state
TestCase
.
setUpClass
()
# (though it shouldn't), do this manually
# from the bash shell to drop it:
@classmethod
# $ mongo test_xmodule --eval "db.dropDatabase()"
def
tearDownClass
(
cls
):
xmodule
.
modulestore
.
django
.
_MODULESTORES
=
{}
'''
update_templates
()
Revert to the old modulestore settings
'''
settings
.
MODULESTORE
=
orig_modulestore
def
_pre_setup
(
self
):
'''
Remove everything but the templates before each test
'''
# Flush anything that is not a template
ModuleStoreTestCase
.
flush_mongo_except_templates
()
# Check that we have templates loaded; if not, load them
ModuleStoreTestCase
.
load_templates_if_necessary
()
# Call superclass implementation
TestCase
.
_pre_setup
(
self
)
def
_post_teardown
(
self
):
def
_post_teardown
(
self
):
# Make sure you flush out the modulestore.
'''
# Drop the collection at the end of the test,
Flush everything we created except the templates
# otherwise there will be lingering collections leftover
'''
#
from executing the tests.
#
Flush anything that is not a template
xmodule
.
modulestore
.
django
.
_MODULESTORES
=
{}
ModuleStoreTestCase
.
flush_mongo_except_templates
()
xmodule
.
modulestore
.
django
.
modulestore
()
.
collection
.
drop
()
settings
.
MODULESTORE
=
self
.
orig_MODULESTORE
# Call superclass implementation
TestCase
.
_post_teardown
(
self
)
super
(
ModuleStoreTestCase
,
self
)
.
_post_teardown
()
def
parse_json
(
response
):
def
parse_json
(
response
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment