Merge pull request #20988 from edx/INCR-335

INCR-335 python3 compatibility
This commit is contained in:
Ayub
2019-07-15 12:29:55 +05:00
committed by GitHub
9 changed files with 83 additions and 57 deletions

View File

@@ -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)

View File

@@ -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)

View File

@@ -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:

View File

@@ -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']

View File

@@ -1,6 +1,8 @@
"""
Unittest for generate a test course in an given modulestore
"""
from __future__ import absolute_import
import json
import ddt

View File

@@ -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))

View File

@@ -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):

View File

@@ -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)))

View File

@@ -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"