Skip to content

Commit

Permalink
Fixing failing test. Using @mock.path decorator, instead of overridin…
Browse files Browse the repository at this point in the history
…g the sys.modules.
  • Loading branch information
mtakaki committed May 19, 2016
1 parent ca358ea commit a83abfd
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
2 changes: 1 addition & 1 deletion config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
endpoint:
url: http://localhost:8080/swagger
method: GET
timeout: 0.1
timeout: 0.01
expectation:
- type: HTTP_STATUS
status: 200
Expand Down
25 changes: 20 additions & 5 deletions tests/test_configuration.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -27,6 +40,7 @@ def test_init(self):
def test_evaluate(self):
def total_seconds():
return 0.1

def request(method, url, timeout=None):
response = mock.Mock()
response.status_code = 200
Expand All @@ -43,6 +57,7 @@ def request(method, url, timeout=None):
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.
Expand Down Expand Up @@ -84,7 +99,7 @@ def request(method, url, timeout=None):

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):
Expand All @@ -99,7 +114,7 @@ def request(method, url, timeout=None):

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):
Expand Down
16 changes: 10 additions & 6 deletions tests/test_expectation.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env python
import unittest

import mock
import re
import unittest
from cachet_url_monitor.configuration import Expectaction,Latency
from cachet_url_monitor.configuration import HttpStatus,Regex
from cachet_url_monitor.configuration import HttpStatus, Regex
from cachet_url_monitor.configuration import Latency


class LatencyTest(unittest.TestCase):
Expand All @@ -16,6 +17,7 @@ def test_init(self):
def test_get_status_healthy(self):
def total_seconds():
return 0.1

request = mock.Mock()
elapsed = mock.Mock()
request.elapsed = elapsed
Expand All @@ -26,6 +28,7 @@ def total_seconds():
def test_get_status_unhealthy(self):
def total_seconds():
return 2

request = mock.Mock()
elapsed = mock.Mock()
request.elapsed = elapsed
Expand All @@ -36,13 +39,14 @@ def total_seconds():
def test_get_message(self):
def total_seconds():
return 0.1

request = mock.Mock()
elapsed = mock.Mock()
request.elapsed = elapsed
elapsed.total_seconds = total_seconds

assert self.expectation.get_message(request) == ('Latency above '
'threshold: 0.1000 seconds')
'threshold: 0.1000 seconds')


class HttpStatusTest(unittest.TestCase):
Expand All @@ -69,7 +73,7 @@ def test_get_message(self):
request.status_code = 400

assert self.expectation.get_message(request) == ('Unexpected HTTP '
'status (400)')
'status (400)')


class RegexTest(unittest.TestCase):
Expand All @@ -96,4 +100,4 @@ def test_get_message(self):
request.text = 'We will not find it here'

assert self.expectation.get_message(request) == ('Regex did not match '
'anything in the body')
'anything in the body')
14 changes: 4 additions & 10 deletions tests/test_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import mock

sys.modules['schedule'] = mock.Mock()
# sys.modules['cachet_url_monitor.configuration.Configuration'] = mock.Mock()
sys.modules['requests'] = mock.Mock()
from cachet_url_monitor.scheduler import Agent, Scheduler


Expand Down Expand Up @@ -36,28 +34,24 @@ def test_start(self):


class SchedulerTest(unittest.TestCase):
@mock.patch('cachet_url_monitor.configuration.Configuration.__init__', mock.Mock(return_value=None))
@mock.patch('cachet_url_monitor.configuration.Configuration.is_create_incident', mock.Mock(return_value=False))
def setUp(self):
@mock.patch('requests.get')
def setUp(self, mock_requests):
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
mock_requests.get = get

self.scheduler = Scheduler('config.yml')

def test_init(self):
assert self.scheduler.stop == False

@mock.patch('cachet_url_monitor.configuration.Configuration', create=True)
def test_start(self, mock_configuration):
def test_start(self):
# TODO(mtakaki|2016-05-01): We need a better way of testing this method.
# Leaving it as a placeholder.
mock_configuration.data = {'frequency': 30}

self.scheduler.stop = True
self.scheduler.start()

0 comments on commit a83abfd

Please sign in to comment.