1- from datetime import datetime , timezone
1+ import time
2+ from datetime import datetime , timedelta
3+ from django .utils import timezone
24from django .conf import settings
35from modeltranslation .utils import build_localized_fieldname
46
1012from api .factories .country import CountryFactory
1113from deployments .factories .molnix_tag import MolnixTagFactory
1214
13- from notifications .models import SurgeAlert , SurgeAlertType
15+ from notifications .models import SurgeAlert , SurgeAlertStatus , SurgeAlertType
1416from notifications .factories import SurgeAlertFactory
17+ from django .core .management import call_command
1518
1619
1720class 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