Commit 6f1bccd5 by Jesse Zoldak

Mock the modulestore for the heartbeat tests

parent a2dd3208
""" """
Test the heartbeat Test the heartbeat
""" """
from django.test.client import Client
from django.core.urlresolvers import reverse
import json import json
from django.core.urlresolvers import reverse
from django.db.utils import DatabaseError from django.db.utils import DatabaseError
import mock from django.test.client import Client
from django.test.testcases import TestCase from django.test.testcases import TestCase
from mock import patch
from xmodule.exceptions import HeartbeatFailure
@patch('heartbeat.views.modulestore')
class HeartbeatTestCase(TestCase): class HeartbeatTestCase(TestCase):
""" """
Test the heartbeat Test the heartbeat
...@@ -19,22 +23,19 @@ class HeartbeatTestCase(TestCase): ...@@ -19,22 +23,19 @@ class HeartbeatTestCase(TestCase):
self.heartbeat_url = reverse('heartbeat') self.heartbeat_url = reverse('heartbeat')
return super(HeartbeatTestCase, self).setUp() return super(HeartbeatTestCase, self).setUp()
def tearDown(self): def test_success(self, mock_modulestore): # pylint: disable=unused-argument
return super(HeartbeatTestCase, self).tearDown()
def test_success(self):
response = self.client.get(self.heartbeat_url) response = self.client.get(self.heartbeat_url)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
def test_sql_fail(self): @patch('heartbeat.views.connection')
with mock.patch('heartbeat.views.connection') as mock_connection: def test_sql_fail(self, mock_connection, mock_modulestore): # pylint: disable=unused-argument
mock_connection.cursor.return_value.execute.side_effect = DatabaseError mock_connection.cursor.return_value.execute.side_effect = DatabaseError
response = self.client.get(self.heartbeat_url) response = self.client.get(self.heartbeat_url)
self.assertEqual(response.status_code, 503) self.assertEqual(response.status_code, 503)
response_dict = json.loads(response.content) response_dict = json.loads(response.content)
self.assertIn('SQL', response_dict) self.assertIn('SQL', response_dict)
def test_mongo_fail(self): def test_modulestore_fail(self, mock_modulestore): # pylint: disable=unused-argument
with mock.patch('pymongo.MongoClient.alive', return_value=False): mock_modulestore.return_value.heartbeat.side_effect = HeartbeatFailure('msg', 'service')
response = self.client.get(self.heartbeat_url) response = self.client.get(self.heartbeat_url)
self.assertEqual(response.status_code, 503) self.assertEqual(response.status_code, 503)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment