|
5 | 5 | from django.utils.timezone import now |
6 | 6 |
|
7 | 7 | from children.factories import ChildFactory |
| 8 | +from projects.factories import ProjectFactory |
8 | 9 | from projects.models import Project |
9 | 10 | from venues.models import Venue |
10 | 11 |
|
11 | | -from ..factories import EnrolmentFactory, EventFactory, OccurrenceFactory |
12 | | -from ..models import Enrolment, Event, Occurrence |
| 12 | +from ..factories import ( |
| 13 | + EnrolmentFactory, |
| 14 | + EventFactory, |
| 15 | + EventGroupFactory, |
| 16 | + OccurrenceFactory, |
| 17 | +) |
| 18 | +from ..models import Enrolment, Event, EventGroup, Occurrence |
13 | 19 |
|
14 | 20 | User = get_user_model() |
15 | 21 |
|
@@ -61,3 +67,213 @@ def test_enrolment_reference_id(): |
61 | 67 | for enrolment in enrolments: |
62 | 68 | assert len(enrolment.reference_id) == settings.KUKKUU_HASHID_MIN_LENGTH |
63 | 69 | assert Enrolment.decode_reference_id(enrolment.reference_id) == enrolment.id |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.django_db |
| 73 | +@pytest.mark.parametrize( |
| 74 | + "has_enrolled,can_child_enroll", [(True, False), (False, True)] |
| 75 | +) |
| 76 | +def test_event_group_can_child_enroll_already_enrolled( |
| 77 | + has_enrolled, can_child_enroll, child_with_random_guardian, future |
| 78 | +): |
| 79 | + """Enrolment shouldn't be allowed since child has enrolled to a different event |
| 80 | + in the same event group. |
| 81 | + """ |
| 82 | + event_group = EventGroupFactory( |
| 83 | + name="Event group with one of two events enrolled", published_at=now() |
| 84 | + ) |
| 85 | + OccurrenceFactory( |
| 86 | + time=future, |
| 87 | + event__published_at=now(), |
| 88 | + event__event_group=event_group, |
| 89 | + ) |
| 90 | + enrolled_occurrence = OccurrenceFactory( |
| 91 | + time=future, |
| 92 | + event__published_at=now(), |
| 93 | + event__event_group=event_group, |
| 94 | + ) |
| 95 | + if has_enrolled: |
| 96 | + EnrolmentFactory( |
| 97 | + child=child_with_random_guardian, occurrence=enrolled_occurrence |
| 98 | + ) |
| 99 | + assert event_group.can_child_enroll(child_with_random_guardian) is can_child_enroll |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.django_db |
| 103 | +@pytest.mark.parametrize( |
| 104 | + "is_published,can_child_enroll", |
| 105 | + [(True, True), (False, False)], |
| 106 | +) |
| 107 | +def test_event_group_can_child_enroll_unpublished( |
| 108 | + is_published, can_child_enroll, child_with_random_guardian, future |
| 109 | +): |
| 110 | + """Enrolment shouldn't be allowed since the event group is unpublished""" |
| 111 | + event_group = EventGroupFactory( |
| 112 | + name="Event group with an occurrence but might not be published yet", |
| 113 | + published_at=now() if is_published else None, |
| 114 | + ) |
| 115 | + OccurrenceFactory( |
| 116 | + time=future, |
| 117 | + event__published_at=now(), |
| 118 | + event__event_group=event_group, |
| 119 | + ) |
| 120 | + assert event_group.can_child_enroll(child_with_random_guardian) is can_child_enroll |
| 121 | + |
| 122 | + |
| 123 | +@pytest.mark.django_db |
| 124 | +def test_event_group_can_child_enroll_no_occurrence(child_with_random_guardian): |
| 125 | + event_group = EventGroupFactory( |
| 126 | + name="Event group without any occurrences", published_at=now() |
| 127 | + ) |
| 128 | + assert event_group.can_child_enroll(child_with_random_guardian) is False |
| 129 | + |
| 130 | + |
| 131 | +@pytest.mark.django_db |
| 132 | +@pytest.mark.parametrize( |
| 133 | + "enrolment_limit,can_child_enroll", |
| 134 | + [(3, True), (2, False), (1, False), (0, False)], |
| 135 | +) |
| 136 | +def test_event_group_can_child_enroll_project_limit_reached( |
| 137 | + enrolment_limit, can_child_enroll, child_with_random_guardian, future |
| 138 | +): |
| 139 | + """ |
| 140 | + Enrolment shouldn't be possible when event group enrolment limit is reached. |
| 141 | + If the child's enrolments count in this year is lower |
| 142 | + than the limit set in the child's project, enrolment should be possible. |
| 143 | + """ |
| 144 | + project = ProjectFactory(enrolment_limit=enrolment_limit) |
| 145 | + child_with_random_guardian.project = project |
| 146 | + child_with_random_guardian.save() |
| 147 | + |
| 148 | + for _ in range(2): |
| 149 | + enrolled_event_group = EventGroupFactory( |
| 150 | + name="Event group where the child has already enrolled", |
| 151 | + published_at=now(), |
| 152 | + project=project, |
| 153 | + ) |
| 154 | + enrolled_occurrence = OccurrenceFactory( |
| 155 | + time=future, |
| 156 | + event__published_at=now(), |
| 157 | + event__event_group=enrolled_event_group, |
| 158 | + ) |
| 159 | + EnrolmentFactory( |
| 160 | + child=child_with_random_guardian, occurrence=enrolled_occurrence |
| 161 | + ) |
| 162 | + |
| 163 | + assert EventGroup.objects.count() == 2 |
| 164 | + assert Enrolment.objects.count() == 2 |
| 165 | + event_group = EventGroupFactory( |
| 166 | + name="Event group with an occurrence", published_at=now(), project=project |
| 167 | + ) |
| 168 | + OccurrenceFactory( |
| 169 | + time=future, |
| 170 | + event__published_at=now(), |
| 171 | + event__event_group=event_group, |
| 172 | + ) |
| 173 | + assert event_group.can_child_enroll(child_with_random_guardian) is can_child_enroll |
| 174 | + |
| 175 | + |
| 176 | +@pytest.mark.django_db |
| 177 | +@pytest.mark.parametrize( |
| 178 | + "is_published,can_child_enroll", |
| 179 | + [(True, True), (False, False)], |
| 180 | +) |
| 181 | +def test_event_can_child_enroll_unpublished( |
| 182 | + is_published, can_child_enroll, child_with_random_guardian, future |
| 183 | +): |
| 184 | + """Enrolment shouldn't be allowed since the event is unpublished""" |
| 185 | + event_group = EventGroupFactory( |
| 186 | + published_at=now(), |
| 187 | + ) |
| 188 | + occurrence = OccurrenceFactory( |
| 189 | + time=future, |
| 190 | + event__published_at=now() if is_published else None, |
| 191 | + event__event_group=event_group, |
| 192 | + ) |
| 193 | + assert ( |
| 194 | + occurrence.event.can_child_enroll(child_with_random_guardian) |
| 195 | + is can_child_enroll |
| 196 | + ) |
| 197 | + |
| 198 | + |
| 199 | +@pytest.mark.django_db |
| 200 | +def test_event_can_child_enroll_no_occurrence(child_with_random_guardian): |
| 201 | + """Enrolment shouldn't be allowed since it has no occurrences""" |
| 202 | + event_group = EventGroupFactory( |
| 203 | + published_at=now(), |
| 204 | + ) |
| 205 | + event = EventFactory(published_at=now(), event_group=event_group) |
| 206 | + assert event.can_child_enroll(child_with_random_guardian) is False |
| 207 | + |
| 208 | + |
| 209 | +@pytest.mark.django_db |
| 210 | +def test_event_can_child_enroll_event_group_unenrollable(child_with_random_guardian): |
| 211 | + """Enrolment shouldn't be allowed since the event group is rejected""" |
| 212 | + event_group = EventGroupFactory( |
| 213 | + name="Unpublished Event Group", |
| 214 | + published_at=None, |
| 215 | + ) |
| 216 | + event = EventFactory(published_at=now(), event_group=event_group) |
| 217 | + assert event.can_child_enroll(child_with_random_guardian) is False |
| 218 | + |
| 219 | + |
| 220 | +@pytest.mark.django_db |
| 221 | +@pytest.mark.parametrize( |
| 222 | + "enrolment_limit,can_child_enroll", |
| 223 | + [(3, True), (2, False), (1, False), (0, False)], |
| 224 | +) |
| 225 | +def test_event_can_child_enroll_project_limit_reached( |
| 226 | + enrolment_limit, can_child_enroll, child_with_random_guardian, future |
| 227 | +): |
| 228 | + """ |
| 229 | + Enrolment shouldn't be possible when event enrolment limit is reached. |
| 230 | + If the child's enrolments count in this year is lower |
| 231 | + than the limit set in the child's project, enrolment should be possible. |
| 232 | + """ |
| 233 | + project = ProjectFactory(enrolment_limit=enrolment_limit) |
| 234 | + child_with_random_guardian.project = project |
| 235 | + child_with_random_guardian.save() |
| 236 | + |
| 237 | + for _ in range(2): |
| 238 | + enrolled_event_group = EventGroupFactory( |
| 239 | + name="Event group where the child has already enrolled", |
| 240 | + published_at=now(), |
| 241 | + project=project, |
| 242 | + ) |
| 243 | + enrolled_occurrence = OccurrenceFactory( |
| 244 | + time=future, |
| 245 | + event__published_at=now(), |
| 246 | + event__event_group=enrolled_event_group, |
| 247 | + ) |
| 248 | + EnrolmentFactory( |
| 249 | + child=child_with_random_guardian, occurrence=enrolled_occurrence |
| 250 | + ) |
| 251 | + |
| 252 | + assert EventGroup.objects.count() == 2 |
| 253 | + assert Enrolment.objects.count() == 2 |
| 254 | + event_group = EventGroupFactory( |
| 255 | + name="Event group with an occurrence", published_at=now(), project=project |
| 256 | + ) |
| 257 | + occurrence = OccurrenceFactory( |
| 258 | + time=future, |
| 259 | + event__published_at=now(), |
| 260 | + event__event_group=event_group, |
| 261 | + ) |
| 262 | + assert ( |
| 263 | + occurrence.event.can_child_enroll(child_with_random_guardian) |
| 264 | + is can_child_enroll |
| 265 | + ) |
| 266 | + |
| 267 | + |
| 268 | +@pytest.mark.django_db |
| 269 | +def test_event_can_child_enroll_already_enrolled(child_with_random_guardian, future): |
| 270 | + event_group = EventGroupFactory(published_at=now()) |
| 271 | + enrolled_occurrence = OccurrenceFactory( |
| 272 | + time=future, |
| 273 | + event__published_at=now(), |
| 274 | + event__event_group=event_group, |
| 275 | + ) |
| 276 | + EnrolmentFactory(child=child_with_random_guardian, occurrence=enrolled_occurrence) |
| 277 | + assert ( |
| 278 | + enrolled_occurrence.event.can_child_enroll(child_with_random_guardian) is False |
| 279 | + ) |
0 commit comments