From 8adaca66d111189dca6e268740187bdf61bef50d Mon Sep 17 00:00:00 2001 From: Jolyon Bloomfield <jkb84@cornell.edu> Date: Fri, 5 Apr 2019 00:46:05 -0400 Subject: [PATCH] INCR-200 Migrate common/djangoapps/util/tests and migrations --- common/djangoapps/util/migrations/0001_initial.py | 2 +- .../0002_data__default_rate_limit_config.py | 3 +-- common/djangoapps/util/tests/mixins/discovery.py | 2 ++ common/djangoapps/util/tests/test_course.py | 2 ++ common/djangoapps/util/tests/test_date_utils.py | 14 ++++++++------ common/djangoapps/util/tests/test_db.py | 5 ++++- .../util/tests/test_disable_rate_limit.py | 2 ++ common/djangoapps/util/tests/test_django_utils.py | 2 ++ common/djangoapps/util/tests/test_file.py | 4 +++- common/djangoapps/util/tests/test_json_request.py | 2 ++ .../util/tests/test_keyword_sub_utils.py | 7 +++++-- common/djangoapps/util/tests/test_memcache.py | 6 +++++- .../util/tests/test_milestones_helpers.py | 13 ++++++++----- .../util/tests/test_organizations_helpers.py | 5 ++++- .../util/tests/test_password_policy_validators.py | 6 +++++- common/djangoapps/util/tests/test_sandboxing.py | 2 ++ common/djangoapps/util/tests/test_string_utils.py | 2 ++ .../djangoapps/util/tests/test_submit_feedback.py | 2 ++ 18 files changed, 60 insertions(+), 21 deletions(-) diff --git a/common/djangoapps/util/migrations/0001_initial.py b/common/djangoapps/util/migrations/0001_initial.py index bce61dccd2c..a90fb9c79a5 100644 --- a/common/djangoapps/util/migrations/0001_initial.py +++ b/common/djangoapps/util/migrations/0001_initial.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -from __future__ import unicode_literals +from __future__ import absolute_import, unicode_literals import django.db.models.deletion from django.conf import settings diff --git a/common/djangoapps/util/migrations/0002_data__default_rate_limit_config.py b/common/djangoapps/util/migrations/0002_data__default_rate_limit_config.py index cb343e10950..a7f1cf90bed 100644 --- a/common/djangoapps/util/migrations/0002_data__default_rate_limit_config.py +++ b/common/djangoapps/util/migrations/0002_data__default_rate_limit_config.py @@ -1,9 +1,8 @@ # -*- coding: utf-8 -*- -from __future__ import unicode_literals +from __future__ import absolute_import, unicode_literals from django.db import migrations, models - # Converted from the original South migration 0002_default_rate_limit_config.py diff --git a/common/djangoapps/util/tests/mixins/discovery.py b/common/djangoapps/util/tests/mixins/discovery.py index ccd4083c091..fa4feb4784b 100644 --- a/common/djangoapps/util/tests/mixins/discovery.py +++ b/common/djangoapps/util/tests/mixins/discovery.py @@ -1,6 +1,8 @@ """ Mixins for the CourseDiscoveryApiClient. """ +from __future__ import absolute_import + import json import httpretty diff --git a/common/djangoapps/util/tests/test_course.py b/common/djangoapps/util/tests/test_course.py index ddc2b9ebb79..151fdd9f893 100644 --- a/common/djangoapps/util/tests/test_course.py +++ b/common/djangoapps/util/tests/test_course.py @@ -1,6 +1,8 @@ """ Tests for course utils. """ +from __future__ import absolute_import + import ddt import mock from django.conf import settings diff --git a/common/djangoapps/util/tests/test_date_utils.py b/common/djangoapps/util/tests/test_date_utils.py index f01609e3d15..0b91f9b443b 100644 --- a/common/djangoapps/util/tests/test_date_utils.py +++ b/common/djangoapps/util/tests/test_date_utils.py @@ -3,6 +3,8 @@ Tests for util.date_utils """ +from __future__ import absolute_import + import unittest from datetime import datetime, timedelta, tzinfo @@ -130,7 +132,7 @@ class StrftimeLocalizedTest(unittest.TestCase): ) def test_usual_strftime_behavior(self, fmt_expected): (fmt, expected) = fmt_expected - dtime = datetime(2013, 02, 14, 16, 41, 17) + dtime = datetime(2013, 2, 14, 16, 41, 17) self.assertEqual(expected, strftime_localized(dtime, fmt)) # strftime doesn't like Unicode, so do the work in UTF8. self.assertEqual(expected, dtime.strftime(fmt.encode('utf8')).decode('utf8')) @@ -144,7 +146,7 @@ class StrftimeLocalizedTest(unittest.TestCase): ) def test_shortcuts(self, fmt_expected): (fmt, expected) = fmt_expected - dtime = datetime(2013, 02, 14, 16, 41, 17) + dtime = datetime(2013, 2, 14, 16, 41, 17) self.assertEqual(expected, strftime_localized(dtime, fmt)) @patch('util.date_utils.pgettext', fake_pgettext(translations={ @@ -163,7 +165,7 @@ class StrftimeLocalizedTest(unittest.TestCase): ) def test_translated_words(self, fmt_expected): (fmt, expected) = fmt_expected - dtime = datetime(2013, 02, 14, 16, 41, 17) + dtime = datetime(2013, 2, 14, 16, 41, 17) self.assertEqual(expected, strftime_localized(dtime, fmt)) @patch('util.date_utils.ugettext', fake_ugettext(translations={ @@ -183,7 +185,7 @@ class StrftimeLocalizedTest(unittest.TestCase): ) def test_translated_formats(self, fmt_expected): (fmt, expected) = fmt_expected - dtime = datetime(2013, 02, 14, 16, 41, 17) + dtime = datetime(2013, 2, 14, 16, 41, 17) self.assertEqual(expected, strftime_localized(dtime, fmt)) @patch('util.date_utils.ugettext', fake_ugettext(translations={ @@ -196,7 +198,7 @@ class StrftimeLocalizedTest(unittest.TestCase): ) def test_recursion_protection(self, fmt_expected): (fmt, expected) = fmt_expected - dtime = datetime(2013, 02, 14, 16, 41, 17) + dtime = datetime(2013, 2, 14, 16, 41, 17) self.assertEqual(expected, strftime_localized(dtime, fmt)) @ddt.data( @@ -205,6 +207,6 @@ class StrftimeLocalizedTest(unittest.TestCase): "%Y/%m/%d%", ) def test_invalid_format_strings(self, fmt): - dtime = datetime(2013, 02, 14, 16, 41, 17) + dtime = datetime(2013, 2, 14, 16, 41, 17) with self.assertRaises(ValueError): strftime_localized(dtime, fmt) diff --git a/common/djangoapps/util/tests/test_db.py b/common/djangoapps/util/tests/test_db.py index 7a54dd467fb..3df590fc95a 100644 --- a/common/djangoapps/util/tests/test_db.py +++ b/common/djangoapps/util/tests/test_db.py @@ -1,5 +1,7 @@ """Tests for util.db module.""" +from __future__ import absolute_import + import threading import time import unittest @@ -12,6 +14,7 @@ from django.db.transaction import TransactionManagementError, atomic from django.test import TestCase, TransactionTestCase from django.test.utils import override_settings from django.utils.six import StringIO +from six.moves import range from util.db import commit_on_success, enable_named_outer_atomic, generate_int_id, outer_atomic @@ -199,7 +202,7 @@ class GenerateIntIdTestCase(TestCase): minimum = 1 maximum = times for __ in range(times): - self.assertIn(generate_int_id(minimum, maximum), range(minimum, maximum + 1)) + self.assertIn(generate_int_id(minimum, maximum), list(range(minimum, maximum + 1))) @ddt.data(10) def test_used_ids(self, times): diff --git a/common/djangoapps/util/tests/test_disable_rate_limit.py b/common/djangoapps/util/tests/test_disable_rate_limit.py index 4e0dbabe4a4..000b00c1cff 100644 --- a/common/djangoapps/util/tests/test_disable_rate_limit.py +++ b/common/djangoapps/util/tests/test_disable_rate_limit.py @@ -1,4 +1,6 @@ """Tests for disabling rate limiting. """ +from __future__ import absolute_import + import unittest import mock diff --git a/common/djangoapps/util/tests/test_django_utils.py b/common/djangoapps/util/tests/test_django_utils.py index bd0dc846ef5..0a444197f21 100644 --- a/common/djangoapps/util/tests/test_django_utils.py +++ b/common/djangoapps/util/tests/test_django_utils.py @@ -6,6 +6,8 @@ cache values can't leak between different TestCase classes and methods. The need for this will go away whenever Django merges the fix to reset the caches between tests (https://code.djangoproject.com/ticket/11505). """ +from __future__ import absolute_import + from django.core.cache import caches from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase, SharedModuleStoreTestCase diff --git a/common/djangoapps/util/tests/test_file.py b/common/djangoapps/util/tests/test_file.py index 44044d819e5..b4d62b66b16 100644 --- a/common/djangoapps/util/tests/test_file.py +++ b/common/djangoapps/util/tests/test_file.py @@ -2,6 +2,8 @@ """ Tests for file.py """ +from __future__ import absolute_import + import os from datetime import datetime from io import StringIO @@ -11,10 +13,10 @@ from django.core import exceptions from django.core.files.uploadedfile import SimpleUploadedFile from django.http import HttpRequest from django.test import TestCase -from pytz import UTC from mock import Mock, patch from opaque_keys.edx.keys import CourseKey from opaque_keys.edx.locations import CourseLocator +from pytz import UTC from six import text_type import util.file diff --git a/common/djangoapps/util/tests/test_json_request.py b/common/djangoapps/util/tests/test_json_request.py index a7983c2f6f6..07dc29ca99f 100644 --- a/common/djangoapps/util/tests/test_json_request.py +++ b/common/djangoapps/util/tests/test_json_request.py @@ -2,6 +2,8 @@ Test for JsonResponse and JsonResponseBadRequest util classes. """ +from __future__ import absolute_import + import json import unittest diff --git a/common/djangoapps/util/tests/test_keyword_sub_utils.py b/common/djangoapps/util/tests/test_keyword_sub_utils.py index 781cdd7135c..9648f934155 100644 --- a/common/djangoapps/util/tests/test_keyword_sub_utils.py +++ b/common/djangoapps/util/tests/test_keyword_sub_utils.py @@ -2,6 +2,9 @@ Tests for keyword_substitution.py """ +from __future__ import absolute_import + +import six from ddt import ddt, file_data from mock import patch @@ -114,13 +117,13 @@ class KeywordSubTest(ModuleStoreTestCase): test_string = 'This string should not be subbed here %%USER_ID%%' no_course_context = dict( - (key, value) for key, value in self.context.iteritems() if key != 'course_title' + (key, value) for key, value in six.iteritems(self.context) if key != 'course_title' ) result = Ks.substitute_keywords_with_data(test_string, no_course_context) self.assertEqual(test_string, result) no_user_id_context = dict( - (key, value) for key, value in self.context.iteritems() if key != 'user_id' + (key, value) for key, value in six.iteritems(self.context) if key != 'user_id' ) result = Ks.substitute_keywords_with_data(test_string, no_user_id_context) self.assertEqual(test_string, result) diff --git a/common/djangoapps/util/tests/test_memcache.py b/common/djangoapps/util/tests/test_memcache.py index d9dcd26222c..bfc47b0a5e2 100644 --- a/common/djangoapps/util/tests/test_memcache.py +++ b/common/djangoapps/util/tests/test_memcache.py @@ -2,8 +2,12 @@ Tests for memcache in util app """ +from __future__ import absolute_import + from django.core.cache import caches from django.test import TestCase +from six import unichr +from six.moves import range from util.memcache import safe_key @@ -14,7 +18,7 @@ class MemcacheTest(TestCase): """ # Test whitespace, control characters, and some non-ASCII UTF-16 - UNICODE_CHAR_CODES = (range(30) + [127] + + UNICODE_CHAR_CODES = (list(range(30)) + [127] + [129, 500, 2 ** 8 - 1, 2 ** 8 + 1, 2 ** 16 - 1]) def setUp(self): diff --git a/common/djangoapps/util/tests/test_milestones_helpers.py b/common/djangoapps/util/tests/test_milestones_helpers.py index e1ac1110a27..eccf7523892 100644 --- a/common/djangoapps/util/tests/test_milestones_helpers.py +++ b/common/djangoapps/util/tests/test_milestones_helpers.py @@ -2,8 +2,11 @@ Tests for the milestones helpers library, which is the integration point for the edx_milestones API """ +from __future__ import absolute_import + import ddt import pytest +import six from django.conf import settings from django.contrib.auth.models import AnonymousUser from milestones import api as milestones_api @@ -78,16 +81,16 @@ class MilestonesHelpersTestCase(ModuleStoreTestCase): self.assertEqual(len(response), 0) def test_add_course_milestone_returns_none_when_app_disabled(self): - response = milestones_helpers.add_course_milestone(unicode(self.course.id), 'requires', self.milestone) + response = milestones_helpers.add_course_milestone(six.text_type(self.course.id), 'requires', self.milestone) self.assertIsNone(response) def test_get_course_milestones_returns_none_when_app_disabled(self): - response = milestones_helpers.get_course_milestones(unicode(self.course.id)) + response = milestones_helpers.get_course_milestones(six.text_type(self.course.id)) self.assertEqual(len(response), 0) def test_add_course_content_milestone_returns_none_when_app_disabled(self): response = milestones_helpers.add_course_content_milestone( - unicode(self.course.id), + six.text_type(self.course.id), 'i4x://any/content/id', 'requires', self.milestone @@ -96,7 +99,7 @@ class MilestonesHelpersTestCase(ModuleStoreTestCase): def test_get_course_content_milestones_returns_none_when_app_disabled(self): response = milestones_helpers.get_course_content_milestones( - unicode(self.course.id), + six.text_type(self.course.id), 'i4x://doesnt/matter/for/this/test', 'requires' ) @@ -111,7 +114,7 @@ class MilestonesHelpersTestCase(ModuleStoreTestCase): self.assertIn('ENTRANCE_EXAM', response) def test_get_course_milestones_fulfillment_paths_returns_none_when_app_disabled(self): - response = milestones_helpers.get_course_milestones_fulfillment_paths(unicode(self.course.id), self.user) + response = milestones_helpers.get_course_milestones_fulfillment_paths(six.text_type(self.course.id), self.user) self.assertIsNone(response) def test_add_user_milestone_returns_none_when_app_disabled(self): diff --git a/common/djangoapps/util/tests/test_organizations_helpers.py b/common/djangoapps/util/tests/test_organizations_helpers.py index 921e801b67a..2e7d305ba45 100644 --- a/common/djangoapps/util/tests/test_organizations_helpers.py +++ b/common/djangoapps/util/tests/test_organizations_helpers.py @@ -1,6 +1,9 @@ """ Tests for the organizations helpers library, which is the integration point for the edx-organizations API """ +from __future__ import absolute_import + +import six from mock import patch from util import organizations_helpers @@ -42,7 +45,7 @@ class OrganizationsHelpersTestCase(ModuleStoreTestCase): self.assertEqual(len(response), 0) def test_get_course_organizations_returns_none_when_app_disabled(self): - response = organizations_helpers.get_course_organizations(unicode(self.course.id)) + response = organizations_helpers.get_course_organizations(six.text_type(self.course.id)) self.assertEqual(len(response), 0) def test_add_organization_returns_none_when_app_disabled(self): diff --git a/common/djangoapps/util/tests/test_password_policy_validators.py b/common/djangoapps/util/tests/test_password_policy_validators.py index 3ba9dc929d6..6b20b587f90 100644 --- a/common/djangoapps/util/tests/test_password_policy_validators.py +++ b/common/djangoapps/util/tests/test_password_policy_validators.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- """Tests for util.password_policy_validators module.""" +from __future__ import absolute_import + import unittest from ddt import data, ddt, unpack @@ -9,7 +11,9 @@ from django.core.exceptions import ValidationError from django.test.utils import override_settings from util.password_policy_validators import ( - create_validator_config, validate_password, password_validators_instruction_texts, + create_validator_config, + password_validators_instruction_texts, + validate_password ) diff --git a/common/djangoapps/util/tests/test_sandboxing.py b/common/djangoapps/util/tests/test_sandboxing.py index 1a98ff751f7..a8624fcb354 100644 --- a/common/djangoapps/util/tests/test_sandboxing.py +++ b/common/djangoapps/util/tests/test_sandboxing.py @@ -2,6 +2,8 @@ Tests for sandboxing.py in util app """ +from __future__ import absolute_import + from django.test import TestCase from django.test.utils import override_settings from opaque_keys.edx.keys import CourseKey diff --git a/common/djangoapps/util/tests/test_string_utils.py b/common/djangoapps/util/tests/test_string_utils.py index 949b6c9ee95..fd1354a5dca 100644 --- a/common/djangoapps/util/tests/test_string_utils.py +++ b/common/djangoapps/util/tests/test_string_utils.py @@ -2,6 +2,8 @@ Tests for string_utils.py """ +from __future__ import absolute_import + from django.test import TestCase from util.string_utils import str_to_bool diff --git a/common/djangoapps/util/tests/test_submit_feedback.py b/common/djangoapps/util/tests/test_submit_feedback.py index 1410b4d472f..abfe3bf3f3c 100644 --- a/common/djangoapps/util/tests/test_submit_feedback.py +++ b/common/djangoapps/util/tests/test_submit_feedback.py @@ -1,5 +1,7 @@ """Tests for the Zendesk""" +from __future__ import absolute_import + import json from smtplib import SMTPException -- GitLab