1
1
# -*- coding: UTF-8 -*-
2
- import io
3
- import json
4
-
5
2
import mock
6
3
import pytest
7
4
import six
8
5
9
- from tests .testing import resource_filename
10
6
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
14
7
15
8
16
9
class TestClient (object ):
17
10
18
- sample_location = 'San Francisco, CA'
19
-
20
11
@classmethod
21
12
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' )
95
24
96
25
def test_make_connection_closes (self ):
97
26
mock_conn = mock .Mock ()
98
- mock_conn .read .return_value = b"{}"
27
+ mock_conn .read .return_value = b'{}'
99
28
with mock .patch (
100
- 'six.moves.urllib.request.urlopen' , return_value = mock_conn ,
29
+ 'six.moves.urllib.request.urlopen' , return_value = mock_conn ,
101
30
):
102
31
self .client ._make_connection ("" )
103
32
mock_conn .close .assert_called_once_with ()
@@ -106,7 +35,7 @@ def test_make_connection_closes_with_exception(self):
106
35
mock_conn = mock .Mock ()
107
36
mock_conn .read .side_effect = Exception
108
37
with mock .patch (
109
- 'six.moves.urllib.request.urlopen' , return_value = mock_conn ,
38
+ 'six.moves.urllib.request.urlopen' , return_value = mock_conn ,
110
39
):
111
40
with pytest .raises (Exception ):
112
41
self .client ._make_connection ("" )
0 commit comments