Skip to content

Commit 0d8cf14

Browse files
authored
Merge pull request #239 from City-of-Helsinki/KK-803-fix-event-group-subscriptions
KK-803 | Fix free spot subscription handling in event group
2 parents 3244c83 + 9e426ac commit 0d8cf14

4 files changed

Lines changed: 55 additions & 9 deletions

File tree

events/models.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,12 @@ def save(self, *args, **kwargs):
493493
super().save(*args, **kwargs)
494494

495495
if created:
496+
event_group = self.occurrence.event.event_group
497+
events = (
498+
event_group.events.all() if event_group else [self.occurrence.event]
499+
)
496500
self.child.free_spot_notification_subscriptions.filter(
497-
occurrence__event=self.occurrence.event
501+
occurrence__event__in=events
498502
).delete()
499503

500504
if created:

subscriptions/tests/test_models.py

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
import pytest
44
from django.utils.timezone import now
55
from subscriptions.factories import FreeSpotNotificationSubscriptionFactory
6-
from subscriptions.tests.utils import assert_subscriptions
6+
from subscriptions.tests.utils import assert_child_has_subscriptions
77

8-
from events.factories import EnrolmentFactory, EventFactory, OccurrenceFactory
8+
from events.factories import (
9+
EnrolmentFactory,
10+
EventFactory,
11+
EventGroupFactory,
12+
OccurrenceFactory,
13+
)
914

1015

1116
@pytest.mark.django_db
@@ -22,6 +27,43 @@ def test_enrolling_deletes_same_event_free_spot_subscriptions(
2227
) # same event subscription
2328
other_event_subscription = FreeSpotNotificationSubscriptionFactory(child=child)
2429

30+
# create an enrolment for the child, subscriptions should be updated accordingly
2531
EnrolmentFactory(occurrence=occurrence_2, child=child)
2632

27-
assert_subscriptions(child, other_event_subscription)
33+
assert_child_has_subscriptions(child, other_event_subscription)
34+
35+
36+
@pytest.mark.django_db
37+
def test_enrolling_deletes_same_event_group_free_spot_subscriptions(child):
38+
event_group = EventGroupFactory(published_at=now())
39+
event = EventFactory(published_at=now(), event_group=event_group)
40+
same_group_event = EventFactory(published_at=now(), event_group=event_group)
41+
other_group_event = EventFactory(
42+
published_at=now(), event_group=EventGroupFactory()
43+
)
44+
45+
occurrence = OccurrenceFactory.create(time=now() + timedelta(days=1), event=event)
46+
same_event_occurrence = OccurrenceFactory.create(
47+
time=now() + timedelta(days=2), event=event
48+
)
49+
same_group_occurrence = OccurrenceFactory(
50+
time=now() + timedelta(days=3), event=same_group_event
51+
)
52+
other_group_occurrence = OccurrenceFactory(
53+
time=now() + timedelta(days=4), event=other_group_event
54+
)
55+
56+
same_event_subscription = FreeSpotNotificationSubscriptionFactory( # noqa: F841
57+
child=child, occurrence=same_event_occurrence
58+
)
59+
same_group_subscription = FreeSpotNotificationSubscriptionFactory( # noqa: F841
60+
child=child, occurrence=same_group_occurrence
61+
)
62+
other_group_subscription = FreeSpotNotificationSubscriptionFactory(
63+
child=child, occurrence=other_group_occurrence
64+
)
65+
66+
# create an enrolment for the child, subscriptions should be updated accordingly
67+
EnrolmentFactory(child=child, occurrence=occurrence)
68+
69+
assert_child_has_subscriptions(child, other_group_subscription)

subscriptions/tests/test_notifications.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from django.core import mail
55
from django.utils.timezone import now
66
from subscriptions.factories import FreeSpotNotificationSubscriptionFactory
7-
from subscriptions.tests.utils import assert_subscriptions
7+
from subscriptions.tests.utils import assert_child_has_subscriptions
88

99
from common.tests.utils import assert_mails_match_snapshot
1010
from events.factories import EnrolmentFactory, OccurrenceFactory
@@ -42,7 +42,7 @@ def test_free_spot_notification_occurrence_capacity_changes(
4242

4343
assert len(mail.outbox) == 1
4444
assert_mails_match_snapshot(snapshot)
45-
assert_subscriptions(child, other_subscription)
45+
assert_child_has_subscriptions(child, other_subscription)
4646

4747

4848
@pytest.mark.django_db
@@ -76,7 +76,7 @@ def test_free_spot_notification_event_capacity_changes(
7676

7777
assert len(mail.outbox) == 1
7878
assert_mails_match_snapshot(snapshot)
79-
assert_subscriptions(child, other_subscription)
79+
assert_child_has_subscriptions(child, other_subscription)
8080

8181

8282
@pytest.mark.django_db
@@ -103,4 +103,4 @@ def test_free_spot_notification_someone_unenrols(
103103

104104
assert len(mail.outbox) == 1
105105
assert_mails_match_snapshot(snapshot)
106-
assert_subscriptions(child, other_subscription)
106+
assert_child_has_subscriptions(child, other_subscription)

subscriptions/tests/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from collections import Iterable
22

33

4-
def assert_subscriptions(child, subscriptions):
4+
def assert_child_has_subscriptions(child, subscriptions):
55
if not isinstance(subscriptions, Iterable):
66
subscriptions = {subscriptions} if subscriptions else {}
77
subscription_ids = {s.pk for s in child.free_spot_notification_subscriptions.all()}

0 commit comments

Comments
 (0)