Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pyupgrade, black, and pretty-format-json precommit hooks #64

Merged
merged 4 commits into from
Jul 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 10 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ repos:
- id: check-merge-conflict
- id: trailing-whitespace
- id: end-of-file-fixer
- id: autopep8-wrapper
- id: check-yaml
- id: debug-statements
- id: name-tests-test
exclude: ^tests/testing.py$
- id: flake8
- id: requirements-txt-fixer
- repo: git://github.com/asottile/reorder_python_imports
rev: v1.1.0
- id: pretty-format-json
args:
- --autofix
- repo: https://github.com/asottile/pyupgrade
rev: v1.4.0
hooks:
- id: pyupgrade
- repo: https://github.com/ambv/black
rev: 18.6b4
hooks:
- id: reorder-python-imports
- id: black
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ matrix:
python: 2.7
- env: TOXENV=py36
python: 3.6
- env: TOXENV=pypy
python: pypy-5.7.1
- env: TOXENV=pre-commit
python: 3.6
install: pip install coveralls tox
script: tox
# Special snowflake. Our tests depend on a credentials_secret.json
Expand Down
45 changes: 16 additions & 29 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,22 @@
from setuptools import setup

setup(
name='yelp',

version='1.0.1',

description='Python Clientlib for Yelp Public API',

url='https://github.com/Yelp/yelp-python',

author='Yelp',
author_email='partnerships@yelp.com',

name="yelp",
version="1.0.1",
description="Python Clientlib for Yelp Public API",
url="https://github.com/Yelp/yelp-python",
author="Yelp",
author_email="partnerships@yelp.com",
classifiers=[
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: Implementation :: CPython'
],

license='MIT',

keywords='yelp',

packages=find_packages(exclude=('tests*',)),

install_requires=[
'httplib2',
'oauth2',
'six',
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: Implementation :: CPython",
],
license="MIT",
keywords="yelp",
packages=find_packages(exclude=("tests*",)),
install_requires=["httplib2", "oauth2", "six"],
)
29 changes: 10 additions & 19 deletions tests/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,38 @@


class TestClient(object):

@classmethod
def setup_class(cls):
auth = mock.Mock()
cls.client = Client(auth)

def test_add_instance_methods(self):
methods = [
('_private', 'private_method'),
('public', 'public_method')
]
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')
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,
):
mock_conn.read.return_value = b"{}"
with mock.patch("six.moves.urllib.request.urlopen", return_value=mock_conn):
self.client._make_connection("")
mock_conn.close.assert_called_once_with()

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,
):
with mock.patch("six.moves.urllib.request.urlopen", return_value=mock_conn):
with pytest.raises(Exception):
self.client._make_connection("")
mock_conn.close.assert_called_once_with()

def test_make_request_calls_raise_error_on_HTTPError(self):
error = six.moves.urllib.error.HTTPError(
'', 400, 'Bad Request', None, None)
error = six.moves.urllib.error.HTTPError("", 400, "Bad Request", None, None)
error.read = mock.Mock()
error.read.return_value = b'{}'
error.read.return_value = b"{}"

with mock.patch('six.moves.urllib.request.urlopen', side_effect=error):
with mock.patch('yelp.errors.ErrorHandler.raise_error') as r:
with mock.patch("six.moves.urllib.request.urlopen", side_effect=error):
with mock.patch("yelp.errors.ErrorHandler.raise_error") as r:
self.client._make_request("")
r.assert_called_once_with(error)
17 changes: 8 additions & 9 deletions tests/endpoint/business_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@


class TestBusiness(object):

@classmethod
def setup_class(cls):
auth = mock.Mock()
cls.client = Client(auth)

def test_get_business_builds_correct_params(self):
with mock.patch('yelp.client.Client._make_request') as request:
request.return_value = '{}'
response = self.client.get_business('test-id')
request.assert_called_once_with('/v2/business/test-id', {})
with mock.patch("yelp.client.Client._make_request") as request:
request.return_value = "{}"
response = self.client.get_business("test-id")
request.assert_called_once_with("/v2/business/test-id", {})
assert type(response) is BusinessResponse

def test_get_business_builds_correct_params_with_lang(self):
with mock.patch('yelp.client.Client._make_request') as request:
params = {'lang': 'fr'}
self.client.get_business('test-id', **params)
request.assert_called_once_with('/v2/business/test-id', params)
with mock.patch("yelp.client.Client._make_request") as request:
params = {"lang": "fr"}
self.client.get_business("test-id", **params)
request.assert_called_once_with("/v2/business/test-id", params)
15 changes: 6 additions & 9 deletions tests/endpoint/phone_search_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@


class TestBusiness(object):

@classmethod
def setup_class(cls):
auth = mock.Mock()
cls.client = Client(auth)

def test_phone_search_builds_correct_params(self):
with mock.patch('yelp.client.Client._make_request') as request:
request.return_value = '{}'
params = {
'category': 'fashion'
}
response = self.client.phone_search('5555555555', **params)
params['phone'] = '5555555555'
request.assert_called_once_with('/v2/phone_search/', params)
with mock.patch("yelp.client.Client._make_request") as request:
request.return_value = "{}"
params = {"category": "fashion"}
response = self.client.phone_search("5555555555", **params)
params["phone"] = "5555555555"
request.assert_called_once_with("/v2/phone_search/", params)
assert type(response) is SearchResponse
43 changes: 16 additions & 27 deletions tests/endpoint/search_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,37 @@

class TestBusiness(object):

sample_location = 'San Francisco, CA'
sample_location = "San Francisco, CA"

@classmethod
def setup_class(cls):
auth = mock.Mock()
cls.client = Client(auth)

def test_search_builds_correct_params(self):
with mock.patch('yelp.client.Client._make_request') as request:
request.return_value = '{}'
params = {
'term': 'food',
}
with mock.patch("yelp.client.Client._make_request") as request:
request.return_value = "{}"
params = {"term": "food"}
response = self.client.search(self.sample_location, **params)
params.update({
'location': self.sample_location
})
request.assert_called_once_with('/v2/search/', params)
params.update({"location": self.sample_location})
request.assert_called_once_with("/v2/search/", params)
assert type(response) is SearchResponse

def test_search_builds_correct_params_with_current_lat_long(self):
with mock.patch('yelp.client.Client._make_request') as request:
params = {
'term': 'food',
}
with mock.patch("yelp.client.Client._make_request") as request:
params = {"term": "food"}
self.client.search(self.sample_location, 0, 0, **params)
params.update({
'location': self.sample_location,
'cll': '0,0'
})
request.assert_called_once_with('/v2/search/', params)
params.update({"location": self.sample_location, "cll": "0,0"})
request.assert_called_once_with("/v2/search/", params)

def test_search_by_bounding_box_builds_correct_params(self):
with mock.patch('yelp.client.Client._make_request') as request:
params = {
'term': 'food',
}
with mock.patch("yelp.client.Client._make_request") as request:
params = {"term": "food"}
self.client.search_by_bounding_box(0, 0, 0, 0, **params)
params['bounds'] = '0,0|0,0'
request.assert_called_once_with('/v2/search/', params)
params["bounds"] = "0,0|0,0"
request.assert_called_once_with("/v2/search/", params)

def test_search_by_coordinates_builds_correct_params(self):
with mock.patch('yelp.client.Client._make_request') as request:
with mock.patch("yelp.client.Client._make_request") as request:
self.client.search_by_coordinates(0, 0, 0, 0, 0)
request.assert_called_once_with('/v2/search/', {'ll': '0,0,0,0,0'})
request.assert_called_once_with("/v2/search/", {"ll": "0,0,0,0,0"})
14 changes: 5 additions & 9 deletions tests/errors_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,25 @@


class TestErrorHandler(object):

@classmethod
def setup_class(cls):
cls.handler = ErrorHandler()

def test_error_handler_throws_unknown_HTTPError(self):
error = six.moves.urllib.error.HTTPError(
'', 400, 'Bad Request', None, None)
error = six.moves.urllib.error.HTTPError("", 400, "Bad Request", None, None)
error.read = mock.Mock()
error.read.return_value = b'{}'
error.read.return_value = b"{}"

with pytest.raises(six.moves.urllib.error.HTTPError):
self.handler.raise_error(error)

def test_error_handler_raises_correct_yelp_error(self):
with io.open(
resource_filename('json/error_response.json'), 'rb',
) as resp_file:
response = resp_file.read().replace(b'\n', b'')
with io.open(resource_filename("json/error_response.json"), "rb") as resp_file:
response = resp_file.read().replace(b"\n", b"")

error = mock.Mock()
error.code = 400
error.msg = 'Bad Request'
error.msg = "Bad Request"
error.read.return_value = response

with pytest.raises(InvalidParameter) as err:
Expand Down
11 changes: 4 additions & 7 deletions tests/integration/business_integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class TestBusinessIntegration(IntegrationTest):
@int_vcr.use_cassette(**cassette_params)
def test_url_with_no_params(self):
with pytest.raises(MissingParameter):
self.client.get_business('')
self.client.get_business("")

@int_vcr.use_cassette(**cassette_params)
def test_get_business_returns_correct_result(self):
Expand All @@ -32,16 +32,13 @@ def test_get_business_with_bad_id(self):

@int_vcr.use_cassette(**cassette_params)
def test_get_business_with_unicode_chars(self):
business_id = u'weingalerie-und-café-nö-berlin'
business_id = u"weingalerie-und-café-nö-berlin"
resp = self.client.get_business(business_id)
assert resp.business.id == business_id

@int_vcr.use_cassette(**cassette_params)
def test_get_business_with_locale_params(self):
business_id = u'yelp-san-francisco'
params = {
'cc': 'CA',
'lang': 'fr'
}
business_id = u"yelp-san-francisco"
params = {"cc": "CA", "lang": "fr"}
resp = self.client.get_business(business_id, **params)
assert resp.business.id == business_id
22 changes: 10 additions & 12 deletions tests/integration/integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,24 @@
class IntegrationTest(object):

int_vcr = vcr.VCR(
cassette_library_dir=resource_filename('integration/vcr_cassettes'),
match_on=['method'],
path_transformer=vcr.VCR.ensure_suffix('.yaml')
cassette_library_dir=resource_filename("integration/vcr_cassettes"),
match_on=["method"],
path_transformer=vcr.VCR.ensure_suffix(".yaml"),
)

cassette_params = {
'filter_query_parameters': [
'oauth_consumer_key',
'oauth_token',
'oauth_body_hash',
'oauth_nonce',
'oauth_signature'
"filter_query_parameters": [
"oauth_consumer_key",
"oauth_token",
"oauth_body_hash",
"oauth_nonce",
"oauth_signature",
]
}

@classmethod
def setup_class(cls):
with io.open(
resource_filename('json/credentials_secret.json'),
) as cred:
with io.open(resource_filename("json/credentials_secret.json")) as cred:
test_creds = json.load(cred)
auth = Oauth1Authenticator(**test_creds)
cls.client = Client(auth)
2 changes: 1 addition & 1 deletion tests/integration/phone_search_integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TestPhoneSearchIntegration(IntegrationTest):

@int_vcr.use_cassette(**cassette_params)
def test_phone_search(self):
phone = '+14158267000'
phone = "+14158267000"
resp = self.client.phone_search(phone)
assert type(resp) is SearchResponse
assert phone in resp.businesses[0].phone