* Consolidates and renames the runtime used as a base for all the others:
* Before: `xmodule.x_module:DescriptorSystem` and
`xmodule.mako_block:MakoDescriptorSystem`.
* After: `xmodule.x_module:ModuleStoreRuntime`.
* Co-locates and renames the runtimes for importing course OLX:
* Before: `xmodule.x_module:XMLParsingSystem` and
`xmodule.modulestore.xml:ImportSystem`.
* After: `xmodule.modulestore.xml:XMLParsingModuleStoreRuntime` and
`xmodule.modulestore.xml:XMLImportingModuleStoreRuntime`.
* Note: I would have liked to consolidate these, but it would have
involved nontrivial test refactoring.
* Renames the stub Old Mongo runtime:
* Before: `xmodule.modulestore.mongo.base:CachingDescriptorSystem`.
* After: `xmodule.modulestore.mongo.base:OldModuleStoreRuntime`.
* Renames the Split Mongo runtime, the which is what runs courses in LMS and CMS:
* Before: `xmodule.modulestore.split_mongo.caching_descriptor_system:CachingDescriptorSystem`.
* After: `xmodule.modulestore.split_mongo.runtime:SplitModuleStoreRuntime`.
* Renames some of the dummy runtimes used only in unit tests.
144 lines
4.5 KiB
Python
144 lines
4.5 KiB
Python
"""
|
|
Utility methods for unit tests.
|
|
"""
|
|
|
|
|
|
import filecmp
|
|
import pprint
|
|
|
|
import pytest
|
|
from path import Path as path
|
|
from xblock.reference.user_service import UserService, XBlockUser
|
|
from xmodule.x_module import ModuleStoreRuntime
|
|
|
|
|
|
def directories_equal(directory1, directory2):
|
|
"""
|
|
Returns True if the 2 directories have equal content, else false.
|
|
"""
|
|
def compare_dirs(dir1, dir2):
|
|
""" Compare directories for equality. """
|
|
comparison = filecmp.dircmp(dir1, dir2)
|
|
if (len(comparison.left_only) > 0) or (len(comparison.right_only) > 0):
|
|
return False
|
|
if (len(comparison.funny_files) > 0) or (len(comparison.diff_files) > 0):
|
|
return False
|
|
for subdir in comparison.subdirs:
|
|
if not compare_dirs(dir1 / subdir, dir2 / subdir):
|
|
return False
|
|
|
|
return True
|
|
|
|
return compare_dirs(path(directory1), path(directory2))
|
|
|
|
|
|
def mock_render_template(*args, **kwargs):
|
|
"""
|
|
Pretty-print the args and kwargs.
|
|
|
|
Allows us to not depend on any actual template rendering mechanism,
|
|
while still returning a unicode object
|
|
"""
|
|
return pprint.pformat((args, kwargs)).encode().decode()
|
|
|
|
|
|
class StubMakoService:
|
|
"""
|
|
Stub MakoService for testing blocks that use mako templates.
|
|
"""
|
|
|
|
def __init__(self, render_template=None):
|
|
self._render_template = render_template or mock_render_template
|
|
|
|
def render_template(self, *args, **kwargs):
|
|
"""
|
|
Invokes the configured render_template method.
|
|
"""
|
|
return self._render_template(*args, **kwargs)
|
|
|
|
def render_lms_template(self, *args, **kwargs):
|
|
"""
|
|
Invokes the configured render_template method.
|
|
"""
|
|
return self._render_template(*args, **kwargs)
|
|
|
|
def render_cms_template(self, *args, **kwargs):
|
|
"""
|
|
Invokes the configured render_template method.
|
|
"""
|
|
return self._render_template(*args, **kwargs)
|
|
|
|
|
|
class StubUserService(UserService):
|
|
"""
|
|
Stub UserService for testing the sequence block.
|
|
"""
|
|
|
|
def __init__(self,
|
|
user=None,
|
|
user_is_staff=False,
|
|
user_role=None,
|
|
anonymous_user_id=None,
|
|
deprecated_anonymous_user_id=None,
|
|
request_country_code=None,
|
|
**kwargs):
|
|
self.user = user
|
|
self.user_is_staff = user_is_staff
|
|
self.user_role = user_role
|
|
self.anonymous_user_id = anonymous_user_id
|
|
self.deprecated_anonymous_user_id = deprecated_anonymous_user_id
|
|
self.request_country_code = request_country_code
|
|
self._django_user = user
|
|
super().__init__(**kwargs)
|
|
|
|
def get_current_user(self):
|
|
"""
|
|
Implements abstract method for getting the current user.
|
|
"""
|
|
user = XBlockUser()
|
|
if self.user and self.user.is_authenticated:
|
|
user.opt_attrs['edx-platform.anonymous_user_id'] = self.anonymous_user_id
|
|
user.opt_attrs['edx-platform.deprecated_anonymous_user_id'] = self.deprecated_anonymous_user_id
|
|
user.opt_attrs['edx-platform.request_country_code'] = self.request_country_code
|
|
user.opt_attrs['edx-platform.user_is_staff'] = self.user_is_staff
|
|
user.opt_attrs['edx-platform.user_id'] = self.user.id
|
|
user.opt_attrs['edx-platform.user_role'] = self.user_role
|
|
user.opt_attrs['edx-platform.username'] = self.user.username
|
|
else:
|
|
user.opt_attrs['edx-platform.username'] = 'anonymous'
|
|
user.opt_attrs['edx-platform.request_country_code'] = self.request_country_code
|
|
user.opt_attrs['edx-platform.is_authenticated'] = False
|
|
return user
|
|
|
|
def get_user_by_anonymous_id(self, uid=None): # pylint: disable=unused-argument
|
|
"""
|
|
Return the original user passed into the service.
|
|
"""
|
|
return self.user
|
|
|
|
|
|
class StubReplaceURLService:
|
|
"""
|
|
Stub ReplaceURLService for testing blocks.
|
|
"""
|
|
|
|
def replace_urls(self, text, static_replace_only=False):
|
|
"""
|
|
Invokes the configured render_template method.
|
|
"""
|
|
return text
|
|
|
|
|
|
@pytest.fixture
|
|
def override_descriptor_system(monkeypatch):
|
|
"""
|
|
Fixture to override get_block method of ModuleStoreRuntime
|
|
"""
|
|
|
|
def get_block(self, usage_id, for_parent=None):
|
|
"""See documentation for `xblock.runtime:Runtime.get_block`"""
|
|
block = self.load_item(usage_id, for_parent=for_parent)
|
|
return block
|
|
|
|
monkeypatch.setattr(ModuleStoreRuntime, "get_block", get_block)
|