Skip to content

Commit

Permalink
Add tests for the service_health view function
Browse files Browse the repository at this point in the history
  • Loading branch information
DavisRayM committed Jun 2, 2021
1 parent 4fda6a9 commit 1a9a3d3
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions onadata/apps/main/tests/test_service_health.py
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')
})

0 comments on commit 1a9a3d3

Please sign in to comment.