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

Skip to content

Commit a263912

Browse files
committed
Merge pull request Yelp#31 from Yelp/dinah/refactor-client
Refactor client to extract each endpoint into its own module
2 parents 1f2b787 + f98c8aa commit a263912

File tree

10 files changed

+351
-262
lines changed

10 files changed

+351
-262
lines changed

tests/client_test.py

Lines changed: 14 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,32 @@
11
# -*- coding: UTF-8 -*-
2-
import io
3-
import json
4-
52
import mock
63
import pytest
74
import six
85

9-
from tests.testing import resource_filename
106
from yelp.client import Client
11-
from yelp.oauth1_authenticator import Oauth1Authenticator
12-
from yelp.obj.business_response import BusinessResponse
13-
from yelp.obj.search_response import SearchResponse
147

158

169
class TestClient(object):
1710

18-
sample_location = 'San Francisco, CA'
19-
2011
@classmethod
2112
def setup_class(cls):
22-
with io.open(resource_filename('json/credentials.json')) as cred:
23-
test_creds = json.load(cred)
24-
auth = Oauth1Authenticator(**test_creds)
25-
cls.client = Client(auth)
26-
27-
with io.open(resource_filename('json/search_response.json')) as resp:
28-
cls.search_response = json.load(resp)
29-
with io.open(resource_filename('json/business_response.json')) as resp:
30-
cls.business_response = json.load(resp)
31-
32-
def test_get_business_builds_correct_params(self):
33-
with mock.patch('yelp.client.Client._make_request') as request:
34-
request.return_value = self.business_response
35-
response = self.client.get_business('test-id')
36-
request.assert_called_once_with('/v2/business/test-id', {})
37-
assert type(response) is BusinessResponse
38-
39-
def test_get_business_builds_correct_params_with_lang(self):
40-
with mock.patch('yelp.client.Client._make_request') as request:
41-
request.return_value = self.business_response
42-
params = {'lang': 'fr'}
43-
self.client.get_business('test-id', **params)
44-
request.assert_called_once_with('/v2/business/test-id', params)
45-
46-
def test_search_builds_correct_params(self):
47-
with mock.patch('yelp.client.Client._make_request') as request:
48-
request.return_value = self.search_response
49-
params = {
50-
'term': 'food',
51-
}
52-
response = self.client.search(self.sample_location, **params)
53-
params.update({
54-
'location': self.sample_location
55-
})
56-
request.assert_called_once_with('/v2/search/', params)
57-
assert type(response) is SearchResponse
58-
59-
def test_search_builds_correct_params_with_current_lat_long(self):
60-
with mock.patch('yelp.client.Client._make_request') as request:
61-
params = {
62-
'term': 'food',
63-
}
64-
self.client.search(self.sample_location, 0, 0, **params)
65-
params.update({
66-
'location': self.sample_location,
67-
'cll': '0,0'
68-
})
69-
request.assert_called_once_with('/v2/search/', params)
70-
71-
def test_search_by_bounding_box_builds_correct_params(self):
72-
with mock.patch('yelp.client.Client._make_request') as request:
73-
params = {
74-
'term': 'food',
75-
}
76-
self.client.search_by_bounding_box(0, 0, 0, 0, **params)
77-
params['bounds'] = '0,0|0,0'
78-
request.assert_called_once_with('/v2/search/', params)
79-
80-
def test_search_by_coordinates_builds_correct_params(self):
81-
with mock.patch('yelp.client.Client._make_request') as request:
82-
self.client.search_by_coordinates(0, 0, 0, 0, 0)
83-
request.assert_called_once_with('/v2/search/', {'ll': '0,0,0,0,0'})
84-
85-
def test_phone_search_builds_correct_params(self):
86-
with mock.patch('yelp.client.Client._make_request') as request:
87-
request.return_value = self.search_response
88-
params = {
89-
'category': 'fashion'
90-
}
91-
response = self.client.phone_search('5555555555', **params)
92-
params['phone'] = '5555555555'
93-
request.assert_called_once_with('/v2/phone_search/', params)
94-
assert type(response) is SearchResponse
13+
auth = mock.Mock()
14+
cls.client = Client(auth)
15+
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')
9524

9625
def test_make_connection_closes(self):
9726
mock_conn = mock.Mock()
98-
mock_conn.read.return_value = b"{}"
27+
mock_conn.read.return_value = b'{}'
9928
with mock.patch(
100-
'six.moves.urllib.request.urlopen', return_value=mock_conn,
29+
'six.moves.urllib.request.urlopen', return_value=mock_conn,
10130
):
10231
self.client._make_connection("")
10332
mock_conn.close.assert_called_once_with()
@@ -106,7 +35,7 @@ def test_make_connection_closes_with_exception(self):
10635
mock_conn = mock.Mock()
10736
mock_conn.read.side_effect = Exception
10837
with mock.patch(
109-
'six.moves.urllib.request.urlopen', return_value=mock_conn,
38+
'six.moves.urllib.request.urlopen', return_value=mock_conn,
11039
):
11140
with pytest.raises(Exception):
11241
self.client._make_connection("")

tests/endpoint/__init__.py

Whitespace-only changes.

tests/endpoint/business_test.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# -*- coding: UTF-8 -*-
2+
import mock
3+
4+
from yelp.client import Client
5+
from yelp.obj.business_response import BusinessResponse
6+
7+
8+
class TestBusiness(object):
9+
10+
@classmethod
11+
def setup_class(cls):
12+
auth = mock.Mock()
13+
cls.client = Client(auth)
14+
15+
def test_get_business_builds_correct_params(self):
16+
with mock.patch('yelp.client.Client._make_request') as request:
17+
request.return_value = '{}'
18+
response = self.client.get_business('test-id')
19+
request.assert_called_once_with('/v2/business/test-id', {})
20+
assert type(response) is BusinessResponse
21+
22+
def test_get_business_builds_correct_params_with_lang(self):
23+
with mock.patch('yelp.client.Client._make_request') as request:
24+
params = {'lang': 'fr'}
25+
self.client.get_business('test-id', **params)
26+
request.assert_called_once_with('/v2/business/test-id', params)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# -*- coding: UTF-8 -*-
2+
import mock
3+
4+
from yelp.client import Client
5+
from yelp.obj.search_response import SearchResponse
6+
7+
8+
class TestBusiness(object):
9+
10+
@classmethod
11+
def setup_class(cls):
12+
auth = mock.Mock()
13+
cls.client = Client(auth)
14+
15+
def test_phone_search_builds_correct_params(self):
16+
with mock.patch('yelp.client.Client._make_request') as request:
17+
request.return_value = '{}'
18+
params = {
19+
'category': 'fashion'
20+
}
21+
response = self.client.phone_search('5555555555', **params)
22+
params['phone'] = '5555555555'
23+
request.assert_called_once_with('/v2/phone_search/', params)
24+
assert type(response) is SearchResponse

tests/endpoint/search_test.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# -*- coding: UTF-8 -*-
2+
import mock
3+
4+
from yelp.client import Client
5+
from yelp.obj.search_response import SearchResponse
6+
7+
8+
class TestBusiness(object):
9+
10+
sample_location = 'San Francisco, CA'
11+
12+
@classmethod
13+
def setup_class(cls):
14+
auth = mock.Mock()
15+
cls.client = Client(auth)
16+
17+
def test_search_builds_correct_params(self):
18+
with mock.patch('yelp.client.Client._make_request') as request:
19+
request.return_value = '{}'
20+
params = {
21+
'term': 'food',
22+
}
23+
response = self.client.search(self.sample_location, **params)
24+
params.update({
25+
'location': self.sample_location
26+
})
27+
request.assert_called_once_with('/v2/search/', params)
28+
assert type(response) is SearchResponse
29+
30+
def test_search_builds_correct_params_with_current_lat_long(self):
31+
with mock.patch('yelp.client.Client._make_request') as request:
32+
params = {
33+
'term': 'food',
34+
}
35+
self.client.search(self.sample_location, 0, 0, **params)
36+
params.update({
37+
'location': self.sample_location,
38+
'cll': '0,0'
39+
})
40+
request.assert_called_once_with('/v2/search/', params)
41+
42+
def test_search_by_bounding_box_builds_correct_params(self):
43+
with mock.patch('yelp.client.Client._make_request') as request:
44+
params = {
45+
'term': 'food',
46+
}
47+
self.client.search_by_bounding_box(0, 0, 0, 0, **params)
48+
params['bounds'] = '0,0|0,0'
49+
request.assert_called_once_with('/v2/search/', params)
50+
51+
def test_search_by_coordinates_builds_correct_params(self):
52+
with mock.patch('yelp.client.Client._make_request') as request:
53+
self.client.search_by_coordinates(0, 0, 0, 0, 0)
54+
request.assert_called_once_with('/v2/search/', {'ll': '0,0,0,0,0'})

0 commit comments

Comments
 (0)