Skip to content

Commit 5463065

Browse files
author
Jon Wayne Parrott
committed
Merge pull request GoogleCloudPlatform#27 from GoogleCloudPlatform/pytest-refactor-step-4
Refactoring step 4 to use py.test
2 parents e97d8ff + ed53141 commit 5463065

File tree

20 files changed

+299
-217
lines changed

20 files changed

+299
-217
lines changed

2-structured-data/bookshelf/model_cloudsql.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
from flask import Flask
1616
from flask.ext.sqlalchemy import SQLAlchemy
17-
import six
1817

1918

2019
builtin_list = list
@@ -29,11 +28,10 @@ def init_app(app):
2928

3029
def from_sql(row):
3130
"""Translates a SQLAlchemy model instance into a dictionary"""
32-
d = {}
33-
for column in row.__table__.columns:
34-
d[column.name] = six.text_type(getattr(row, column.name))
35-
36-
return d
31+
data = row.__dict__.copy()
32+
data['id'] = row.id
33+
data.pop('_sa_instance_state')
34+
return data
3735

3836

3937
# [START model]

2-structured-data/tests/test_crud.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ def tracking_create(*args, **kwargs):
7575
list(map(model.delete, ids_to_delete))
7676

7777

78-
def flaky_filter(e, *args):
78+
def flaky_filter(info, *args):
7979
"""Used by flaky to determine when to re-run a test case."""
80+
_, e, _ = info
8081
return isinstance(e, ServiceUnavailable)
8182

8283

3-binary-data/bookshelf/model_cloudsql.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
from flask import Flask
1616
from flask.ext.sqlalchemy import SQLAlchemy
17-
import six
1817

1918

2019
builtin_list = list
@@ -29,11 +28,10 @@ def init_app(app):
2928

3029
def from_sql(row):
3130
"""Translates a SQLAlchemy model instance into a dictionary"""
32-
d = {}
33-
for column in row.__table__.columns:
34-
d[column.name] = six.text_type(getattr(row, column.name))
35-
36-
return d
31+
data = row.__dict__.copy()
32+
data['id'] = row.id
33+
data.pop('_sa_instance_state')
34+
return data
3735

3836

3937
class Book(db.Model):

3-binary-data/requirements.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
Flask==0.10.1
2-
gcloud==0.9.0
2+
gcloud==0.10.1
33
gunicorn==19.4.5
4-
oauth2client==1.5.2
4+
oauth2client==2.0.0.post1
55
Flask-SQLAlchemy==2.1
6-
PyMySQL==0.7.1
6+
PyMySQL==0.7.2
77
Flask-PyMongo==0.4.0
88
PyMongo==3.2.1
99
six==1.10.0

3-binary-data/tests/conftest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def tracking_create(*args, **kwargs):
7676
list(map(model.delete, ids_to_delete))
7777

7878

79-
def flaky_filter(e, *args):
79+
def flaky_filter(info, *args):
8080
"""Used by flaky to determine when to re-run a test case."""
81+
_, e, _ = info
8182
return isinstance(e, ServiceUnavailable)

3-binary-data/tests/test_storage.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414

1515
import re
1616

17-
import httplib2
1817
from conftest import flaky_filter
1918
from flaky import flaky
20-
from six import BytesIO
19+
import httplib2
2120
import pytest
21+
from six import BytesIO
2222

2323

2424
# Mark all test cases in this class as flaky, so that if errors occur they
@@ -64,7 +64,7 @@ def test_upload_bad_file(self, app):
6464
}
6565

6666
with app.test_client() as c:
67-
rv = c.post('/books/add', data=data,
68-
follow_redirects=True)
67+
rv = c.post('/books/add', data=data, follow_redirects=True)
68+
6969
# check we weren't pwned
7070
assert rv.status == '400 BAD REQUEST'

4-auth/bookshelf/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import httplib2
2020

2121
# [START include]
22-
from oauth2client.flask_util import UserOAuth2
22+
from oauth2client.contrib.flask_util import UserOAuth2
2323

2424
oauth2 = UserOAuth2()
2525
# [END include]

4-auth/bookshelf/model_cloudsql.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
from flask import Flask
1616
from flask.ext.sqlalchemy import SQLAlchemy
17-
import six
1817

1918

2019
builtin_list = list
@@ -29,11 +28,10 @@ def init_app(app):
2928

3029
def from_sql(row):
3130
"""Translates a SQLAlchemy model instance into a dictionary"""
32-
d = {}
33-
for column in row.__table__.columns:
34-
d[column.name] = six.text_type(getattr(row, column.name))
35-
36-
return d
31+
data = row.__dict__.copy()
32+
data['id'] = row.id
33+
data.pop('_sa_instance_state')
34+
return data
3735

3836

3937
class Book(db.Model):
@@ -85,7 +83,9 @@ def read(id):
8583

8684

8785
def create(data):
86+
print(data)
8887
book = Book(**data)
88+
print(book)
8989
db.session.add(book)
9090
db.session.commit()
9191
return from_sql(book)

4-auth/bookshelf/templates/view.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@ <h3>Book</h3>
4040
{% endif %}
4141
</div>
4242
<div class="media-body">
43+
{{book|pprint}}
4344
<h4 class="book-title">
4445
{{book.title}}
4546
<small>{{book.publishedDate}}</small>
4647
</h4>
4748
<h5 class="book-author">By {{book.author|default('Unknown', True)}}</h5>
4849
<p class="book-description">{{book.description}}</p>
49-
<small class="book-added-by">Added by {{book.createdBy|default('Anonymous', True)}}</small>
50+
<small class="book-added-by">Added by {{book.get('createdBy')|default('Anonymous', True)}}</small>
5051
</div>
5152
</div>
5253

4-auth/requirements-dev.txt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
tox==2.3.1
2-
unittest2==1.1.0
3-
nose==1.3.7
42
flake8==2.5.4
5-
coverage==4.1b2
6-
BeautifulSoup4==4.4.1
7-
mock==1.3.0
8-
flaky==3.0.3
3+
flaky==3.1.0
4+
pytest==2.8.7
5+
pytest-cov==2.2.1

0 commit comments

Comments
 (0)