mirror of
https://github.com/mtan93/cachet-url-monitor.git
synced 2026-03-08 05:31:58 +00:00
Fixing failing test. Using @mock.path decorator, instead of overriding the sys.modules.
This commit is contained in:
@@ -1,20 +1,33 @@
|
||||
#!/usr/bin/env python
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
import cachet_url_monitor.status
|
||||
import mock
|
||||
import unittest
|
||||
import sys
|
||||
from requests import ConnectionError,HTTPError,Timeout
|
||||
from requests import ConnectionError, HTTPError, Timeout
|
||||
|
||||
sys.modules['requests'] = mock.Mock()
|
||||
sys.modules['logging'] = mock.Mock()
|
||||
from cachet_url_monitor.configuration import Configuration
|
||||
|
||||
|
||||
class ConfigurationTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
def getLogger(name):
|
||||
self.mock_logger = mock.Mock()
|
||||
return self.mock_logger
|
||||
|
||||
sys.modules['logging'].getLogger = getLogger
|
||||
|
||||
def get(url, headers):
|
||||
get_return = mock.Mock()
|
||||
get_return.ok = True
|
||||
get_return.json = mock.Mock()
|
||||
get_return.json.return_value = {'data': {'status': 1}}
|
||||
return get_return
|
||||
|
||||
sys.modules['requests'].get = get
|
||||
|
||||
self.configuration = Configuration('config.yml')
|
||||
sys.modules['requests'].Timeout = Timeout
|
||||
sys.modules['requests'].ConnectionError = ConnectionError
|
||||
@@ -27,6 +40,7 @@ class ConfigurationTest(unittest.TestCase):
|
||||
def test_evaluate(self):
|
||||
def total_seconds():
|
||||
return 0.1
|
||||
|
||||
def request(method, url, timeout=None):
|
||||
response = mock.Mock()
|
||||
response.status_code = 200
|
||||
@@ -43,6 +57,7 @@ class ConfigurationTest(unittest.TestCase):
|
||||
def test_evaluate_with_failure(self):
|
||||
def total_seconds():
|
||||
return 0.1
|
||||
|
||||
def request(method, url, timeout=None):
|
||||
response = mock.Mock()
|
||||
# We are expecting a 200 response, so this will fail the expectation.
|
||||
@@ -84,7 +99,7 @@ class ConfigurationTest(unittest.TestCase):
|
||||
|
||||
assert self.configuration.status == 3
|
||||
self.mock_logger.warning.assert_called_with(('The URL is '
|
||||
'unreachable: GET http://localhost:8080/swagger'))
|
||||
'unreachable: GET http://localhost:8080/swagger'))
|
||||
|
||||
def test_evaluate_with_http_error(self):
|
||||
def request(method, url, timeout=None):
|
||||
@@ -99,7 +114,7 @@ class ConfigurationTest(unittest.TestCase):
|
||||
|
||||
assert self.configuration.status == 3
|
||||
self.mock_logger.exception.assert_called_with(('Unexpected HTTP '
|
||||
'response'))
|
||||
'response'))
|
||||
|
||||
def test_push_status(self):
|
||||
def put(url, params=None, headers=None):
|
||||
|
||||
Reference in New Issue
Block a user