Skip to content
test_analytics.py 17.7 KiB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
from __future__ import division

__copyright__ = "Copyright (C) 2018 Dong Zhuang"

__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.test import TestCase
from django.core.exceptions import ObjectDoesNotExist
from django.urls import reverse

from relate.utils import dict_to_struct

from course.models import FlowSession
from course import analytics


from tests.base_test_mixins import (  # noqa
    SingleCourseTestMixin, CoursesTestMixinBase, SingleCoursePageTestMixin,
    SingleCourseQuizPageTestMixin, MockAddMessageMixing, HackRepoMixin)
from tests.utils import mock
from tests import factories


class FlowListTest(SingleCourseTestMixin, TestCase):
    """test analytics.flow_list"""
    def get_flow_list_url(self, course_identifier=None):
        course_identifier = course_identifier or self.get_default_course_identifier()
        kwargs = {"course_identifier": course_identifier}
        return reverse("relate-flow_list", kwargs=kwargs)

    def get_flow_list_view(self, course_identifier=None,
                           force_login_instructor=True):
        course_identifier = course_identifier or self.get_default_course_identifier()
        if not force_login_instructor:
            user = self.get_logged_in_user()
        else:
            user = self.instructor_participation.user

        with self.temporarily_switch_to_user(user):
            return self.c.get(
                self.get_flow_list_url(course_identifier))

    def test_not_authenticated(self):
        with self.temporarily_switch_to_user(None):
            resp = self.get_flow_list_view(force_login_instructor=False)
            self.assertEqual(resp.status_code, 302)

    def test_no_pperm(self):
        with self.temporarily_switch_to_user(self.student_participation.user):
            resp = self.get_flow_list_view(force_login_instructor=False)
            self.assertEqual(resp.status_code, 403)

    def test_result(self):
        flow_ids = ["c", "b", "a"]

        for flow_id in flow_ids:
            factories.FlowSessionFactory.create_batch(
                size=2,
                participation=self.student_participation,
                flow_id=flow_id)

        another_course = factories.CourseFactory(identifier="another-course")
        another_participation = factories.ParticipationFactory(course=another_course)

        # This make sure other courses' flow_id won't be included
        factories.FlowSessionFactory(participation=another_participation,
                                     flow_id="d")

        resp = self.get_flow_list_view()
        self.assertEqual(resp.status_code, 200)

        self.assertResponseContextEqual(resp, "flow_ids", sorted(flow_ids))


class HistogramTest(CoursesTestMixinBase, TestCase):
    """test analytics.Histogram (for cases not covered by other tests)"""
    def test_add_data_point_num_max_value(self):
        """self.num_max_value is not None and value > self.num_max_value"""
        his = analytics.Histogram(num_max_value=100)
        his.add_data_point(value=110, weight=1.1)
        self.assertEqual(his.string_weights["(value greater than max)"], 1.1)

    def test_add_data_point_num_min_value(self):
        """self.num_min_value is not None and value < self.num_min_value"""
        his = analytics.Histogram(num_min_value=60)
        his.add_data_point(value=50, weight=1.2)
        self.assertEqual(his.string_weights["(value smaller than min)"], 1.2)

    def test_get_bin_info_list_num_bin_starts_is_not_none(self):
        # just make sure it works
        his = analytics.Histogram(num_bin_starts=[2])
        his.get_bin_info_list()

    def test_get_bin_info_nothing_configured(self):
        # just make sure it works
        his = analytics.Histogram()
        his.get_bin_info_list()
        his.html()
        self.assertTemplateUsed("course/histogram-wide.html")

    def test_non_wide_html_template_render(self):
        his = analytics.Histogram()
        faked_get_bin_info_list = [analytics.BinInfo(
            title="foo"*30, raw_weight="bar", percentage=100)]
        with mock.patch(
                "course.analytics.Histogram.get_bin_info_list"
        ) as mock_get_bin_info_list:
            mock_get_bin_info_list.return_value = faked_get_bin_info_list

            his.html()
            self.assertTemplateUsed("course/histogram.html")


class IsFlowMultipleSubmitTest(SingleCourseTestMixin, TestCase):
    """test course.analytics.is_flow_multiple_submit"""
    def test_flow_desc_has_no_rule(self):
        flow_desc = self.get_hacked_flow_desc(del_rules=True)
        self.assertFalse(analytics.is_flow_multiple_submit(flow_desc))

    def test_flow_desc_access_rule_has_no_change_answer_perm(self):
        flow_desc_dict = self.get_hacked_flow_desc(as_dict=True)
        rules = flow_desc_dict["rules"]
        rules.access = [dict_to_struct(
            {"permissions": ["submit_answer"]})]
        flow_desc = dict_to_struct(flow_desc_dict)
        self.assertFalse(analytics.is_flow_multiple_submit(flow_desc))

    def test_flow_desc_access_rule_has_change_answer_perm(self):
        flow_desc_dict = self.get_hacked_flow_desc(as_dict=True)
        rules = flow_desc_dict["rules"]
        rules.access = [dict_to_struct(
            {"permissions": ["submit_answer", "change_answer"]})]
        flow_desc = dict_to_struct(flow_desc_dict)
        self.assertTrue(analytics.is_flow_multiple_submit(flow_desc))


class IsPageMultipleSubmitTest(SingleCoursePageTestMixin, HackRepoMixin, TestCase):
    """test course.analytics.is_page_multiple_submit"""
    @classmethod
    def setUpTestData(cls):  # noqa
        super(IsPageMultipleSubmitTest, cls).setUpTestData()
        cls.course.active_git_commit_sha = "my_fake_commit_sha_for_page_analytics"
        cls.course.save()

        # cache the page_descs
        cls.start_flow(cls.flow_id)
        cls.flow_desc = cls.get_hacked_flow_desc()

    def setUp(self):
        super(IsPageMultipleSubmitTest, self).setUp()
        faked_is_flow_multiple_submit = \
            mock.patch("course.analytics.is_flow_multiple_submit")
        self.mock_is_flow_multiple_submit = faked_is_flow_multiple_submit.start()
        self.mock_is_flow_multiple_submit.return_value = False
        self.addCleanup(faked_is_flow_multiple_submit.stop)

    def get_page_desc_by_page_id(self, page_id):
        for group_desc in self.flow_desc.groups:
            for page_desc in group_desc.pages:
                if page_desc.id == page_id:
                    return page_desc

    def test_page_has_no_access_rules(self):
        page_id = "fear"
        page_desc = self.get_page_desc_by_page_id(page_id)

        self.assertFalse(analytics.is_page_multiple_submit(
            self.flow_desc, page_desc))

        self.mock_is_flow_multiple_submit.return_value = True
        self.assertTrue(analytics.is_page_multiple_submit(
            self.flow_desc, page_desc))

    def test_page_access_rules_remove_permissions_not_remove_change_permission(self):
        page_id = "half"
        page_desc = self.get_page_desc_by_page_id(page_id)

        self.mock_is_flow_multiple_submit.return_value = True
        self.assertTrue(analytics.is_page_multiple_submit(
            self.flow_desc, page_desc))

    def test_page_access_rules_remove_permissions_removed_change_permission(self):
        page_id = "ice_cream_toppings"
        page_desc = self.get_page_desc_by_page_id(page_id)

        self.mock_is_flow_multiple_submit.return_value = True
        self.assertFalse(analytics.is_page_multiple_submit(
            self.flow_desc, page_desc))

    def test_page_access_rules_add_permissions_not_add_change_permission(self):
        page_id = "lsq"
        page_desc = self.get_page_desc_by_page_id(page_id)
        self.assertFalse(analytics.is_page_multiple_submit(
            self.flow_desc, page_desc))

    def test_page_access_rules_add_permissions_added_change_permission(self):
        page_id = "krylov"
        page_desc = self.get_page_desc_by_page_id(page_id)
        self.assertTrue(analytics.is_page_multiple_submit(
            self.flow_desc, page_desc))

    def test_page_access_rules_neither_add_nor_remove_permission(self):
        page_id = "age_group"
        page_desc = self.get_page_desc_by_page_id(page_id)
        self.assertFalse(analytics.is_page_multiple_submit(
            self.flow_desc, page_desc))

        self.mock_is_flow_multiple_submit.return_value = True
        page_desc = self.get_page_desc_by_page_id(page_id)
        self.assertTrue(analytics.is_page_multiple_submit(
            self.flow_desc, page_desc))


class PageAnalyticsTest(SingleCourseTestMixin, TestCase):
    """test analytics.page_analytics, (for cases not covered by other tests)"""
    def test_not_authenticated(self):
        with self.temporarily_switch_to_user(None):
            resp = self.get_flow_page_analytics(
                flow_id="blabla", group_id="foo", page_id="bar")
            self.assertEqual(resp.status_code, 302)

    def test_no_pperm(self):
        # student user is logged in
        resp = self.get_flow_page_analytics(
            flow_id="blabla", group_id="foo", page_id="bar")
        self.assertEqual(resp.status_code, 403)


class FlowAnalyticsTest(SingleCourseQuizPageTestMixin, HackRepoMixin,
                        MockAddMessageMixing, TestCase):
    """analytics.flow_analytics"""

    @classmethod
    def setUpTestData(cls):  # noqa
        super(FlowAnalyticsTest, cls).setUpTestData()
        cls.course.active_git_commit_sha = "my_fake_commit_sha_for_flow_analytics"
        cls.course.save()
        cls.start_flow(cls.flow_id)
        fs = FlowSession.objects.last()
        for page_ordinal in range(fs.page_count):
            cls.submit_page_answer_by_ordinal_and_test(
                page_ordinal=page_ordinal, do_grading=False, do_human_grade=False)
        cls.end_flow()

        # start another in-progress session with answers
        cls.start_flow(cls.flow_id)
        fs = FlowSession.objects.last()
        for page_ordinal in range(fs.page_count):
            cls.submit_page_answer_by_ordinal_and_test(
                page_ordinal=page_ordinal, do_grading=False, do_human_grade=False)

        # start another ended session with out answers
        cls.start_flow(cls.flow_id)
        cls.end_flow()

        # create another participation and with a flow session
        another_partcpt = factories.ParticipationFactory(course=cls.course)
        with cls.temporarily_switch_to_user(another_partcpt.user):
            cls.start_flow(cls.flow_id)
            cls.end_flow()

    def get_flow_analytics_url(self, flow_id, course_identifier,
                               restrict_to_first_attempt=None):
        course_identifier = course_identifier or self.get_default_course_identifier()
        kwargs = {
            "flow_id": flow_id,
            "course_identifier": course_identifier}
        result = reverse("relate-flow_analytics", kwargs=kwargs)
        if restrict_to_first_attempt:
            result += "?restrict_to_first_attempt=%s" % restrict_to_first_attempt
        return result

    def get_flow_analytics_view(self, flow_id, course_identifier=None,
                                restrict_to_first_attempt=None,
                                force_login_instructor=True):
        course_identifier = course_identifier or self.get_default_course_identifier()
        if not force_login_instructor:
            user = self.get_logged_in_user()
        else:
            user = self.instructor_participation.user

        with self.temporarily_switch_to_user(user):
            return self.c.get(
                self.get_flow_analytics_url(
                    flow_id, course_identifier=course_identifier,
                    restrict_to_first_attempt=restrict_to_first_attempt))

    def test_not_authenticated(self):
        with self.temporarily_switch_to_user(None):
            resp = self.get_flow_analytics_view(
                flow_id="blabla", force_login_instructor=False)
            self.assertEqual(resp.status_code, 302)

    def test_no_pperm(self):
        # student user is logged in
        resp = self.get_flow_analytics_view(
            flow_id="blabla", force_login_instructor=False)
        self.assertEqual(resp.status_code, 403)

    def test_success(self):
        resp = self.get_flow_analytics_view(flow_id=self.flow_id)
        self.assertEqual(resp.status_code, 200)
        self.assertResponseContextEqual(
            resp, "flow_identifier", self.flow_id)

        resp = self.get_flow_analytics_view(flow_id=self.flow_id,
                                            restrict_to_first_attempt=1)
        self.assertEqual(resp.status_code, 200)
        self.assertResponseContextEqual(
            resp, "flow_identifier", self.flow_id)

    def test_success_check_func_call(self):
        with mock.patch(
                "course.analytics.make_grade_histogram"
        ) as mock_make_g_his, mock.patch(
            "course.analytics.make_page_answer_stats_list"
        ) as mock_make_stats_list, mock.patch(
            "course.analytics.make_time_histogram"
        ) as mock_make_t_his, mock.patch(
            "course.analytics.count_participants"
        ) as mock_count_particpt:
            resp = self.get_flow_analytics_view(flow_id=self.flow_id)
            self.assertEqual(resp.status_code, 200)
            self.assertResponseContextEqual(
                resp, "flow_identifier", self.flow_id)
            self.assertResponseContextEqual(resp, "participant_count", 2)
            self.assertResponseContextEqual(resp, "restrict_to_first_attempt", 0)

            self.assertEqual(mock_make_g_his.call_count, 1)
            self.assertEqual(mock_make_stats_list.call_count, 1)
            self.assertEqual(mock_make_t_his.call_count, 1)
            self.assertEqual(mock_count_particpt.call_count, 1)

    def test_success_test_restrict_to_first_attempt(self):
        with mock.patch(
                "course.analytics.make_grade_histogram"
        ) as mock_make_g_his, mock.patch(
            "course.analytics.make_page_answer_stats_list"
        ) as mock_make_stats_list, mock.patch(
            "course.analytics.make_time_histogram"
        ) as mock_make_t_his, mock.patch(
            "course.analytics.count_participants"
        ) as mock_count_particpt:
            resp = self.get_flow_analytics_view(flow_id=self.flow_id,
                                                restrict_to_first_attempt=1)
            self.assertEqual(resp.status_code, 200)
            self.assertResponseContextEqual(
                resp, "flow_identifier", self.flow_id)
            self.assertResponseContextEqual(resp, "participant_count", 2)

            # make_page_answer_stats_list is called
            # using restrict_to_first_attempt = 1
            self.assertIn(1, mock_make_stats_list.call_args[0])
            self.assertResponseContextEqual(resp, "restrict_to_first_attempt", 1)

            self.assertEqual(mock_make_g_his.call_count, 1)
            self.assertEqual(mock_make_stats_list.call_count, 1)
            self.assertEqual(mock_make_t_his.call_count, 1)
            self.assertEqual(mock_count_particpt.call_count, 1)

    def test_success_test_restrict_to_first_attempt_invalid(self):
        with mock.patch(
                "course.analytics.make_grade_histogram"
        ) as mock_make_g_his, mock.patch(
            "course.analytics.make_page_answer_stats_list"
        ) as mock_make_stats_list, mock.patch(
            "course.analytics.make_time_histogram"
        ) as mock_make_t_his, mock.patch(
            "course.analytics.count_participants"
        ) as mock_count_particpt:
            resp = self.get_flow_analytics_view(flow_id=self.flow_id,
                                                restrict_to_first_attempt="foo")
            self.assertEqual(resp.status_code, 200)
            self.assertResponseContextEqual(
                resp, "flow_identifier", self.flow_id)

            # make_page_answer_stats_list is called
            # using restrict_to_first_attempt = 0
            self.assertIn(0, mock_make_stats_list.call_args[0])
            self.assertResponseContextEqual(resp, "restrict_to_first_attempt", 0)

            self.assertEqual(mock_make_g_his.call_count, 1)
            self.assertEqual(mock_make_stats_list.call_count, 1)
            self.assertEqual(mock_make_t_his.call_count, 1)
            self.assertEqual(mock_count_particpt.call_count, 1)

    def test_flow_id_does_not_exist(self):
        with mock.patch(
            "course.analytics.make_page_answer_stats_list"
        ) as mock_make_stats_list:
            mock_make_stats_list.side_effect = ObjectDoesNotExist()
            resp = self.get_flow_analytics_view(flow_id=self.flow_id)
            self.assertEqual(resp.status_code, 404)
            self.assertAddMessageCalledWith(
                ("Flow '%s' was not found in the repository, but it exists in "
                    "the database--maybe it was deleted?")
                % self.flow_id)

# vim: fdm=marker