From 787868d48b89bfa77c1b7d1a5d6f4cfe58c4c5fa Mon Sep 17 00:00:00 2001 From: Stu Young Date: Tue, 21 May 2019 11:43:22 -0400 Subject: [PATCH] INCR-308 Run python-modernize on lms/djangoapps/ccx/tests (#20617) * run python modernize * run isort * Fix quality --- lms/djangoapps/ccx/tests/factories.py | 2 + .../ccx/tests/test_ccx_modulestore.py | 15 +++-- .../tests/test_field_override_performance.py | 23 +++++--- lms/djangoapps/ccx/tests/test_models.py | 4 +- lms/djangoapps/ccx/tests/test_overrides.py | 13 +++-- lms/djangoapps/ccx/tests/test_tasks.py | 5 +- lms/djangoapps/ccx/tests/test_utils.py | 2 + lms/djangoapps/ccx/tests/test_views.py | 58 ++++++++++--------- lms/djangoapps/ccx/tests/utils.py | 11 ++-- 9 files changed, 80 insertions(+), 53 deletions(-) diff --git a/lms/djangoapps/ccx/tests/factories.py b/lms/djangoapps/ccx/tests/factories.py index b13d7e5a6a..d9b7d046c8 100644 --- a/lms/djangoapps/ccx/tests/factories.py +++ b/lms/djangoapps/ccx/tests/factories.py @@ -1,6 +1,8 @@ """ Dummy factories for tests """ +from __future__ import absolute_import + from factory import Sequence, SubFactory from factory.django import DjangoModelFactory diff --git a/lms/djangoapps/ccx/tests/test_ccx_modulestore.py b/lms/djangoapps/ccx/tests/test_ccx_modulestore.py index c1c3f94d99..b552c4d0e0 100644 --- a/lms/djangoapps/ccx/tests/test_ccx_modulestore.py +++ b/lms/djangoapps/ccx/tests/test_ccx_modulestore.py @@ -1,12 +1,15 @@ """ Test the CCXModulestoreWrapper """ +from __future__ import absolute_import + import datetime from collections import deque -from itertools import chain, izip_longest +from itertools import chain import pytz from ccx_keys.locator import CCXLocator +from six.moves import range, zip_longest from lms.djangoapps.ccx.models import CustomCourseForEdX from student.tests.factories import AdminFactory, UserFactory @@ -27,18 +30,18 @@ class TestCCXModulestoreWrapper(SharedModuleStoreTestCase): due = datetime.datetime(2010, 7, 7, 0, 0, tzinfo=pytz.UTC) # Create a course outline cls.chapters = chapters = [ - ItemFactory.create(start=start, parent=cls.course) for _ in xrange(2) + ItemFactory.create(start=start, parent=cls.course) for _ in range(2) ] cls.sequentials = sequentials = [ - ItemFactory.create(parent=c) for _ in xrange(2) for c in chapters + ItemFactory.create(parent=c) for _ in range(2) for c in chapters ] cls.verticals = verticals = [ ItemFactory.create( due=due, parent=s, graded=True, format='Homework' - ) for _ in xrange(2) for s in sequentials + ) for _ in range(2) for s in sequentials ] cls.blocks = [ - ItemFactory.create(parent=v, category='html') for _ in xrange(2) for v in verticals + ItemFactory.create(parent=v, category='html') for _ in range(2) for v in verticals ] @classmethod @@ -93,7 +96,7 @@ class TestCCXModulestoreWrapper(SharedModuleStoreTestCase): course_key = self.ccx_locator.to_course_locator() course = self.get_course(course_key) ccx = self.get_course(self.ccx_locator) - test_fodder = izip_longest( + test_fodder = zip_longest( self.get_all_children_bf(course), self.get_all_children_bf(ccx) ) for expected, actual in test_fodder: diff --git a/lms/djangoapps/ccx/tests/test_field_override_performance.py b/lms/djangoapps/ccx/tests/test_field_override_performance.py index 848aee4f9b..a376c16f4c 100644 --- a/lms/djangoapps/ccx/tests/test_field_override_performance.py +++ b/lms/djangoapps/ccx/tests/test_field_override_performance.py @@ -2,31 +2,36 @@ """ Performance tests for field overrides. """ +from __future__ import absolute_import + import itertools from datetime import datetime import ddt import mock import pytest +import six +from six.moves import range from ccx_keys.locator import CCXLocator -from lms.djangoapps.courseware.field_overrides import OverrideFieldData -from courseware.testutils import FieldOverrideTestMixin -from courseware.views.views import progress from django.conf import settings from django.contrib.messages.storage.fallback import FallbackStorage from django.core.cache import caches from django.test.client import RequestFactory from django.test.utils import override_settings from edx_django_utils.cache import RequestCache -from lms.djangoapps.ccx.tests.factories import CcxFactory from opaque_keys.edx.keys import CourseKey +from pytz import UTC +from xblock.core import XBlock + +from courseware.testutils import FieldOverrideTestMixin +from courseware.views.views import progress +from lms.djangoapps.ccx.tests.factories import CcxFactory +from lms.djangoapps.courseware.field_overrides import OverrideFieldData from openedx.core.djangoapps.content.block_structure.api import get_course_in_cache from openedx.core.djangoapps.waffle_utils.testutils import WAFFLE_TABLES from openedx.features.content_type_gating.models import ContentTypeGatingConfig -from pytz import UTC from student.models import CourseEnrollment from student.tests.factories import UserFactory -from xblock.core import XBlock from xmodule.modulestore.tests.django_utils import ( TEST_DATA_MONGO_MODULESTORE, TEST_DATA_SPLIT_MODULESTORE, @@ -134,7 +139,7 @@ class FieldOverridePerformanceTestCase(FieldOverrideTestMixin, ProceduralCourseT self.student, course_key ) - return CourseKey.from_string(unicode(course_key)) + return CourseKey.from_string(six.text_type(course_key)) def grade_course(self, course_key): """ @@ -142,7 +147,7 @@ class FieldOverridePerformanceTestCase(FieldOverrideTestMixin, ProceduralCourseT """ return progress( self.request, - course_id=unicode(course_key), + course_id=six.text_type(course_key), student_id=self.student.id ) @@ -191,7 +196,7 @@ class FieldOverridePerformanceTestCase(FieldOverrideTestMixin, ProceduralCourseT with self.assertXBlockInstantiations(1): self.grade_course(course_key) - @ddt.data(*itertools.product(('no_overrides', 'ccx'), range(1, 4), (True, False), (True, False))) + @ddt.data(*itertools.product(('no_overrides', 'ccx'), list(range(1, 4)), (True, False), (True, False))) @ddt.unpack @override_settings( XBLOCK_FIELD_DATA_WRAPPERS=[], diff --git a/lms/djangoapps/ccx/tests/test_models.py b/lms/djangoapps/ccx/tests/test_models.py index 8bc19a0d73..7490fdb40f 100644 --- a/lms/djangoapps/ccx/tests/test_models.py +++ b/lms/djangoapps/ccx/tests/test_models.py @@ -1,6 +1,8 @@ """ tests for the models """ +from __future__ import absolute_import + import json from datetime import datetime, timedelta @@ -179,4 +181,4 @@ class TestCCX(ModuleStoreTestCase): Verify that the locator helper property returns a correct CCXLocator """ locator = self.ccx.locator - self.assertEqual(self.ccx.id, long(locator.ccx)) + self.assertEqual(self.ccx.id, int(locator.ccx)) diff --git a/lms/djangoapps/ccx/tests/test_overrides.py b/lms/djangoapps/ccx/tests/test_overrides.py index 7fefec3d4e..a3132dd220 100644 --- a/lms/djangoapps/ccx/tests/test_overrides.py +++ b/lms/djangoapps/ccx/tests/test_overrides.py @@ -2,6 +2,8 @@ """ tests for overrides """ +from __future__ import absolute_import + import datetime import mock @@ -9,13 +11,14 @@ import pytz from ccx_keys.locator import CCXLocator from django.test.utils import override_settings from edx_django_utils.cache import RequestCache +from six.moves import range from courseware.courses import get_course_by_id -from lms.djangoapps.courseware.field_overrides import OverrideFieldData from courseware.testutils import FieldOverrideTestMixin from lms.djangoapps.ccx.models import CustomCourseForEdX from lms.djangoapps.ccx.overrides import override_field_for_ccx from lms.djangoapps.ccx.tests.utils import flatten, iter_blocks +from lms.djangoapps.courseware.field_overrides import OverrideFieldData from lms.djangoapps.courseware.tests.test_field_overrides import inject_field_overrides from student.tests.factories import AdminFactory from xmodule.modulestore.tests.django_utils import TEST_DATA_SPLIT_MODULESTORE, SharedModuleStoreTestCase @@ -45,15 +48,15 @@ class TestFieldOverrides(FieldOverrideTestMixin, SharedModuleStoreTestCase): start = datetime.datetime(2010, 5, 12, 2, 42, tzinfo=pytz.UTC) due = datetime.datetime(2010, 7, 7, 0, 0, tzinfo=pytz.UTC) chapters = [ItemFactory.create(start=start, parent=cls.course) - for _ in xrange(2)] + for _ in range(2)] sequentials = flatten([ - [ItemFactory.create(parent=chapter) for _ in xrange(2)] + [ItemFactory.create(parent=chapter) for _ in range(2)] for chapter in chapters]) verticals = flatten([ - [ItemFactory.create(due=due, parent=sequential) for _ in xrange(2)] + [ItemFactory.create(due=due, parent=sequential) for _ in range(2)] for sequential in sequentials]) blocks = flatten([ # pylint: disable=unused-variable - [ItemFactory.create(parent=vertical) for _ in xrange(2)] + [ItemFactory.create(parent=vertical) for _ in range(2)] for vertical in verticals]) def setUp(self): diff --git a/lms/djangoapps/ccx/tests/test_tasks.py b/lms/djangoapps/ccx/tests/test_tasks.py index 3dea8f2713..9330195484 100644 --- a/lms/djangoapps/ccx/tests/test_tasks.py +++ b/lms/djangoapps/ccx/tests/test_tasks.py @@ -1,9 +1,12 @@ """ Tests for celery tasks defined in tasks module """ +from __future__ import absolute_import + import contextlib import mock +import six from ccx_keys.locator import CCXLocator from lms.djangoapps.ccx.tasks import send_ccx_course_published @@ -51,7 +54,7 @@ class TestSendCCXCoursePublished(ModuleStoreTestCase): """ Call the function under test """ - send_ccx_course_published(unicode(course_key)) + send_ccx_course_published(six.text_type(course_key)) def test_signal_not_sent_for_ccx(self): """ diff --git a/lms/djangoapps/ccx/tests/test_utils.py b/lms/djangoapps/ccx/tests/test_utils.py index cc700cb86c..c5b94d8832 100644 --- a/lms/djangoapps/ccx/tests/test_utils.py +++ b/lms/djangoapps/ccx/tests/test_utils.py @@ -1,6 +1,8 @@ """ test utils """ +from __future__ import absolute_import + import uuid from smtplib import SMTPException diff --git a/lms/djangoapps/ccx/tests/test_views.py b/lms/djangoapps/ccx/tests/test_views.py index 034a52d6e1..f3cfd272fa 100644 --- a/lms/djangoapps/ccx/tests/test_views.py +++ b/lms/djangoapps/ccx/tests/test_views.py @@ -1,21 +1,26 @@ """ test views """ +from __future__ import absolute_import + import datetime import json import re -import urlparse import ddt +import six +import six.moves.urllib.parse # pylint: disable=import-error +from six.moves import range, zip from ccx_keys.locator import CCXLocator from django.conf import settings -from django.urls import resolve, reverse from django.test import RequestFactory from django.test.utils import override_settings -from pytz import UTC +from django.urls import resolve, reverse from django.utils.translation import ugettext as _ +from edx_django_utils.cache import RequestCache from mock import MagicMock, patch from opaque_keys.edx.keys import CourseKey +from pytz import UTC from capa.tests.response_xml_factory import StringResponseXMLFactory from courseware.courses import get_course_by_id @@ -23,7 +28,6 @@ from courseware.tabs import get_course_tab_list from courseware.tests.factories import StudentModuleFactory from courseware.tests.helpers import LoginEnrollmentTestCase from courseware.testutils import FieldOverrideTestMixin -from edx_django_utils.cache import RequestCache from edxmako.shortcuts import render_to_response from lms.djangoapps.ccx.models import CustomCourseForEdX from lms.djangoapps.ccx.overrides import get_override_for_ccx, override_field_for_ccx @@ -109,7 +113,7 @@ def setup_students_and_grades(context): module_state_key=problem.location ) - task_compute_all_grades_for_course.apply_async(kwargs={'course_key': unicode(context.course.id)}) + task_compute_all_grades_for_course.apply_async(kwargs={'course_key': six.text_type(context.course.id)}) def unhide(unit): @@ -210,7 +214,7 @@ class TestCCXProgressChanges(CcxTestCase, LoginEnrollmentTestCase): category="problem", data=StringResponseXMLFactory().build_xml(answer='foo'), metadata={'rerandomize': 'always'} - )] for _ in xrange(2)) + )] for _ in range(2)) def assert_progress_summary(self, ccx_course_key, due): """ @@ -242,7 +246,7 @@ class TestCCXProgressChanges(CcxTestCase, LoginEnrollmentTestCase): """ self.make_coach() ccx = self.make_ccx() - ccx_course_key = CCXLocator.from_course_locator(self.course.id, unicode(ccx.id)) + ccx_course_key = CCXLocator.from_course_locator(self.course.id, six.text_type(ccx.id)) self.client.login(username=self.coach.username, password="test") url = reverse('ccx_coach_dashboard', kwargs={'course_id': ccx_course_key}) @@ -337,7 +341,7 @@ class TestCoachDashboard(CcxTestCase, LoginEnrollmentTestCase): self.make_coach() url = reverse( 'ccx_coach_dashboard', - kwargs={'course_id': unicode(self.course.id)}) + kwargs={'course_id': six.text_type(self.course.id)}) response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertTrue(re.search( @@ -353,7 +357,7 @@ class TestCoachDashboard(CcxTestCase, LoginEnrollmentTestCase): url = reverse( 'create_ccx', - kwargs={'course_id': unicode(self.course_with_ccx_connect_set.id)}) + kwargs={'course_id': six.text_type(self.course_with_ccx_connect_set.id)}) response = self.client.get(url) self.assertEqual(response.status_code, 200) @@ -372,7 +376,7 @@ class TestCoachDashboard(CcxTestCase, LoginEnrollmentTestCase): self.make_coach() url = reverse( 'create_ccx', - kwargs={'course_id': unicode(self.course.id)}) + kwargs={'course_id': six.text_type(self.course.id)}) response = self.client.post(url, {'name': ccx_name}) self.assertEqual(response.status_code, 302) @@ -381,7 +385,7 @@ class TestCoachDashboard(CcxTestCase, LoginEnrollmentTestCase): self.assertEqual(response.status_code, 200) # Get the ccx_key - path = urlparse.urlparse(url).path + path = six.moves.urllib.parse.urlparse(url).path resolver = resolve(path) ccx_key = resolver.kwargs['course_id'] @@ -856,18 +860,18 @@ class TestCoachDashboardSchedule(CcxTestCase, LoginEnrollmentTestCase, ModuleSto ) self.chapters = [ - ItemFactory.create(start=start, parent=course) for _ in xrange(2) + ItemFactory.create(start=start, parent=course) for _ in range(2) ] self.sequentials = flatten([ [ - ItemFactory.create(parent=chapter) for _ in xrange(2) + ItemFactory.create(parent=chapter) for _ in range(2) ] for chapter in self.chapters ]) self.verticals = flatten([ [ ItemFactory.create( start=start, due=due, parent=sequential, graded=True, format='Homework', category=u'vertical' - ) for _ in xrange(2) + ) for _ in range(2) ] for sequential in self.sequentials ]) @@ -876,7 +880,7 @@ class TestCoachDashboardSchedule(CcxTestCase, LoginEnrollmentTestCase, ModuleSto with self.store.bulk_operations(course.id, emit_signals=False): blocks = flatten([ # pylint: disable=unused-variable [ - ItemFactory.create(parent=vertical) for _ in xrange(2) + ItemFactory.create(parent=vertical) for _ in range(2) ] for vertical in self.verticals ]) @@ -950,17 +954,17 @@ class TestCoachDashboardSchedule(CcxTestCase, LoginEnrollmentTestCase, ModuleSto vertical = self.verticals[0] self.hide_node(vertical) locations = self.assert_elements_in_schedule(url, n_verticals=7) - self.assertNotIn(unicode(vertical.location), locations) + self.assertNotIn(six.text_type(vertical.location), locations) # hide a sequential sequential = self.sequentials[0] self.hide_node(sequential) locations = self.assert_elements_in_schedule(url, n_sequentials=3, n_verticals=6) - self.assertNotIn(unicode(sequential.location), locations) + self.assertNotIn(six.text_type(sequential.location), locations) # hide a chapter chapter = self.chapters[0] self.hide_node(chapter) locations = self.assert_elements_in_schedule(url, n_chapters=1, n_sequentials=2, n_verticals=4) - self.assertNotIn(unicode(chapter.location), locations) + self.assertNotIn(six.text_type(chapter.location), locations) GET_CHILDREN = XModuleMixin.get_children @@ -1006,7 +1010,7 @@ class TestCCXGrades(FieldOverrideTestMixin, SharedModuleStoreTestCase, LoginEnro parent=chapter, category="sequential", metadata={'graded': True, 'format': 'Homework'}) - for _ in xrange(4) + for _ in range(4) ] # making problems available at class level for possible future use in tests cls.problems = [ @@ -1016,7 +1020,7 @@ class TestCCXGrades(FieldOverrideTestMixin, SharedModuleStoreTestCase, LoginEnro category="problem", data=StringResponseXMLFactory().build_xml(answer='foo'), metadata={'rerandomize': 'always'} - ) for _ in xrange(4) + ) for _ in range(4) ] for section in sections ] @@ -1052,7 +1056,7 @@ class TestCCXGrades(FieldOverrideTestMixin, SharedModuleStoreTestCase, LoginEnro # create a ccx locator and retrieve the course structure using that key # which emulates how a student would get access. - self.ccx_key = CCXLocator.from_course_locator(self._course.id, unicode(ccx.id)) + self.ccx_key = CCXLocator.from_course_locator(self._course.id, six.text_type(ccx.id)) self.course = get_course_by_id(self.ccx_key, depth=None) CourseOverview.load_from_module_store(self.course.id) setup_students_and_grades(self) @@ -1082,7 +1086,7 @@ class TestCCXGrades(FieldOverrideTestMixin, SharedModuleStoreTestCase, LoginEnro self.assertEqual(len(response.mako_context['students']), 1) student_info = response.mako_context['students'][0] self.assertEqual(student_info['grade_summary']['percent'], 0.5) - self.assertEqual(student_info['grade_summary']['grade_breakdown'].values()[0]['percent'], 0.5) + self.assertEqual(list(student_info['grade_summary']['grade_breakdown'].values())[0]['percent'], 0.5) self.assertEqual(len(student_info['grade_summary']['section_breakdown']), 4) def test_grades_csv(self): @@ -1104,7 +1108,7 @@ class TestCCXGrades(FieldOverrideTestMixin, SharedModuleStoreTestCase, LoginEnro headers = rows[0] # picking first student records - data = dict(zip(headers.strip().split(','), rows[1].strip().split(','))) + data = dict(list(zip(headers.strip().split(','), rows[1].strip().split(',')))) self.assertNotIn('HW 04', data) self.assertEqual(data['HW 01'], '0.75') self.assertEqual(data['HW 02'], '0.5') @@ -1128,7 +1132,7 @@ class TestCCXGrades(FieldOverrideTestMixin, SharedModuleStoreTestCase, LoginEnro self.assertEqual(response.status_code, 200) grades = response.mako_context['grade_summary'] self.assertEqual(grades['percent'], 0.5) - self.assertEqual(grades['grade_breakdown'].values()[0]['percent'], 0.5) + self.assertEqual(list(grades['grade_breakdown'].values())[0]['percent'], 0.5) self.assertEqual(len(grades['section_breakdown']), 4) @@ -1199,7 +1203,7 @@ class CCXCoachTabTestCase(CcxTestCase): """ self.make_coach() ccx = self.make_ccx() - ccx_key = CCXLocator.from_course_locator(self.course.id, unicode(ccx.id)) + ccx_key = CCXLocator.from_course_locator(self.course.id, six.text_type(ccx.id)) staff = self.make_staff() with ccx_course(ccx_key) as course_ccx: @@ -1228,7 +1232,7 @@ class CCXCoachTabTestCase(CcxTestCase): """ self.make_coach() ccx = self.make_ccx() - ccx_key = CCXLocator.from_course_locator(self.course.id, unicode(ccx.id)) + ccx_key = CCXLocator.from_course_locator(self.course.id, six.text_type(ccx.id)) instructor = self.make_instructor() with ccx_course(ccx_key) as course_ccx: @@ -1276,5 +1280,5 @@ class TestStudentViewsWithCCX(ModuleStoreTestCase): def test_load_courseware(self): self.client.login(username=self.student.username, password=self.student_password) - response = self.client.get(reverse('courseware', kwargs={'course_id': unicode(self.ccx_course_key)})) + response = self.client.get(reverse('courseware', kwargs={'course_id': six.text_type(self.ccx_course_key)})) self.assertEqual(response.status_code, 200) diff --git a/lms/djangoapps/ccx/tests/utils.py b/lms/djangoapps/ccx/tests/utils.py index 42e4c718d1..7739a0d2b7 100644 --- a/lms/djangoapps/ccx/tests/utils.py +++ b/lms/djangoapps/ccx/tests/utils.py @@ -1,10 +1,13 @@ """ Test utils for CCX """ +from __future__ import absolute_import + import datetime import pytz from django.conf import settings +from six.moves import range from lms.djangoapps.ccx.overrides import override_field_for_ccx from lms.djangoapps.ccx.tests.factories import CcxFactory @@ -38,18 +41,18 @@ class CcxTestCase(EmailTemplateTagMixin, SharedModuleStoreTestCase): ) cls.chapters = [ - ItemFactory.create(start=start, parent=course) for _ in xrange(2) + ItemFactory.create(start=start, parent=course) for _ in range(2) ] cls.sequentials = flatten([ [ - ItemFactory.create(parent=chapter) for _ in xrange(2) + ItemFactory.create(parent=chapter) for _ in range(2) ] for chapter in cls.chapters ]) cls.verticals = flatten([ [ ItemFactory.create( start=start, due=due, parent=sequential, graded=True, format='Homework', category=u'vertical' - ) for _ in xrange(2) + ) for _ in range(2) ] for sequential in cls.sequentials ]) @@ -58,7 +61,7 @@ class CcxTestCase(EmailTemplateTagMixin, SharedModuleStoreTestCase): with cls.store.bulk_operations(course.id, emit_signals=False): blocks = flatten([ # pylint: disable=unused-variable [ - ItemFactory.create(parent=vertical) for _ in xrange(2) + ItemFactory.create(parent=vertical) for _ in range(2) ] for vertical in cls.verticals ])