Use freezegun for testing alert acknowledgements

This commit is contained in:
Frank Hamand 2017-03-02 10:37:40 +00:00
parent b490459925
commit c969d99e7f
2 changed files with 30 additions and 17 deletions

View File

@ -24,6 +24,7 @@ from django.core.urlresolvers import reverse
from django.test.client import Client from django.test.client import Client
from django.test.utils import override_settings from django.test.utils import override_settings
from django.utils import timezone from django.utils import timezone
from freezegun import freeze_time
from mock import Mock, patch from mock import Mock, patch
from rest_framework import status, HTTP_HEADER_ENCODING from rest_framework import status, HTTP_HEADER_ENCODING
from rest_framework.reverse import reverse as api_reverse from rest_framework.reverse import reverse as api_reverse
@ -225,6 +226,7 @@ class TestCheckRun(LocalTestCase):
@patch('cabot.cabotapp.models.send_alert') @patch('cabot.cabotapp.models.send_alert')
@patch('cabot.cabotapp.models.send_alert_update') @patch('cabot.cabotapp.models.send_alert_update')
@freeze_time('2017-03-02 10:30:43.714759')
def test_alert_acknowledgement(self, fake_send_alert_update, fake_send_alert): def test_alert_acknowledgement(self, fake_send_alert_update, fake_send_alert):
self.assertEqual(self.service.overall_status, Service.PASSING_STATUS) self.assertEqual(self.service.overall_status, Service.PASSING_STATUS)
self.most_recent_result.succeeded = False self.most_recent_result.succeeded = False
@ -235,18 +237,28 @@ class TestCheckRun(LocalTestCase):
Service.CALCULATED_FAILING_STATUS) Service.CALCULATED_FAILING_STATUS)
self.service.update_status() self.service.update_status()
fake_send_alert.assert_called_with(self.service, duty_officers=[]) fake_send_alert.assert_called_with(self.service, duty_officers=[])
fake_send_alert.reset_mock() fake_send_alert.reset_mock()
self.service.last_alert_sent = timezone.now() - timedelta(minutes=30)
self.service.update_status()
fake_send_alert.assert_called_with(self.service, duty_officers=[])
fake_send_alert.reset_mock() with freeze_time(timezone.now() + timedelta(minutes=30)):
self.service.update_status()
fake_send_alert.assert_called_with(self.service, duty_officers=[])
fake_send_alert.reset_mock()
self.service.acknowledge_alert(user=self.user) self.service.acknowledge_alert(user=self.user)
self.service.last_alert_sent = timezone.now() - timedelta(minutes=30)
self.service.update_status() self.service.update_status()
self.assertEqual(self.service.unexpired_acknowledgement().user, self.user) self.assertEqual(self.service.unexpired_acknowledgement().user, self.user)
fake_send_alert_update.assert_called_with(self.service, duty_officers=[]) self.assertFalse(fake_send_alert_update.called)
with freeze_time(timezone.now() + timedelta(minutes=60)):
self.service.update_status()
self.assertEqual(self.service.unexpired_acknowledgement(), None)
fake_send_alert.assert_called_with(self.service, duty_officers=[])
with freeze_time(timezone.now() + timedelta(minutes=90)):
self.service.acknowledge_alert(user=self.user)
self.service.update_status()
self.assertEqual(self.service.unexpired_acknowledgement().user, self.user)
fake_send_alert_update.assert_called_with(self.service, duty_officers=[])
@patch('cabot.cabotapp.graphite.requests.get', fake_graphite_response) @patch('cabot.cabotapp.graphite.requests.get', fake_graphite_response)
def test_graphite_run(self): def test_graphite_run(self):

View File

@ -1,32 +1,33 @@
Django>=1.8,<1.9
Markdown==2.5
PyJWT==0.1.2
amqp<2.0 amqp<2.0
anyjson==0.3.3 anyjson==0.3.3
argparse==1.2.1 argparse==1.2.1
celery[redis]==3.1.23 celery[redis]==3.1.23
dj-database-url==0.4.2 dj-database-url==0.4.2
django-appconf==1.0.2 django-appconf==1.0.2
django-auth-ldap==1.2.6
django-celery==3.1.17 django-celery==3.1.17
django-compressor==2.1.1 django-compressor==2.1.1
django-filter==0.13 django-filter==0.13
django-jsonify==0.3.0 django-jsonify==0.3.0
django-mptt==0.6.0 django-mptt==0.6.0
django-polymorphic==0.7.2 django-polymorphic==0.7.2
Django>=1.8,<1.9
djangorestframework==2.4.8 djangorestframework==2.4.8
gunicorn==19.6.0 freezegun==0.3.8
gevent==1.0.1 gevent==1.0.1
gunicorn==19.6.0
httplib2==0.7.7 httplib2==0.7.7
icalendar==3.2 icalendar==3.2
Markdown==2.5
ndg-httpsclient==0.4.2
psycogreen==1.0 psycogreen==1.0
psycopg2==2.5.1 psycopg2==2.5.1
pytz==2014.10
pyOpenSSL==16.1.0
ndg-httpsclient==0.4.2
pyasn1==0.1.9 pyasn1==0.1.9
PyJWT==0.1.2
pyOpenSSL==16.1.0
python-dateutil==2.1
pytz==2014.10
requests==2.7.0 requests==2.7.0
twilio==3.4.1 twilio==3.4.1
wsgiref==0.1.2
python-dateutil==2.1
django-auth-ldap==1.2.6
whitenoise==3.3.0 whitenoise==3.3.0
wsgiref==0.1.2