diff --git a/common/lib/xmodule/xmodule/modulestore/tests/django_utils.py b/common/lib/xmodule/xmodule/modulestore/tests/django_utils.py index 58ba94f29a..a3072f6d82 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/django_utils.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/django_utils.py @@ -2,19 +2,23 @@ """ Modulestore configuration for test cases. """ +from __future__ import absolute_import + import copy import functools import os from contextlib import contextmanager from enum import Enum -from lms.djangoapps.courseware.field_overrides import OverrideFieldData # pylint: disable=import-error -from courseware.tests.factories import StaffFactory from django.conf import settings from django.contrib.auth.models import AnonymousUser, User from django.test import TestCase from django.test.utils import override_settings from mock import patch +from six.moves import range + +from courseware.tests.factories import StaffFactory +from lms.djangoapps.courseware.field_overrides import OverrideFieldData # pylint: disable=import-error from openedx.core.djangolib.testing.utils import CacheIsolationMixin, CacheIsolationTestCase, FilteredQueryCountMixin from openedx.core.lib.tempdir import mkdtemp_clean from student.models import CourseEnrollment @@ -40,7 +44,7 @@ class CourseUserType(Enum): class StoreConstructors(object): """Enumeration of store constructor types.""" - draft, split = range(2) + draft, split = list(range(2)) def mixed_store_config(data_dir, mappings, store_order=None): diff --git a/common/lib/xmodule/xmodule/modulestore/tests/factories.py b/common/lib/xmodule/xmodule/modulestore/tests/factories.py index e68f79c542..c20499ec22 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/factories.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/factories.py @@ -1,30 +1,31 @@ """ Factories for use in tests of XBlocks. """ -from __future__ import print_function +from __future__ import absolute_import, print_function import datetime import functools -import pymongo.message -import pytz import threading import traceback from collections import defaultdict from contextlib import contextmanager from uuid import uuid4 -from factory import Factory, Sequence, lazy_attribute_sequence, lazy_attribute +import pymongo.message +import pytz +import six +from factory import Factory, Sequence, lazy_attribute, lazy_attribute_sequence from factory.errors import CyclicDefinitionError from mock import patch - -from opaque_keys.edx.locator import BlockUsageLocator from opaque_keys.edx.keys import UsageKey +from opaque_keys.edx.locator import BlockUsageLocator from xblock.core import XBlock -from xmodule.modulestore import prefer_xmodules, ModuleStoreEnum -from xmodule.modulestore.tests.sample_courses import default_block_info_tree, TOY_BLOCK_INFO_TREE + +from xmodule.course_module import Textbook +from xmodule.modulestore import ModuleStoreEnum, prefer_xmodules +from xmodule.modulestore.tests.sample_courses import TOY_BLOCK_INFO_TREE, default_block_info_tree from xmodule.tabs import CourseTab from xmodule.x_module import DEPRECATION_VSCOMPAT_EVENT -from xmodule.course_module import Textbook class Dummy(object): @@ -370,7 +371,7 @@ class ItemFactory(XModuleFactory): template = clz.get_template(template_id) assert template is not None metadata.update(template.get('metadata', {})) - if not isinstance(data, basestring): + if not isinstance(data, six.string_types): data.update(template.get('data')) # replace the display name with an optional parameter passed in from the caller @@ -506,7 +507,7 @@ class StackTraceCounter(object): """ Iterate over all unique captured stacks. """ - return iter(sorted(self._stacks.keys(), key=lambda stack: (self.stack_calls(stack), stack), reverse=True)) + return iter(sorted(list(self._stacks.keys()), key=lambda stack: (self.stack_calls(stack), stack), reverse=True)) def __getitem__(self, stack): """ diff --git a/common/lib/xmodule/xmodule/modulestore/tests/mongo_connection.py b/common/lib/xmodule/xmodule/modulestore/tests/mongo_connection.py index 6469997453..082bc0b5a5 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/mongo_connection.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/mongo_connection.py @@ -4,6 +4,8 @@ The settings can be provided by environment variables in the shell running the t in a variety of environment variables but provides sensible defaults in case those env var overrides don't exist """ +from __future__ import absolute_import + import os MONGO_PORT_NUM = int(os.environ.get('EDXAPP_TEST_MONGO_PORT', '27017')) diff --git a/common/lib/xmodule/xmodule/modulestore/tests/sample_courses.py b/common/lib/xmodule/xmodule/modulestore/tests/sample_courses.py index 6dcc73a718..adc98fb25e 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/sample_courses.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/sample_courses.py @@ -2,12 +2,16 @@ """ The data type and use of it for declaratively creating test courses. """ +from __future__ import absolute_import + +import datetime +from collections import namedtuple + # used to create course subtrees in ModuleStoreTestCase.create_test_course # adds to self properties w/ the given block_id which hold the UsageKey for easy retrieval. # fields is a dictionary of keys and values. sub_tree is a collection of BlockInfo -from collections import namedtuple -import datetime BlockInfo = namedtuple('BlockInfo', 'block_id, category, fields, sub_tree') + default_block_info_tree = [ # pylint: disable=invalid-name BlockInfo( 'chapter_x', 'chapter', {}, [ diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_abstraction.py b/common/lib/xmodule/xmodule/modulestore/tests/test_abstraction.py index 5aac079ae9..e334060eb8 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/test_abstraction.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_abstraction.py @@ -1,6 +1,8 @@ """ Simple test to ensure that modulestore base classes remain abstract """ +from __future__ import absolute_import + from unittest import TestCase from xmodule.modulestore import ModuleStoreRead, ModuleStoreWrite diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_asides.py b/common/lib/xmodule/xmodule/modulestore/tests/test_asides.py index 3f2d94659d..a1b474e9bd 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/test_asides.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_asides.py @@ -1,12 +1,16 @@ """ Tests for Asides """ +from __future__ import absolute_import + +from unittest import TestCase + +from mock import patch from web_fragments.fragment import Fragment from xblock.core import XBlockAside from xblock.fields import Scope, String -from unittest import TestCase + from xmodule.modulestore.tests.utils import XmlModulestoreBuilder -from mock import patch class AsideTestType(XBlockAside): diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_assetstore.py b/common/lib/xmodule/xmodule/modulestore/tests/test_assetstore.py index b50dc26048..246cda5313 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/test_assetstore.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_assetstore.py @@ -2,23 +2,30 @@ Tests for assetstore using any of the modulestores for metadata. May extend to testing the storage options too. """ -from datetime import datetime, timedelta -import ddt -from django.test import TestCase -import pytz -import unittest +from __future__ import absolute_import +import unittest +from datetime import datetime, timedelta + +import ddt +import pytz +import six + +from django.test import TestCase from opaque_keys.edx.keys import CourseKey from opaque_keys.edx.locator import CourseLocator +from six.moves import range, zip from openedx.core.lib.tests import attr from xmodule.assetstore import AssetMetadata -from xmodule.modulestore import ModuleStoreEnum, SortedAssetList, IncorrectlySortedList +from xmodule.modulestore import IncorrectlySortedList, ModuleStoreEnum, SortedAssetList from xmodule.modulestore.exceptions import ItemNotFoundError from xmodule.modulestore.tests.factories import CourseFactory from xmodule.modulestore.tests.utils import ( - MIXED_MODULESTORE_BOTH_SETUP, MODULESTORE_SETUPS, - XmlModulestoreBuilder, MixedModulestoreBuilder + MIXED_MODULESTORE_BOTH_SETUP, + MODULESTORE_SETUPS, + MixedModulestoreBuilder, + XmlModulestoreBuilder ) @@ -28,7 +35,11 @@ class AssetStoreTestData(object): """ now = datetime.now(pytz.utc) user_id = 144 - user_id_long = long(user_id) + if six.PY2: + user_id_long = long(user_id) + else: + user_id_long = int(user_id) + user_email = "me@example.com" asset_fields = ( @@ -66,7 +77,7 @@ class TestSortedAssetList(unittest.TestCase): def setUp(self): super(TestSortedAssetList, self).setUp() - asset_list = [dict(zip(AssetStoreTestData.asset_fields, asset)) for asset in AssetStoreTestData.all_asset_data] + asset_list = [dict(list(zip(AssetStoreTestData.asset_fields, asset))) for asset in AssetStoreTestData.all_asset_data] self.sorted_asset_list_by_filename = SortedAssetList(iterable=asset_list) self.sorted_asset_list_by_last_edit = SortedAssetList(iterable=asset_list, key=lambda x: x['edited_on']) self.course_key = CourseLocator('org', 'course', 'run') @@ -151,7 +162,7 @@ class TestMongoAssetMetadataStorage(TestCase): Setup assets. Save in store if given """ for i, asset in enumerate(AssetStoreTestData.all_asset_data): - asset_dict = dict(zip(AssetStoreTestData.asset_fields[1:], asset[1:])) + asset_dict = dict(list(zip(AssetStoreTestData.asset_fields[1:], asset[1:]))) if i in (0, 1) and course1_key: asset_key = course1_key.make_asset_key('asset', asset[0]) asset_md = AssetMetadata(asset_key, **asset_dict) @@ -604,7 +615,7 @@ class TestMongoAssetMetadataStorage(TestCase): ) # First, with paging across all sorts. for sort_test in expected_sorts_by_2: - for i in xrange(3): + for i in range(3): asset_page = store.get_all_asset_metadata( course2.id, 'asset', start=2 * i, maxresults=2, sort=sort_test[0] ) diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_contentstore.py b/common/lib/xmodule/xmodule/modulestore/tests/test_contentstore.py index 36798d04d4..b9e19e521f 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/test_contentstore.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_contentstore.py @@ -1,22 +1,25 @@ """ Test contentstore.mongo functionality """ -import logging -from uuid import uuid4 -import unittest -import mimetypes -from tempfile import mkdtemp -import path -import shutil +from __future__ import absolute_import + +import logging +import mimetypes +import shutil +import unittest +from tempfile import mkdtemp +from uuid import uuid4 -from opaque_keys.edx.locator import CourseLocator, AssetLocator -from opaque_keys.edx.keys import AssetKey -from xmodule.tests import DATA_DIR -from xmodule.contentstore.mongo import MongoContentStore -from xmodule.contentstore.content import StaticContent -from xmodule.exceptions import NotFoundError import ddt -from xmodule.modulestore.tests.mongo_connection import MONGO_PORT_NUM, MONGO_HOST +import path +from opaque_keys.edx.keys import AssetKey +from opaque_keys.edx.locator import AssetLocator, CourseLocator + +from xmodule.contentstore.content import StaticContent +from xmodule.contentstore.mongo import MongoContentStore +from xmodule.exceptions import NotFoundError +from xmodule.modulestore.tests.mongo_connection import MONGO_HOST, MONGO_PORT_NUM +from xmodule.tests import DATA_DIR log = logging.getLogger(__name__) diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_cross_modulestore_import_export.py b/common/lib/xmodule/xmodule/modulestore/tests/test_cross_modulestore_import_export.py index 53f5428b19..3708c08bfa 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/test_cross_modulestore_import_export.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_cross_modulestore_import_export.py @@ -12,25 +12,30 @@ and then for each combination of modulestores, performing the sequence: """ +from __future__ import absolute_import + import itertools import os -from path import Path as path from shutil import rmtree from tempfile import mkdtemp import ddt from mock import patch +from path import Path as path from openedx.core.lib.tests import attr -from xmodule.tests import CourseComparisonTest -from xmodule.modulestore.xml_importer import import_course_from_xml -from xmodule.modulestore.xml_exporter import export_course_to_xml -from xmodule.modulestore.tests.utils import mock_tab_from_json -from xmodule.partitions.tests.test_partitions import PartitionTestCase from xmodule.modulestore.tests.utils import ( - MongoContentstoreBuilder, MODULESTORE_SETUPS, SPLIT_MODULESTORE_SETUP, - CONTENTSTORE_SETUPS, TEST_DATA_DIR + CONTENTSTORE_SETUPS, + MODULESTORE_SETUPS, + SPLIT_MODULESTORE_SETUP, + TEST_DATA_DIR, + MongoContentstoreBuilder, + mock_tab_from_json ) +from xmodule.modulestore.xml_exporter import export_course_to_xml +from xmodule.modulestore.xml_importer import import_course_from_xml +from xmodule.partitions.tests.test_partitions import PartitionTestCase +from xmodule.tests import CourseComparisonTest COURSE_DATA_NAMES = ( 'toy', diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_libraries.py b/common/lib/xmodule/xmodule/modulestore/tests/test_libraries.py index cdd2e795b2..700760a262 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/test_libraries.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_libraries.py @@ -5,13 +5,16 @@ Basic unit tests related to content libraries. Higher-level tests are in `cms/djangoapps/contentstore`. """ -import ddt +from __future__ import absolute_import +import ddt +import six from bson.objectid import ObjectId from opaque_keys.edx.locator import LibraryLocator +from six.moves import range from xmodule.modulestore.exceptions import DuplicateCourseError -from xmodule.modulestore.tests.factories import LibraryFactory, ItemFactory, check_mongo_calls +from xmodule.modulestore.tests.factories import ItemFactory, LibraryFactory, check_mongo_calls from xmodule.modulestore.tests.utils import MixedSplitTestCase @@ -55,8 +58,8 @@ class TestLibraries(MixedSplitTestCase): Test __unicode__() and __str__() methods of libraries """ library = LibraryFactory.create(metadata={"display_name": name}, modulestore=self.store) - self.assertIn(name, unicode(library)) - if not isinstance(name, unicode): + self.assertIn(name, six.text_type(library)) + if not isinstance(name, six.text_type): self.assertIn(name, str(library)) def test_display_with_default_methods(self): diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_mixed_modulestore.py b/common/lib/xmodule/xmodule/modulestore/tests/test_mixed_modulestore.py index fc2b7ab0db..4029ed57a7 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/test_mixed_modulestore.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_mixed_modulestore.py @@ -1,58 +1,70 @@ """ Unit tests for the Mixed Modulestore, with DDT for the various stores (Split, Draft, XML) """ -from collections import namedtuple -import datetime -import logging -import ddt -import itertools -import mimetypes -from uuid import uuid4 -from contextlib import contextmanager -import pytest -from mock import patch, Mock, call +from __future__ import absolute_import +import datetime +import itertools +import logging +import mimetypes +from collections import namedtuple +from contextlib import contextmanager +from shutil import rmtree +from tempfile import mkdtemp +from uuid import uuid4 + +import ddt +import pymongo +import pytest +import six # Mixed modulestore depends on django, so we'll manually configure some django settings # before importing the module # TODO remove this import and the configuration -- xmodule should not depend on django! from django.conf import settings -import pymongo +from mock import Mock, call, patch +from opaque_keys.edx.keys import CourseKey +from opaque_keys.edx.locator import BlockUsageLocator, CourseLocator, LibraryLocator from pytz import UTC -from shutil import rmtree -from tempfile import mkdtemp +from six.moves import range from web_fragments.fragment import Fragment - -from xmodule.x_module import XModuleMixin -from xmodule.modulestore.edit_info import EditInfoMixin -from xmodule.modulestore.inheritance import InheritanceMixin -from xmodule.modulestore.tests.utils import MongoContentstoreBuilder -from xmodule.contentstore.content import StaticContent -from xmodule.modulestore.xml_importer import import_course_from_xml -from xmodule.modulestore.xml_exporter import export_course_to_xml -from xmodule.modulestore.tests.test_asides import AsideTestType from xblock.core import XBlockAside -from xblock.fields import Scope, String, ScopeIds +from xblock.fields import Scope, ScopeIds, String from xblock.runtime import DictKeyValueStore, KvsFieldData from xblock.test.tools import TestRuntime +from openedx.core.lib.tests import attr +from xmodule.contentstore.content import StaticContent +from xmodule.exceptions import InvalidVersionError +from xmodule.modulestore import ModuleStoreEnum +from xmodule.modulestore.draft_and_published import DIRECT_ONLY_CATEGORIES, UnsupportedRevisionError +from xmodule.modulestore.edit_info import EditInfoMixin +from xmodule.modulestore.exceptions import ( + DuplicateCourseError, + ItemNotFoundError, + NoPathToItem, + ReferentialIntegrityError +) +from xmodule.modulestore.inheritance import InheritanceMixin +from xmodule.modulestore.mixed import MixedModuleStore +from xmodule.modulestore.search import navigation_index, path_to_location +from xmodule.modulestore.store_utilities import DETACHED_XBLOCK_TYPES +from xmodule.modulestore.tests.factories import check_exact_number_of_calls, check_mongo_calls, mongo_uses_error_check +from xmodule.modulestore.tests.mongo_connection import MONGO_HOST, MONGO_PORT_NUM +from xmodule.modulestore.tests.test_asides import AsideTestType +from xmodule.modulestore.tests.utils import ( + LocationMixin, + MongoContentstoreBuilder, + create_modulestore_instance, + mock_tab_from_json +) +from xmodule.modulestore.xml_exporter import export_course_to_xml +from xmodule.modulestore.xml_importer import import_course_from_xml +from xmodule.tests import DATA_DIR, CourseComparisonTest +from xmodule.x_module import XModuleMixin + if not settings.configured: settings.configure() -from opaque_keys.edx.keys import CourseKey -from opaque_keys.edx.locator import BlockUsageLocator, CourseLocator, LibraryLocator -from openedx.core.lib.tests import attr -from xmodule.exceptions import InvalidVersionError -from xmodule.modulestore import ModuleStoreEnum -from xmodule.modulestore.draft_and_published import UnsupportedRevisionError, DIRECT_ONLY_CATEGORIES -from xmodule.modulestore.exceptions import ItemNotFoundError, DuplicateCourseError, ReferentialIntegrityError, NoPathToItem -from xmodule.modulestore.mixed import MixedModuleStore -from xmodule.modulestore.search import path_to_location, navigation_index -from xmodule.modulestore.store_utilities import DETACHED_XBLOCK_TYPES -from xmodule.modulestore.tests.factories import check_mongo_calls, check_exact_number_of_calls, \ - mongo_uses_error_check -from xmodule.modulestore.tests.utils import create_modulestore_instance, LocationMixin, mock_tab_from_json -from xmodule.modulestore.tests.mongo_connection import MONGO_PORT_NUM, MONGO_HOST -from xmodule.tests import DATA_DIR, CourseComparisonTest log = logging.getLogger(__name__) @@ -109,7 +121,7 @@ class CommonMixedModuleStoreSetup(CourseComparisonTest): AssertEqual replacement for CourseLocator """ if loc1.for_branch(None) != loc2.for_branch(None): - self.fail(self._formatMessage(msg, u"{} != {}".format(unicode(loc1), unicode(loc2)))) + self.fail(self._formatMessage(msg, u"{} != {}".format(six.text_type(loc1), six.text_type(loc2)))) def setUp(self): """ @@ -1614,7 +1626,7 @@ class TestMixedModuleStore(CommonMixedModuleStoreSetup): # add another parent (unit) "vertical_x1b" for problem "problem_x1a_1" mongo_store.collection.update( self.vertical_x1b.to_deprecated_son('_id.'), - {'$push': {'definition.children': unicode(self.problem_x1a_1)}} + {'$push': {'definition.children': six.text_type(self.problem_x1a_1)}} ) # convert first parent (unit) "vertical_x1a" of problem "problem_x1a_1" to draft @@ -1864,11 +1876,11 @@ class TestMixedModuleStore(CommonMixedModuleStoreSetup): # add orphan vertical and sequential as another parents of problem "problem_x1a_1" mongo_store.collection.update( orphan_sequential.to_deprecated_son('_id.'), - {'$push': {'definition.children': unicode(self.problem_x1a_1)}} + {'$push': {'definition.children': six.text_type(self.problem_x1a_1)}} ) mongo_store.collection.update( orphan_vertical.to_deprecated_son('_id.'), - {'$push': {'definition.children': unicode(self.problem_x1a_1)}} + {'$push': {'definition.children': six.text_type(self.problem_x1a_1)}} ) # test that "get_parent_location" method of published branch still returns the correct non-orphan parent for # problem "problem_x1a_1" since the two other parents are orphans @@ -1879,7 +1891,7 @@ class TestMixedModuleStore(CommonMixedModuleStoreSetup): # now add valid published vertical as another parent of problem mongo_store.collection.update( self.sequential_x1.to_deprecated_son('_id.'), - {'$push': {'definition.children': unicode(self.problem_x1a_1)}} + {'$push': {'definition.children': six.text_type(self.problem_x1a_1)}} ) # now check that "get_parent_location" method of published branch raises "ReferentialIntegrityError" for # problem "problem_x1a_1" since it has now 2 valid published parents diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_modulestore_settings.py b/common/lib/xmodule/xmodule/modulestore/tests/test_modulestore_settings.py index 868ebf5bea..2484a6b5b3 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/test_modulestore_settings.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_modulestore_settings.py @@ -1,16 +1,18 @@ """ Tests for testing the modulestore settings migration code. """ +from __future__ import absolute_import + import copy +from unittest import TestCase + import ddt from openedx.core.lib.tempdir import mkdtemp_clean - -from unittest import TestCase from xmodule.modulestore.modulestore_settings import ( convert_module_store_setting_if_needed, - update_module_store_settings, get_mixed_stores, + update_module_store_settings ) diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_mongo.py b/common/lib/xmodule/xmodule/modulestore/tests/test_mongo.py index 189fbd505d..599a810024 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/test_mongo.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_mongo.py @@ -1,47 +1,49 @@ """ Unit tests for the Mongo modulestore """ -# pylint: disable=protected-access -# pylint: disable=no-name-in-module -# pylint: disable=bad-continuation -from django.test import TestCase -# pylint: enable=E0611 -from path import Path as path -import pymongo -import pytest + +from __future__ import absolute_import + import logging import shutil +from datetime import datetime from tempfile import mkdtemp from uuid import uuid4 -from datetime import datetime -from pytz import UTC + +import pymongo +import pytest +import six + +# pylint: disable=no-name-in-module +# pylint: disable=bad-continuation +# pylint: disable=protected-access +from django.test import TestCase +# pylint: enable=E0611 from mock import patch -from xblock.core import XBlock - -from xblock.fields import Scope, Reference, ReferenceList, ReferenceValueDict -from xblock.runtime import KeyValueStore -from xblock.exceptions import InvalidScopeError - -from xmodule.tests import DATA_DIR -from opaque_keys.edx.keys import CourseKey -from xmodule.modulestore import ModuleStoreEnum -from xmodule.modulestore.mongo import MongoKeyValueStore -from xmodule.modulestore.draft import DraftModuleStore +from opaque_keys.edx.keys import CourseKey, UsageKey from opaque_keys.edx.locator import AssetLocator, BlockUsageLocator, CourseLocator, LibraryLocator -from opaque_keys.edx.keys import UsageKey -from xmodule.modulestore.xml_exporter import export_course_to_xml -from xmodule.modulestore.xml_importer import import_course_from_xml, perform_xlint -from xmodule.contentstore.mongo import MongoContentStore +from path import Path as path +from pytz import UTC +from xblock.core import XBlock +from xblock.exceptions import InvalidScopeError +from xblock.fields import Reference, ReferenceList, ReferenceValueDict, Scope +from xblock.runtime import KeyValueStore +from xmodule.contentstore.mongo import MongoContentStore from xmodule.exceptions import NotFoundError -from xmodule.x_module import XModuleMixin -from xmodule.modulestore.mongo.base import as_draft -from xmodule.modulestore.tests.mongo_connection import MONGO_PORT_NUM, MONGO_HOST -from xmodule.modulestore.tests.utils import LocationMixin, mock_tab_from_json +from xmodule.modulestore import ModuleStoreEnum +from xmodule.modulestore.draft import DraftModuleStore from xmodule.modulestore.edit_info import EditInfoMixin from xmodule.modulestore.exceptions import ItemNotFoundError from xmodule.modulestore.inheritance import InheritanceMixin - +from xmodule.modulestore.mongo import MongoKeyValueStore +from xmodule.modulestore.mongo.base import as_draft +from xmodule.modulestore.tests.mongo_connection import MONGO_HOST, MONGO_PORT_NUM +from xmodule.modulestore.tests.utils import LocationMixin, mock_tab_from_json +from xmodule.modulestore.xml_exporter import export_course_to_xml +from xmodule.modulestore.xml_importer import import_course_from_xml, perform_xlint +from xmodule.tests import DATA_DIR +from xmodule.x_module import XModuleMixin log = logging.getLogger(__name__) @@ -526,7 +528,7 @@ class TestMongoModuleStore(TestMongoModuleStoreBase): for ref in refele.reference_list: assert isinstance(ref, UsageKey) assert len(refele.reference_dict) > 0 - for ref in refele.reference_dict.itervalues(): + for ref in six.itervalues(refele.reference_dict): assert isinstance(ref, UsageKey) def check_mongo_fields(): @@ -535,17 +537,17 @@ class TestMongoModuleStore(TestMongoModuleStoreBase): def check_children(payload): for child in payload['definition']['children']: - assert isinstance(child, basestring) + assert isinstance(child, six.string_types) refele = get_item(self.refloc) check_children(refele) - assert isinstance(refele['definition']['data']['reference_link'], basestring) + assert isinstance(refele['definition']['data']['reference_link'], six.string_types) assert len(refele['definition']['data']['reference_list']) > 0 for ref in refele['definition']['data']['reference_list']: - assert isinstance(ref, basestring) + assert isinstance(ref, six.string_types) assert len(refele['metadata']['reference_dict']) > 0 - for ref in refele['metadata']['reference_dict'].itervalues(): - assert isinstance(ref, basestring) + for ref in six.itervalues(refele['metadata']['reference_dict']): + assert isinstance(ref, six.string_types) setup_test() check_xblock_fields() @@ -704,8 +706,8 @@ class TestMongoModuleStore(TestMongoModuleStoreBase): # First child should have been moved to second position, and better child takes the lead course = self.draft_store.get_course(course.id) - self.assertEqual(unicode(course.children[1]), unicode(first_child.location)) - self.assertEqual(unicode(course.children[0]), unicode(second_child.location)) + self.assertEqual(six.text_type(course.children[1]), six.text_type(first_child.location)) + self.assertEqual(six.text_type(course.children[0]), six.text_type(second_child.location)) # Clean up the data so we don't break other tests which apparently expect a particular state self.draft_store.delete_course(course.id, self.dummy_user) diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_mongo_call_count.py b/common/lib/xmodule/xmodule/modulestore/tests/test_mongo_call_count.py index dccdc1a304..ebc7b4b852 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/test_mongo_call_count.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_mongo_call_count.py @@ -3,20 +3,26 @@ Tests to verify correct number of MongoDB calls during course import/export and when using the Split modulestore. """ -from tempfile import mkdtemp +from __future__ import absolute_import + from shutil import rmtree +from tempfile import mkdtemp from unittest import TestCase, skip + import ddt +import six from django.test import TestCase -from xmodule.modulestore.xml_importer import import_course_from_xml -from xmodule.modulestore.xml_exporter import export_course_to_xml from xmodule.modulestore.tests.factories import check_mongo_calls from xmodule.modulestore.tests.utils import ( - MixedModulestoreBuilder, VersioningModulestoreBuilder, - MongoModulestoreBuilder, TEST_DATA_DIR, + TEST_DATA_DIR, MemoryCache, + MixedModulestoreBuilder, + MongoModulestoreBuilder, + VersioningModulestoreBuilder ) +from xmodule.modulestore.xml_exporter import export_course_to_xml +from xmodule.modulestore.xml_importer import import_course_from_xml MIXED_OLD_MONGO_MODULESTORE_BUILDER = MixedModulestoreBuilder([('draft', MongoModulestoreBuilder())]) MIXED_SPLIT_MODULESTORE_BUILDER = MixedModulestoreBuilder([('split', VersioningModulestoreBuilder())]) @@ -107,7 +113,7 @@ class CountMongoCallsCourseTraversal(TestCase): if access_all_block_fields: # Read the fields on each block in order to ensure each block and its definition is loaded. for xblock in all_blocks: - for __, field in xblock.fields.iteritems(): + for __, field in six.iteritems(xblock.fields): if field.is_set_on(xblock): __ = field.read_from(xblock) diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_publish.py b/common/lib/xmodule/xmodule/modulestore/tests/test_publish.py index fd76ae3e9f..7610c07d87 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/test_publish.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_publish.py @@ -1,7 +1,8 @@ """ Test the publish code (mostly testing that publishing doesn't result in orphans) """ -import ddt +from __future__ import absolute_import + import itertools import os import re @@ -12,17 +13,24 @@ from contextlib import contextmanager from shutil import rmtree from tempfile import mkdtemp +import ddt +import six +from six.moves import range + from openedx.core.lib.tests import attr from xmodule.exceptions import InvalidVersionError from xmodule.modulestore import ModuleStoreEnum from xmodule.modulestore.exceptions import ItemNotFoundError -from xmodule.modulestore.xml_exporter import export_course_to_xml +from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory, check_mongo_calls, mongo_uses_error_check from xmodule.modulestore.tests.test_split_w_old_mongo import SplitWMongoCourseBootstrapper -from xmodule.modulestore.tests.factories import check_mongo_calls, mongo_uses_error_check, CourseFactory, ItemFactory from xmodule.modulestore.tests.utils import ( - MongoContentstoreBuilder, MODULESTORE_SETUPS, - DRAFT_MODULESTORE_SETUP, SPLIT_MODULESTORE_SETUP, MongoModulestoreBuilder, + DRAFT_MODULESTORE_SETUP, + MODULESTORE_SETUPS, + SPLIT_MODULESTORE_SETUP, + MongoContentstoreBuilder, + MongoModulestoreBuilder ) +from xmodule.modulestore.xml_exporter import export_course_to_xml @attr('mongo') @@ -208,7 +216,7 @@ class DraftPublishedOpTestCourseSetup(unittest.TestCase): Add a level of the binary course structure by creating the items as children of the proper parents. """ parent_id = 'course' - for idx in xrange(num_items): + for idx in range(num_items): if parent_type != 'course': parent_id = _make_block_id(parent_type, idx / 2) parent_item = getattr(self, parent_id) @@ -244,13 +252,13 @@ class DraftPublishedOpTestCourseSetup(unittest.TestCase): # Create a list of all verticals for convenience. block_type = 'vertical' - for idx in xrange(8): + for idx in range(8): block_id = _make_block_id(block_type, idx) self.all_verticals.append((block_type, block_id)) # Create a list of all html units for convenience. block_type = 'html' - for idx in xrange(16): + for idx in range(16): block_id = _make_block_id(block_type, idx) self.all_units.append((block_type, block_id)) @@ -406,7 +414,7 @@ class OLXFormatChecker(unittest.TestCase): parent_key = course_key.make_usage_key(parent_type, parent_id) self.assertElementAttrsSubset(element, { - 'parent_url': re.escape(unicode(parent_key)), + 'parent_url': re.escape(six.text_type(parent_key)), 'index_in_children_list': re.escape(str(index_in_children_list)), }) @@ -564,7 +572,7 @@ class DraftPublishedOpBaseTestSetup(OLXFormatChecker, DraftPublishedOpTestCourse """ Make a unique name for the new export dir. """ - return self.EXPORTED_COURSE_AFTER_DIR_NAME.format(unicode(uuid.uuid4())[:8]) + return self.EXPORTED_COURSE_AFTER_DIR_NAME.format(six.text_type(uuid.uuid4())[:8]) def publish(self, block_list): """ diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_semantics.py b/common/lib/xmodule/xmodule/modulestore/tests/test_semantics.py index f70f209241..67da0fd8dd 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/test_semantics.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_semantics.py @@ -2,25 +2,25 @@ Tests of modulestore semantics: How do the interfaces methods of ModuleStore relate to each other? """ -import ddt +from __future__ import absolute_import + import itertools from collections import namedtuple -from xmodule.course_module import CourseSummary -from mock import patch -from xmodule.modulestore.tests.utils import ( - PureModulestoreTestCase, MongoModulestoreBuilder, - SPLIT_MODULESTORE_SETUP -) -from xmodule.modulestore.exceptions import ItemNotFoundError -from xmodule.modulestore import ModuleStoreEnum -from xmodule.modulestore.tests.factories import CourseFactory -from xmodule.modulestore.draft_and_published import DIRECT_ONLY_CATEGORIES +import ddt +from mock import patch from xblock.core import XBlock, XBlockAside from xblock.fields import Scope, String from xblock.runtime import DictKeyValueStore, KvsFieldData from xblock.test.tools import TestRuntime +from xmodule.course_module import CourseSummary +from xmodule.modulestore import ModuleStoreEnum +from xmodule.modulestore.draft_and_published import DIRECT_ONLY_CATEGORIES +from xmodule.modulestore.exceptions import ItemNotFoundError +from xmodule.modulestore.tests.factories import CourseFactory +from xmodule.modulestore.tests.utils import SPLIT_MODULESTORE_SETUP, MongoModulestoreBuilder, PureModulestoreTestCase + DETACHED_BLOCK_TYPES = dict(XBlock.load_tagged_classes('detached')) # These tests won't work with courses, since they're creating blocks inside courses diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_split_copy_from_template.py b/common/lib/xmodule/xmodule/modulestore/tests/test_split_copy_from_template.py index d78d34637c..be075965e6 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/test_split_copy_from_template.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_split_copy_from_template.py @@ -3,7 +3,11 @@ Tests for split's copy_from_template method. Currently it is only used for content libraries. However for these tests, we make sure it also works when copying from course to course. """ +from __future__ import absolute_import + import ddt +from six.moves import range + from xmodule.modulestore import ModuleStoreEnum from xmodule.modulestore.exceptions import ItemNotFoundError from xmodule.modulestore.tests.factories import CourseFactory, LibraryFactory diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_split_migrator.py b/common/lib/xmodule/xmodule/modulestore/tests/test_split_migrator.py index 25aed75393..265b296efe 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/test_split_migrator.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_split_migrator.py @@ -2,13 +2,17 @@ Tests for split_migrator """ +from __future__ import absolute_import + import random import uuid import mock +import six +from six.moves import range, zip +from xblock.fields import UNIQUE_ID, Reference, ReferenceList, ReferenceValueDict from openedx.core.lib.tests import attr -from xblock.fields import Reference, ReferenceList, ReferenceValueDict, UNIQUE_ID from xmodule.modulestore.split_migrator import SplitMigrator from xmodule.modulestore.tests.test_split_w_old_mongo import SplitWMongoCourseBootstrapper @@ -163,7 +167,7 @@ class TestMigration(SplitWMongoCourseBootstrapper): if split_dag_root.category != 'course': self.assertEqual(presplit_dag_root.location.block_id, split_dag_root.location.block_id) # compare all fields but references - for name, field in presplit_dag_root.fields.iteritems(): + for name, field in six.iteritems(presplit_dag_root.fields): # fields generated from UNIQUE_IDs are unique to an XBlock's scope, # so if such a field is unset on an XBlock, we don't expect it # to persist across courses diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_split_modulestore.py b/common/lib/xmodule/xmodule/modulestore/tests/test_split_modulestore.py index 23b68c555d..6b72e27e81 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/test_split_modulestore.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_split_modulestore.py @@ -1,42 +1,47 @@ """ Test split modulestore w/o using any django stuff. """ -from mock import patch +from __future__ import absolute_import + import datetime -from importlib import import_module -from path import Path as path +import os import random import re import unittest -import os +from importlib import import_module import ddt +import six +from ccx_keys.locator import CCXBlockUsageLocator from contracts import contract -from django.core.cache import caches, InvalidCacheBackendError +from django.core.cache import InvalidCacheBackendError, caches +from mock import patch +from opaque_keys.edx.locator import BlockUsageLocator, CourseKey, CourseLocator, LocalId, VersionTree +from path import Path as path +from six.moves import range +from xblock.fields import Reference, ReferenceList, ReferenceValueDict from openedx.core.lib import tempdir from openedx.core.lib.tests import attr -from xblock.fields import Reference, ReferenceList, ReferenceValueDict from xmodule.course_module import CourseDescriptor -from xmodule.modulestore import ModuleStoreEnum -from xmodule.modulestore.exceptions import ( - ItemNotFoundError, VersionConflictError, - DuplicateItemError, DuplicateCourseError, - InsufficientSpecificationError -) -from opaque_keys.edx.locator import CourseKey, CourseLocator, BlockUsageLocator, VersionTree, LocalId -from ccx_keys.locator import CCXBlockUsageLocator -from xmodule.modulestore.inheritance import InheritanceMixin -from xmodule.x_module import XModuleMixin from xmodule.fields import Date, Timedelta -from xmodule.modulestore.split_mongo.split import SplitMongoModuleStore -from xmodule.modulestore.tests.test_modulestore import check_has_course_method -from xmodule.modulestore.split_mongo import BlockKey -from xmodule.modulestore.tests.factories import check_mongo_calls -from xmodule.modulestore.tests.mongo_connection import MONGO_PORT_NUM, MONGO_HOST -from xmodule.modulestore.tests.utils import mock_tab_from_json +from xmodule.modulestore import ModuleStoreEnum from xmodule.modulestore.edit_info import EditInfoMixin - +from xmodule.modulestore.exceptions import ( + DuplicateCourseError, + DuplicateItemError, + InsufficientSpecificationError, + ItemNotFoundError, + VersionConflictError +) +from xmodule.modulestore.inheritance import InheritanceMixin +from xmodule.modulestore.split_mongo import BlockKey +from xmodule.modulestore.split_mongo.split import SplitMongoModuleStore +from xmodule.modulestore.tests.factories import check_mongo_calls +from xmodule.modulestore.tests.mongo_connection import MONGO_HOST, MONGO_PORT_NUM +from xmodule.modulestore.tests.test_modulestore import check_has_course_method +from xmodule.modulestore.tests.utils import mock_tab_from_json +from xmodule.x_module import XModuleMixin BRANCH_NAME_DRAFT = ModuleStoreEnum.BranchName.draft BRANCH_NAME_PUBLISHED = ModuleStoreEnum.BranchName.published @@ -497,7 +502,7 @@ class SplitModuleTest(unittest.TestCase): ''' Sets up the initial data into the db ''' - for _course_id, course_spec in SplitModuleTest.COURSE_CONTENT.iteritems(): + for _course_id, course_spec in six.iteritems(SplitModuleTest.COURSE_CONTENT): course = split_store.create_course( course_spec['org'], course_spec['course'], @@ -508,7 +513,7 @@ class SplitModuleTest(unittest.TestCase): root_block_id=course_spec['root_block_id'] ) for revision in course_spec.get('revisions', []): - for (block_type, block_id), fields in revision.get('update', {}).iteritems(): + for (block_type, block_id), fields in six.iteritems(revision.get('update', {})): # cheat since course is most frequent if course.location.block_id == block_id: block = course @@ -516,7 +521,7 @@ class SplitModuleTest(unittest.TestCase): # not easy to figure out the category but get_item won't care block_usage = BlockUsageLocator.make_relative(course.location, block_type, block_id) block = split_store.get_item(block_usage) - for key, value in fields.iteritems(): + for key, value in six.iteritems(fields): setattr(block, key, value) # create new blocks into dag: parent must already exist; thus, order is important new_ele_dict = {} @@ -1755,8 +1760,8 @@ class TestItemCrud(SplitModuleTest): # First child should have been moved to second position, and better child takes the lead refetch_course = store.get_course(versionless_course_locator) children = refetch_course.get_children() - self.assertEqual(unicode(children[1].location), unicode(first_child.location)) - self.assertEqual(unicode(children[0].location), unicode(second_child.location)) + self.assertEqual(six.text_type(children[1].location), six.text_type(first_child.location)) + self.assertEqual(six.text_type(children[0].location), six.text_type(second_child.location)) # Clean up the data so we don't break other tests which apparently expect a particular state store.delete_course(refetch_course.id, user) diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_split_modulestore_bulk_operations.py b/common/lib/xmodule/xmodule/modulestore/tests/test_split_modulestore_bulk_operations.py index 39758fafab..c95b9fa7c9 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/test_split_modulestore_bulk_operations.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_split_modulestore_bulk_operations.py @@ -2,16 +2,19 @@ Tests for bulk operations in Split Modulestore. """ # pylint: disable=protected-access +from __future__ import absolute_import + import copy -import ddt import unittest + +import ddt from bson.objectid import ObjectId from mock import MagicMock, Mock, call -from xmodule.modulestore.split_mongo.split import SplitBulkWriteMixin -from xmodule.modulestore.split_mongo.mongo_connection import MongoConnection - from opaque_keys.edx.locator import CourseLocator +from six.moves import range +from xmodule.modulestore.split_mongo.mongo_connection import MongoConnection +from xmodule.modulestore.split_mongo.split import SplitBulkWriteMixin VERSION_GUID_DICT = { 'SAMPLE_VERSION_GUID': 'deadbeef1234' * 2, @@ -605,7 +608,7 @@ class TestBulkWriteMixinOpen(TestBulkWriteMixin): # Reading the same structure multiple times shouldn't hit the database # more than once version_guid = VERSION_GUID_DICT[version_guid_name] - for _ in xrange(2): + for _ in range(2): result = self.bulk.get_structure(self.course_key, version_guid) self.assertEquals(self.conn.get_structure.call_count, 1) self.assertEqual(result, self.conn.get_structure.return_value) @@ -648,7 +651,7 @@ class TestBulkWriteMixinOpen(TestBulkWriteMixin): # Reading the same definition multiple times shouldn't hit the database # more than once version_guid = VERSION_GUID_DICT[version_guid_name] - for _ in xrange(2): + for _ in range(2): result = self.bulk.get_definition(self.course_key, version_guid) self.assertEquals(self.conn.get_definition.call_count, 1) self.assertEqual(result, self.conn.get_definition.return_value) @@ -687,7 +690,7 @@ class TestBulkWriteMixinOpen(TestBulkWriteMixin): def test_read_index_without_write_only_reads_once(self, ignore_case): # Reading the index multiple times should only result in one read from # the database - for _ in xrange(2): + for _ in range(2): result = self.bulk.get_course_index(self.course_key, ignore_case=ignore_case) self.assertEquals(self.conn.get_course_index.call_count, 1) self.assertEquals(self.conn.get_course_index.return_value, result) diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_split_mongo_mongo_connection.py b/common/lib/xmodule/xmodule/modulestore/tests/test_split_mongo_mongo_connection.py index 1c012297b1..c66d4a4997 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/test_split_mongo_mongo_connection.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_split_mongo_mongo_connection.py @@ -1,8 +1,12 @@ """ Test the behavior of split_mongo/MongoConnection """ +from __future__ import absolute_import + import unittest + from mock import patch -from xmodule.modulestore.split_mongo.mongo_connection import MongoConnection + from xmodule.exceptions import HeartbeatFailure +from xmodule.modulestore.split_mongo.mongo_connection import MongoConnection class TestHeartbeatFailureException(unittest.TestCase): diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_split_w_old_mongo.py b/common/lib/xmodule/xmodule/modulestore/tests/test_split_w_old_mongo.py index 15f677469c..5b0955a8d2 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/test_split_w_old_mongo.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_split_w_old_mongo.py @@ -1,18 +1,22 @@ +from __future__ import absolute_import + import datetime +import os import random import unittest -import os + import mock import pytest +import six +from opaque_keys.edx.locator import BlockUsageLocator, CourseLocator -from opaque_keys.edx.locator import CourseLocator, BlockUsageLocator from xmodule.modulestore import ModuleStoreEnum -from xmodule.x_module import XModuleMixin from xmodule.modulestore.inheritance import InheritanceMixin from xmodule.modulestore.mongo import DraftMongoModuleStore from xmodule.modulestore.split_mongo.split import SplitMongoModuleStore -from xmodule.modulestore.tests.mongo_connection import MONGO_PORT_NUM, MONGO_HOST +from xmodule.modulestore.tests.mongo_connection import MONGO_HOST, MONGO_PORT_NUM from xmodule.modulestore.tests.utils import MemoryCache +from xmodule.x_module import XModuleMixin @pytest.mark.mongo @@ -86,7 +90,7 @@ class SplitWMongoCourseBootstrapper(unittest.TestCase): ) if not draft: self.draft_mongo.publish(location, self.user_id) - if isinstance(data, basestring): + if isinstance(data, six.string_types): fields = {'data': data} else: fields = data.copy() diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_store_utilities.py b/common/lib/xmodule/xmodule/modulestore/tests/test_store_utilities.py index 381dcacac4..3fb93d3cfc 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/test_store_utilities.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_store_utilities.py @@ -1,13 +1,14 @@ """ Tests for store_utilities.py """ -import unittest -from mock import Mock -import ddt +from __future__ import absolute_import -from xmodule.modulestore.store_utilities import ( - get_draft_subtree_roots, draft_node_constructor -) +import unittest + +import ddt +from mock import Mock + +from xmodule.modulestore.store_utilities import draft_node_constructor, get_draft_subtree_roots @ddt.ddt diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_xml.py b/common/lib/xmodule/xmodule/modulestore/tests/test_xml.py index 0fee0bc5ff..59b80aa1e5 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/test_xml.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_xml.py @@ -2,22 +2,22 @@ Tests around our XML modulestore, including importing well-formed and not-well-formed XML. """ +from __future__ import absolute_import + import os.path -from django.test import TestCase from glob import glob -from mock import patch, Mock -from xmodule.modulestore.xml import XMLModuleStore -from xmodule.modulestore import ModuleStoreEnum -from xmodule.x_module import XModuleMixin - -from xmodule.tests import DATA_DIR +from django.test import TestCase +from mock import Mock, patch from opaque_keys.edx.keys import CourseKey from opaque_keys.edx.locator import CourseLocator + +from xmodule.modulestore import ModuleStoreEnum from xmodule.modulestore.tests.test_modulestore import check_has_course_method -from xmodule.modulestore.tests.utils import ( - add_temp_files_from_dict, remove_temp_files_from_list, TILDA_FILES_DICT -) +from xmodule.modulestore.tests.utils import TILDA_FILES_DICT, add_temp_files_from_dict, remove_temp_files_from_list +from xmodule.modulestore.xml import XMLModuleStore +from xmodule.tests import DATA_DIR +from xmodule.x_module import XModuleMixin def glob_tildes_at_end(path): @@ -146,7 +146,7 @@ class TestModuleStoreIgnore(TestXMLModuleStore): def setUp(self): super(TestModuleStoreIgnore, self).setUp() - self.addCleanup(remove_temp_files_from_list, TILDA_FILES_DICT.keys(), self.course_dir / "static") + self.addCleanup(remove_temp_files_from_list, list(TILDA_FILES_DICT.keys()), self.course_dir / "static") add_temp_files_from_dict(TILDA_FILES_DICT, self.course_dir / "static") @patch("xmodule.modulestore.xml.glob.glob", side_effect=glob_tildes_at_end) diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_xml_importer.py b/common/lib/xmodule/xmodule/modulestore/tests/test_xml_importer.py index b0e8fd8700..070e4163fc 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/test_xml_importer.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_xml_importer.py @@ -1,26 +1,26 @@ """ Tests for XML importer. """ +from __future__ import absolute_import + +import importlib +import os +import unittest +from uuid import uuid4 + import mock +from opaque_keys.edx.keys import CourseKey from opaque_keys.edx.locator import BlockUsageLocator, CourseLocator -from xblock.fields import String, Scope, ScopeIds, List -from xblock.runtime import Runtime, KvsFieldData, DictKeyValueStore -from xmodule.x_module import XModuleMixin +from path import Path as path +from xblock.fields import List, Scope, ScopeIds, String +from xblock.runtime import DictKeyValueStore, KvsFieldData, Runtime + from xmodule.modulestore import ModuleStoreEnum from xmodule.modulestore.inheritance import InheritanceMixin -from xmodule.modulestore.xml_importer import ( - StaticContentImporter, - _update_and_import_module, - _update_module_location -) -from xmodule.modulestore.tests.mongo_connection import MONGO_PORT_NUM, MONGO_HOST -from opaque_keys.edx.keys import CourseKey +from xmodule.modulestore.tests.mongo_connection import MONGO_HOST, MONGO_PORT_NUM +from xmodule.modulestore.xml_importer import StaticContentImporter, _update_and_import_module, _update_module_location from xmodule.tests import DATA_DIR -import os -from uuid import uuid4 -from path import Path as path -import unittest -import importlib +from xmodule.x_module import XModuleMixin class ModuleStoreNoSettings(unittest.TestCase): diff --git a/common/lib/xmodule/xmodule/modulestore/tests/utils.py b/common/lib/xmodule/xmodule/modulestore/tests/utils.py index 19103f2083..063e561c62 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/utils.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/utils.py @@ -1,18 +1,21 @@ """ Helper classes and methods for running modulestore tests without Django. """ -from uuid import uuid4 +from __future__ import absolute_import + import io import os - from contextlib import contextmanager, nested from importlib import import_module -from path import Path as path from shutil import rmtree from tempfile import mkdtemp from unittest import TestCase +from uuid import uuid4 + +import six +from path import Path as path +from six.moves import range, zip -from xmodule.x_module import XModuleMixin from xmodule.contentstore.mongo import MongoContentStore from xmodule.modulestore.draft_and_published import ModuleStoreDraftAndPublished from xmodule.modulestore.edit_info import EditInfoMixin @@ -22,10 +25,11 @@ from xmodule.modulestore.mongo.base import ModuleStoreEnum from xmodule.modulestore.mongo.draft import DraftModuleStore from xmodule.modulestore.split_mongo.split_draft import DraftVersioningModuleStore from xmodule.modulestore.tests.factories import ItemFactory -from xmodule.modulestore.tests.mongo_connection import MONGO_PORT_NUM, MONGO_HOST +from xmodule.modulestore.tests.mongo_connection import MONGO_HOST, MONGO_PORT_NUM from xmodule.modulestore.xml import XMLModuleStore from xmodule.modulestore.xml_importer import LocationMixin from xmodule.tests import DATA_DIR +from xmodule.x_module import XModuleMixin def load_function(path): @@ -82,7 +86,7 @@ def add_temp_files_from_dict(file_dict, dir): with io.open("{}/{}".format(dir, file_name), "w") as opened_file: content = file_dict[file_name] if content: - opened_file.write(unicode(content)) + opened_file.write(six.text_type(content)) def remove_temp_files_from_list(file_list, dir): @@ -413,12 +417,12 @@ class MixedModulestoreBuilder(StoreBuilderBase): contentstore: The contentstore that this modulestore should use to store all of its assets. """ - names, generators = zip(*self.store_builders) + names, generators = list(zip(*self.store_builders)) with nested(*(gen.build_with_contentstore(contentstore, **kwargs) for gen in generators)) as modulestores: # Make the modulestore creation function just return the already-created modulestores store_iterator = iter(modulestores) - next_modulestore = lambda *args, **kwargs: store_iterator.next() + next_modulestore = lambda *args, **kwargs: next(store_iterator) # Generate a fake list of stores to give the already generated stores appropriate names stores = [{'NAME': name, 'ENGINE': 'This space deliberately left blank'} for name in names] @@ -491,7 +495,7 @@ DIRECT_MS_SETUPS_SHORT = ( ) MODULESTORE_SETUPS = DIRECT_MODULESTORE_SETUPS + MIXED_MODULESTORE_SETUPS MODULESTORE_SHORTNAMES = DIRECT_MS_SETUPS_SHORT + MIXED_MS_SETUPS_SHORT -SHORT_NAME_MAP = dict(zip(MODULESTORE_SETUPS, MODULESTORE_SHORTNAMES)) +SHORT_NAME_MAP = dict(list(zip(MODULESTORE_SETUPS, MODULESTORE_SHORTNAMES))) CONTENTSTORE_SETUPS = (MongoContentstoreBuilder(),)