From c7a28edac25660c3fad86e2092f15319fb710144 Mon Sep 17 00:00:00 2001 From: Mitsuo Takaki Date: Fri, 29 Apr 2016 23:49:51 -0700 Subject: [PATCH] Adding Agent unit test and asserting calls to logging. --- tests/test_configuration.py | 8 +++++++- tests/test_scheduler.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 tests/test_scheduler.py diff --git a/tests/test_configuration.py b/tests/test_configuration.py index 8e03952..a59c8ed 100644 --- a/tests/test_configuration.py +++ b/tests/test_configuration.py @@ -4,9 +4,9 @@ import unittest import sys 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): self.configuration = Configuration('config.yml') @@ -38,6 +38,7 @@ class ConfigurationTest(unittest.TestCase): return 0.1 def request(method, url, timeout=None): response = mock.Mock() + # We are expecting a 200 response, so this will fail the expectation. response.status_code = 400 response.elapsed = mock.Mock() response.elapsed.total_seconds = total_seconds @@ -60,6 +61,7 @@ class ConfigurationTest(unittest.TestCase): self.configuration.evaluate() assert self.configuration.status == 3 + sys.modules['logging'].warning.assert_called_with('Request timed out') def test_evaluate_with_connection_error(self): def request(method, url, timeout=None): @@ -73,6 +75,8 @@ class ConfigurationTest(unittest.TestCase): self.configuration.evaluate() assert self.configuration.status == 3 + sys.modules['logging'].warning.assert_called_with(('The URL is ' + 'unreachable: GET http://localhost:8080/swagger')) def test_evaluate_with_http_error(self): def request(method, url, timeout=None): @@ -86,6 +90,8 @@ class ConfigurationTest(unittest.TestCase): self.configuration.evaluate() assert self.configuration.status == 3 + sys.modules['logging'].exception.assert_called_with(('Unexpected HTTP ' + 'response')) def test_push_status_and_metrics(self): def put(url, params=None, headers=None): diff --git a/tests/test_scheduler.py b/tests/test_scheduler.py new file mode 100644 index 0000000..e57e43a --- /dev/null +++ b/tests/test_scheduler.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python +import mock +import unittest +import sys +sys.modules['schedule'] = mock.Mock() +from cachet_url_monitor.scheduler import Agent + + +class AgentTest(unittest.TestCase): + def setUp(self): + self.configuration = mock.Mock() + self.agent = Agent(self.configuration) + + def test_init(self): + assert self.agent.configuration == self.configuration + + def test_execute(self): + evaluate = self.configuration.evaluate + push_status_and_metrics = self.configuration.push_status_and_metrics + self.agent.execute() + + evaluate.assert_called_once() + push_status_and_metrics.assert_called_once() + + def test_start(self): + every = sys.modules['schedule'].every + self.configuration.data = {'frequency': 5} + + self.agent.start() + + every.assert_called_with(5)