From d7daac92e796abedb4d20d4b9aa9b76d48f66e8e Mon Sep 17 00:00:00 2001 From: Ayub khan Date: Tue, 9 Jul 2019 14:43:10 +0500 Subject: [PATCH] INCR-335 python3 compatibility --- .../commands/tests/test_delete_orphans.py | 17 +++++---- .../management/commands/tests/test_export.py | 5 ++- .../commands/tests/test_export_olx.py | 7 ++-- .../commands/tests/test_force_publish.py | 24 +++++++------ .../commands/tests/test_generate_courses.py | 2 ++ .../commands/tests/test_git_export.py | 29 ++++++++------- .../commands/tests/test_migrate_to_split.py | 7 ++-- .../commands/tests/test_reindex_library.py | 35 ++++++++++--------- .../commands/tests/test_video_thumbnails.py | 14 +++++--- 9 files changed, 83 insertions(+), 57 deletions(-) diff --git a/cms/djangoapps/contentstore/management/commands/tests/test_delete_orphans.py b/cms/djangoapps/contentstore/management/commands/tests/test_delete_orphans.py index 816d579eb6..07b2030c21 100644 --- a/cms/djangoapps/contentstore/management/commands/tests/test_delete_orphans.py +++ b/cms/djangoapps/contentstore/management/commands/tests/test_delete_orphans.py @@ -1,11 +1,14 @@ """Tests running the delete_orphan command""" -import ddt -from django.core.management import call_command, CommandError -from contentstore.tests.test_orphan import TestOrphanBase +from __future__ import absolute_import -from xmodule.modulestore.tests.factories import CourseFactory +import ddt +import six +from django.core.management import CommandError, call_command + +from contentstore.tests.test_orphan import TestOrphanBase from xmodule.modulestore import ModuleStoreEnum +from xmodule.modulestore.tests.factories import CourseFactory @ddt.ddt @@ -28,7 +31,7 @@ class TestDeleteOrphan(TestOrphanBase): results in no orphans being deleted """ course = self.create_course_with_orphans(default_store) - call_command('delete_orphans', unicode(course.id)) + call_command('delete_orphans', six.text_type(course.id)) self.assertTrue(self.store.has_item(course.id.make_usage_key('html', 'multi_parent_html'))) self.assertTrue(self.store.has_item(course.id.make_usage_key('vertical', 'OrphanVert'))) self.assertTrue(self.store.has_item(course.id.make_usage_key('chapter', 'OrphanChapter'))) @@ -42,7 +45,7 @@ class TestDeleteOrphan(TestOrphanBase): """ course = self.create_course_with_orphans(default_store) - call_command('delete_orphans', unicode(course.id), '--commit') + call_command('delete_orphans', six.text_type(course.id), '--commit') # make sure this module wasn't deleted self.assertTrue(self.store.has_item(course.id.make_usage_key('html', 'multi_parent_html'))) @@ -66,7 +69,7 @@ class TestDeleteOrphan(TestOrphanBase): # call delete orphans, specifying the published branch # of the course - call_command('delete_orphans', unicode(published_branch), '--commit') + call_command('delete_orphans', six.text_type(published_branch), '--commit') # now all orphans should be deleted self.assertOrphanCount(course.id, 0) diff --git a/cms/djangoapps/contentstore/management/commands/tests/test_export.py b/cms/djangoapps/contentstore/management/commands/tests/test_export.py index 32123455c0..4f992c0783 100644 --- a/cms/djangoapps/contentstore/management/commands/tests/test_export.py +++ b/cms/djangoapps/contentstore/management/commands/tests/test_export.py @@ -1,11 +1,14 @@ """ Tests for exporting courseware to the desired path """ +from __future__ import absolute_import + import shutil import unittest from tempfile import mkdtemp import ddt +import six from django.core.management import CommandError, call_command from xmodule.modulestore import ModuleStoreEnum @@ -49,7 +52,7 @@ class TestCourseExport(ModuleStoreTestCase): Create a new course try exporting in a path specified """ course = CourseFactory.create(default_store=store) - course_id = unicode(course.id) + course_id = six.text_type(course.id) self.assertTrue( modulestore().has_course(course.id), u"Could not find course in {}".format(store) diff --git a/cms/djangoapps/contentstore/management/commands/tests/test_export_olx.py b/cms/djangoapps/contentstore/management/commands/tests/test_export_olx.py index ebfe27ff77..f4a70dce75 100644 --- a/cms/djangoapps/contentstore/management/commands/tests/test_export_olx.py +++ b/cms/djangoapps/contentstore/management/commands/tests/test_export_olx.py @@ -2,6 +2,8 @@ Tests for exporting OLX content. """ +from __future__ import absolute_import + import shutil import tarfile import unittest @@ -9,6 +11,7 @@ from StringIO import StringIO from tempfile import mkdtemp import ddt +import six from django.core.management import CommandError, call_command from path import Path as path @@ -79,7 +82,7 @@ class TestCourseExportOlx(ModuleStoreTestCase): tmp_dir = path(mkdtemp()) self.addCleanup(shutil.rmtree, tmp_dir) filename = tmp_dir / 'test.tar.gz' - call_command('export_olx', '--output', filename, unicode(test_course_key)) + call_command('export_olx', '--output', filename, six.text_type(test_course_key)) with tarfile.open(filename) as tar_file: self.check_export_file(tar_file, test_course_key) @@ -87,7 +90,7 @@ class TestCourseExportOlx(ModuleStoreTestCase): def test_export_course_stdout(self, store_type): test_course_key = self.create_dummy_course(store_type) out = StringIO() - call_command('export_olx', unicode(test_course_key), stdout=out) + call_command('export_olx', six.text_type(test_course_key), stdout=out) out.seek(0) output = out.read() with tarfile.open(fileobj=StringIO(output)) as tar_file: diff --git a/cms/djangoapps/contentstore/management/commands/tests/test_force_publish.py b/cms/djangoapps/contentstore/management/commands/tests/test_force_publish.py index 5fd69318e6..0057069bee 100644 --- a/cms/djangoapps/contentstore/management/commands/tests/test_force_publish.py +++ b/cms/djangoapps/contentstore/management/commands/tests/test_force_publish.py @@ -1,13 +1,17 @@ """ Tests for the force_publish management command """ +from __future__ import absolute_import + import mock -from django.core.management import call_command, CommandError -from xmodule.modulestore import ModuleStoreEnum -from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase, ModuleStoreTestCase -from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory +import six +from django.core.management import CommandError, call_command + from contentstore.management.commands.force_publish import Command from contentstore.management.commands.utils import get_course_versions +from xmodule.modulestore import ModuleStoreEnum +from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase, SharedModuleStoreTestCase +from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory class TestForcePublish(SharedModuleStoreTestCase): @@ -43,7 +47,7 @@ class TestForcePublish(SharedModuleStoreTestCase): """ errstring = "Error: unrecognized arguments: invalid-arg" with self.assertRaisesRegexp(CommandError, errstring): - call_command('force_publish', unicode(self.course.id), '--commit', 'invalid-arg') + call_command('force_publish', six.text_type(self.course.id), '--commit', 'invalid-arg') def test_course_key_not_found(self): """ @@ -51,7 +55,7 @@ class TestForcePublish(SharedModuleStoreTestCase): """ errstring = "Course not found." with self.assertRaisesRegexp(CommandError, errstring): - call_command('force_publish', unicode('course-v1:org+course+run')) + call_command('force_publish', six.text_type('course-v1:org+course+run')) def test_force_publish_non_split(self): """ @@ -60,7 +64,7 @@ class TestForcePublish(SharedModuleStoreTestCase): course = CourseFactory.create(default_store=ModuleStoreEnum.Type.mongo) errstring = 'The owning modulestore does not support this command.' with self.assertRaisesRegexp(CommandError, errstring): - call_command('force_publish', unicode(course.id)) + call_command('force_publish', six.text_type(course.id)) class TestForcePublishModifications(ModuleStoreTestCase): @@ -92,7 +96,7 @@ class TestForcePublishModifications(ModuleStoreTestCase): self.assertTrue(self.store.has_changes(self.store.get_item(self.course.location))) # get draft and publish branch versions - versions = get_course_versions(unicode(self.course.id)) + versions = get_course_versions(six.text_type(self.course.id)) draft_version = versions['draft-branch'] published_version = versions['published-branch'] @@ -103,13 +107,13 @@ class TestForcePublishModifications(ModuleStoreTestCase): patched_yes_no.return_value = True # force publish course - call_command('force_publish', unicode(self.course.id), '--commit') + call_command('force_publish', six.text_type(self.course.id), '--commit') # verify that course has no changes self.assertFalse(self.store.has_changes(self.store.get_item(self.course.location))) # get new draft and publish branch versions - versions = get_course_versions(unicode(self.course.id)) + versions = get_course_versions(six.text_type(self.course.id)) new_draft_version = versions['draft-branch'] new_published_version = versions['published-branch'] diff --git a/cms/djangoapps/contentstore/management/commands/tests/test_generate_courses.py b/cms/djangoapps/contentstore/management/commands/tests/test_generate_courses.py index aa83b9d7ce..9d2e560dd4 100644 --- a/cms/djangoapps/contentstore/management/commands/tests/test_generate_courses.py +++ b/cms/djangoapps/contentstore/management/commands/tests/test_generate_courses.py @@ -1,6 +1,8 @@ """ Unittest for generate a test course in an given modulestore """ +from __future__ import absolute_import + import json import ddt diff --git a/cms/djangoapps/contentstore/management/commands/tests/test_git_export.py b/cms/djangoapps/contentstore/management/commands/tests/test_git_export.py index 9b2583b85f..2362292fdf 100644 --- a/cms/djangoapps/contentstore/management/commands/tests/test_git_export.py +++ b/cms/djangoapps/contentstore/management/commands/tests/test_git_export.py @@ -2,6 +2,8 @@ Unittests for exporting to git via management command. """ +from __future__ import absolute_import + import copy import os import shutil @@ -10,15 +12,16 @@ import subprocess import unittest from uuid import uuid4 +import six from django.conf import settings from django.core.management import call_command from django.core.management.base import CommandError from django.test.utils import override_settings +from opaque_keys.edx.locator import CourseLocator -from contentstore.tests.utils import CourseTestCase import contentstore.git_export_utils as git_export_utils from contentstore.git_export_utils import GitExportError -from opaque_keys.edx.locator import CourseLocator +from contentstore.tests.utils import CourseTestCase FEATURES_WITH_EXPORT_GIT = settings.FEATURES.copy() FEATURES_WITH_EXPORT_GIT['ENABLE_EXPORT_GIT'] = True @@ -63,23 +66,23 @@ class TestGitExport(CourseTestCase): call_command('git_export', stderr=StringIO.StringIO()) # Send bad url to get course not exported - with self.assertRaisesRegexp(CommandError, unicode(GitExportError.URL_BAD)): + with self.assertRaisesRegexp(CommandError, six.text_type(GitExportError.URL_BAD)): call_command('git_export', 'foo/bar/baz', 'silly', stderr=StringIO.StringIO()) # Send bad course_id to get course not exported - with self.assertRaisesRegexp(CommandError, unicode(GitExportError.BAD_COURSE)): + with self.assertRaisesRegexp(CommandError, six.text_type(GitExportError.BAD_COURSE)): call_command('git_export', 'foo/bar:baz', 'silly', stderr=StringIO.StringIO()) def test_error_output(self): """ Verify that error output is actually resolved as the correct string """ - with self.assertRaisesRegexp(CommandError, unicode(GitExportError.BAD_COURSE)): + with self.assertRaisesRegexp(CommandError, six.text_type(GitExportError.BAD_COURSE)): call_command( 'git_export', 'foo/bar:baz', 'silly' ) - with self.assertRaisesRegexp(CommandError, unicode(GitExportError.URL_BAD)): + with self.assertRaisesRegexp(CommandError, six.text_type(GitExportError.URL_BAD)): call_command( 'git_export', 'foo/bar/baz', 'silly' ) @@ -89,14 +92,14 @@ class TestGitExport(CourseTestCase): Test several bad URLs for validation """ course_key = CourseLocator('org', 'course', 'run') - with self.assertRaisesRegexp(GitExportError, unicode(GitExportError.URL_BAD)): + with self.assertRaisesRegexp(GitExportError, six.text_type(GitExportError.URL_BAD)): git_export_utils.export_to_git(course_key, 'Sillyness') - with self.assertRaisesRegexp(GitExportError, unicode(GitExportError.URL_BAD)): + with self.assertRaisesRegexp(GitExportError, six.text_type(GitExportError.URL_BAD)): git_export_utils.export_to_git(course_key, 'example.com:edx/notreal') with self.assertRaisesRegexp(GitExportError, - unicode(GitExportError.URL_NO_AUTH)): + six.text_type(GitExportError.URL_NO_AUTH)): git_export_utils.export_to_git(course_key, 'http://blah') def test_bad_git_repos(self): @@ -108,7 +111,7 @@ class TestGitExport(CourseTestCase): course_key = CourseLocator('foo', 'blah', '100-') # Test bad clones with self.assertRaisesRegexp(GitExportError, - unicode(GitExportError.CANNOT_PULL)): + six.text_type(GitExportError.CANNOT_PULL)): git_export_utils.export_to_git( course_key, 'https://user:blah@example.com/test_repo.git') @@ -116,14 +119,14 @@ class TestGitExport(CourseTestCase): # Setup good repo with bad course to test xml export with self.assertRaisesRegexp(GitExportError, - unicode(GitExportError.XML_EXPORT_FAIL)): + six.text_type(GitExportError.XML_EXPORT_FAIL)): git_export_utils.export_to_git( course_key, 'file://{0}'.format(self.bare_repo_dir)) # Test bad git remote after successful clone with self.assertRaisesRegexp(GitExportError, - unicode(GitExportError.CANNOT_PULL)): + six.text_type(GitExportError.CANNOT_PULL)): git_export_utils.export_to_git( course_key, 'https://user:blah@example.com/r.git') @@ -180,6 +183,6 @@ class TestGitExport(CourseTestCase): ) with self.assertRaisesRegexp(GitExportError, - unicode(GitExportError.CANNOT_COMMIT)): + six.text_type(GitExportError.CANNOT_COMMIT)): git_export_utils.export_to_git( self.course.id, 'file://{0}'.format(self.bare_repo_dir)) diff --git a/cms/djangoapps/contentstore/management/commands/tests/test_migrate_to_split.py b/cms/djangoapps/contentstore/management/commands/tests/test_migrate_to_split.py index 96373b6fd4..e80dbda627 100644 --- a/cms/djangoapps/contentstore/management/commands/tests/test_migrate_to_split.py +++ b/cms/djangoapps/contentstore/management/commands/tests/test_migrate_to_split.py @@ -1,13 +1,16 @@ """ Unittests for migrating a course to split mongo """ +from __future__ import absolute_import + from django.core.management import CommandError, call_command from django.test import TestCase + from xmodule.modulestore import ModuleStoreEnum -from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase -from xmodule.modulestore.tests.factories import CourseFactory from xmodule.modulestore.django import modulestore from xmodule.modulestore.exceptions import ItemNotFoundError +from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase +from xmodule.modulestore.tests.factories import CourseFactory class TestArgParsing(TestCase): diff --git a/cms/djangoapps/contentstore/management/commands/tests/test_reindex_library.py b/cms/djangoapps/contentstore/management/commands/tests/test_reindex_library.py index 0bb7a58de6..5104cf0185 100644 --- a/cms/djangoapps/contentstore/management/commands/tests/test_reindex_library.py +++ b/cms/djangoapps/contentstore/management/commands/tests/test_reindex_library.py @@ -1,18 +1,19 @@ """ Tests for library reindex command """ -import ddt -from django.core.management import call_command, CommandError -import mock +from __future__ import absolute_import +import ddt +import mock +import six +from django.core.management import CommandError, call_command +from opaque_keys import InvalidKeyError + +from contentstore.courseware_index import SearchIndexingError +from contentstore.management.commands.reindex_library import Command as ReindexCommand from xmodule.modulestore import ModuleStoreEnum from xmodule.modulestore.django import modulestore from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase from xmodule.modulestore.tests.factories import CourseFactory, LibraryFactory -from opaque_keys import InvalidKeyError - -from contentstore.management.commands.reindex_library import Command as ReindexCommand -from contentstore.courseware_index import SearchIndexingError - @ddt.ddt class TestReindexLibrary(ModuleStoreTestCase): @@ -61,34 +62,34 @@ class TestReindexLibrary(ModuleStoreTestCase): def test_given_course_key_raises_command_error(self): """ Test that raises CommandError if course key is passed """ with self.assertRaisesRegexp(CommandError, ".* is not a library key"): - call_command('reindex_library', unicode(self.first_course.id)) + call_command('reindex_library', six.text_type(self.first_course.id)) with self.assertRaisesRegexp(CommandError, ".* is not a library key"): - call_command('reindex_library', unicode(self.second_course.id)) + call_command('reindex_library', six.text_type(self.second_course.id)) with self.assertRaisesRegexp(CommandError, ".* is not a library key"): call_command( 'reindex_library', - unicode(self.second_course.id), - unicode(self._get_lib_key(self.first_lib)) + six.text_type(self.second_course.id), + six.text_type(self._get_lib_key(self.first_lib)) ) def test_given_id_list_indexes_libraries(self): """ Test that reindexes libraries when given single library key or a list of library keys """ with mock.patch(self.REINDEX_PATH_LOCATION) as patched_index, \ mock.patch(self.MODULESTORE_PATCH_LOCATION, mock.Mock(return_value=self.store)): - call_command('reindex_library', unicode(self._get_lib_key(self.first_lib))) + call_command('reindex_library', six.text_type(self._get_lib_key(self.first_lib))) self.assertEqual(patched_index.mock_calls, self._build_calls(self.first_lib)) patched_index.reset_mock() - call_command('reindex_library', unicode(self._get_lib_key(self.second_lib))) + call_command('reindex_library', six.text_type(self._get_lib_key(self.second_lib))) self.assertEqual(patched_index.mock_calls, self._build_calls(self.second_lib)) patched_index.reset_mock() call_command( 'reindex_library', - unicode(self._get_lib_key(self.first_lib)), - unicode(self._get_lib_key(self.second_lib)) + six.text_type(self._get_lib_key(self.first_lib)), + six.text_type(self._get_lib_key(self.second_lib)) ) expected_calls = self._build_calls(self.first_lib, self.second_lib) self.assertEqual(patched_index.mock_calls, expected_calls) @@ -122,4 +123,4 @@ class TestReindexLibrary(ModuleStoreTestCase): patched_index.side_effect = SearchIndexingError("message", []) with self.assertRaises(SearchIndexingError): - call_command('reindex_library', unicode(self._get_lib_key(self.second_lib))) + call_command('reindex_library', six.text_type(self._get_lib_key(self.second_lib))) diff --git a/cms/djangoapps/contentstore/management/commands/tests/test_video_thumbnails.py b/cms/djangoapps/contentstore/management/commands/tests/test_video_thumbnails.py index a8a184f512..617b3383b2 100644 --- a/cms/djangoapps/contentstore/management/commands/tests/test_video_thumbnails.py +++ b/cms/djangoapps/contentstore/management/commands/tests/test_video_thumbnails.py @@ -2,16 +2,20 @@ """ Tests for course video thumbnails management command. """ +from __future__ import absolute_import + import logging -from mock import patch -from django.core.management import call_command, CommandError + +from django.core.management import CommandError, call_command from django.test import TestCase -from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase -from xmodule.modulestore.tests.factories import CourseFactory -from openedx.core.djangoapps.video_config.models import VideoThumbnailSetting +from mock import patch from six import text_type from testfixtures import LogCapture +from openedx.core.djangoapps.video_config.models import VideoThumbnailSetting +from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase +from xmodule.modulestore.tests.factories import CourseFactory + LOGGER_NAME = "contentstore.management.commands.video_thumbnails"