Skip to content

Commit 2d95a9b

Browse files
author
Aiden Scandella
committed
Fix test harness
Switch to py.test and make it actually work. Separate out requirements into production and testing. Fix pep257 issues with docstrings and some pep8 issues. Add a setup.py to be able to import the app from tests.
1 parent 2f481a3 commit 2d95a9b

File tree

7 files changed

+59
-32
lines changed

7 files changed

+59
-32
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.pyc
2+
*.egg-info

README.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,15 @@ How To Use This
2424
Testing
2525
-------
2626

27-
1. Install the dependencies with `pip install -r requirements.txt`
28-
2. Run the command `nosetests -v`
27+
1. Install the dependencies with:
28+
29+
```bash
30+
pip install -r requirements.txt
31+
pip install -r requirements-test.txt
32+
python setup.py develop
33+
```
34+
35+
2. Run the command `py.test test/`
2936
3. If you delete the fixtures, or decide to add some of your own, you’ll have to re-generate them, and the way this is done is by running the app, getting an auth_token from the main page of the app. Paste that token in place of the `test_auth_token` at the top of the `test_endpoints.py` file, then run the tests.
3037

3138

@@ -35,7 +42,7 @@ Development
3542
If you want to work on this application we’d love your pull requests and tickets on GitHub!
3643

3744
1. If you open up a ticket, please make sure it describes the problem or feature request fully.
38-
2. If you send us a pull request, make sure you add a test for what you added, and make sure the full test suite runs with `nosetests -v`.
45+
2. If you send us a pull request, make sure you add a test for what you added, and make sure the full test suite runs with `py.test`.
3946

4047
Deploy to Heroku
4148
----------------

app.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121

2222
def generate_oauth_service():
23-
"""Prepares the OAuth2Service that is used to make requests later."""
23+
"""Prepare the OAuth2Service that is used to make requests later."""
2424
return OAuth2Service(
2525
client_id=os.environ.get('UBER_CLIENT_ID'),
2626
client_secret=os.environ.get('UBER_CLIENT_SECRET'),
@@ -32,7 +32,7 @@ def generate_oauth_service():
3232

3333

3434
def generate_ride_headers(token):
35-
"""Generates the header object that is used to make api requests."""
35+
"""Generate the header object that is used to make api requests."""
3636
return {
3737
'Authorization': 'bearer %s' % token,
3838
'Content-Type': 'application/json',
@@ -41,7 +41,7 @@ def generate_ride_headers(token):
4141

4242
@app.route('/health', methods=['GET'])
4343
def health():
44-
"""Used to check the status of this application."""
44+
"""Check the status of this application."""
4545
return ';-)'
4646

4747

@@ -96,10 +96,10 @@ def demo():
9696

9797
@app.route('/products', methods=['GET'])
9898
def products():
99-
'''Example call to the products endpoint.
99+
"""Example call to the products endpoint.
100100
101101
Returns all the products currently available in San Francisco.
102-
'''
102+
"""
103103
url = config.get('base_uber_url') + 'products'
104104
params = {
105105
'latitude': config.get('start_latitude'),
@@ -179,7 +179,7 @@ def price():
179179

180180
@app.route('/history', methods=['GET'])
181181
def history():
182-
"""Returns the last 5 trips made by the logged in user."""
182+
"""Return the last 5 trips made by the logged in user."""
183183
url = config.get('base_uber_url') + 'history'
184184
params = {
185185
'offset': 0,
@@ -203,7 +203,7 @@ def history():
203203

204204
@app.route('/me', methods=['GET'])
205205
def me():
206-
"""Returns user information including name, picture and email."""
206+
"""Return user information including name, picture and email."""
207207
url = config.get('base_uber_url') + 'me'
208208
response = app.requests_session.get(
209209
url,
@@ -218,8 +218,9 @@ def me():
218218
data=response.text,
219219
)
220220

221+
221222
def get_redirect_uri(request):
222-
"""Returns OAuth redirect URI."""
223+
"""Return OAuth redirect URI."""
223224
parsed_url = urlparse(request.url)
224225
if parsed_url.hostname == 'localhost':
225226
return 'http://{hostname}:{port}/submit'.format(hostname=parsed_url.hostname, port=parsed_url.port)

requirements-test.txt

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Test harness
2+
pytest==2.5.2
3+
4+
# Coverage
5+
pytest-cov==1.6
6+
7+
# HTTP Fixtures
8+
betamax==0.4.0

requirements.txt

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ Flask==0.10.1
22
Jinja2==2.7.3
33
MarkupSafe==0.23
44
Werkzeug==0.9.6
5-
betamax==0.4.0
65
gnureadline==6.3.3
76
itsdangerous==0.24
8-
nose==1.3.3
97
rauth==0.7.0
108
requests==2.3.0
119
wsgiref==0.1.2
1210

1311
gunicorn==18.0
14-
Flask-SSLify==0.1.4
12+
Flask-SSLify==0.1.4

setup.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from setuptools import setup, find_packages
2+
3+
setup(
4+
name='Python-Sample-Application',
5+
version='0.0.1',
6+
author='Uber Engineering',
7+
author_email='[email protected]',
8+
packages=find_packages(),
9+
description='Python sample application',
10+
)

test/test_endpoints.py

+19-18
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,26 @@
88

99
test_auth_token = '42Kq726Vv6lzJ0TMhXWsgUulVjRsxh'
1010

11+
1112
class TestCases(unittest.TestCase):
1213
def setUp(self):
1314
self.test_app = app.test_client()
1415
self.session = app.requests_session
1516

1617
def test_health_endpoint(self):
17-
"""Asserts that the health endpoint works."""
18+
"""Assert that the health endpoint works."""
1819
response = app.test_client().get('/health')
1920
self.assertEquals(response.data, ';-)')
2021

2122
def test_root_endpoint(self):
22-
"""Asserts that the / endpoint correctly redirects to login.uber.com."""
23+
"""Assert that the / endpoint correctly redirects to login.uber.com."""
2324
response = app.test_client().get('/')
2425
self.assertIn('login.uber.com', response.data)
2526

2627
def test_products_endpoint_returns_success(self):
27-
"""Asserts that the products endpoint returns success
28+
"""Assert that the products endpoint returns success.
2829
29-
when a valid key is passed in.
30+
When a valid key is passed in.
3031
"""
3132
with app.test_client() as client:
3233
with client.session_transaction() as session:
@@ -36,9 +37,9 @@ def test_products_endpoint_returns_success(self):
3637
self.assertEquals(response.status_code, 200)
3738

3839
def test_products_endpoint_returns_failure(self):
39-
"""Asserts that the products endpoint returns failure
40+
"""Assert that the products endpoint returns failure.
4041
41-
when an invalid key is passed in.
42+
When an invalid key is passed in.
4243
"""
4344
with app.test_client() as client:
4445
with client.session_transaction() as session:
@@ -48,9 +49,9 @@ def test_products_endpoint_returns_failure(self):
4849
self.assertEquals(response.status_code, 401)
4950

5051
def test_time_estimates_endpoint_returns_success(self):
51-
"""Asserts that the time estimates endpoint returns success
52+
"""Assert that the time estimates endpoint returns success.
5253
53-
when a valid key is passed in.
54+
When a valid key is passed in.
5455
"""
5556
with app.test_client() as client:
5657
with client.session_transaction() as session:
@@ -60,9 +61,9 @@ def test_time_estimates_endpoint_returns_success(self):
6061
self.assertEquals(response.status_code, 200)
6162

6263
def test_time_estimates_endpoint_returns_failure(self):
63-
"""Asserts that the time estimates endpoint returns failure
64+
"""Assert that the time estimates endpoint returns failure.
6465
65-
when an invalid key is passed in.
66+
When an invalid key is passed in.
6667
"""
6768
with app.test_client() as client:
6869
with client.session_transaction() as session:
@@ -72,9 +73,9 @@ def test_time_estimates_endpoint_returns_failure(self):
7273
self.assertEquals(response.status_code, 401)
7374

7475
def test_price_estimates_endpoint_returns_success(self):
75-
"""Asserts that the price estimates endpoint returns success
76+
"""Assert that the price estimates endpoint returns success.
7677
77-
when a valid key is passed in.
78+
When a valid key is passed in.
7879
"""
7980
with app.test_client() as client:
8081
with client.session_transaction() as session:
@@ -84,9 +85,9 @@ def test_price_estimates_endpoint_returns_success(self):
8485
self.assertEquals(response.status_code, 200)
8586

8687
def test_price_estimates_endpoint_returns_failure(self):
87-
"""Asserts that the price estimates endpoint returns failure
88+
"""Assert that the price estimates endpoint returns failure.
8889
89-
when an invalid key is passed in.
90+
When an invalid key is passed in.
9091
"""
9192
with app.test_client() as client:
9293
with client.session_transaction() as session:
@@ -96,9 +97,9 @@ def test_price_estimates_endpoint_returns_failure(self):
9697
self.assertEquals(response.status_code, 401)
9798

9899
def test_history_endpoint_returns_success(self):
99-
"""Asserts that the history endpoint returns success
100+
"""Assert that the history endpoint returns success.
100101
101-
when a valid key is passed in.
102+
When a valid key is passed in.
102103
"""
103104
with app.test_client() as client:
104105
with client.session_transaction() as session:
@@ -108,9 +109,9 @@ def test_history_endpoint_returns_success(self):
108109
self.assertEquals(response.status_code, 200)
109110

110111
def test_history_endpoint_returns_failure(self):
111-
"""Asserts that the price estimates endpoint returns failure
112+
"""Assert that the price estimates endpoint returns failure.
112113
113-
when an invalid key is passed in.
114+
When an invalid key is passed in.
114115
"""
115116
with app.test_client() as client:
116117
with client.session_transaction() as session:

0 commit comments

Comments
 (0)