From bec66e8c8650a0d433f1ed50bf3b104dd0c07f3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ionu=C8=9B=20Ar=C8=9B=C4=83ri=C8=99i?= Date: Tue, 16 Oct 2012 17:45:58 +0200 Subject: [PATCH] use mock context managers instead of decorators+functions Change-Id: I761ee19169b39e47c4aa191b553965446432dba9 --- tests/test_http.py | 31 +++++++++++-------------------- tests/test_https.py | 26 ++++++++++---------------- 2 files changed, 21 insertions(+), 36 deletions(-) diff --git a/tests/test_http.py b/tests/test_http.py index a015793be..676cee66e 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -29,18 +29,15 @@ class ClientTest(utils.TestCase): def test_get(self): cl = get_authed_client() - @mock.patch.object(httplib2.Http, "request", mock_request) - @mock.patch('time.time', mock.Mock(return_value=1234)) - def test_get_call(): - resp, body = cl.get("/hi") - headers = {"X-Auth-Token": "token", - "User-Agent": cl.USER_AGENT} - mock_request.assert_called_with("http://127.0.0.1:5000/hi", - "GET", headers=headers) - # Automatic JSON parsing - self.assertEqual(body, {"hi": "there"}) - - test_get_call() + with mock.patch.object(httplib2.Http, "request", mock_request): + with mock.patch('time.time', mock.Mock(return_value=1234)): + resp, body = cl.get("/hi") + headers = {"X-Auth-Token": "token", + "User-Agent": cl.USER_AGENT} + mock_request.assert_called_with("http://127.0.0.1:5000/hi", + "GET", headers=headers) + # Automatic JSON parsing + self.assertEqual(body, {"hi": "there"}) def test_get_error(self): cl = get_authed_client() @@ -50,17 +47,13 @@ class ClientTest(utils.TestCase): err_mock_request = mock.Mock(return_value=(fake_err_response, fake_err_body)) - @mock.patch.object(httplib2.Http, "request", err_mock_request) - def test_get_call(): + with mock.patch.object(httplib2.Http, "request", err_mock_request): self.assertRaises(exceptions.BadRequest, cl.get, '/hi') - test_get_call() - def test_post(self): cl = get_authed_client() - @mock.patch.object(httplib2.Http, "request", mock_request) - def test_post_call(): + with mock.patch.object(httplib2.Http, "request", mock_request): cl.post("/hi", body=[1, 2, 3]) headers = { "X-Auth-Token": "token", @@ -69,5 +62,3 @@ class ClientTest(utils.TestCase): } mock_request.assert_called_with("http://127.0.0.1:5000/hi", "POST", headers=headers, body='[1, 2, 3]') - - test_post_call() diff --git a/tests/test_https.py b/tests/test_https.py index 4d433c695..a812bca14 100644 --- a/tests/test_https.py +++ b/tests/test_https.py @@ -29,24 +29,20 @@ class ClientTest(utils.TestCase): def test_get(self): cl = get_authed_client() - @mock.patch.object(httplib2.Http, "request", MOCK_REQUEST) - @mock.patch('time.time', mock.Mock(return_value=1234)) - def test_get_call(): - resp, body = cl.get("/hi") - headers = {"X-Auth-Token": "token", - "User-Agent": cl.USER_AGENT} - MOCK_REQUEST.assert_called_with("https://127.0.0.1:5000/hi", - "GET", headers=headers) - # Automatic JSON parsing - self.assertEqual(body, {"hi": "there"}) - - test_get_call() + with mock.patch.object(httplib2.Http, "request", MOCK_REQUEST): + with mock.patch('time.time', mock.Mock(return_value=1234)): + resp, body = cl.get("/hi") + headers = {"X-Auth-Token": "token", + "User-Agent": cl.USER_AGENT} + MOCK_REQUEST.assert_called_with("https://127.0.0.1:5000/hi", + "GET", headers=headers) + # Automatic JSON parsing + self.assertEqual(body, {"hi": "there"}) def test_post(self): cl = get_authed_client() - @mock.patch.object(httplib2.Http, "request", MOCK_REQUEST) - def test_post_call(): + with mock.patch.object(httplib2.Http, "request", MOCK_REQUEST): cl.post("/hi", body=[1, 2, 3]) headers = { "X-Auth-Token": "token", @@ -56,5 +52,3 @@ class ClientTest(utils.TestCase): MOCK_REQUEST.assert_called_with("https://127.0.0.1:5000/hi", "POST", headers=headers, body='[1, 2, 3]') - - test_post_call()