Merge "use mock context managers instead of decorators+functions"

This commit is contained in:
Jenkins
2012-10-19 19:48:03 +00:00
committed by Gerrit Code Review
2 changed files with 21 additions and 36 deletions

View File

@@ -29,18 +29,15 @@ class ClientTest(utils.TestCase):
def test_get(self): def test_get(self):
cl = get_authed_client() cl = get_authed_client()
@mock.patch.object(httplib2.Http, "request", mock_request) with mock.patch.object(httplib2.Http, "request", mock_request):
@mock.patch('time.time', mock.Mock(return_value=1234)) with mock.patch('time.time', mock.Mock(return_value=1234)):
def test_get_call(): resp, body = cl.get("/hi")
resp, body = cl.get("/hi") headers = {"X-Auth-Token": "token",
headers = {"X-Auth-Token": "token", "User-Agent": cl.USER_AGENT}
"User-Agent": cl.USER_AGENT} mock_request.assert_called_with("http://127.0.0.1:5000/hi",
mock_request.assert_called_with("http://127.0.0.1:5000/hi", "GET", headers=headers)
"GET", headers=headers) # Automatic JSON parsing
# Automatic JSON parsing self.assertEqual(body, {"hi": "there"})
self.assertEqual(body, {"hi": "there"})
test_get_call()
def test_get_error(self): def test_get_error(self):
cl = get_authed_client() cl = get_authed_client()
@@ -50,17 +47,13 @@ class ClientTest(utils.TestCase):
err_mock_request = mock.Mock(return_value=(fake_err_response, err_mock_request = mock.Mock(return_value=(fake_err_response,
fake_err_body)) fake_err_body))
@mock.patch.object(httplib2.Http, "request", err_mock_request) with mock.patch.object(httplib2.Http, "request", err_mock_request):
def test_get_call():
self.assertRaises(exceptions.BadRequest, cl.get, '/hi') self.assertRaises(exceptions.BadRequest, cl.get, '/hi')
test_get_call()
def test_post(self): def test_post(self):
cl = get_authed_client() cl = get_authed_client()
@mock.patch.object(httplib2.Http, "request", mock_request) with mock.patch.object(httplib2.Http, "request", mock_request):
def test_post_call():
cl.post("/hi", body=[1, 2, 3]) cl.post("/hi", body=[1, 2, 3])
headers = { headers = {
"X-Auth-Token": "token", "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", mock_request.assert_called_with("http://127.0.0.1:5000/hi", "POST",
headers=headers, body='[1, 2, 3]') headers=headers, body='[1, 2, 3]')
test_post_call()

View File

@@ -29,24 +29,20 @@ class ClientTest(utils.TestCase):
def test_get(self): def test_get(self):
cl = get_authed_client() cl = get_authed_client()
@mock.patch.object(httplib2.Http, "request", MOCK_REQUEST) with mock.patch.object(httplib2.Http, "request", MOCK_REQUEST):
@mock.patch('time.time', mock.Mock(return_value=1234)) with mock.patch('time.time', mock.Mock(return_value=1234)):
def test_get_call(): resp, body = cl.get("/hi")
resp, body = cl.get("/hi") headers = {"X-Auth-Token": "token",
headers = {"X-Auth-Token": "token", "User-Agent": cl.USER_AGENT}
"User-Agent": cl.USER_AGENT} MOCK_REQUEST.assert_called_with("https://127.0.0.1:5000/hi",
MOCK_REQUEST.assert_called_with("https://127.0.0.1:5000/hi", "GET", headers=headers)
"GET", headers=headers) # Automatic JSON parsing
# Automatic JSON parsing self.assertEqual(body, {"hi": "there"})
self.assertEqual(body, {"hi": "there"})
test_get_call()
def test_post(self): def test_post(self):
cl = get_authed_client() cl = get_authed_client()
@mock.patch.object(httplib2.Http, "request", MOCK_REQUEST) with mock.patch.object(httplib2.Http, "request", MOCK_REQUEST):
def test_post_call():
cl.post("/hi", body=[1, 2, 3]) cl.post("/hi", body=[1, 2, 3])
headers = { headers = {
"X-Auth-Token": "token", "X-Auth-Token": "token",
@@ -56,5 +52,3 @@ class ClientTest(utils.TestCase):
MOCK_REQUEST.assert_called_with("https://127.0.0.1:5000/hi", MOCK_REQUEST.assert_called_with("https://127.0.0.1:5000/hi",
"POST", headers=headers, "POST", headers=headers,
body='[1, 2, 3]') body='[1, 2, 3]')
test_post_call()