Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 61a0d71

Browse files
committed
Add client test for setting instance methods
1 parent e24f50a commit 61a0d71

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

tests/client_test.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,20 @@ def setup_class(cls):
1313
auth = mock.Mock()
1414
cls.client = Client(auth)
1515

16+
def test_add_instance_methods(self):
17+
methods = [
18+
('_private', 'private_method'),
19+
('public', 'public_method')
20+
]
21+
self.client._add_instance_methods(methods)
22+
assert self.client.public == 'public_method'
23+
assert not hasattr(self.client, '_private')
24+
1625
def test_make_connection_closes(self):
1726
mock_conn = mock.Mock()
1827
mock_conn.read.return_value = b'{}'
1928
with mock.patch(
20-
'six.moves.urllib.request.urlopen', return_value=mock_conn,
29+
'six.moves.urllib.request.urlopen', return_value=mock_conn,
2130
):
2231
self.client._make_connection("")
2332
mock_conn.close.assert_called_once_with()
@@ -26,7 +35,7 @@ def test_make_connection_closes_with_exception(self):
2635
mock_conn = mock.Mock()
2736
mock_conn.read.side_effect = Exception
2837
with mock.patch(
29-
'six.moves.urllib.request.urlopen', return_value=mock_conn,
38+
'six.moves.urllib.request.urlopen', return_value=mock_conn,
3039
):
3140
with pytest.raises(Exception):
3241
self.client._make_connection("")

yelp/client.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@ def __init__(self, authenticator):
2727
def _define_request_methods(self):
2828
endpoint_instances = [end(self) for end in self._endpoints]
2929
for endpoint in endpoint_instances:
30-
# inspect.getmembers returns a list of (name, value) tuples
3130
instance_methods = inspect.getmembers(endpoint, inspect.ismethod)
32-
for method in instance_methods:
33-
if method[0][0] is not '_':
34-
self.__setattr__(method[0], method[1])
31+
self._add_instance_methods(instance_methods)
32+
33+
def _add_instance_methods(self, instance_methods):
34+
# instance_methods is a list of (name, value) tuples where value is the
35+
# instance of the bound method
36+
for method in instance_methods:
37+
if method[0][0] is not '_':
38+
self.__setattr__(method[0], method[1])
3539

3640
def _make_request(self, path, url_params={}):
3741
url = 'https://{0}{1}?'.format(

0 commit comments

Comments
 (0)