Skip to content

Commit fdaad42

Browse files
committed
Add test cases and code refactor
1 parent 2d2d2b7 commit fdaad42

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

notifications/management/commands/update_alert_status.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ def handle(self, *args, **options):
2020
try:
2121
SurgeAlert.objects.update(
2222
status=models.Case(
23-
models.When(closes__gte=now, then=models.Value(SurgeAlertStatus.OPEN)),
24-
models.When(closes__lt=now, then=models.Value(SurgeAlertStatus.CLOSED)),
2523
models.When(is_stood_down=True, then=models.Value(SurgeAlertStatus.STOOD_DOWN)),
24+
models.When(closes__lt=now, then=models.Value(SurgeAlertStatus.CLOSED)),
25+
models.When(closes__gte=now, then=models.Value(SurgeAlertStatus.OPEN)),
2626
default=models.F('status'),
2727
output_field=models.IntegerField()
2828
)

notifications/tests.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from datetime import datetime, timezone
1+
import time
2+
from datetime import datetime, timedelta
3+
from django.utils import timezone
24
from django.conf import settings
35
from modeltranslation.utils import build_localized_fieldname
46

@@ -10,8 +12,9 @@
1012
from api.factories.country import CountryFactory
1113
from deployments.factories.molnix_tag import MolnixTagFactory
1214

13-
from notifications.models import SurgeAlert, SurgeAlertType
15+
from notifications.models import SurgeAlert, SurgeAlertStatus, SurgeAlertType
1416
from notifications.factories import SurgeAlertFactory
17+
from django.core.management import call_command
1518

1619

1720
class NotificationTestCase(APITestCase):
@@ -136,3 +139,40 @@ def _to_csv(*items):
136139
molnix_tag_names=_to_csv('OP-6700', 'L-FRA', 'AMER'),
137140
))
138141
self.assertEqual(response['count'], 2)
142+
143+
class SurgeAlertTestCase(APITestCase):
144+
def test_update_alert_status_command(self):
145+
region_1, region_2 = RegionFactory.create_batch(2)
146+
country_1 = CountryFactory.create(iso3='NPP', region=region_1)
147+
country_2 = CountryFactory.create(iso3='CTT', region=region_2)
148+
149+
molnix_tag_1 = MolnixTagFactory.create(name='OP-6700')
150+
molnix_tag_2 = MolnixTagFactory.create(name='L-FRA')
151+
molnix_tag_3 = MolnixTagFactory.create(name='AMER')
152+
153+
alert1 = SurgeAlertFactory.create(
154+
message='CEA Coordinator, Floods, Atlantis',
155+
country=country_1,
156+
molnix_tags=[molnix_tag_1, molnix_tag_2],
157+
opens = timezone.now() - timedelta(days=2),
158+
closes = timezone.now() + timedelta(seconds=5)
159+
)
160+
alert2 = SurgeAlertFactory.create(
161+
message='WASH Coordinator, Earthquake, Neptunus',
162+
country=country_2,
163+
molnix_tags=[molnix_tag_1, molnix_tag_3],
164+
opens = timezone.now() - timedelta(days=2),
165+
closes = timezone.now() - timedelta(days=1)
166+
167+
)
168+
self.assertEqual(alert1.status, SurgeAlertStatus.OPEN)
169+
self.assertEqual(alert2.status, SurgeAlertStatus.CLOSED)
170+
171+
time.sleep(10)
172+
call_command('update_alert_status')
173+
174+
alert1.refresh_from_db()
175+
alert2.refresh_from_db()
176+
177+
self.assertEqual(alert1.status, SurgeAlertStatus.CLOSED)
178+
self.assertEqual(alert2.status, SurgeAlertStatus.CLOSED)

0 commit comments

Comments
 (0)