Skip to content

Commit 55455b3

Browse files
susilnemthenav56
authored andcommitted
Update test case
- Mock timezone.now
1 parent 5c8b03e commit 55455b3

File tree

5 files changed

+81
-15
lines changed

5 files changed

+81
-15
lines changed

deploy/helm/ifrcgo-helm/values.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ cronjobs:
111111
schedule: '0 9 * * *'
112112
- command: 'ingest_country_plan_file'
113113
schedule: '1 0 * * *'
114-
- command: 'update_alert_status'
115-
schedule: '0 */12 * * *'
114+
- command: 'update_surge_alert_status'
115+
schedule: '1 */12 * * *'
116116

117117
elasticsearch:
118118
enabled: true

notifications/management/commands/update_alert_status.py renamed to notifications/management/commands/update_surge_alert_status.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ def handle(self, *args, **options):
2828
)
2929
)
3030
except Exception as e:
31-
logger.error('Error while updating alerts status: %s' % str(e))
31+
logger.error('Error while updating alerts status', exc_info=True)

notifications/migrations/0014_surgealert_status.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Generated by Django 3.2.23 on 2024-02-13 05:20
1+
# Generated by Django 3.2.23 on 2024-02-13 09:43
22

33
from django.db import migrations, models
44

notifications/models.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,16 @@ class SurgeAlertCategory(models.IntegerChoices):
2323
SHELTER = 3, _('shelter')
2424
STAND_DOWN = 4, _('stand down')
2525

26+
2627
class SurgeAlertStatus(models.IntegerChoices):
28+
'''
29+
Note: Ordering value should be in order of Open, Stood Down, Closed to supported custom defined ordering logic
30+
'''
2731
OPEN = 0, _('Open')
2832
STOOD_DOWN = 1, _('Stood Down')
2933
CLOSED = 2, _('Closed')
3034

35+
3136
class SurgeAlert(models.Model):
3237

3338
atype = models.IntegerField(choices=SurgeAlertType.choices, verbose_name=_('alert type'), default=0)
@@ -58,19 +63,23 @@ class SurgeAlert(models.Model):
5863

5964
# Don't set `auto_now_add` so we can modify it on save
6065
created_at = models.DateTimeField(verbose_name=_('created at'))
61-
status = models.IntegerField(choices=SurgeAlertStatus.choices, verbose_name=_('alert status'), default=0)
66+
status = models.IntegerField(choices=SurgeAlertStatus.choices, verbose_name=_('alert status'), default=SurgeAlertStatus.OPEN)
6267

6368
class Meta:
6469
ordering = ['-created_at']
6570
verbose_name = _('Surge Alert')
6671
verbose_name_plural = _('Surge Alerts')
6772

6873
def save(self, *args, **kwargs):
74+
"""
75+
If the alert status is marked as stood_down, then the status is Stood Down.
76+
If the closing timestamp (closes) is earlier than the current date, the status is displayed as Closed.
77+
Otherwise, it is displayed as Open.
78+
"""
6979
# On save, if `created` is not set, make it the current time
7080
if (not self.id and not self.created_at) or (self.created_at > timezone.now()):
7181
self.created_at = timezone.now()
7282
self.is_stood_down = self.molnix_status == 'unfilled'
73-
7483
if self.is_stood_down:
7584
self.status = SurgeAlertStatus.STOOD_DOWN
7685
elif self.closes and self.closes < timezone.now():

notifications/tests.py

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import time
21
from datetime import datetime, timedelta
32
from django.utils import timezone
3+
from unittest.mock import patch
44
from django.conf import settings
55
from modeltranslation.utils import build_localized_fieldname
66

@@ -140,6 +140,63 @@ def _to_csv(*items):
140140
))
141141
self.assertEqual(response['count'], 2)
142142

143+
def test_surge_alert_status(self):
144+
region_1, region_2 = RegionFactory.create_batch(2)
145+
country_1 = CountryFactory.create(iso3='ATL', region=region_1)
146+
country_2 = CountryFactory.create(iso3='NPT', region=region_2)
147+
148+
molnix_tag_1 = MolnixTagFactory.create(name='OP-6700')
149+
molnix_tag_2 = MolnixTagFactory.create(name='L-FRA')
150+
molnix_tag_3 = MolnixTagFactory.create(name='AMER')
151+
152+
alert1 = SurgeAlertFactory.create(
153+
message='CEA Coordinator, Floods, Atlantis',
154+
country=country_1,
155+
molnix_tags=[molnix_tag_1, molnix_tag_2],
156+
opens=timezone.now() - timedelta(days=2),
157+
closes=timezone.now() + timedelta(days=5)
158+
)
159+
alert2 = SurgeAlertFactory.create(
160+
message='WASH Coordinator, Earthquake, Neptunus',
161+
country=country_2,
162+
molnix_tags=[molnix_tag_1, molnix_tag_3],
163+
opens=timezone.now() - timedelta(days=2),
164+
closes=timezone.now() - timedelta(days=1)
165+
)
166+
alert3 = SurgeAlertFactory.create(
167+
message='New One',
168+
country=country_2,
169+
molnix_tags=[molnix_tag_1, molnix_tag_3],
170+
molnix_status='unfilled',
171+
)
172+
173+
self.assertEqual(alert1.status, SurgeAlertStatus.OPEN)
174+
self.assertEqual(alert2.status, SurgeAlertStatus.CLOSED)
175+
self.assertEqual(alert3.status, SurgeAlertStatus.STOOD_DOWN)
176+
177+
def _fetch(filters):
178+
return self.client.get('/api/v2/surge_alert/', filters).json()
179+
180+
response = _fetch(dict({
181+
'status': SurgeAlertStatus.OPEN
182+
}))
183+
184+
self.assertEqual(response['count'], 1)
185+
self.assertEqual(response['results'][0]['status'], SurgeAlertStatus.OPEN)
186+
187+
response = _fetch(dict({
188+
'status': SurgeAlertStatus.CLOSED
189+
}))
190+
self.assertEqual(response['count'], 1)
191+
self.assertEqual(response['results'][0]['status'], SurgeAlertStatus.CLOSED)
192+
193+
response = _fetch(dict({
194+
'status': SurgeAlertStatus.STOOD_DOWN
195+
}))
196+
self.assertEqual(response['count'], 1)
197+
self.assertEqual(response['results'][0]['status'], SurgeAlertStatus.STOOD_DOWN)
198+
199+
143200
class SurgeAlertTestCase(APITestCase):
144201
def test_update_alert_status_command(self):
145202
region_1, region_2 = RegionFactory.create_batch(2)
@@ -154,25 +211,25 @@ def test_update_alert_status_command(self):
154211
message='CEA Coordinator, Floods, Atlantis',
155212
country=country_1,
156213
molnix_tags=[molnix_tag_1, molnix_tag_2],
157-
opens = timezone.now() - timedelta(days=2),
158-
closes = timezone.now() + timedelta(seconds=5)
214+
opens=timezone.now() - timedelta(days=2),
215+
closes=timezone.now() + timedelta(seconds=5)
159216
)
160217
alert2 = SurgeAlertFactory.create(
161218
message='WASH Coordinator, Earthquake, Neptunus',
162219
country=country_2,
163220
molnix_tags=[molnix_tag_1, molnix_tag_3],
164-
opens = timezone.now() - timedelta(days=2),
165-
closes = timezone.now() - timedelta(days=1)
166-
221+
opens=timezone.now() - timedelta(days=2),
222+
closes=timezone.now() - timedelta(days=1)
167223
)
168224
self.assertEqual(alert1.status, SurgeAlertStatus.OPEN)
169225
self.assertEqual(alert2.status, SurgeAlertStatus.CLOSED)
170226

171-
time.sleep(10)
172-
call_command('update_alert_status')
227+
with patch('django.utils.timezone.now') as mock_now:
228+
mock_now.return_value = datetime.now() + timedelta(days=1)
229+
call_command('update_surge_alert_status')
173230

174231
alert1.refresh_from_db()
175232
alert2.refresh_from_db()
176233

177234
self.assertEqual(alert1.status, SurgeAlertStatus.CLOSED)
178-
self.assertEqual(alert2.status, SurgeAlertStatus.CLOSED)
235+
self.assertEqual(alert2.status, SurgeAlertStatus.CLOSED)

0 commit comments

Comments
 (0)