@@ -980,20 +980,90 @@ def test_palvelutarjotin_event_has_space_for_enrolments_returning_boolean(
980980 mock_get_event_data ,
981981):
982982 p_event = PalvelutarjotinEventFactory ()
983+ # Past occurrences
984+ OccurrenceFactory .create_batch (
985+ 4 ,
986+ p_event = p_event ,
987+ cancelled = False ,
988+ start_time = timezone .now () - timedelta (days = 2 ),
989+ end_time = timezone .now () - timedelta (days = 1 ),
990+ )
991+ # Ongoing occurrences
992+ OccurrenceFactory .create_batch (
993+ 2 ,
994+ p_event = p_event ,
995+ cancelled = False ,
996+ start_time = timezone .now () - timedelta (days = 1 ),
997+ end_time = timezone .now () + timedelta (days = 1 ),
998+ )
999+ # Future occurrences
9831000 OccurrenceFactory .create_batch (
9841001 3 ,
9851002 p_event = p_event ,
9861003 cancelled = False ,
9871004 start_time = timezone .now () + timedelta (days = 1 ),
1005+ end_time = timezone .now () + timedelta (days = 2 ),
1006+ )
1007+ assert p_event .occurrences .count () == 9
1008+ assert p_event .occurrences .filter_upcoming ().count () == 3
1009+ assert p_event .occurrences .filter_ongoing ().count () == 2
1010+ with patch (
1011+ "occurrences.models.PalvelutarjotinEvent.has_external_enrolments_system" ,
1012+ return_value = False ,
1013+ ):
1014+ with patch (
1015+ "occurrences.models.PalvelutarjotinEvent.has_internal_enrolments_system" ,
1016+ return_value = True ,
1017+ ):
1018+ with patch (
1019+ "occurrences.models.Occurrence.has_space_left"
1020+ ) as mock_has_space_left :
1021+ mock_has_space_left .return_value = False
1022+ assert p_event .has_space_for_enrolments () is False
1023+ assert mock_has_space_left .call_count == 3
1024+ with patch (
1025+ "occurrences.models.PalvelutarjotinEvent.has_external_enrolments_system" ,
1026+ return_value = False ,
1027+ ):
1028+ with patch (
1029+ "occurrences.models.PalvelutarjotinEvent.has_internal_enrolments_system" ,
1030+ return_value = True ,
1031+ ):
1032+ with patch (
1033+ "occurrences.models.Occurrence.has_space_left"
1034+ ) as mock_has_space_left :
1035+ # 2nd occurrence returns True, so result is True
1036+ # and any-call returns after 2nd round.
1037+ mock_has_space_left .side_effect = [False , True , False ]
1038+ assert p_event .has_space_for_enrolments () is True
1039+ assert mock_has_space_left .call_count == 2
1040+
1041+
1042+ @pytest .mark .django_db
1043+ def test_palvelutarjotin_event_has_space_for_enrolments_ongoing_events (
1044+ mock_get_event_data ,
1045+ ):
1046+ """Ongoing events are not considered"""
1047+ p_event = PalvelutarjotinEventFactory (enrolment_end_days = None )
1048+ # Past occurrences
1049+ OccurrenceFactory .create_batch (
1050+ 2 ,
1051+ p_event = p_event ,
1052+ cancelled = False ,
1053+ start_time = timezone .now () - timedelta (days = 2 ),
1054+ end_time = timezone .now () - timedelta (days = 1 ),
9881055 )
1056+ # Ongoing occurrences
9891057 OccurrenceFactory .create_batch (
9901058 3 ,
9911059 p_event = p_event ,
9921060 cancelled = False ,
9931061 start_time = timezone .now () - timedelta (days = 1 ),
1062+ end_time = timezone .now () + timedelta (days = 1 ),
9941063 )
995- assert p_event .occurrences .count () == 6
996- assert p_event .occurrences .filter_upcoming ().count () == 3
1064+ assert p_event .occurrences .count () == 5
1065+ assert p_event .occurrences .filter_upcoming ().count () == 0
1066+ assert p_event .occurrences .filter_ongoing ().count () == 3
9971067 with patch (
9981068 "occurrences.models.PalvelutarjotinEvent.has_external_enrolments_system" ,
9991069 return_value = False ,
0 commit comments