Skip to content

Commit b8f3ad0

Browse files
committed
Convert unit tests to work with pytest
1 parent 1e50596 commit b8f3ad0

File tree

3 files changed

+26
-79
lines changed

3 files changed

+26
-79
lines changed

src/etcd/tests/unit/test_lock.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,7 @@ def test_acquired(self):
9999
"""
100100
self.locker._sequence = "4"
101101
retval = ("/_locks/test_lock/4", None)
102-
self.locker._get_locker = mock.MagicMock(
103-
spec=self.locker._get_locker, return_value=retval
104-
)
102+
self.locker._get_locker = mock.MagicMock(return_value=retval)
105103
self.assertTrue(self.locker._acquired())
106104
self.assertTrue(self.locker.is_taken)
107105
retval = ("/_locks/test_lock/1", "/_locks/test_lock/4")
@@ -125,9 +123,7 @@ def test_acquired(self):
125123
def side_effect():
126124
return returns.pop()
127125

128-
self.locker._get_locker = mock.MagicMock(
129-
spec=self.locker._get_locker, side_effect=side_effect
130-
)
126+
self.locker._get_locker = mock.MagicMock(side_effect=side_effect)
131127
self.assertTrue(self.locker._acquired())
132128

133129
def test_acquired_no_timeout(self):
@@ -136,9 +132,7 @@ def test_acquired_no_timeout(self):
136132
("/_locks/test_lock/4", None),
137133
(
138134
"/_locks/test_lock/1",
139-
etcd.EtcdResult(
140-
node={"key": "/_locks/test_lock/4", "modifiedIndex": 1}
141-
),
135+
etcd.EtcdResult(node={"key": "/_locks/test_lock/4", "modifiedIndex": 1}),
142136
),
143137
]
144138

@@ -155,9 +149,7 @@ def side_effect():
155149
}
156150
self._mock_api(200, d)
157151

158-
self.locker._get_locker = mock.create_autospec(
159-
self.locker._get_locker, side_effect=side_effect
160-
)
152+
self.locker._get_locker = mock.create_autospec(self.locker._get_locker, side_effect=side_effect)
161153
self.assertTrue(self.locker._acquired())
162154

163155
def test_lock_key(self):

src/etcd/tests/unit/test_old_request.py

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,14 @@ def test_test_and_test_failure(self):
9898

9999
client = etcd.Client()
100100
client.api_execute = mock.Mock(
101-
side_effect=ValueError(
102-
"The given PrevValue is not equal"
103-
" to the value of the key : TestAndSet: 1!=3"
104-
)
101+
side_effect=ValueError("The given PrevValue is not equal" " to the value of the key : TestAndSet: 1!=3")
105102
)
106103
try:
107104
result = client.test_and_set("/testkey", "newvalue", "test", ttl=19)
108105
except ValueError as e:
109106
# from ipdb import set_trace; set_trace()
110107
self.assertEqual(
111-
"The given PrevValue is not equal"
112-
" to the value of the key : TestAndSet: 1!=3",
108+
"The given PrevValue is not equal" " to the value of the key : TestAndSet: 1!=3",
113109
str(e),
114110
)
115111

@@ -149,11 +145,7 @@ def test_get(self):
149145
client.api_execute = mock.Mock(
150146
return_value=FakeHTTPResponse(
151147
200,
152-
'{"action":"GET",'
153-
'"node": {'
154-
'"key":"/testkey",'
155-
'"value":"test",'
156-
'"modifiedIndex":190}}',
148+
'{"action":"GET",' '"node": {' '"key":"/testkey",' '"value":"test",' '"modifiedIndex":190}}',
157149
)
158150
)
159151

@@ -189,11 +181,7 @@ def test_in(self):
189181
client.api_execute = mock.Mock(
190182
return_value=FakeHTTPResponse(
191183
200,
192-
'{"action":"GET",'
193-
'"node": {'
194-
'"key":"/testkey",'
195-
'"value":"test",'
196-
'"modifiedIndex":190}}',
184+
'{"action":"GET",' '"node": {' '"key":"/testkey",' '"value":"test",' '"modifiedIndex":190}}',
197185
)
198186
)
199187
result = "/testkey" in client
@@ -306,7 +294,7 @@ def test_eternal_watch(self):
306294
)
307295
for result in range(1, 5):
308296
result = next(client.eternal_watch("/testkey", index=180))
309-
yield self.check_watch, result
297+
self.check_watch(result)
310298

311299

312300
class TestClientApiExecutor(unittest.TestCase):
@@ -408,6 +396,4 @@ def test_get_error_invalid(self):
408396
client = etcd.Client()
409397
response = FakeHTTPResponse(status=400, data="{){){)*garbage*")
410398
client.http.request = mock.Mock(return_value=response)
411-
self.assertRaises(
412-
etcd.EtcdException, client.api_execute, "/v2/keys/testkey", client._MGET
413-
)
399+
self.assertRaises(etcd.EtcdException, client.api_execute, "/v2/keys/testkey", client._MGET)

src/etcd/tests/unit/test_request.py

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,15 @@ class TestClientApiInterface(TestClientApiBase):
6868
If a test should be run only in this class, please override the method there.
6969
"""
7070

71-
@mock.patch("urllib3.request.RequestMethods.request")
72-
def test_machines(self, mocker):
71+
def test_machines(self):
7372
"""Can request machines"""
7473
data = [
7574
"http://127.0.0.1:4001",
7675
"http://127.0.0.1:4002",
7776
"http://127.0.0.1:4003",
7877
]
7978
d = ",".join(data)
80-
mocker.return_value = self._prepare_response(200, d)
79+
self.client.http.request = mock.MagicMock(return_value=self._prepare_response(200, d))
8180
self.assertEqual(data, self.client.machines)
8281

8382
@mock.patch("etcd.Client.machines", new_callable=mock.PropertyMock)
@@ -121,9 +120,7 @@ def test_members(self):
121120
]
122121
}
123122
self._mock_api(200, data)
124-
self.assertEqual(
125-
self.client.members["ce2a822cea30bfca"]["id"], "ce2a822cea30bfca"
126-
)
123+
self.assertEqual(self.client.members["ce2a822cea30bfca"]["id"], "ce2a822cea30bfca")
127124

128125
def test_self_stats(self):
129126
"""Request for stats"""
@@ -156,9 +153,7 @@ def test_leader(self, mocker):
156153
"""Can request the leader"""
157154
members = {"ce2a822cea30bfca": {"id": "ce2a822cea30bfca", "name": "default"}}
158155
mocker.return_value = members
159-
self._mock_api(
160-
200, {"leaderInfo": {"leader": "ce2a822cea30bfca", "followers": {}}}
161-
)
156+
self._mock_api(200, {"leaderInfo": {"leader": "ce2a822cea30bfca", "followers": {}}})
162157
self.assertEqual(self.client.leader, members["ce2a822cea30bfca"])
163158

164159
def test_set_plain(self):
@@ -257,9 +252,7 @@ def test_compare_and_swap(self):
257252
def test_compare_and_swap_failure(self):
258253
"""Exception will be raised if prevValue != value in test_set"""
259254
self._mock_exception(ValueError, "Test Failed : [ 1!=3 ]")
260-
self.assertRaises(
261-
ValueError, self.client.write, "/testKey", "test", prevValue="oldbog"
262-
)
255+
self.assertRaises(ValueError, self.client.write, "/testKey", "test", prevValue="oldbog")
263256

264257
def test_set_append(self):
265258
"""Can append a new key"""
@@ -278,9 +271,7 @@ def test_set_append(self):
278271

279272
def test_set_dir_with_value(self):
280273
"""Creating a directory with a value raises an error."""
281-
self.assertRaises(
282-
etcd.EtcdException, self.client.write, "/bar", "testvalye", dir=True
283-
)
274+
self.assertRaises(etcd.EtcdException, self.client.write, "/bar", "testvalye", dir=True)
284275

285276
def test_delete(self):
286277
"""Can delete a value"""
@@ -312,11 +303,7 @@ def test_pop(self):
312303
self._mock_api(200, d)
313304
res = self.client.pop(d["node"]["key"])
314305
self.assertEqual(
315-
{
316-
attr: getattr(res, attr)
317-
for attr in dir(res)
318-
if attr in etcd.EtcdResult._node_props
319-
},
306+
{attr: getattr(res, attr) for attr in dir(res) if attr in etcd.EtcdResult._node_props},
320307
d["prevNode"],
321308
)
322309
self.assertEqual(res.value, d["prevNode"]["value"])
@@ -398,34 +385,24 @@ def _mock_api(self, status, d, cluster_id=None):
398385
self.client.http.request_encode_body = mock.MagicMock(return_value=resp)
399386
self.client.http.request = mock.MagicMock(return_value=resp)
400387

401-
def _mock_error(
402-
self, error_code, msg, cause, method="PUT", fields=None, cluster_id=None
403-
):
404-
resp = self._prepare_response(
405-
500, {"errorCode": error_code, "message": msg, "cause": cause}
406-
)
388+
def _mock_error(self, error_code, msg, cause, method="PUT", fields=None, cluster_id=None):
389+
resp = self._prepare_response(500, {"errorCode": error_code, "message": msg, "cause": cause})
407390
resp.getheader.return_value = cluster_id or "abcdef1234"
408391
self.client.http.request_encode_body = mock.create_autospec(
409392
self.client.http.request_encode_body, return_value=resp
410393
)
411-
self.client.http.request = mock.create_autospec(
412-
self.client.http.request, return_value=resp
413-
)
394+
self.client.http.request = mock.create_autospec(self.client.http.request, return_value=resp)
414395

415396
def test_compare_and_swap_failure(self):
416397
"""Exception will be raised if prevValue != value in test_set"""
417398
self._mock_error(200, "Test Failed", "[ 1!=3 ]", fields={"prevValue": "oldbog"})
418-
self.assertRaises(
419-
ValueError, self.client.write, "/testKey", "test", prevValue="oldbog"
420-
)
399+
self.assertRaises(ValueError, self.client.write, "/testKey", "test", prevValue="oldbog")
421400

422401
def test_watch_timeout(self):
423402
"""Exception will be raised if prevValue != value in test_set"""
424403
self.client.http.request = mock.create_autospec(
425404
self.client.http.request,
426-
side_effect=urllib3.exceptions.ReadTimeoutError(
427-
self.client.http, "foo", "Read timed out"
428-
),
405+
side_effect=urllib3.exceptions.ReadTimeoutError(self.client.http, "foo", "Read timed out"),
429406
)
430407
self.assertRaises(
431408
etcd.EtcdWatchTimedOut,
@@ -435,15 +412,11 @@ def test_watch_timeout(self):
435412

436413
def test_path_without_trailing_slash(self):
437414
"""Exception will be raised if a path without a trailing slash is used"""
438-
self.assertRaises(
439-
ValueError, self.client.api_execute, "testpath/bar", self.client._MPUT
440-
)
415+
self.assertRaises(ValueError, self.client.api_execute, "testpath/bar", self.client._MPUT)
441416

442417
def test_api_method_not_supported(self):
443418
"""Exception will be raised if an unsupported HTTP method is used"""
444-
self.assertRaises(
445-
etcd.EtcdException, self.client.api_execute, "/testpath/bar", "TRACE"
446-
)
419+
self.assertRaises(etcd.EtcdException, self.client.api_execute, "/testpath/bar", "TRACE")
447420

448421
def test_read_cluster_id_changed(self):
449422
"""Read timeout set to the default"""
@@ -462,14 +435,10 @@ def test_read_cluster_id_changed(self):
462435
self.client.read("/testkey")
463436

464437
def test_read_connection_error(self):
465-
self.client.http.request = mock.create_autospec(
466-
self.client.http.request, side_effect=socket.error()
467-
)
438+
self.client.http.request = mock.create_autospec(self.client.http.request, side_effect=socket.error())
468439
self.assertRaises(etcd.EtcdConnectionFailed, self.client.read, "/something")
469440
# Direct GET request
470-
self.assertRaises(
471-
etcd.EtcdConnectionFailed, self.client.api_execute, "/a", "GET"
472-
)
441+
self.assertRaises(etcd.EtcdConnectionFailed, self.client.api_execute, "/a", "GET")
473442

474443
def test_not_in(self):
475444
pass

0 commit comments

Comments
 (0)