44
55from lego .apps .events import constants
66from lego .apps .events .models import Event , Registration
7+ from lego .apps .users .constants import LATE_PRESENCE_PENALTY_WEIGHT
78from lego .apps .users .models import AbakusGroup , Penalty
89from lego .utils .test_utils import BaseTestCase
910
@@ -398,6 +399,19 @@ def test_penalties_created_when_not_present(self):
398399 self .assertEqual (penalties_before , 0 )
399400 self .assertEqual (penalties_after , event .penalty_weight_on_not_present )
400401
402+ def test_penalties_created_when_late_present (self ):
403+ """Test that user gets penalties when late present"""
404+ event = Event .objects .get (title = "POOLS_WITH_REGISTRATIONS" )
405+
406+ registration = event .registrations .first ()
407+ penalties_before = registration .user .number_of_penalties ()
408+
409+ registration .set_presence (constants .PRESENCE_CHOICES .LATE )
410+
411+ penalties_after = registration .user .number_of_penalties ()
412+ self .assertEqual (penalties_before , 0 )
413+ self .assertEqual (penalties_after , LATE_PRESENCE_PENALTY_WEIGHT )
414+
401415 def test_penalties_removed_when_not_present_changes (self ):
402416 """Test that penalties for not_present gets removed when resetting presence"""
403417 event = Event .objects .get (title = "POOLS_WITH_REGISTRATIONS" )
@@ -411,8 +425,23 @@ def test_penalties_removed_when_not_present_changes(self):
411425 self .assertEqual (penalties_before , event .penalty_weight_on_not_present )
412426 self .assertEqual (penalties_after , 0 )
413427
428+ def test_penalties_removed_when_late_present_changes (self ):
429+ """Test that penalties for late presence gets removed when changing to present"""
430+ event = Event .objects .get (title = "POOLS_WITH_REGISTRATIONS" )
431+ registration = event .registrations .first ()
432+ registration .set_presence (constants .PRESENCE_CHOICES .LATE )
433+
434+ penalties_before = registration .user .number_of_penalties ()
435+ registration .set_presence (constants .PRESENCE_CHOICES .PRESENT )
436+
437+ penalties_after = registration .user .number_of_penalties ()
438+ self .assertEqual (penalties_before , LATE_PRESENCE_PENALTY_WEIGHT )
439+ self .assertEqual (penalties_after , 0 )
440+
414441 def test_only_correct_penalties_are_removed_on_presence_change (self ):
415- """Test that only penalties for given event are removed when changing presence"""
442+ """
443+ Test that only penalties of type presence for given event are removed when changing presence
444+ """
416445 event = Event .objects .get (title = "POOLS_WITH_REGISTRATIONS" )
417446 other_event = Event .objects .get (title = "POOLS_NO_REGISTRATIONS" )
418447 registration = event .registrations .first ()
@@ -447,6 +476,41 @@ def test_only_correct_penalties_are_removed_on_presence_change(self):
447476 )
448477 self .assertEqual (penalties_after , other_event .penalty_weight_on_not_present )
449478
479+ def test_only_correct_penalties_are_removed_on_presence_change_on_same_event (self ):
480+ """Test that only penalties of type presence are removed when changing presence"""
481+ event = Event .objects .get (title = "POOLS_WITH_REGISTRATIONS" )
482+ registration = event .registrations .first ()
483+
484+ registration .set_presence (constants .PRESENCE_CHOICES .NOT_PRESENT )
485+ penalties_before = registration .user .number_of_penalties ()
486+ penalties_object_before = list (registration .user .penalties .all ())
487+
488+ # Default penalty type is other
489+ Penalty .objects .create (
490+ user = registration .user ,
491+ reason = "SAME EVENT" ,
492+ weight = 2 ,
493+ source_event = event ,
494+ )
495+ penalties_during = registration .user .number_of_penalties ()
496+ penalties_objects_during = list (registration .user .penalties .all ())
497+
498+ registration .set_presence (constants .PRESENCE_CHOICES .UNKNOWN )
499+ penalties_after = registration .user .number_of_penalties ()
500+ penalties_object_after = list (registration .user .penalties .all ())
501+
502+ self .assertEqual (penalties_object_before [0 ].source_event , event )
503+ self .assertEqual (penalties_object_after [0 ].source_event , event )
504+ self .assertEqual (len (penalties_object_before ), 1 )
505+ self .assertEqual (len (penalties_objects_during ), 2 )
506+ self .assertEqual (len (penalties_object_after ), 1 )
507+ self .assertEqual (penalties_before , event .penalty_weight_on_not_present )
508+ self .assertEqual (
509+ penalties_during ,
510+ event .penalty_weight_on_not_present + event .penalty_weight_on_not_present ,
511+ )
512+ self .assertEqual (penalties_after , event .penalty_weight_on_not_present )
513+
450514 def test_able_to_register_when_not_heed_penalties_with_penalties (self ):
451515 """Test that user is able to register when heed_penalties is false and user has penalties"""
452516 event = Event .objects .get (title = "POOLS_WITH_REGISTRATIONS" )
0 commit comments