INCR-315 (#20610): python-modernize lms/djangoapps/grades/tests

* run python modernize
* run isort
* fix pylint issues
This commit is contained in:
Stu Young
2019-05-28 11:21:34 -04:00
committed by Robert Raposa
parent a0468787aa
commit 22856fdc1a
16 changed files with 130 additions and 86 deletions

View File

@@ -1,6 +1,8 @@
"""
Base file for Grades tests
"""
from __future__ import absolute_import
from crum import set_current_request
from capa.tests.response_xml_factory import MultipleChoiceResponseXMLFactory

View File

@@ -1,6 +1,8 @@
"""
Test grading with access changes.
"""
from __future__ import absolute_import
from crum import set_current_request
from capa.tests.response_xml_factory import MultipleChoiceResponseXMLFactory

View File

@@ -2,8 +2,12 @@
Test grading events across apps.
"""
from mock import call as mock_call, patch
from __future__ import absolute_import
import six
from crum import set_current_request
from mock import call as mock_call
from mock import patch
from capa.tests.response_xml_factory import MultipleChoiceResponseXMLFactory
from courseware.tests.test_submitting_problems import ProblemSubmissionTestMixin
@@ -85,11 +89,11 @@ class GradesEventIntegrationTest(ProblemSubmissionTestMixin, SharedModuleStoreTe
mock_call(
events.PROBLEM_SUBMITTED_EVENT_TYPE,
{
'user_id': unicode(self.student.id),
'user_id': six.text_type(self.student.id),
'event_transaction_id': event_transaction_id,
'event_transaction_type': events.PROBLEM_SUBMITTED_EVENT_TYPE,
'course_id': unicode(self.course.id),
'problem_id': unicode(self.problem.location),
'course_id': six.text_type(self.course.id),
'problem_id': six.text_type(self.problem.location),
'weighted_earned': 2.0,
'weighted_possible': 2.0,
},
@@ -97,15 +101,15 @@ class GradesEventIntegrationTest(ProblemSubmissionTestMixin, SharedModuleStoreTe
mock_call(
events.COURSE_GRADE_CALCULATED,
{
'course_version': unicode(course.course_version),
'course_version': six.text_type(course.course_version),
'percent_grade': 0.02,
'grading_policy_hash': u'ChVp0lHGQGCevD0t4njna/C44zQ=',
'user_id': unicode(self.student.id),
'user_id': six.text_type(self.student.id),
'letter_grade': u'',
'event_transaction_id': event_transaction_id,
'event_transaction_type': events.PROBLEM_SUBMITTED_EVENT_TYPE,
'course_id': unicode(self.course.id),
'course_edited_timestamp': unicode(course.subtree_edited_on),
'course_id': six.text_type(self.course.id),
'course_edited_timestamp': six.text_type(course.subtree_edited_on),
}
),
],
@@ -126,10 +130,10 @@ class GradesEventIntegrationTest(ProblemSubmissionTestMixin, SharedModuleStoreTe
enrollment_tracker.emit.assert_called_with(
events.STATE_DELETED_EVENT_TYPE,
{
'user_id': unicode(self.student.id),
'course_id': unicode(self.course.id),
'problem_id': unicode(self.problem.location),
'instructor_id': unicode(self.instructor.id),
'user_id': six.text_type(self.student.id),
'course_id': six.text_type(self.course.id),
'problem_id': six.text_type(self.problem.location),
'instructor_id': six.text_type(self.instructor.id),
'event_transaction_id': event_transaction_id,
'event_transaction_type': events.STATE_DELETED_EVENT_TYPE,
}
@@ -140,13 +144,13 @@ class GradesEventIntegrationTest(ProblemSubmissionTestMixin, SharedModuleStoreTe
{
'percent_grade': 0.0,
'grading_policy_hash': u'ChVp0lHGQGCevD0t4njna/C44zQ=',
'user_id': unicode(self.student.id),
'user_id': six.text_type(self.student.id),
'letter_grade': u'',
'event_transaction_id': event_transaction_id,
'event_transaction_type': events.STATE_DELETED_EVENT_TYPE,
'course_id': unicode(self.course.id),
'course_edited_timestamp': unicode(course.subtree_edited_on),
'course_version': unicode(course.course_version),
'course_id': six.text_type(self.course.id),
'course_edited_timestamp': six.text_type(course.subtree_edited_on),
'course_version': six.text_type(course.course_version),
}
)
@@ -175,7 +179,7 @@ class GradesEventIntegrationTest(ProblemSubmissionTestMixin, SharedModuleStoreTe
for args in events_tracker.get_tracker().context.call_args_list:
self.assertEqual(
args[0][1],
{'course_id': unicode(self.course.id), 'org_id': unicode(self.course.org)}
{'course_id': six.text_type(self.course.id), 'org_id': six.text_type(self.course.org)}
)
event_transaction_id = events_tracker.emit.mock_calls[0][1][1]['event_transaction_id']
@@ -184,13 +188,13 @@ class GradesEventIntegrationTest(ProblemSubmissionTestMixin, SharedModuleStoreTe
mock_call(
events.GRADES_RESCORE_EVENT_TYPE,
{
'course_id': unicode(self.course.id),
'user_id': unicode(self.student.id),
'problem_id': unicode(self.problem.location),
'course_id': six.text_type(self.course.id),
'user_id': six.text_type(self.student.id),
'problem_id': six.text_type(self.problem.location),
'new_weighted_earned': 2,
'new_weighted_possible': 2,
'only_if_higher': False,
'instructor_id': unicode(self.instructor.id),
'instructor_id': six.text_type(self.instructor.id),
'event_transaction_id': event_transaction_id,
'event_transaction_type': events.GRADES_RESCORE_EVENT_TYPE,
},
@@ -198,15 +202,15 @@ class GradesEventIntegrationTest(ProblemSubmissionTestMixin, SharedModuleStoreTe
mock_call(
events.COURSE_GRADE_CALCULATED,
{
'course_version': unicode(course.course_version),
'course_version': six.text_type(course.course_version),
'percent_grade': 0.02,
'grading_policy_hash': u'ChVp0lHGQGCevD0t4njna/C44zQ=',
'user_id': unicode(self.student.id),
'user_id': six.text_type(self.student.id),
'letter_grade': u'',
'event_transaction_id': event_transaction_id,
'event_transaction_type': events.GRADES_RESCORE_EVENT_TYPE,
'course_id': unicode(self.course.id),
'course_edited_timestamp': unicode(course.subtree_edited_on),
'course_id': six.text_type(self.course.id),
'course_edited_timestamp': six.text_type(course.subtree_edited_on),
},
),
],

View File

@@ -1,9 +1,14 @@
# pylint: disable=missing-docstring
from __future__ import absolute_import
import datetime
import itertools
import ddt
import pytz
from crum import set_current_request
from six.moves import range
from capa.tests.response_xml_factory import MultipleChoiceResponseXMLFactory
from courseware.tests.test_submitting_problems import ProblemSubmissionTestMixin
from lms.djangoapps.course_blocks.api import get_course_blocks

View File

@@ -1,8 +1,12 @@
"""
Tests for CourseData utility class.
"""
from lms.djangoapps.course_blocks.api import get_course_blocks
from __future__ import absolute_import
import six
from mock import patch
from lms.djangoapps.course_blocks.api import get_course_blocks
from openedx.core.djangoapps.content.block_structure.api import get_course_in_cache
from student.tests.factories import UserFactory
from xmodule.modulestore import ModuleStoreEnum
@@ -73,7 +77,7 @@ class CourseDataTest(ModuleStoreTestCase):
self.assertEquals(course_data.course.id, self.course.id)
self.assertEquals(course_data.version, self.course.course_version)
self.assertEquals(course_data.edited_on, expected_edited_on)
self.assertIn(u'Course: course_key', unicode(course_data))
self.assertIn(u'Course: course_key', six.text_type(course_data))
self.assertIn(u'Course: course_key', course_data.full_string())
def test_no_data(self):

View File

@@ -1,7 +1,11 @@
# pylint: disable=missing-docstring
from __future__ import absolute_import
import ddt
import six
from crum import set_current_request
from django.conf import settings
from mock import patch
from crum import set_current_request
from openedx.core.djangolib.testing.utils import get_mock_request
from student.models import CourseEnrollment
@@ -35,7 +39,7 @@ class ZeroGradeTest(GradeTestBase):
chapter_grades = ZeroCourseGrade(self.request.user, course_data).chapter_grades
for chapter in chapter_grades:
for section in chapter_grades[chapter]['sections']:
for score in section.problem_scores.itervalues():
for score in six.itervalues(section.problem_scores):
self.assertEqual(score.earned, 0)
self.assertEqual(score.first_attempted, None)
self.assertEqual(section.all_total.earned, 0)

View File

@@ -1,16 +1,18 @@
"""
Tests for the CourseGradeFactory class.
"""
from __future__ import absolute_import
import itertools
import ddt
from courseware.access import has_access
from django.conf import settings
from lms.djangoapps.grades.config.tests.utils import persistent_grades_feature_flags
from mock import patch
from openedx.core.djangoapps.content.block_structure.factory import BlockStructureFactory
from six import text_type
from courseware.access import has_access
from lms.djangoapps.grades.config.tests.utils import persistent_grades_feature_flags
from openedx.core.djangoapps.content.block_structure.factory import BlockStructureFactory
from student.tests.factories import UserFactory
from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory

View File

@@ -1,6 +1,8 @@
"""
Unit tests for grades models.
"""
from __future__ import absolute_import
import json
from base64 import b64encode
from collections import OrderedDict
@@ -9,6 +11,7 @@ from hashlib import sha1
import ddt
import pytz
import six
from django.db.utils import IntegrityError
from django.test import TestCase
from django.utils.timezone import now
@@ -46,7 +49,7 @@ class BlockRecordListTestCase(TestCase):
def test_empty_block_record_set(self):
empty_json = u'{"blocks":[],"course_key":"%s","version":%s}' % (
unicode(self.course_key),
six.text_type(self.course_key),
BLOCK_RECORD_LIST_VERSION,
)
@@ -150,15 +153,15 @@ class VisibleBlocksTest(GradesModelTestCase):
vblocks = self._create_block_record_list([self.record_a])
list_of_block_dicts = [self.record_a._asdict()]
for block_dict in list_of_block_dicts:
block_dict['locator'] = unicode(block_dict['locator']) # BlockUsageLocator is not json-serializable
block_dict['locator'] = six.text_type(block_dict['locator']) # BlockUsageLocator is not json-serializable
expected_data = {
'blocks': [{
'locator': unicode(self.record_a.locator),
'locator': six.text_type(self.record_a.locator),
'raw_possible': 10,
'weight': 1,
'graded': self.record_a.graded,
}],
'course_key': unicode(self.record_a.locator.course_key),
'course_key': six.text_type(self.record_a.locator.course_key),
'version': BLOCK_RECORD_LIST_VERSION,
}
expected_json = json.dumps(expected_data, separators=(',', ':'), sort_keys=True)
@@ -334,19 +337,19 @@ class PersistentSubsectionGradeTest(GradesModelTestCase):
tracker_mock.emit.assert_called_with(
u'edx.grades.subsection.grade_calculated',
{
'user_id': unicode(grade.user_id),
'course_id': unicode(grade.course_id),
'block_id': unicode(grade.usage_key),
'course_version': unicode(grade.course_version),
'user_id': six.text_type(grade.user_id),
'course_id': six.text_type(grade.course_id),
'block_id': six.text_type(grade.usage_key),
'course_version': six.text_type(grade.course_version),
'weighted_total_earned': grade.earned_all,
'weighted_total_possible': grade.possible_all,
'weighted_graded_earned': grade.earned_graded,
'weighted_graded_possible': grade.possible_graded,
'first_attempted': unicode(grade.first_attempted),
'subtree_edited_timestamp': unicode(grade.subtree_edited_timestamp),
'event_transaction_id': unicode(get_event_transaction_id()),
'event_transaction_type': unicode(get_event_transaction_type()),
'visible_blocks_hash': unicode(grade.visible_blocks_id),
'first_attempted': six.text_type(grade.first_attempted),
'subtree_edited_timestamp': six.text_type(grade.subtree_edited_timestamp),
'event_transaction_id': six.text_type(get_event_transaction_id()),
'event_transaction_type': six.text_type(get_event_transaction_type()),
'visible_blocks_hash': six.text_type(grade.visible_blocks_id),
}
)
@@ -481,14 +484,14 @@ class PersistentCourseGradesTest(GradesModelTestCase):
tracker_mock.emit.assert_called_with(
u'edx.grades.course.grade_calculated',
{
'user_id': unicode(grade.user_id),
'course_id': unicode(grade.course_id),
'course_version': unicode(grade.course_version),
'user_id': six.text_type(grade.user_id),
'course_id': six.text_type(grade.course_id),
'course_version': six.text_type(grade.course_version),
'percent_grade': grade.percent_grade,
'letter_grade': unicode(grade.letter_grade),
'course_edited_timestamp': unicode(grade.course_edited_timestamp),
'event_transaction_id': unicode(get_event_transaction_id()),
'event_transaction_type': unicode(get_event_transaction_type()),
'grading_policy_hash': unicode(grade.grading_policy_hash),
'letter_grade': six.text_type(grade.letter_grade),
'course_edited_timestamp': six.text_type(grade.course_edited_timestamp),
'event_transaction_id': six.text_type(get_event_transaction_id()),
'event_transaction_type': six.text_type(get_event_transaction_type()),
'grading_policy_hash': six.text_type(grade.grading_policy_hash),
}
)

View File

@@ -1,6 +1,8 @@
"""
Tests for grades.scores module.
"""
from __future__ import absolute_import
import itertools
# pylint: disable=protected-access
from collections import namedtuple

View File

@@ -1,18 +1,23 @@
"""
Grades Service Tests
"""
from __future__ import absolute_import
from datetime import datetime
import ddt
import pytz
import six
from freezegun import freeze_time
from mock import call, patch
from lms.djangoapps.grades.constants import GradeOverrideFeatureEnum
from lms.djangoapps.grades.models import (
PersistentSubsectionGrade,
PersistentSubsectionGradeOverride,
PersistentSubsectionGradeOverrideHistory,
PersistentSubsectionGradeOverrideHistory
)
from lms.djangoapps.grades.services import GradesService
from mock import patch, call
from student.tests.factories import UserFactory
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory
@@ -107,8 +112,8 @@ class GradesServiceTests(ModuleStoreTestCase):
# test with id strings as parameters instead
self.assertDictEqual(self.subsection_grade_to_dict(self.service.get_subsection_grade(
user_id=self.user.id,
course_key_or_id=unicode(self.course.id),
usage_key_or_id=unicode(self.subsection.location)
course_key_or_id=six.text_type(self.course.id),
usage_key_or_id=six.text_type(self.subsection.location)
)), {
'earned_all': 6.0,
'earned_graded': 5.0
@@ -136,7 +141,7 @@ class GradesServiceTests(ModuleStoreTestCase):
# test with course key parameter as string instead
self.assertDictEqual(self.subsection_grade_override_to_dict(self.service.get_subsection_grade_override(
user_id=self.user.id,
course_key_or_id=unicode(self.course.id),
course_key_or_id=six.text_type(self.course.id),
usage_key_or_id=self.subsection.location
)), {
'earned_all_override': override.earned_all_override,
@@ -190,8 +195,8 @@ class GradesServiceTests(ModuleStoreTestCase):
call(
sender=None,
user_id=self.user.id,
course_id=unicode(self.course.id),
usage_id=unicode(self.subsection.location),
course_id=six.text_type(self.course.id),
usage_id=six.text_type(self.subsection.location),
only_if_higher=False,
modified=override_obj.modified,
score_deleted=False,
@@ -241,8 +246,8 @@ class GradesServiceTests(ModuleStoreTestCase):
call(
sender=None,
user_id=self.user.id,
course_id=unicode(self.course.id),
usage_id=unicode(self.subsection_without_grade.location),
course_id=six.text_type(self.course.id),
usage_id=six.text_type(self.subsection_without_grade.location),
only_if_higher=False,
modified=override_obj.modified,
score_deleted=False,
@@ -270,8 +275,8 @@ class GradesServiceTests(ModuleStoreTestCase):
call(
sender=None,
user_id=self.user.id,
course_id=unicode(self.course.id),
usage_id=unicode(self.subsection.location),
course_id=six.text_type(self.course.id),
usage_id=six.text_type(self.subsection.location),
only_if_higher=False,
modified=datetime.now().replace(tzinfo=pytz.UTC),
score_deleted=True,

View File

@@ -1,6 +1,8 @@
"""
Tests for the score change signals defined in the courseware models module.
"""
from __future__ import absolute_import
import re
from datetime import datetime
@@ -8,8 +10,8 @@ import ddt
import pytz
from django.test import TestCase
from mock import MagicMock, patch
from submissions.models import score_reset, score_set
from util.date_utils import to_timestamp
from ..constants import ScoreDatabaseTableEnum
@@ -17,7 +19,7 @@ from ..signals.handlers import (
disconnect_submissions_signal_receiver,
problem_raw_score_changed_handler,
submissions_score_reset_handler,
submissions_score_set_handler,
submissions_score_set_handler
)
from ..signals.signals import PROBLEM_RAW_SCORE_CHANGED

View File

@@ -1,12 +1,14 @@
"""
Tests of the SubsectionGrade classes.
"""
from __future__ import absolute_import
from ddt import data, ddt, unpack
from ..models import PersistentSubsectionGrade
from ..subsection_grade import CreateSubsectionGrade, ReadSubsectionGrade
from .utils import mock_get_score
from .base import GradeTestBase
from .utils import mock_get_score
@ddt

View File

@@ -1,19 +1,18 @@
"""
Tests for the SubsectionGradeFactory class.
"""
from __future__ import absolute_import
import ddt
from mock import patch
from django.conf import settings
from mock import patch
from courseware.tests.test_submitting_problems import ProblemSubmissionTestMixin
from lms.djangoapps.grades.config.tests.utils import persistent_grades_feature_flags
from student.tests.factories import UserFactory
from ..constants import GradeOverrideFeatureEnum
from ..models import (
PersistentSubsectionGrade,
PersistentSubsectionGradeOverride,
)
from ..models import PersistentSubsectionGrade, PersistentSubsectionGradeOverride
from ..subsection_grade_factory import ZeroSubsectionGrade
from .base import GradeTestBase
from .utils import mock_get_score

View File

@@ -2,6 +2,8 @@
Tests for the functionality and infrastructure of grades tasks.
"""
from __future__ import absolute_import
import itertools
from collections import OrderedDict
from contextlib import contextmanager
@@ -14,10 +16,11 @@ from django.conf import settings
from django.db.utils import IntegrityError
from django.utils import timezone
from mock import MagicMock, patch
from six.moves import range
from lms.djangoapps.grades import tasks
from lms.djangoapps.grades.config.models import PersistentGradesEnabledFlag
from lms.djangoapps.grades.config.waffle import waffle_flags, ENFORCE_FREEZE_GRADE_AFTER_COURSE_END
from lms.djangoapps.grades.config.waffle import ENFORCE_FREEZE_GRADE_AFTER_COURSE_END, waffle_flags
from lms.djangoapps.grades.constants import ScoreDatabaseTableEnum
from lms.djangoapps.grades.models import PersistentCourseGrade, PersistentSubsectionGrade
from lms.djangoapps.grades.services import GradesService
@@ -41,7 +44,6 @@ from xmodule.modulestore.django import modulestore
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory, check_mongo_calls
from .utils import mock_get_score
@@ -90,8 +92,8 @@ class HasCourseWithProblemsMixin(object):
('weighted_possible', 2.0),
('user_id', self.user.id),
('anonymous_user_id', 5),
('course_id', unicode(self.course.id)),
('usage_id', unicode(self.problem.location)),
('course_id', six.text_type(self.course.id)),
('usage_id', six.text_type(self.problem.location)),
('only_if_higher', None),
('modified', self.frozen_now_datetime),
('score_db_table', ScoreDatabaseTableEnum.courseware_student_module),
@@ -101,13 +103,13 @@ class HasCourseWithProblemsMixin(object):
self.recalculate_subsection_grade_kwargs = OrderedDict([
('user_id', self.user.id),
('course_id', unicode(self.course.id)),
('usage_id', unicode(self.problem.location)),
('course_id', six.text_type(self.course.id)),
('usage_id', six.text_type(self.problem.location)),
('anonymous_user_id', 5),
('only_if_higher', None),
('expected_modified_time', self.frozen_now_timestamp),
('score_deleted', False),
('event_transaction_id', unicode(get_event_transaction_id())),
('event_transaction_id', six.text_type(get_event_transaction_id())),
('event_transaction_type', u'edx.grades.problem.submitted'),
('score_db_table', ScoreDatabaseTableEnum.courseware_student_module),
])
@@ -424,12 +426,12 @@ class ComputeGradesForCourseTest(HasCourseWithProblemsMixin, ModuleStoreTestCase
def setUp(self):
super(ComputeGradesForCourseTest, self).setUp()
self.users = [UserFactory.create() for _ in xrange(12)]
self.users = [UserFactory.create() for _ in range(12)]
self.set_up_course()
for user in self.users:
CourseEnrollment.enroll(user, self.course.id)
@ddt.data(*xrange(0, 12, 3))
@ddt.data(*range(0, 12, 3))
def test_behavior(self, batch_size):
with mock_get_score(1, 2):
result = compute_grades_for_course_v2.delay(
@@ -447,7 +449,7 @@ class ComputeGradesForCourseTest(HasCourseWithProblemsMixin, ModuleStoreTestCase
min(batch_size, 8) # No more than 8 due to offset
)
@ddt.data(*xrange(1, 12, 3))
@ddt.data(*range(1, 12, 3))
def test_course_task_args(self, test_batch_size):
offset_expected = 0
for course_key, offset, batch_size in _course_task_args(
@@ -513,7 +515,7 @@ class FreezeGradingAfterCourseEndTest(HasCourseWithProblemsMixin, ModuleStoreTes
"""
def setUp(self):
super(FreezeGradingAfterCourseEndTest, self).setUp()
self.users = [UserFactory.create() for _ in xrange(12)]
self.users = [UserFactory.create() for _ in range(12)]
self.user = self.users[0]
self.freeze_grade_flag = waffle_flags()[ENFORCE_FREEZE_GRADE_AFTER_COURSE_END]

View File

@@ -2,12 +2,16 @@
Test the behavior of the GradesTransformer
"""
from __future__ import absolute_import
import datetime
import random
from copy import deepcopy
import ddt
import pytz
import six
from six.moves import range
from lms.djangoapps.course_blocks.api import get_course_blocks
from lms.djangoapps.course_blocks.transformers.tests.helpers import CourseStructureTestCase
@@ -214,7 +218,7 @@ class GradesTransformerTestCase(CourseStructureTestCase):
}
blocks = self.build_complicated_hypothetical_course()
block_structure = get_course_blocks(self.student, blocks[u'course'].location, self.transformers)
for block_ref, expected_subsections in expected_subsections.iteritems():
for block_ref, expected_subsections in six.iteritems(expected_subsections):
actual_subsections = block_structure.get_transformer_block_field(
blocks[block_ref].location,
self.TRANSFORMER_CLASS_TO_TEST,
@@ -427,7 +431,7 @@ class MultiProblemModulestoreAccessTestCase(CourseStructureTestCase, SharedModul
u'#children': [],
},
]
for problem_number in xrange(random.randrange(10, 20)):
for problem_number in range(random.randrange(10, 20)):
course[0][u'#children'].append(
{
u'metadata': {

View File

@@ -1,11 +1,13 @@
"""
Utilities for grades related tests
"""
from __future__ import absolute_import
from contextlib import contextmanager
from datetime import datetime
import pytz
from mock import patch, MagicMock
from mock import MagicMock, patch
from courseware.model_data import FieldDataCache
from courseware.module_render import get_module