Skip to content

Commit f0f4172

Browse files
committed
add unit tests
1 parent 4aa22b1 commit f0f4172

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

kolibri/core/courses/test/test_unit_test_assignment.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,89 @@ class UnitTestAssignmentDeserializeSyncFilterTestCase(
401401
):
402402
model_class = models.UnitTestAssignment
403403
user_field_name = "activated_by_id"
404+
class PreSaveKwargsTestMixin:
405+
"""
406+
Shared tests for pre_save update_dirty_bit_to logic.
407+
Subclasses set model_class, user_field_name, and build_instance().
408+
"""
409+
410+
databases = "__all__"
411+
model_class = None
412+
user_field_name = None
413+
414+
@classmethod
415+
def setUpTestData(cls):
416+
provision_device()
417+
cls.facility = Facility.objects.create(name="TestFacility")
418+
cls.classroom = Classroom.objects.create(
419+
name="TestClassroom", parent=cls.facility
420+
)
421+
cls.coach = FacilityUser.objects.create(username="coach", facility=cls.facility)
422+
cls.course_session = models.CourseSession.objects.create(
423+
course=uuid.uuid4().hex,
424+
title="Test",
425+
collection=cls.classroom,
426+
created_by=cls.coach,
427+
)
428+
429+
def build_instance(self, with_user=True):
430+
raise NotImplementedError
431+
432+
def test_normal_save_raises_when_user_field_is_null(self):
433+
instance = self.build_instance(with_user=False)
434+
with self.assertRaises(IntegrityError):
435+
instance.save()
436+
437+
def test_deserialization_save_allows_null_user_field(self):
438+
instance = self.build_instance(with_user=False)
439+
instance.save(update_dirty_bit_to=False)
440+
instance.refresh_from_db()
441+
self.assertIsNone(getattr(instance, self.user_field_name))
442+
443+
def test_normal_save_passes_with_user_field_set(self):
444+
instance = self.build_instance(with_user=True)
445+
instance.save()
446+
instance.refresh_from_db()
447+
self.assertIsNotNone(getattr(instance, self.user_field_name))
448+
449+
450+
class CourseSessionPreSaveTestCase(PreSaveKwargsTestMixin, TestCase):
451+
model_class = models.CourseSession
452+
user_field_name = "created_by"
453+
454+
def build_instance(self, with_user=True):
455+
return models.CourseSession(
456+
course=uuid.uuid4().hex,
457+
title="Test",
458+
collection=self.classroom,
459+
created_by=self.coach if with_user else None,
460+
)
461+
462+
463+
class CourseSessionAssignmentPreSaveTestCase(PreSaveKwargsTestMixin, TestCase):
464+
model_class = models.CourseSessionAssignment
465+
user_field_name = "assigned_by"
466+
467+
def build_instance(self, with_user=True):
468+
return models.CourseSessionAssignment(
469+
course_session=self.course_session,
470+
collection=self.classroom,
471+
assigned_by=self.coach if with_user else None,
472+
)
473+
474+
475+
class UnitTestAssignmentPreSaveTestCase(PreSaveKwargsTestMixin, TestCase):
476+
model_class = models.UnitTestAssignment
477+
user_field_name = "activated_by"
478+
479+
def build_instance(self, with_user=True):
480+
return models.UnitTestAssignment(
481+
course_session=self.course_session,
482+
unit_contentnode_id=uuid.uuid4().hex,
483+
collection=self.classroom,
484+
test_type="pre",
485+
activated_by=self.coach if with_user else None,
486+
)
404487

405488

406489
class TestTypeEnumTestCase(TestCase):

0 commit comments

Comments
 (0)