Skip to content
test_generic.py 20.5 KiB
Newer Older
from __future__ import division

Dong Zhuang's avatar
Dong Zhuang committed
__copyright__ = "Copyright (C) 2017 Zesheng Wang, Andreas Kloeckner"

__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

from django.urls import reverse
Dong Zhuang's avatar
Dong Zhuang committed
from tests.base_test_mixins import SingleCoursePageTestMixin
zwang180's avatar
zwang180 committed

Dong Zhuang's avatar
Dong Zhuang committed
from django.test import TestCase
from course.models import (
    Participation, GradingOpportunity, FlowSession,
    FlowRuleException, GradeChange
)
class GradeTestMixin(SingleCoursePageTestMixin):
Dong Zhuang's avatar
Dong Zhuang committed
    # This serve as a base test cases for other grade tests to subclass
    # Nice little tricks :)
zwang180's avatar
zwang180 committed
    def setUpTestData(cls):  # noqa
Dong Zhuang's avatar
Dong Zhuang committed
        super(GradeTestMixin, cls).setUpTestData()
        cls.flow_session_ids = []
Dong Zhuang's avatar
Dong Zhuang committed
        cls.do_quiz(cls.student_participation)
Dong Zhuang's avatar
Dong Zhuang committed
    @classmethod
    def tearDownClass(cls):
        super(GradeTestMixin, cls).tearDownClass()

    # Use specified user to take a quiz
    @classmethod
Dong Zhuang's avatar
Dong Zhuang committed
    def do_quiz(cls, participation):
Dong Zhuang's avatar
Dong Zhuang committed
        cls.c.force_login(participation.user)
        cls.start_flow(cls.flow_id)
        cls.flow_session_ids.append(
            int(cls.default_flow_params["flow_session_id"]))

    # Seperate the test here
    def test_grading_opportunity(self):
        # Should only have one grading opportunity object
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(GradingOpportunity.objects.all().count(), 1)

    def test_view_my_grade(self):
        resp = self.c.get(reverse("relate-view_participant_grades",
Dong Zhuang's avatar
Dong Zhuang committed
                                  args=[self.course.identifier]))
        self.assertEqual(resp.status_code, 200)

    def test_view_participant_grades(self):
Dong Zhuang's avatar
Dong Zhuang committed
        params = {"course_identifier": self.course.identifier,
                  "participation_id": self.instructor_participation.user.id}
        resp = self.c.get(reverse("relate-view_participant_grades",
                                                    kwargs=params))
        self.assertEqual(resp.status_code, 200)

    def test_view_participant_list(self):
        resp = self.c.get(reverse("relate-view_participant_list",
Dong Zhuang's avatar
Dong Zhuang committed
                                  args=[self.course.identifier]))
        self.assertEqual(resp.status_code, 200)

    def test_view_grading_opportunity_list(self):
        resp = self.c.get(reverse("relate-view_grading_opportunity_list",
Dong Zhuang's avatar
Dong Zhuang committed
                                  args=[self.course.identifier]))
        self.assertEqual(resp.status_code, 200)

    def test_view_gradebook(self):
        resp = self.c.get(reverse("relate-view_gradebook",
Dong Zhuang's avatar
Dong Zhuang committed
                                  args=[self.course.identifier]))
        self.assertEqual(resp.status_code, 200)

    def test_view_export_gradebook_csv(self):
        resp = self.c.get(reverse("relate-export_gradebook_csv",
Dong Zhuang's avatar
Dong Zhuang committed
                                  args=[self.course.identifier]))
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp["Content-Disposition"],
Dong Zhuang's avatar
Dong Zhuang committed
                         'attachment; filename="grades-test-course.csv"')

    def test_view_grades_by_opportunity(self):
        # Check attributes
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(GradingOpportunity.objects.all().count(), 1)
        opportunity = GradingOpportunity.objects.first()
        self.assertEqual(self.course, opportunity.course)
        self.assertEqual(self.flow_id, opportunity.flow_id)
Dong Zhuang's avatar
Dong Zhuang committed
        params = {"course_identifier": self.course.identifier,
                  "opp_id": opportunity.id}
        resp = self.c.get(reverse("relate-view_grades_by_opportunity",
Dong Zhuang's avatar
Dong Zhuang committed
                                  kwargs=params))
        self.assertEqual(resp.status_code, 200)

    def test_view_participant_grade_by_opportunity(self):
        # Check attributes
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(GradingOpportunity.objects.all().count(), 1)
        opportunity = GradingOpportunity.objects.first()
        self.assertEqual(self.course, opportunity.course)
        self.assertEqual(self.flow_id, opportunity.flow_id)
Dong Zhuang's avatar
Dong Zhuang committed
        params = {"course_identifier": self.course.identifier,
                  "opportunity_id": opportunity.id,
                  "participation_id": self.student_participation.id}
        resp = self.c.get(reverse("relate-view_single_grade", kwargs=params))
        self.assertEqual(resp.status_code, 200)

    def test_view_reopen_session(self):
        # Check attributes
zwang180's avatar
zwang180 committed
        self.assertEqual(len(GradingOpportunity.objects.all()), 1)
        opportunity = GradingOpportunity.objects.all()[0]
        self.assertEqual(self.course, opportunity.course)
        self.assertEqual(self.flow_id, opportunity.flow_id)
zwang180's avatar
zwang180 committed
        all_session = FlowSession.objects.all()
        # Check flow numbers
        self.assertEqual(len(all_session), len(self.flow_session_ids))
        # Check each flow session
zwang180's avatar
zwang180 committed
        for session in all_session:
            self.check_reopen_session(session.id, opportunity.id)
        # Check flow numbers again
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(FlowSession.objects.all().count(),
                         len(self.flow_session_ids))
zwang180's avatar
zwang180 committed
    def test_view_import_grades_without_header(self):
Dong Zhuang's avatar
Dong Zhuang committed
        csv_data = [(self.instructor_participation.user.username,
                        99, "Almost!"),
                    (self.student_participation.user.username,
                        50, "I hate this course :(")]
        self.check_import_grade(csv_data)
zwang180's avatar
zwang180 committed

    def test_view_import_grades_with_header(self):
Dong Zhuang's avatar
Dong Zhuang committed
        csv_data = [("username", "grade", "feedback"),
                    (self.instructor_participation.user.username,
                        99, "Almost!"),
                    (self.student_participation.user.username,
                        50, "I hate this course :(")]
        self.check_import_grade(csv_data, True)

    # Seems just show the answer
    def test_view_grade_flow_page(self):
Dong Zhuang's avatar
Dong Zhuang committed
        params = {"course_identifier": self.course.identifier,
                  "flow_session_id": self.flow_session_ids[0]}

        page_count = FlowSession.objects.get(id=self.flow_session_ids[0]).page_count
        for i in range(page_count):
            resp = self.c.get(
                self.get_page_grading_url_by_ordinal(page_ordinal=i, **params))
            self.assertEqual(resp.status_code, 200)

        # test PageOrdinalOutOfRange
        resp = self.c.get(
            self.get_page_grading_url_by_ordinal(page_ordinal=page_count+1,
                                                 **params))
        self.assertEqual(resp.status_code, 404)

    def test_view_grader_statistics(self):
Dong Zhuang's avatar
Dong Zhuang committed
        params = {"course_identifier": self.course.identifier,
        resp = self.c.get(reverse("relate-show_grader_statistics",
                                            kwargs=params))
        self.assertEqual(resp.status_code, 200)

    def test_view_download_submissions(self):
        # Check download form first
        resp = self.get_download_all_submissions(flow_id=self.flow_id)
        self.assertEqual(resp.status_code, 200)

        # Check download here, only test intro page
        # Maybe we should include an "all" option in the future?
        resp = (
            self.post_download_all_submissions_by_group_page_id(
                flow_id=self.flow_id,
                group_page_id="intro/welcome")
        )

        self.assertEqual(resp.status_code, 200)
        prefix, zip_file = resp["Content-Disposition"].split('=')
        self.assertEqual(prefix, "attachment; filename")
        zip_file_name = zip_file.replace('"', '').split('_')
        self.assertEqual(zip_file_name[0], "submissions")
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(zip_file_name[1], self.course.identifier)
        self.assertEqual(zip_file_name[2], self.flow_id)
        self.assertEqual(zip_file_name[3], "intro")
        self.assertEqual(zip_file_name[4], "welcome")
        self.assertTrue(zip_file_name[5].endswith(".zip"))

    def test_view_edit_grading_opportunity(self):
        # Check attributes
zwang180's avatar
zwang180 committed
        self.assertEqual(len(GradingOpportunity.objects.all()), 1)
        opportunity = GradingOpportunity.objects.all()[0]
        self.assertEqual(self.course, opportunity.course)
        self.assertEqual(self.flow_id, opportunity.flow_id)
Dong Zhuang's avatar
Dong Zhuang committed
        params = {"course_identifier": self.course.identifier,
                    "opportunity_id": opportunity.id}
        # Check page
        resp = self.c.get(reverse("relate-edit_grading_opportunity",
                                            kwargs=params))
        self.assertEqual(resp.status_code, 200)
        # Try making a change
        self.assertEqual(opportunity.page_scores_in_participant_gradebook, False)
Dong Zhuang's avatar
Dong Zhuang committed
        data = {'page_scores_in_participant_gradebook': ['on'],
                 'name': ['Flow: RELATE Test Quiz'],
                 'hide_superseded_grade_history_before': [''],
                 'submit': ['Update'],
                 'shown_in_participant_grade_book': ['on'],
                 'aggregation_strategy': ['use_latest'],
                 'shown_in_grade_book': ['on'],
                 'result_shown_in_participant_grade_book': ['on']}
        resp = self.c.post(reverse("relate-edit_grading_opportunity",
Dong Zhuang's avatar
Dong Zhuang committed
                                                    kwargs=params), data)
        self.assertEqual(resp.status_code, 302)
        self.assertEqual(resp.url, reverse("relate-edit_grading_opportunity",
                                                            kwargs=params))

        # Check objects and attributes
        # Should still be one
        self.assertEqual(len(GradingOpportunity.objects.all()), 1)
        opportunity = GradingOpportunity.objects.all()[0]
        self.assertEqual(self.course, opportunity.course)
        self.assertEqual(self.flow_id, opportunity.flow_id)
        # Check changes
        self.assertEqual(opportunity.page_scores_in_participant_gradebook, True)

zwang180's avatar
zwang180 committed
    def test_view_flow_list_analytics(self):
        resp = self.c.get(reverse("relate-flow_list",
Dong Zhuang's avatar
Dong Zhuang committed
                                            args=[self.course.identifier]))
zwang180's avatar
zwang180 committed
        self.assertEqual(resp.status_code, 200)

    def test_view_flow_analytics(self):
Dong Zhuang's avatar
Dong Zhuang committed
        params = {"course_identifier": self.course.identifier,
zwang180's avatar
zwang180 committed
        resp = self.c.get(reverse("relate-flow_analytics",
                                            kwargs=params))
        self.assertEqual(resp.status_code, 200)

    # Only check page for now
    def test_view_regrade_flow(self):
        resp = self.c.get(reverse("relate-regrade_flows_view",
Dong Zhuang's avatar
Dong Zhuang committed
                                            args=[self.course.identifier]))
zwang180's avatar
zwang180 committed
        self.assertEqual(resp.status_code, 200)

    def test_view_grant_exception_new_session(self):
zwang180's avatar
zwang180 committed
        all_session = FlowSession.objects.all()
        # Check number of flow sessions and ids
        self.assertEqual(all_session.count(), len(self.flow_session_ids))
zwang180's avatar
zwang180 committed
        for session in all_session:
            # Perform all checking before moving to stage three
            params = self.check_stage_one_and_two(session.participation)
            self.assertTrue(session.id in self.flow_session_ids)
            self.check_grant_new_exception(params)
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(FlowSession.objects.all().count(),
                         2 * self.n_quiz_takers)

    def test_view_grant_exception_exist_session(self):
        # Store numbers to reuse
        session_nums = len(self.flow_session_ids)
zwang180's avatar
zwang180 committed

        all_session = FlowSession.objects.all()
        # Check session numbers
zwang180's avatar
zwang180 committed
        self.assertEqual(len(all_session), session_nums)

        # Check for each existing session
zwang180's avatar
zwang180 committed
        for session in all_session:
            # Perform all checking before moving to stage three
            params = self.check_stage_one_and_two(session.participation)
            self.check_grant_exist_exception(session.id, params)

        # Should have two exception rules now
        # One for access and one for grading
        self.assertEqual(len(FlowRuleException.objects.all()), 2 * session_nums)
zwang180's avatar
zwang180 committed
    # Helper method for creating in memory csv files to test import grades
Dong Zhuang's avatar
Dong Zhuang committed
    def creat_grading_csv(self, data):
        try:
            import cStringIO  # PY2
        except ImportError:
            import io as cStringIO  # PY3

zwang180's avatar
zwang180 committed
        csvfile = cStringIO.StringIO()
Dong Zhuang's avatar
Dong Zhuang committed

        import csv
zwang180's avatar
zwang180 committed
        csvwriter = csv.writer(csvfile)
Dong Zhuang's avatar
Dong Zhuang committed
        for d in data:
zwang180's avatar
zwang180 committed
            # (username, grades, feedback)
Dong Zhuang's avatar
Dong Zhuang committed
            csvwriter.writerow([d[0], d[1], d[2]])
zwang180's avatar
zwang180 committed
        # Reset back to the start of file to avoid invalid form error
        # Otherwise it will consider the file as empty
        csvfile.seek(0)
        return csvfile

    # Helper method for testing import grades
Dong Zhuang's avatar
Dong Zhuang committed
    def check_import_grade(self, csv_data, headers=False):
zwang180's avatar
zwang180 committed
        # Check import form works well
        resp = self.c.get(reverse("relate-import_grades",
Dong Zhuang's avatar
Dong Zhuang committed
                                  args=[self.course.identifier]))
zwang180's avatar
zwang180 committed
        self.assertEqual(resp.status_code, 200)

        # Check number of GradeChange
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(GradeChange.objects.all().count(), self.n_quiz_takers)
zwang180's avatar
zwang180 committed

        # Check attributes
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(GradingOpportunity.objects.all().count(), 1)
        opportunity = GradingOpportunity.objects.all().first()
zwang180's avatar
zwang180 committed
        self.assertEqual(self.course, opportunity.course)
        self.assertEqual(self.flow_id, opportunity.flow_id)
Dong Zhuang's avatar
Dong Zhuang committed
        # Prepare data
zwang180's avatar
zwang180 committed
        # Prepare csv
Dong Zhuang's avatar
Dong Zhuang committed
        csv_file = self.creat_grading_csv(csv_data)
        # Prepare form data
        data = {'points_column': ['2'], 'attr_column': ['1'],
zwang180's avatar
zwang180 committed
                'feedback_column': ['3'],
                'grading_opportunity': [str(opportunity.id)],
                'format': ['csv' + ('head' if headers else '')],
                'attempt_id': ['main'], 'max_points': ['100'],
                'import': ['Import'], 'attr_type': ['email_or_id'],
                'file': csv_file}

        # Check importing
        resp = self.c.post(reverse("relate-import_grades",
Dong Zhuang's avatar
Dong Zhuang committed
                                    args=[self.course.identifier]), data)
zwang180's avatar
zwang180 committed
        self.assertEqual(resp.status_code, 200)

        # Check number of GradeChange
Dong Zhuang's avatar
Dong Zhuang committed
        num_diff = len(csv_data) - 1 if headers else len(csv_data)
        self.assertEqual(GradeChange.objects.all().count(),
                         self.n_quiz_takers + num_diff)
    # Helper method for testing grant exceptions for new session
    def check_grant_new_exception(self, params):
zwang180's avatar
zwang180 committed
        # Grant a new one
Dong Zhuang's avatar
Dong Zhuang committed
        data = {'access_rules_tag_for_new_session': ['<<<NONE>>>'],
zwang180's avatar
zwang180 committed
                    'create_session': ['Create session']}
        resp = self.c.post(reverse("relate-grant_exception_stage_2",
Dong Zhuang's avatar
Dong Zhuang committed
                                                kwargs=params), data)
zwang180's avatar
zwang180 committed
        self.assertEqual(resp.status_code, 200)

    # Helper method for testing grant exceptions for existing one
    def check_grant_exist_exception(self, session_id, parameters):
        params = parameters.copy()
        flow_session = FlowSession.objects.filter(id=session_id)[0]
        self.assertTrue(flow_session.id in self.flow_session_ids)
zwang180's avatar
zwang180 committed

        # Grant an existing one
Dong Zhuang's avatar
Dong Zhuang committed
        data = {'session': [str(flow_session.id)], 'next': ['Next \xbb']}
zwang180's avatar
zwang180 committed
        resp = self.c.post(reverse("relate-grant_exception_stage_2",
Dong Zhuang's avatar
Dong Zhuang committed
                                                kwargs=params), data)
zwang180's avatar
zwang180 committed
        self.assertEqual(resp.status_code, 302)

        # Prepare parameters
Dong Zhuang's avatar
Dong Zhuang committed
        params["session_id"] = data["session"][0]
zwang180's avatar
zwang180 committed
        # Check redirect
        self.assertEqual(resp.url, reverse("relate-grant_exception_stage_3",
                                                                kwargs=params))

        # Check stage three page
        resp = self.c.get(reverse("relate-grant_exception_stage_3",
                                                                kwargs=params))
        self.assertEqual(resp.status_code, 200)

        # Create a new exception rule
Dong Zhuang's avatar
Dong Zhuang committed
        data = {'comment': ['test-rule'], 'save': ['Save'], 'view': ['on'],
zwang180's avatar
zwang180 committed
                'see_answer_after_submission': ['on'],
                'create_grading_exception': ['on'],
                'create_access_exception': ['on'],
                'access_expires': [''], 'due': [''],
                'bonus_points': ['0.0'], 'max_points': [''],
                'credit_percent': ['100.0'], 'max_points_enforced_cap': [''],
                'generates_grade': ['on'], 'see_correctness': ['on']}
        resp = self.c.post(reverse("relate-grant_exception_stage_3",
Dong Zhuang's avatar
Dong Zhuang committed
                                                kwargs=params), data)
zwang180's avatar
zwang180 committed
        self.assertEqual(resp.status_code, 302)

        # Check redirect
        self.assertEqual(resp.url, reverse("relate-grant_exception",
Dong Zhuang's avatar
Dong Zhuang committed
                                        args=[self.course.identifier]))
    # Helper method for testing reopen session
    def check_reopen_session(self, session_id, opportunity_id):
        flow_session = FlowSession.objects.filter(id=session_id)[0]
        self.assertEqual(flow_session.in_progress, False)

        # Check reopen session form
Dong Zhuang's avatar
Dong Zhuang committed
        params = {"course_identifier": self.course.identifier,
                    "opportunity_id": opportunity_id,
                    "flow_session_id": session_id}
        resp = self.c.get(reverse("relate-view_reopen_session",
                                                    kwargs=params))
        self.assertEqual(resp.status_code, 200)

        # Reopen session
Dong Zhuang's avatar
Dong Zhuang committed
        data = {'set_access_rules_tag': ['<<<NONE>>>'],
                'comment': ['test-reopen'],
                'unsubmit_pages': ['on'],
                'reopen': ['Reopen']}
        resp = self.c.post(
            reverse("relate-view_reopen_session", kwargs=params), data)

        flow_session = FlowSession.objects.filter(id=session_id)[0]
        self.assertEqual(flow_session.in_progress, True)
zwang180's avatar
zwang180 committed

    # Helper method for testing grant exception view
    def check_stage_one_and_two(self, participation):
zwang180's avatar
zwang180 committed
        # Check stage one page
Dong Zhuang's avatar
Dong Zhuang committed
        resp = self.c.get(
            reverse("relate-grant_exception", args=[self.course.identifier]))
zwang180's avatar
zwang180 committed
        self.assertEqual(resp.status_code, 200)

        # Move to stage two
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(Participation.objects.all().count(), self.n_participations)
Dong Zhuang's avatar
Dong Zhuang committed
        data = {"next": ["Next \xbb"],
                "participation": [str(participation.id)],
                "flow_id": [self.flow_id]}
zwang180's avatar
zwang180 committed
        resp = self.c.post(reverse("relate-grant_exception",
Dong Zhuang's avatar
Dong Zhuang committed
                                   args=[self.course.identifier]), data)
zwang180's avatar
zwang180 committed
        self.assertEqual(resp.status_code, 302)

        # Prepare parameters
Dong Zhuang's avatar
Dong Zhuang committed
        params = data.copy()
zwang180's avatar
zwang180 committed
        params["participation_id"] = params["participation"][0]
Dong Zhuang's avatar
Dong Zhuang committed
        params["course_identifier"] = self.course.identifier
zwang180's avatar
zwang180 committed
        params["flow_id"] = params["flow_id"][0]
        del params["next"]
        del params["participation"]
        # Check redirect
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertEqual(resp.url,
                         reverse(
                             "relate-grant_exception_stage_2", kwargs=params))
zwang180's avatar
zwang180 committed

        # Check stage two page
Dong Zhuang's avatar
Dong Zhuang committed
        resp = self.c.get(
            reverse("relate-grant_exception_stage_2", kwargs=params))
zwang180's avatar
zwang180 committed
        self.assertEqual(resp.status_code, 200)

        # Return params to reuse
        return params
Dong Zhuang's avatar
Dong Zhuang committed


class GradeTwoQuizTakerTest(GradeTestMixin, TestCase):
    @classmethod
    def setUpTestData(cls): # noqa
        super(GradeTwoQuizTakerTest, cls).setUpTestData()
        cls.do_quiz(cls.instructor_participation)
        cls.n_quiz_takers = 2
        cls.n_participations = 3

        # Make sure the instructor is logged in after all quizes finished
        cls.c.force_login(cls.instructor_participation.user)


class GradeThreeQuizTakerTest(GradeTestMixin, TestCase):
    @classmethod
    def setUpTestData(cls): # noqa
        super(GradeThreeQuizTakerTest, cls).setUpTestData()
        cls.do_quiz(cls.ta_participation)
        cls.do_quiz(cls.instructor_participation)
        cls.n_quiz_takers = 3
        cls.n_participations = 3

        cls.c.force_login(cls.instructor_participation.user)