From 9d7f0ca46e3995b81dff19291e05f759febacbd6 Mon Sep 17 00:00:00 2001 From: Feanil Patel Date: Mon, 12 Aug 2019 17:24:52 -0400 Subject: [PATCH 1/2] Get LMS test collection working in python 3. This should be all the changes needed to get test collection working in python 3 for the `lms` folder. --- lms/djangoapps/course_api/blocks/tests/test_views.py | 3 +-- lms/djangoapps/courseware/tests/test_i18n.py | 4 +++- lms/djangoapps/courseware/tests/test_video_handlers.py | 7 ++++--- lms/djangoapps/discussion/tests/test_views.py | 8 ++++---- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lms/djangoapps/course_api/blocks/tests/test_views.py b/lms/djangoapps/course_api/blocks/tests/test_views.py index 6f5c45c414..783e8e998f 100644 --- a/lms/djangoapps/course_api/blocks/tests/test_views.py +++ b/lms/djangoapps/course_api/blocks/tests/test_views.py @@ -4,7 +4,6 @@ Tests for Blocks Views from __future__ import absolute_import from datetime import datetime -from string import join import six from six.moves.urllib.parse import urlencode, urlunparse # pylint: disable=import-error @@ -239,7 +238,7 @@ class TestBlocksView(SharedModuleStoreTestCase): query = urlencode(list(self.query_params.items()) + [ ('requested_fields', self.requested_fields[0]), ('requested_fields', self.requested_fields[1]), - ('requested_fields', join(self.requested_fields[1:], ',')), + ('requested_fields', ",".join(self.requested_fields[1:])), ]) self.query_params = None response = self.verify_response( diff --git a/lms/djangoapps/courseware/tests/test_i18n.py b/lms/djangoapps/courseware/tests/test_i18n.py index 20ef90d5b6..2c658685b0 100644 --- a/lms/djangoapps/courseware/tests/test_i18n.py +++ b/lms/djangoapps/courseware/tests/test_i18n.py @@ -6,6 +6,7 @@ from __future__ import absolute_import import json import re +import six from django.conf import settings from django.contrib.auth.models import User @@ -36,7 +37,8 @@ class BaseI18nTestCase(CacheIsolationTestCase): def assert_tag_has_attr(self, content, tag, attname, value): """Assert that a tag in `content` has a certain value in a certain attribute.""" - regex = ur"""<{tag} [^>]*\b{attname}=['"]([\w\d\- ]+)['"][^>]*>""".format(tag=tag, attname=attname) + regex_string = six.text_type(r"""<{tag} [^>]*\b{attname}=['"]([\w\d\- ]+)['"][^>]*>""") # noqa: W605,E501 pylint: disable=unicode-format-string + regex = regex_string.format(tag=tag, attname=attname) match = re.search(regex, content) self.assertTrue(match, u"Couldn't find desired tag '%s' with attr '%s' in %r" % (tag, attname, content)) attvalues = match.group(1).split() diff --git a/lms/djangoapps/courseware/tests/test_video_handlers.py b/lms/djangoapps/courseware/tests/test_video_handlers.py index ef953fd2ae..5da28bd291 100644 --- a/lms/djangoapps/courseware/tests/test_video_handlers.py +++ b/lms/djangoapps/courseware/tests/test_video_handlers.py @@ -34,7 +34,7 @@ from .test_video_xml import SOURCE_XML TRANSCRIPT = {"start": [10], "end": [100], "text": ["Hi, welcome to Edx."]} BUMPER_TRANSCRIPT = {"start": [1], "end": [10], "text": ["A bumper"]} -SRT_content = textwrap.dedent(""" +SRT_content = textwrap.dedent(u""" 0 00:00:00,12 --> 00:00:00,100 Привіт, edX вітає вас. @@ -46,9 +46,10 @@ def _create_srt_file(content=None): Create srt file in filesystem. """ content = content or SRT_content + srt_file = tempfile.NamedTemporaryFile(suffix=".srt") srt_file.content_type = 'application/x-subrip; charset=utf-8' - srt_file.write(content) + srt_file.write(content.encode('utf-8')) srt_file.seek(0) return srt_file @@ -985,7 +986,7 @@ class TestStudioTranscriptTranslationPostDispatch(TestVideo): "edx_video_id": "", "language_code": "ar", "new_language_code": "uk", - "file": ("filename.srt", SRT_content.decode("utf8").encode("cp1251")) + "file": ("filename.srt", SRT_content.encode("cp1251")) } request = Request.blank("/translation", POST=post_data) diff --git a/lms/djangoapps/discussion/tests/test_views.py b/lms/djangoapps/discussion/tests/test_views.py index 0c5b435b98..1d649bcc5f 100644 --- a/lms/djangoapps/discussion/tests/test_views.py +++ b/lms/djangoapps/discussion/tests/test_views.py @@ -1321,10 +1321,10 @@ class UserProfileTestCase(ForumsEnableMixin, UrlResetMixin, ModuleStoreTestCase) self.assertRegexpMatches(html, r'data-num-pages="1"') self.assertRegexpMatches(html, r'1 discussion started') self.assertRegexpMatches(html, r'2 comments') - self.assertRegexpMatches(html, ur''id': '{}''.format(self.TEST_THREAD_ID)) - self.assertRegexpMatches(html, ur''title': '{}''.format(self.TEST_THREAD_TEXT)) - self.assertRegexpMatches(html, ur''body': '{}''.format(self.TEST_THREAD_TEXT)) - self.assertRegexpMatches(html, ur''username': u'{}''.format(self.student.username)) + self.assertRegexpMatches(html, u''id': '{}''.format(self.TEST_THREAD_ID)) + self.assertRegexpMatches(html, u''title': '{}''.format(self.TEST_THREAD_TEXT)) + self.assertRegexpMatches(html, u''body': '{}''.format(self.TEST_THREAD_TEXT)) + self.assertRegexpMatches(html, u''username': u'{}''.format(self.student.username)) def check_ajax(self, mock_request, **params): response = self.get_response(mock_request, params, HTTP_X_REQUESTED_WITH="XMLHttpRequest") From dc078bb5dac6a7f3761f277601af9e6d97b63fc8 Mon Sep 17 00:00:00 2001 From: Feanil Patel Date: Tue, 13 Aug 2019 16:33:09 -0400 Subject: [PATCH 2/2] Create a new SRT file for this test. Usingthe shared file with the other tests wolud sometimes cause this test to fail. Made this change to make it less flaky. --- lms/djangoapps/courseware/tests/test_video_handlers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lms/djangoapps/courseware/tests/test_video_handlers.py b/lms/djangoapps/courseware/tests/test_video_handlers.py index 5da28bd291..a1894ef465 100644 --- a/lms/djangoapps/courseware/tests/test_video_handlers.py +++ b/lms/djangoapps/courseware/tests/test_video_handlers.py @@ -259,7 +259,7 @@ class TestTranscriptAvailableTranslationsDispatch(TestVideo): self.assertEqual(json.loads(response.body), ['en']) def test_available_translation_non_en(self): - _upload_file(self.srt_file, self.item_descriptor.location, os.path.split(self.srt_file.name)[1]) + _upload_file(_create_srt_file(), self.item_descriptor.location, os.path.split(self.srt_file.name)[1]) request = Request.blank('/available_translations') response = self.item.transcript(request=request, dispatch='available_translations')