<commit_before>"""
Tests for recalculate_learner_grades management command.
"""

from __future__ import absolute_import

from tempfile import NamedTemporaryFile

import mock

from lms.djangoapps.grades.management.commands import recalculate_learner_grades
from lms.djangoapps.grades.tests.test_tasks import HasCourseWithProblemsMixin
from student.tests.factories import CourseEnrollmentFactory, UserFactory
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory

DATE_FORMAT = u"%Y-%m-%d %H:%M"


class TestRecalculateLearnerGrades(HasCourseWithProblemsMixin, ModuleStoreTestCase):
    """
    Tests recalculate learner grades management command.
    """

    def setUp(self):
        super(TestRecalculateLearnerGrades, self).setUp()
        self.command = recalculate_learner_grades.Command()

        self.course1 = CourseFactory.create()
        self.course2 = CourseFactory.create()
        self.course3 = CourseFactory.create()
        self.user1 = UserFactory.create()
        self.user2 = UserFactory.create()
        CourseEnrollmentFactory(course_id=self.course1.id, user=self.user1)
        CourseEnrollmentFactory(course_id=self.course1.id, user=self.user2)
        CourseEnrollmentFactory(course_id=self.course2.id, user=self.user1)
        CourseEnrollmentFactory(course_id=self.course2.id, user=self.user2)

        self.user_course_pairs = [
            (str(self.user1.id), str(self.course1.id)),
            (str(self.user1.id), str(self.course2.id)),
            (str(self.user2.id), str(self.course1.id)),
            (str(self.user2.id), str(self.course2.id))
        ]

    @mock.patch(
        'lms.djangoapps.grades.management.commands.recalculate_learner_grades.'
        'recalculate_course_and_subsection_grades_for_user'
    )
    def test_recalculate_grades(self, task_mock):
        with NamedTemporaryFile() as csv:
            csv.write("course_id,user_id\n")
            csv.writelines(course + "," + user + "\n" for user, course in self.user_course_pairs)
            csv.seek(0)

            self.command.handle(csv=csv.name)

            expected_calls = []
            for user, course in self.user_course_pairs:
                expected_calls.append(mock.call(<commit_msg>Grades python-3 upgrade: test_recalculate_learner_grades.py<commit_after>"""
Tests for recalculate_learner_grades management command.
"""

from __future__ import absolute_import

from tempfile import NamedTemporaryFile

import mock

from lms.djangoapps.grades.management.commands import recalculate_learner_grades
from lms.djangoapps.grades.tests.test_tasks import HasCourseWithProblemsMixin
from student.tests.factories import CourseEnrollmentFactory, UserFactory
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory

DATE_FORMAT = u"%Y-%m-%d %H:%M"


class TestRecalculateLearnerGrades(HasCourseWithProblemsMixin, ModuleStoreTestCase):
    """
    Tests recalculate learner grades management command.
    """

    def setUp(self):
        super(TestRecalculateLearnerGrades, self).setUp()
        self.command = recalculate_learner_grades.Command()

        self.course1 = CourseFactory.create()
        self.course2 = CourseFactory.create()
        self.course3 = CourseFactory.create()
        self.user1 = UserFactory.create()
        self.user2 = UserFactory.create()
        CourseEnrollmentFactory(course_id=self.course1.id, user=self.user1)
        CourseEnrollmentFactory(course_id=self.course1.id, user=self.user2)
        CourseEnrollmentFactory(course_id=self.course2.id, user=self.user1)
        CourseEnrollmentFactory(course_id=self.course2.id, user=self.user2)

        self.user_course_pairs = [
            (str(self.user1.id), str(self.course1.id)),
            (str(self.user1.id), str(self.course2.id)),
            (str(self.user2.id), str(self.course1.id)),
            (str(self.user2.id), str(self.course2.id))
        ]

    @mock.patch(
        'lms.djangoapps.grades.management.commands.recalculate_learner_grades.'
        'recalculate_course_and_subsection_grades_for_user'
    )
    def test_recalculate_grades(self, task_mock):
        with NamedTemporaryFile() as csv:
            csv.write(b"course_id,user_id\n")
            csv.writelines(
                "{},{}\n".format(course, user).encode()
                for user, course in self.user_course_pairs
            )
            csv.seek(0)

            self.command.handle(csv=csv.name)

            expected_calls = []
            for user, course in self.user_course_pairs:
                expected_calls.append(mock.call(
{'fraction_non_alphanumeric': 0.10102520290474157, 'fraction_numerical': 0.009611277231952157, 'mean_word_length': 3.340129749768304, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6003483', 'n_tokens_mistral': 1460, 'n_tokens_neox': 1428, 'n_words': 222}