Skip to content

Commit

Permalink
Add client test for setting instance methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dinahshi committed Dec 2, 2015
1 parent e24f50a commit 61a0d71
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
13 changes: 11 additions & 2 deletions tests/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,20 @@ def setup_class(cls):
auth = mock.Mock()
cls.client = Client(auth)

def test_add_instance_methods(self):
methods = [
('_private', 'private_method'),
('public', 'public_method')
]
self.client._add_instance_methods(methods)
assert self.client.public == 'public_method'
assert not hasattr(self.client, '_private')

def test_make_connection_closes(self):
mock_conn = mock.Mock()
mock_conn.read.return_value = b'{}'
with mock.patch(
'six.moves.urllib.request.urlopen', return_value=mock_conn,
'six.moves.urllib.request.urlopen', return_value=mock_conn,
):
self.client._make_connection("")
mock_conn.close.assert_called_once_with()
Expand All @@ -26,7 +35,7 @@ def test_make_connection_closes_with_exception(self):
mock_conn = mock.Mock()
mock_conn.read.side_effect = Exception
with mock.patch(
'six.moves.urllib.request.urlopen', return_value=mock_conn,
'six.moves.urllib.request.urlopen', return_value=mock_conn,
):
with pytest.raises(Exception):
self.client._make_connection("")
Expand Down
12 changes: 8 additions & 4 deletions yelp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ def __init__(self, authenticator):
def _define_request_methods(self):
endpoint_instances = [end(self) for end in self._endpoints]
for endpoint in endpoint_instances:
# inspect.getmembers returns a list of (name, value) tuples
instance_methods = inspect.getmembers(endpoint, inspect.ismethod)
for method in instance_methods:
if method[0][0] is not '_':
self.__setattr__(method[0], method[1])
self._add_instance_methods(instance_methods)

def _add_instance_methods(self, instance_methods):
# instance_methods is a list of (name, value) tuples where value is the
# instance of the bound method
for method in instance_methods:
if method[0][0] is not '_':
self.__setattr__(method[0], method[1])

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

0 comments on commit 61a0d71

Please sign in to comment.