-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for the
service_health
view function
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import json | ||
from django.http import HttpRequest | ||
from django.core.cache.backends.base import InvalidCacheBackendError | ||
from mock import patch | ||
|
||
from onadata.apps.main.tests.test_base import TestBase | ||
from onadata.apps.main.views import service_health | ||
|
||
|
||
class TestServiceHealthView(TestBase): | ||
def test_service_health(self): | ||
""" | ||
Test that the `service_health` view function | ||
works as expected: | ||
1. Returns a 200 when secondary services are healthy | ||
2. Returns a 500 when a secondary service is not available | ||
""" | ||
req = HttpRequest() | ||
resp = service_health(req) | ||
|
||
self.assertEqual(resp.status_code, 200) | ||
self.assertEqual( | ||
json.loads(resp.content.decode('utf-8')), | ||
{ | ||
'default-Database': 'OK', | ||
'Cache-Service': 'OK' | ||
}) | ||
|
||
with patch('onadata.apps.main.views.cache') as cache_mock: | ||
cache_mock.set.side_effect = InvalidCacheBackendError( | ||
'Invalid cache configuration') | ||
resp = service_health(req) | ||
|
||
self.assertEqual(resp.status_code, 500) | ||
self.assertEqual( | ||
json.loads(resp.content.decode('utf-8')), | ||
{ | ||
'default-Database': 'OK', | ||
'Cache-Service': ( | ||
'Degraded state; ' | ||
'Invalid cache configuration') | ||
}) |