From 9d7f0ca46e3995b81dff19291e05f759febacbd6 Mon Sep 17 00:00:00 2001
From: Feanil Patel <feanil@edx.org>
Date: Mon, 12 Aug 2019 17:24:52 -0400
Subject: [PATCH] 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 6f5c45c4149..783e8e998f0 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 20ef90d5b64..2c658685b05 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 ef953fd2ae8..5da28bd2917 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 0c5b435b98a..1d649bcc5fb 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'<span class="discussion-count">1</span> discussion started')
         self.assertRegexpMatches(html, r'<span class="discussion-count">2</span> comments')
-        self.assertRegexpMatches(html, ur'&#39;id&#39;: &#39;{}&#39;'.format(self.TEST_THREAD_ID))
-        self.assertRegexpMatches(html, ur'&#39;title&#39;: &#39;{}&#39;'.format(self.TEST_THREAD_TEXT))
-        self.assertRegexpMatches(html, ur'&#39;body&#39;: &#39;{}&#39;'.format(self.TEST_THREAD_TEXT))
-        self.assertRegexpMatches(html, ur'&#39;username&#39;: u&#39;{}&#39;'.format(self.student.username))
+        self.assertRegexpMatches(html, u'&#39;id&#39;: &#39;{}&#39;'.format(self.TEST_THREAD_ID))
+        self.assertRegexpMatches(html, u'&#39;title&#39;: &#39;{}&#39;'.format(self.TEST_THREAD_TEXT))
+        self.assertRegexpMatches(html, u'&#39;body&#39;: &#39;{}&#39;'.format(self.TEST_THREAD_TEXT))
+        self.assertRegexpMatches(html, u'&#39;username&#39;: u&#39;{}&#39;'.format(self.student.username))
 
     def check_ajax(self, mock_request, **params):
         response = self.get_response(mock_request, params, HTTP_X_REQUESTED_WITH="XMLHttpRequest")
-- 
GitLab