From ec163eb4d4d45a0ee1412634968570aa7ccec436 Mon Sep 17 00:00:00 2001 From: sneakypete81 Date: Fri, 16 Aug 2013 17:46:30 +0100 Subject: [PATCH] Add DDT support to test_http, to reduce duplication --- tests/README.markdown | 3 +- tests/unit/test_http.py | 145 +++++++++++++--------------------------- tox.ini | 2 + 3 files changed, 52 insertions(+), 98 deletions(-) diff --git a/tests/README.markdown b/tests/README.markdown index 3aa3d35..4aef0d6 100644 --- a/tests/README.markdown +++ b/tests/README.markdown @@ -12,6 +12,7 @@ They run very quickly and don't require any external test hosts. #### Requirements * mock >= 1.0.0 * httpretty >= 0.6.1 + * ddt >= 0.3.0 * tox (optional) #### Running the Unit Tests @@ -19,7 +20,7 @@ They run very quickly and don't require any external test hosts. python -m unittest discover tests/unit To run the unit tests against all supported Python versions, use ```tox```: - + tox ---------------------------------------- diff --git a/tests/unit/test_http.py b/tests/unit/test_http.py index 82df5ed..98883f1 100644 --- a/tests/unit/test_http.py +++ b/tests/unit/test_http.py @@ -3,6 +3,8 @@ import os import json import mock import httpretty +from httpretty import GET, POST +from ddt import ddt, data try: import unittest2 as unittest # Python2.6 except ImportError: @@ -10,6 +12,21 @@ except ImportError: import trovebox +class GetOrPost(object): + """Helper class to call the correct (GET/POST) method""" + def __init__(self, client, method): + self.client = client + self.method = method + + def call(self, *args, **kwds): + if self.method == GET: + return self.client.get(*args, **kwds) + elif self.method == POST: + return self.client.post(*args, **kwds) + else: + raise ValueError("unknown method: %s" % self.method) + +@ddt class TestHttp(unittest.TestCase): test_host = "test.example.com" test_endpoint = "test.json" @@ -48,14 +65,15 @@ class TestHttp(unittest.TestCase): self.assertEqual(self.client.auth.host, self.test_host) @httpretty.activate - def test_get_with_http_scheme(self): - """Check that the get method works with a host starting with 'http://'""" - self._register_uri(httpretty.GET, + @data(GET, POST) + def test_http_scheme(self, method): + """Check that we can access hosts starting with 'http://'""" + self._register_uri(method, uri="http://test.example.com/%s" % self.test_endpoint) self.client = trovebox.Trovebox(host="http://test.example.com", **self.test_oauth) - response = self.client.get(self.test_endpoint) + response = GetOrPost(self.client, method).call(self.test_endpoint) self.assertIn("OAuth", self._last_request().headers["authorization"]) self.assertEqual(response, self.test_data) self.assertEqual(self.client.last_url, @@ -63,14 +81,15 @@ class TestHttp(unittest.TestCase): self.assertEqual(self.client.last_response.json(), self.test_data) @httpretty.activate - def test_get_with_no_scheme(self): - """Check that the get method works with a host without a 'http://' prefix""" - self._register_uri(httpretty.GET, + @data(GET, POST) + def test_no_scheme(self, method): + """Check that we can access hosts without a 'http://' prefix""" + self._register_uri(method, uri="http://test.example.com/%s" % self.test_endpoint) self.client = trovebox.Trovebox(host="test.example.com", **self.test_oauth) - response = self.client.get(self.test_endpoint) + response = GetOrPost(self.client, method).call(self.test_endpoint) self.assertIn("OAuth", self._last_request().headers["authorization"]) self.assertEqual(response, self.test_data) self.assertEqual(self.client.last_url, @@ -78,59 +97,15 @@ class TestHttp(unittest.TestCase): self.assertEqual(self.client.last_response.json(), self.test_data) @httpretty.activate - def test_get_with_https_scheme(self): - """Check that the get method works with a host starting with 'https://'""" - self._register_uri(httpretty.GET, + @data(GET, POST) + def test_https_scheme(self, method): + """Check that we can access hosts starting with 'https://'""" + self._register_uri(method, uri="https://test.example.com/%s" % self.test_endpoint) self.client = trovebox.Trovebox(host="https://test.example.com", **self.test_oauth) - response = self.client.get(self.test_endpoint) - self.assertIn("OAuth", self._last_request().headers["authorization"]) - self.assertEqual(response, self.test_data) - self.assertEqual(self.client.last_url, - "https://test.example.com/%s" % self.test_endpoint) - self.assertEqual(self.client.last_response.json(), self.test_data) - - @httpretty.activate - def test_post_with_http_scheme(self): - """Check that the post method works with a host starting with 'http://'""" - self._register_uri(httpretty.POST, - uri="http://test.example.com/%s" % self.test_endpoint) - - self.client = trovebox.Trovebox(host="http://test.example.com", - **self.test_oauth) - response = self.client.post(self.test_endpoint) - self.assertIn("OAuth", self._last_request().headers["authorization"]) - self.assertEqual(response, self.test_data) - self.assertEqual(self.client.last_url, - "http://test.example.com/%s" % self.test_endpoint) - self.assertEqual(self.client.last_response.json(), self.test_data) - - @httpretty.activate - def test_post_with_no_scheme(self): - """Check that the post method works with a host without a 'http://' prefix""" - self._register_uri(httpretty.POST, - uri="http://test.example.com/%s" % self.test_endpoint) - - self.client = trovebox.Trovebox(host="test.example.com", - **self.test_oauth) - response = self.client.post(self.test_endpoint) - self.assertIn("OAuth", self._last_request().headers["authorization"]) - self.assertEqual(response, self.test_data) - self.assertEqual(self.client.last_url, - "http://test.example.com/%s" % self.test_endpoint) - self.assertEqual(self.client.last_response.json(), self.test_data) - - @httpretty.activate - def test_post_with_https_scheme(self): - """Check that the post method works with a host starting with 'https://'""" - self._register_uri(httpretty.POST, - uri="https://test.example.com/%s" % self.test_endpoint) - - self.client = trovebox.Trovebox(host="https://test.example.com", - **self.test_oauth) - response = self.client.post(self.test_endpoint) + response = GetOrPost(self.client, method).call(self.test_endpoint) self.assertIn("OAuth", self._last_request().headers["authorization"]) self.assertEqual(response, self.test_data) self.assertEqual(self.client.last_url, @@ -184,17 +159,12 @@ class TestHttp(unittest.TestCase): self.client.post(self.test_endpoint) @httpretty.activate - def test_get_without_response_processing(self): - """Check that the get method works with response processing disabled""" - self._register_uri(httpretty.GET) - response = self.client.get(self.test_endpoint, process_response=False) - self.assertEqual(response, json.dumps(self.test_data)) - - @httpretty.activate - def test_post_without_response_processing(self): - """Check that the post method works with response processing disabled""" - self._register_uri(httpretty.POST) - response = self.client.post(self.test_endpoint, process_response=False) + @data(GET, POST) + def test_no_response_processing(self, method): + """Check that get/post methods work with response processing disabled""" + self._register_uri(method) + response = GetOrPost(self.client, method).call(self.test_endpoint, + process_response=False) self.assertEqual(response, json.dumps(self.test_data)) @httpretty.activate @@ -218,49 +188,30 @@ class TestHttp(unittest.TestCase): self.assertIn(params["unicode_"], [["\xc3\xbcmlaut"], ["\xfcmlaut"]]) @httpretty.activate - def test_get_with_api_version(self): - """Check that an API version can be specified for the get method""" - self.client = trovebox.Trovebox(host=self.test_host) - self.client.configure(api_version=1) - self._register_uri(httpretty.GET, - uri="http://%s/v1/%s" % (self.test_host, - self.test_endpoint)) - self.client.get(self.test_endpoint) - - @httpretty.activate - def test_post_with_api_version(self): - """Check that an API version can be specified for the post method""" + @data(GET, POST) + def test_api_version(self, method): + """Check that an API version can be specified""" self.client = trovebox.Trovebox(host=self.test_host, **self.test_oauth) self.client.configure(api_version=1) - self._register_uri(httpretty.POST, + self._register_uri(method, uri="http://%s/v1/%s" % (self.test_host, self.test_endpoint)) - self.client.post(self.test_endpoint) + GetOrPost(self.client, method).call(self.test_endpoint) @mock.patch.object(trovebox.http.requests, 'Session') - def test_get_with_ssl_verify_disabled(self, mock_session): + @data(GET, POST) + def test_ssl_verify_disabled(self, method, mock_session): """Check that SSL verification can be disabled for the get method""" session = mock_session.return_value.__enter__.return_value session.get.return_value.text = "response text" session.get.return_value.status_code = 200 session.get.return_value.json.return_value = self.test_data + # Handle either post or get + session.post = session.get self.client = trovebox.Trovebox(host=self.test_host, **self.test_oauth) self.client.configure(ssl_verify=False) - self.client.get(self.test_endpoint) - self.assertEqual(session.verify, False) - - @mock.patch.object(trovebox.http.requests, 'Session') - def test_post_with_ssl_verify_disabled(self, mock_session): - """Check that SSL verification can be disabled for the post method""" - session = mock_session.return_value.__enter__.return_value - session.post.return_value.text = "response text" - session.post.return_value.status_code = 200 - session.post.return_value.json.return_value = self.test_data - - self.client = trovebox.Trovebox(host=self.test_host, **self.test_oauth) - self.client.configure(ssl_verify=False) - self.client.post(self.test_endpoint) + GetOrPost(self.client, method).call(self.test_endpoint) self.assertEqual(session.verify, False) @httpretty.activate diff --git a/tox.ini b/tox.ini index 2657d68..5e0c3da 100644 --- a/tox.ini +++ b/tox.ini @@ -6,11 +6,13 @@ commands = python -m unittest discover --catch tests/unit deps = mock >= 1.0.0 httpretty >= 0.6.1 + ddt >= 0.3.0 [testenv:py26] commands = unit2 discover --catch tests/unit deps = mock >= 1.0.0 httpretty >= 0.6.1 + ddt >= 0.3.0 unittest2 discover