Newer
Older
from __future__ import annotations
__doc__ = """
This is testing uncovering part of course.models by other tests
"""
__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 datetime import datetime, timedelta
from zoneinfo import ZoneInfo
from django.conf import settings
from django.core.exceptions import ValidationError
from course.constants import participation_permission as pperm
from course.content import dict_to_struct
from tests import factories
UTC = ZoneInfo("UTC")
class CourseTest(CoursesTestMixinBase, unittest.TestCase):
def setUp(self):
self.course1 = factories.CourseFactory()
self.course2 = factories.CourseFactory(identifier="another-course")
def test_get_absolute_url(self):
self.assertEqual(
self.course1.get_absolute_url(),
self.get_course_page_url(self.course1.identifier)
)
self.assertEqual(
self.course2.get_absolute_url(),
self.get_course_page_url(self.course2.identifier)
)
def test_get_from_email(self):
with override_settings(RELATE_EMAIL_SMTP_ALLOW_NONAUTHORIZED_SENDER=True):
self.assertEqual(self.course1.get_from_email(), self.course1.from_email)
self.assertEqual(self.course2.get_from_email(), self.course2.from_email)
notification_email_from = "expected_email_from@example.com"
robot_email_from = "robot@example.com"
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
with override_settings(
RELATE_EMAIL_SMTP_ALLOW_NONAUTHORIZED_SENDER=False,
NOTIFICATION_EMAIL_FROM=notification_email_from,
ROBOT_EMAIL_FROM=robot_email_from):
self.assertEqual(self.course1.get_from_email(), notification_email_from)
self.assertEqual(self.course2.get_from_email(), notification_email_from)
del settings.NOTIFICATION_EMAIL_FROM
self.assertEqual(self.course1.get_from_email(), robot_email_from)
self.assertEqual(self.course2.get_from_email(), robot_email_from)
def test_get_reply_to_email(self):
with override_settings(RELATE_EMAIL_SMTP_ALLOW_NONAUTHORIZED_SENDER=True):
self.assertEqual(
self.course1.get_reply_to_email(), self.course1.from_email)
self.assertEqual(
self.course2.get_reply_to_email(), self.course2.from_email)
with override_settings(
RELATE_EMAIL_SMTP_ALLOW_NONAUTHORIZED_SENDER=False):
self.assertEqual(
self.course1.get_reply_to_email(), self.course1.notify_email)
self.assertEqual(
self.course2.get_reply_to_email(), self.course2.notify_email)
def test_add_default_roles_and_permissions(self):
# make sure add_default_roles_and_permissions is called after
# a course is created, and not called when updated.
with mock.patch(
"course.models.add_default_roles_and_permissions"
) as mock_add_default_roles_and_permissions:
new_course = factories.CourseFactory(identifier="yet-another-course")
self.assertEqual(mock_add_default_roles_and_permissions.call_count, 1)
self.assertIn(
new_course, mock_add_default_roles_and_permissions.call_args[0])
mock_add_default_roles_and_permissions.reset_mock()
new_course.is_hidden = False
new_course.save()
self.assertEqual(mock_add_default_roles_and_permissions.call_count, 0)
def setUp(self):
self.course = factories.CourseFactory()
class EventTest(RelateModelTestMixin, unittest.TestCase):
def test_unicode(self):
event1 = factories.EventFactory(course=self.course, kind="my_event",
ordinal=None)
event2 = factories.EventFactory(course=self.course, kind="my_event2",
ordinal=None)
self.assertNotEqual(str(event1), str(event2))
event3 = factories.EventFactory(course=self.course, kind="my_event3",
ordinal=1)
event4 = factories.EventFactory(course=self.course, kind="my_event3",
ordinal=2)
self.assertNotEqual(str(event3), str(event4))
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
def test_validate_kind_with_spaces(self):
with self.assertRaises(ValidationError) as cm:
factories.EventFactory(course=self.course, kind="my event")
expected_error_msg = (
"Should be lower_case_with_underscores, no spaces "
"allowed.")
self.assertIn(expected_error_msg, cm.exception.message_dict["kind"])
def test_validate_kind_with_upper_case(self):
with self.assertRaises(ValidationError) as cm:
factories.EventFactory(course=self.course, kind="myEvent")
expected_error_msg = (
"Should be lower_case_with_underscores, no spaces "
"allowed.")
self.assertIn(expected_error_msg, cm.exception.message_dict["kind"])
def test_validate_kind_with_hyphen(self):
with self.assertRaises(ValidationError) as cm:
factories.EventFactory(course=self.course, kind="my-event")
expected_error_msg = (
"Should be lower_case_with_underscores, no spaces "
"allowed.")
self.assertIn(expected_error_msg, cm.exception.message_dict["kind"])
def test_clean_end_time(self):
now_dt = now()
with self.assertRaises(ValidationError) as cm:
factories.EventFactory(
course=self.course, time=now_dt, kind="some_kind",
end_time=now_dt - timedelta(seconds=1))
expected_error_msg = "End time must not be ahead of start time."
self.assertIn(expected_error_msg, cm.exception.message_dict["end_time"])
# make sure end_time >= time is valid
factories.EventFactory(
course=self.course, time=now(), kind="some_kind",
end_time=now())
factories.EventFactory(
course=self.course, time=now(), kind="some_kind",
end_time=now() + timedelta(seconds=1))
def test_event_with_no_ordinal_uniqueness(self):
kwargs = {"course": self.course, "time": now(),
"kind": "some_kind", "ordinal": None}
event = factories.EventFactory(**kwargs)
# make sure it can be updated
event.time = now() - timedelta(days=1)
event.save()
from django.core.exceptions import ValidationError
with self.assertRaises(ValidationError):
factories.EventFactory(**kwargs)
class ParticipationTagTest(RelateModelTestMixin, unittest.TestCase):
def test_unicode(self):
tag1 = factories.ParticipationTagFactory(course=self.course, name="tag1")
tag2 = factories.ParticipationTagFactory(course=self.course, name="tag2")
self.assertNotEqual(str(tag1), str(tag2))
def test_clean_success(self):
tag = models.ParticipationTag(course=self.course, name="abcd")
tag.clean()
tag = models.ParticipationTag(course=self.course, name="tag_1")
tag.clean()
tag = models.ParticipationTag(course=self.course, name="标签")
tag.clean()
def test_clean_failure(self):
tag = models.ParticipationTag(course=self.course, name="~abcd")
expected_error_msg = "'name' contains invalid characters."
with self.assertRaises(ValidationError) as cm:
tag.clean()
self.assertIn(expected_error_msg, cm.exception.message_dict["name"])
tag = models.ParticipationTag(course=self.course, name="ab-cd")
with self.assertRaises(ValidationError) as cm:
tag.clean()
self.assertIn(expected_error_msg, cm.exception.message_dict["name"])
class ParticipationRoleTest(RelateModelTestMixin, unittest.TestCase):
def test_unicode(self):
tag1 = factories.ParticipationRoleFactory(course=self.course,
identifier="ta")
tag2 = factories.ParticipationRoleFactory(course=self.course,
identifier="stu")
self.assertNotEqual(str(tag1), str(tag2))
def test_clean_success(self):
role = models.ParticipationRole(
course=self.course, name="role 1", identifier="role1")
role.clean()
role = models.ParticipationRole(
course=self.course, name="role 1", identifier="role_1")
role.clean()
role = models.ParticipationRole(
course=self.course, name="role 1", identifier="指导老师")
role.clean()
def test_clean_failure(self):
role = models.ParticipationRole(
course=self.course, name="role 1", identifier="role 1")
expected_error_msg = "'identifier' contains invalid characters."
with self.assertRaises(ValidationError) as cm:
role.clean()
self.assertIn(expected_error_msg, cm.exception.message_dict["identifier"])
role = models.ParticipationRole(
course=self.course, name="role 1", identifier="role-1")
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
with self.assertRaises(ValidationError) as cm:
role.clean()
self.assertIn(expected_error_msg, cm.exception.message_dict["identifier"])
def test_has_permission(self):
student_pr = models.ParticipationRole.objects.get(
course=self.course, identifier="student")
self.assertFalse(
student_pr.has_permission(pperm.access_files_for, "ta"))
self.assertTrue(
student_pr.has_permission(pperm.access_files_for, "student"))
self.assertFalse(
student_pr.has_permission(pperm.view_gradebook))
instructor_pr = models.ParticipationRole.objects.get(
course=self.course, identifier="instructor")
self.assertTrue(
instructor_pr.has_permission(pperm.access_files_for, "instructor"))
self.assertTrue(
instructor_pr.has_permission(pperm.view_gradebook))
def test_permission_tuples_cached(self):
student_pr = models.ParticipationRole.objects.get(
course=self.course, identifier="student")
self.assertFalse(
student_pr.has_permission(pperm.access_files_for, "ta"))
with mock.patch(
"course.models.ParticipationRolePermission.objects.filter"
) as mock_filter:
self.assertFalse(
student_pr.has_permission(pperm.access_files_for, "instructor"))
self.assertTrue(
student_pr.has_permission(pperm.access_files_for, "unenrolled"))
self.assertEqual(
mock_filter.call_count, 0,
"permission_tuples is expected to be cached.")
class ParticipationRolePermissionTest(RelateModelTestMixin, unittest.TestCase):
def test_unicode(self):
all_objects = models.ParticipationRolePermission.objects.all()
count = all_objects.count()
self.assertGreater(count, 0)
prp_unicode_list = []
for prp in all_objects:
self.assertNotIn(
str(prp), prp_unicode_list,
"ParticipationRolePermission objects are expected to "
"have different stringified result")
prp_unicode_list.append(str(prp))
class ParticipationTest(RelateModelTestMixin, unittest.TestCase):
def test_unicode(self):
course2 = factories.CourseFactory(identifier="another-course")
user = factories.UserFactory()
participation1 = factories.ParticipationFactory(course=self.course,
user=user)
participation2 = factories.ParticipationFactory(course=course2,
user=user)
self.assertNotEqual(str(participation1), str(participation2))
def test_has_permission(self):
user = factories.UserFactory()
participation = factories.ParticipationFactory(course=self.course,
user=user)
self.assertTrue(
participation.has_permission(pperm.access_files_for, "unenrolled"))
self.assertFalse(
participation.has_permission(pperm.view_gradebook))
instructor = factories.UserFactory()
instructor_role = factories.ParticipationRoleFactory(
course=self.course,
identifier="instructor"
)
instructor_participation = factories.ParticipationFactory(
course=self.course,
user=instructor)
instructor_participation.roles.set([instructor_role])
self.assertTrue(
participation.has_permission(pperm.access_files_for, "unenrolled"))
self.assertTrue(
participation.has_permission(pperm.access_files_for, "student"))
def test_get_role_desc(self):
course2 = factories.CourseFactory(identifier="another-course")
user = factories.UserFactory()
participation1 = factories.ParticipationFactory(course=self.course,
user=user)
participation2 = factories.ParticipationFactory(course=course2,
user=user)
self.assertIsInstance(participation1.get_role_desc(), str)
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
self.assertEqual(
participation1.get_role_desc(), participation2.get_role_desc())
instructor_role = factories.ParticipationRoleFactory(
course=self.course,
identifier="instructor"
)
participation2.roles.set([instructor_role])
self.assertNotEqual(
participation1.get_role_desc(), participation2.get_role_desc())
def test_permission_cached(self):
user = factories.UserFactory()
participation = factories.ParticipationFactory(course=self.course,
user=user)
self.assertTrue(
participation.has_permission(pperm.access_files_for, "unenrolled"))
with mock.patch(
"course.models.ParticipationRolePermission.objects.filter"
) as mock_filter:
self.assertFalse(
participation.has_permission(pperm.view_gradebook))
self.assertEqual(
mock_filter.call_count, 0,
"participation permissions is expected to be cached.")
class ParticipationPreapprovalTest(RelateModelTestMixin, unittest.TestCase):
def test_unicode(self):
paprv1 = factories.ParticipationPreapprovalFactory(
course=self.course, institutional_id=None
)
paprv2 = factories.ParticipationPreapprovalFactory(
course=self.course, institutional_id=None
)
self.assertNotEqual(str(paprv1), str(paprv2))
paprv3 = factories.ParticipationPreapprovalFactory(
course=self.course, email=None
)
paprv4 = factories.ParticipationPreapprovalFactory(
course=self.course, email=None
)
self.assertNotEqual(str(paprv3), str(paprv4))
paprv5 = factories.ParticipationPreapprovalFactory(
course=self.course, email=None, institutional_id=None,
)
paprv6 = factories.ParticipationPreapprovalFactory(
course=self.course, email=None, institutional_id=None,
)
self.assertNotEqual(str(paprv5), str(paprv6))
class AuthenticationTokenTest(RelateModelTestMixin, unittest.TestCase):
def test_unicode(self):
participation1 = factories.ParticipationFactory(
course=self.course,
user=factories.UserFactory())
participation2 = factories.ParticipationFactory(
course=self.course,
user=factories.UserFactory())
token1 = factories.AuthenticationTokenFactory(
participation=participation1)
token2 = factories.AuthenticationTokenFactory(
participation=participation2)
self.assertNotEqual(str(token1), str(token2))
class InstantFlowRequestTest(RelateModelTestMixin, unittest.TestCase):
def test_unicode(self):
ifr1 = factories.InstantFlowRequestFactory(course=self.course)
ifr2 = factories.InstantFlowRequestFactory(course=self.course,
flow_id="another-flow")
self.assertNotEqual(str(ifr1), str(ifr2))
class FlowSessionTest(RelateModelTestMixin, unittest.TestCase):
def setUp(self):
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
self.user = factories.UserFactory()
self.participation = factories.ParticipationFactory(
course=self.course,
user=self.user)
def test_unicode(self):
fs1 = factories.FlowSessionFactory(
participation=self.participation)
fs2 = factories.FlowSessionFactory(
participation=self.participation)
self.assertNotEqual(str(fs1), str(fs2))
fs3 = factories.FlowSessionFactory(
user=None,
course=self.course,
participation=None)
fs4 = factories.FlowSessionFactory(
user=None,
course=self.course,
participation=None)
self.assertNotEqual(str(fs3), str(fs4))
def test_points_percentage(self):
fs = factories.FlowSessionFactory(
participation=self.participation, points=None)
self.assertEqual(fs.points_percentage(), None)
fs = factories.FlowSessionFactory(
participation=self.participation, points=20, max_points=None)
self.assertEqual(fs.points_percentage(), None)
fs = factories.FlowSessionFactory(
participation=self.participation, points=20, max_points=20)
self.assertEqual(fs.points_percentage(), 100)
def test_last_activity(self):
fs = factories.FlowSessionFactory(participation=self.participation)
fpdata = factories.FlowPageDataFactory(flow_session=fs)
factories.FlowPageVisitFactory(
page_data=fpdata, answer=None,
visit_time=datetime(2019, 1, 1, tzinfo=UTC)
)
factories.FlowPageVisitFactory(
page_data=fpdata, answer=None,
visit_time=datetime(2019, 1, 2, tzinfo=UTC)
)
self.assertEqual(fs.last_activity(), None)
fpv = factories.FlowPageVisitFactory(
page_data=fpdata, answer={"answer": "hi"},
visit_time=datetime(2018, 12, 31, tzinfo=UTC)
)
self.assertEqual(fs.last_activity(), fpv.visit_time)
class FlowPageDataTest(RelateModelTestMixin, unittest.TestCase):
def setUp(self):
self.user = factories.UserFactory()
self.participation = factories.ParticipationFactory(
course=self.course,
user=self.user)
def test_unicode(self):
fs = factories.FlowSessionFactory(participation=self.participation)
fpdata = factories.FlowPageDataFactory(flow_session=fs)
self.assertIsNotNone(str(fpdata))
class FlowPageVisitTest(RelateModelTestMixin, unittest.TestCase):
def setUp(self):
self.user = factories.UserFactory()
self.participation = factories.ParticipationFactory(
course=self.course,
user=self.user)
fs = factories.FlowSessionFactory(participation=self.participation)
self.fpdata = factories.FlowPageDataFactory(flow_session=fs)
def test_unicode(self):
visit1 = factories.FlowPageVisitFactory(
page_data=self.fpdata, answer=None,
visit_time=datetime(2019, 1, 1, tzinfo=UTC)
)
visit2 = factories.FlowPageVisitFactory(
page_data=self.fpdata, answer=None,
visit_time=datetime(2019, 1, 2, tzinfo=UTC)
)
self.assertNotEqual(str(visit1), str(visit2))
self.assertNotIn("with answer", str(visit1))
def test_unicode_with_answer(self):
visit1 = factories.FlowPageVisitFactory(
page_data=self.fpdata, answer={"answer": "hi"},
visit_time=datetime(2019, 1, 1, tzinfo=UTC)
)
visit2 = factories.FlowPageVisitFactory(
page_data=self.fpdata, answer={"answer": "hi"},
visit_time=datetime(2019, 1, 2, tzinfo=UTC)
)
self.assertNotEqual(str(visit1), str(visit2))
self.assertIn("with answer", str(visit1))
class FlowPageVisitGradeTest(RelateModelTestMixin, unittest.TestCase):
def setUp(self):
self.user = factories.UserFactory()
self.participation = factories.ParticipationFactory(
course=self.course,
user=self.user)
fs = factories.FlowSessionFactory(participation=self.participation)
self.fpdata = factories.FlowPageDataFactory(flow_session=fs)
def test_percentage_none(self):
visit = factories.FlowPageVisitFactory(
page_data=self.fpdata, answer=None,
visit_time=datetime(2019, 1, 1, tzinfo=UTC),
)
fpvg = factories.FlowPageVisitGradeFactory(
visit=visit, correctness=None
)
self.assertEqual(fpvg.percentage(), None)
def test_percentage(self):
visit = factories.FlowPageVisitFactory(
page_data=self.fpdata, answer={"answer": "hi"},
visit_time=datetime(2019, 1, 1, tzinfo=UTC),
)
fpvg = factories.FlowPageVisitGradeFactory(
visit=visit, correctness=0.5
)
self.assertEqual(fpvg.percentage(), 50)
def test_uniqueness(self):
visit = factories.FlowPageVisitFactory(
page_data=self.fpdata, answer=None,
visit_time=datetime(2019, 1, 1, tzinfo=UTC),
)
factories.FlowPageVisitGradeFactory(
visit=visit
)
from django.db import IntegrityError
with self.assertRaises(IntegrityError):
factories.FlowPageVisitGradeFactory(
visit=visit
)
def test_unicode(self):
visit = factories.FlowPageVisitFactory(
page_data=self.fpdata, answer=None,
visit_time=datetime(2019, 1, 1, tzinfo=UTC),
)
visit2 = factories.FlowPageVisitFactory(
page_data=self.fpdata, answer=None,
visit_time=datetime(2019, 1, 2, tzinfo=UTC),
)
fpvg = factories.FlowPageVisitGradeFactory(
visit=visit
)
fpvg2 = factories.FlowPageVisitGradeFactory(
visit=visit2
)
self.assertEqual(fpvg.percentage(), None)
self.assertNotEqual(str(fpvg), str(fpvg2))
class GetFeedbackForGradeTest(RelateModelTestMixin, unittest.TestCase):
# test models.get_feedback_for_grade
def test_grade_is_none(self):
self.assertIsNone(models.get_feedback_for_grade(None))
class FlowRuleExceptionTest(RelateModelTestMixin, TestCase):
def setUp(self):
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
user = factories.UserFactory()
self.participation = factories.ParticipationFactory(
course=self.course,
user=user)
fake_get_course_repo = mock.patch("course.content.get_course_repo")
self.mock_get_course_repo = fake_get_course_repo.start()
self.mock_get_course_repo.return_value = mock.MagicMock()
self.addCleanup(fake_get_course_repo.stop)
fake_get_flow_desc = mock.patch("course.content.get_flow_desc")
self.mock_get_flow_desc = fake_get_flow_desc.start()
self.addCleanup(fake_get_flow_desc.stop)
fake_validate_session_start_rule = mock.patch(
"course.validation.validate_session_start_rule")
self.mock_validate_session_start_rule = (
fake_validate_session_start_rule.start())
self.addCleanup(fake_validate_session_start_rule.stop)
fake_validate_session_access_rule = mock.patch(
"course.validation.validate_session_access_rule")
self.mock_validate_session_access_rule = (
fake_validate_session_access_rule.start())
self.addCleanup(fake_validate_session_access_rule.stop)
fake_validate_session_grading_rule = mock.patch(
"course.validation.validate_session_grading_rule")
self.mock_validate_session_grading_rule = (
fake_validate_session_grading_rule.start())
self.addCleanup(fake_validate_session_grading_rule.stop)
def test_unicode(self):
fre1 = factories.FlowRuleExceptionFactory(
participation=self.participation)
fre2 = factories.FlowRuleExceptionFactory(
participation=self.participation)
self.assertNotEqual(str(fre1), str(fre2))
def test_clean_success_null_exception_rule(self):
fre = models.FlowRuleException(
flow_id=factories.DEFAULT_FLOW_ID,
participation=self.participation,
kind=constants.flow_rule_kind.start,
rule=rule,
expiration=None
)
fre.clean()
self.assertEqual(self.mock_get_course_repo.call_count, 1)
self.assertEqual(self.mock_get_flow_desc.call_count, 1)
self.assertEqual(self.mock_validate_session_start_rule.call_count, 1)
def test_clean_failure_with_invalid_existing_session_rules(self):
fre = models.FlowRuleException(
flow_id=factories.DEFAULT_FLOW_ID,
participation=self.participation,
kind=constants.flow_rule_kind.start,
rule=rule,
expiration=None
)
from course import validation
my_custom_error = "my custom error"
self.mock_validate_session_start_rule.side_effect = (
validation.ValidationError(my_custom_error))
with self.assertRaises(ValidationError) as cm:
fre.clean()
expected_error_msg = f"invalid existing_session_rules: {my_custom_error}"
self.assertIn(expected_error_msg, str(cm.exception))
self.assertEqual(self.mock_get_course_repo.call_count, 1)
self.assertEqual(self.mock_get_flow_desc.call_count, 1)
self.assertEqual(self.mock_validate_session_start_rule.call_count, 1)
def test_clean_success_no_existing_rules(self):
self.mock_get_flow_desc.return_value = dict_to_struct(
{"id": "no_existing_flow"})
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
fre = models.FlowRuleException(
flow_id=factories.DEFAULT_FLOW_ID,
participation=self.participation,
kind=constants.flow_rule_kind.start,
rule=rule,
expiration=None
)
fre.clean()
self.assertEqual(self.mock_get_course_repo.call_count, 1)
self.assertEqual(self.mock_get_flow_desc.call_count, 1)
self.assertEqual(self.mock_validate_session_start_rule.call_count, 1)
def test_clean_grading_success(self):
rule = {
"if_completed_before": now(),
"credit_percent": 100
}
fre = models.FlowRuleException(
flow_id=factories.DEFAULT_FLOW_ID,
participation=self.participation,
kind=constants.flow_rule_kind.grading,
rule=rule,
expiration=None
)
fre.clean()
self.assertEqual(self.mock_get_course_repo.call_count, 1)
self.assertEqual(self.mock_get_flow_desc.call_count, 1)
self.assertEqual(self.mock_validate_session_grading_rule.call_count, 1)
def test_clean_grading_no_expire_failure(self):
rule = {
"if_completed_before": now(),
"credit_percent": 100
}
expected_error_msg = "grading rules may not expire"
with self.assertRaises(ValidationError) as cm:
fre = models.FlowRuleException(
flow_id=factories.DEFAULT_FLOW_ID,
participation=self.participation,
kind=constants.flow_rule_kind.grading,
rule=rule,
expiration=now()
)
fre.clean()
self.assertIn(expected_error_msg, str(cm.exception))
self.assertEqual(
self.mock_get_course_repo.call_count, 0,
"The expensive operation should be skipped in this case")
self.assertEqual(
self.mock_get_flow_desc.call_count, 0,
"The expensive operation should be skipped in this case")
self.assertEqual(self.mock_validate_session_grading_rule.call_count, 0,
"The expensive operation should be skipped in this case")
def test_clean_access_success(self):
rule = {
"if_before": now()
}
fre = models.FlowRuleException(
flow_id=factories.DEFAULT_FLOW_ID,
participation=self.participation,
kind=constants.flow_rule_kind.access,
rule=rule,
expiration=now()
)
fre.clean()
self.assertEqual(self.mock_get_course_repo.call_count, 1)
self.assertEqual(self.mock_get_flow_desc.call_count, 1)
self.assertEqual(self.mock_validate_session_access_rule.call_count, 1)
def test_clean_unknown_exception_rule(self):
unknown_flow_rule_kind = "unknown_kind"
rule = {
"if_before": now()
}
fre = models.FlowRuleException(
flow_id=factories.DEFAULT_FLOW_ID,
participation=self.participation,
kind=unknown_flow_rule_kind,
rule=rule,
expiration=now()
)
with self.assertRaises(ValidationError) as cm:
fre.clean()
expected_error_msg = "invalid exception rule kind"
self.assertIn(expected_error_msg, str(cm.exception))
for call in (self.mock_get_course_repo,
self.mock_get_flow_desc,
self.mock_validate_session_access_rule,
self.mock_validate_session_access_rule,
self.mock_validate_session_access_rule,
self.mock_validate_session_access_rule):
self.assertEqual(
call.call_count, 0,
"The expensive operation should be skipped in this case")
class GradingChangeTest(RelateModelTestMixin, unittest.TestCase):
def setUp(self):
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
self.user = factories.UserFactory()
self.participation = factories.ParticipationFactory(
course=self.course,
user=self.user)
self.flow_session = factories.FlowSessionFactory(
participation=self.participation)
self.opportunity1 = factories.GradingOpportunityFactory(
course=self.course, identifier="gopp1"
)
self.opportunity2 = factories.GradingOpportunityFactory(
course=self.course, identifier="gopp2"
)
def test_unicode(self):
gc1 = factories.GradeChangeFactory(
opportunity=self.opportunity1, participation=self.participation)
self.assertIsNotNone(str(gc1))
def test_clean(self):
gc = factories.GradeChangeFactory(
opportunity=self.opportunity1, participation=self.participation)
gc.clean()
def test_clean_fail(self):
course2 = factories.CourseFactory(identifier="another-course")
opportunity3 = factories.GradingOpportunityFactory(
course=course2, identifier="gopp3"
)
gc = factories.GradeChangeFactory(
opportunity=opportunity3, participation=self.participation)
with self.assertRaises(ValidationError) as cm:
gc.clean()
expected_error_msg = ("Participation and opportunity must live "
"in the same course")
self.assertIn(expected_error_msg, str(cm.exception))
class InstantMessageTest(RelateModelTestMixin, unittest.TestCase):
def test_unicode(self):
user = factories.UserFactory()
participation = factories.ParticipationFactory(
course=self.course,
user=user)
im1 = factories.InstantMessageFactory(
participation=participation, text="my message")
im2 = factories.InstantMessageFactory(
participation=participation, text="my message2")
self.assertNotEqual(str(im1), str(im2))
class ExamTest(RelateModelTestMixin, unittest.TestCase):
def test_unicode(self):
exam1 = factories.ExamFactory(
course=self.course, description="hello", flow_id="exam-1")
exam2 = factories.ExamFactory(
course=self.course, description="exam", flow_id="exam-1")
self.assertNotEqual(str(exam1), str(exam2))
class ExamTicketTest(RelateModelTestMixin, unittest.TestCase):
def setUp(self):
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
self.exam = factories.ExamFactory(course=self.course)
self.user1 = factories.UserFactory()
self.participation1 = factories.ParticipationFactory(
course=self.course,
user=self.user1)
self.user2 = factories.UserFactory()
self.participation2 = factories.ParticipationFactory(
course=self.course,
user=self.user2)
def test_unicode(self):
et1 = factories.ExamTicketFactory(
exam=self.exam, participation=self.participation1, code="abcd")
et2 = factories.ExamTicketFactory(
exam=self.exam, participation=self.participation2, code="cdef")
self.assertNotEqual(str(et1), str(et2))
def test_clean(self):
et1 = models.ExamTicket(
exam=self.exam, participation=self.participation1, code="abcd")
et1.clean()
def test_clean_failure(self):
course2 = factories.CourseFactory(identifier="another-course")
participation3 = factories.ParticipationFactory(
course=course2,
user=self.user2)
et2 = models.ExamTicket(
exam=self.exam, participation=participation3, code="cdef")
with self.assertRaises(ValidationError) as cm:
et2.clean()
expected_error_msg = ("Participation and exam must live "
"in the same course")
self.assertIn(expected_error_msg, str(cm.exception))
def test_clean_no_participation(self):
et1 = models.ExamTicket(
exam=self.exam, participation=None, code="abcd")
et1.clean()