8000 Moved things around again to make space for intro and python refreshe… · ciguler/testing-python-apps@6af4485 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6af4485

Browse files
committed
Moved things around again to make space for intro and python refresher sections.
1 parent f8688e7 commit 6af4485

File tree

115 files changed

+284
-280
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+284
-280
lines changed

section1/video_code/app.py

Lines changed: 0 additions & 66 deletions
This file was deleted.

section2/video_code/app.py

Lines changed: 0 additions & 13 deletions
This file was deleted.

section3/video_code/app.py

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,66 @@
1-
import os
1+
from blog import Blog
22

3-
from flask import Flask
4-
from flask_restful import Api
5-
from flask_jwt import JWT
63

7-
from security import authenticate, identity
8-
from resources.user import UserRegister
9-
from resources.item import Item, ItemList
10-
from resources.store import Store, StoreList
4+
MENU_PROMPT = '\nEnter "c" to create a blog, "l" to list them, "r" to read one, "p" to write a post, or "q" to quit: '
5+
POST_TEMPLATE = """
6+
--- {} ---
117
12-
app = Flask(__name__)
8+
{}
139
14-
app.config['DEBUG'] = True
10+
"""
1511

16-
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', 'sqlite:///data.db')
17-
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
18-
app.secret_key = 'jose'
19-
api = Api(app)
12+
blogs = dict()
2013

21-
jwt = JWT(app, authenticate, identity) # /auth
2214

23-
api.add_resource(Store, '/store/<string:name>')
24-
api.add_resource(Item, '/item/<string:name>')
25-
api.add_resource(ItemList, '/items')
26-
api.add_resource(StoreList, '/stores')
15+
def menu():
16+
print_blogs()
17+
selection = input(MENU_PROMPT)
18+
while selection != 'q':
19+
if selection == 'c':
20+
ask_create_blog()
21+
elif selection == 'l':
22+
print_blogs()
23+
elif selection == 'r':
24+
ask_read_blog()
25+
elif selection == 'p':
26+
ask_create_post()
27+
selection = input(MENU_PROMPT)
2728

28-
api.add_resource(UserRegister, '/register')
2929

30-
if __name__ == '__main__':
31-
from db import db
30+
def print_blogs():
31+
for key, blog in blogs.items():
32+
print('- {}'.format(blog))
33+
34+
35+
def ask_create_blog():
36+
title = input("Enter your blog title: ")
37+
author = input("Enter your name: ")
38+
39+
blogs[title] = Blog(title, author)
40+
41+
42+
def ask_read_blog():
43+
title = input("Enter the blog title you want to read: ")
44+
45+
print_posts(blogs[title])
3246

33-
db.init_app(app)
3447

35-
if app.config['DEBUG']:
36-
@app.before_first_request
37-
def create_tables():
38-
db.create_all()
48+
def print_posts(blog):
49+
for post in blog.posts:
50+
print_post(post)
3951

40-
app.run(port=5000)
52+
53+
def print_post(post):
54+
print(POST_TEMPLATE.format(post.title, post.content))
55+
56+
57+
def ask_create_post():
58+
blog = input("Enter the blog title you want to create a post in: ")
59+
title = input("Enter your post title: ")
60+
content = input("Enter your post content: ")
61+
62+
blogs[blog].create_post(title, content)
63+
64+
65+
if __name__ == '__main__':
66+
menu()
File renamed without changes.
File renamed without changes.

section3/video_code/readme.md

Lines changed: 0 additions & 17 deletions
This file was deleted.

section3/video_code/requirements.txt

Lines changed: 0 additions & 6 deletions
This file was deleted.

section4/video_code/app.py

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,13 @@
1-
import os
2-
31
from flask import Flask
4-
from flask_restful import Api
5-
from flask_jwt import JWT
62

7-
from security import authenticate, identity
8-
from resources.user import UserRegister
9-
from resources.item import Item, ItemList
10-
from resources.store import Store, StoreList
113

124
app = Flask(__name__)
135

14-
app.config['DEBUG'] = True
15-
16-
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', 'sqlite:///data.db')
17-
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
18-
app.secret_key = 'jose'
19-
api = Api(app)
20-
21-
jwt = JWT(app, authenticate, identity) # /auth
226

23-
api.add_resource(Store, '/store/<string:name>')
24-
api.add_resource(Item, '/item/<string:name>')
25-
api.add_resource(ItemList, '/items')
26-
api.add_resource(StoreList, '/stores')
7+
@app.route('/')
8+
def home():
9+
return render_template('home.html')
2710

28-
api.add_resource(UserRegister, '/register')
2911

3012
if __name__ == '__main__':
31-
from db import db
32-
33-
db.init_app(app)
34-
35-
if app.config['DEBUG']:
36-
@app.before_first_request
37-
def create_tables():
38-
db.create_all()
39-
40-
app.run(port=5000)
13+
app.run()

section5/video_code/app.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import os
22

3-
from flask import Flask, jsonify
3+
from flask import Flask
44
from flask_restful import Api
5-
from flask_jwt import JWT, JWTError
5+
from flask_jwt import JWT
66

77
from security import authenticate, identity
88
from resources.user import UserRegister
@@ -15,7 +15,6 @@
1515

1616
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', 'sqlite:///data.db')
1717
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
18-
app.config['PROPAGATE_EXCEPTIONS'] = True
1918
app.secret_key = 'jose'
2019
api = Api(app)
2120

@@ -28,11 +27,6 @@
2827

2928
api.add_resource(UserRegister, '/register')
3029

31-
32-
@app.errorhandler(JWTError)
33-
def auth_error(err):
34-
return jsonify({'message': 'Could not authorize. Did you include a valid Authorization header?'}), 401
35-
3630
if __name__ == '__main__':
3731
from db import db
3832

section5/video_code/readme.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,15 @@
33
This is built with Flask, Flask-RESTful, Flask-JWT, and Flask-SQLAlchemy.
44

55
Deployed on Heroku.
6+
7+
To get started:
8+
9+
- Create a virtualenv for this project
10+
- Install requirements using `pip install -r requirements.txt`
11+
12+
When you've created the first test, you'll also need to create a correct runtime configuration in PyCharm.
13+
14+
Create a sample unittest configuration, and choose:
15+
16+
- `Path` as as target, with your project's `/tests` folder.
17+
- `Pattern` being `_test.py`

section5/video_code/resources/store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from flask_restful import Resource
1+
from flask_restful import Resource, reqparse
22
from models.store import StoreModel
33

44

section5/video_code/tests/unit/models/item_test.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
from unittest import TestCase
2+
13
from models.item import ItemModel
2-
from tests.base_test import BaseTest
34

45

5-
class ItemTest(BaseTest):
6+
class ItemTest(TestCase):
67
def test_create_item(self):
8+
# Notice this won't work with PostgreSQL, because of
9+
# foreign key constraints.
10+
# The store doesn't exist yet, so it would raise an error.
11+
# SQLite has no foreign key constraint enforcement, so it works there.
712
item = ItemModel('test', 19.99, 1)
813

914
self.assertEqual(item.name, 'test',

section5/video_code/tests/unit/models/store_test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
from unittest import TestCase
2+
13
from models.store import StoreModel
2-
from tests.base_test import BaseTest
34

45

5-
class StoreTest(BaseTest):
6+
class StoreTest(TestCase):
67
def test_create_store(self):
78
store = StoreModel('test')
89

section5/video_code/tests/unit/models/user_test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
from unittest import TestCase
2+
13
from models.user import UserModel
2-
from tests.base_test import BaseTest
34

45

5-
class UserTest(BaseTest):
6+
class UserTest(TestCase):
67
def test_create_user(self):
78
user = UserModel('test', 'abcd')
89

section6/video_code/app.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import os
22

3-
from flask import Flask, jsonify
3+
from flask import Flask
44
from flask_restful import Api
5-
from flask_jwt import JWT, JWTError
5+
from flask_jwt import JWT
66

77
from security import authenticate, identity
88
from resources.user import UserRegister
@@ -15,7 +15,6 @@
1515

1616
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', 'sqlite:///data.db')
1717
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
18-
app.config['PROPAGATE_EXCEPTIONS'] = True
1918
app.secret_key = 'jose'
2019
api = Api(app)
2120

@@ -28,12 +27,6 @@
2827

2928
api.add_resource(UserRegister, '/register')
3029

31-
32-
@app.errorhandler(JWTError)
33-
def auth_error(err):
34-
return jsonify({'message': 'Could not authorize. Did you include a valid Authorization header?'}), 401
35-
36-
3730
if __name__ == '__main__':
3831
from db import db
3932

section6/video_code/models/item.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def __init__(self, name, price, store_id):
1717
self.store_id = store_id
1818

1919
def json(self):
20-
return {'id': self.id, 'name': self.name, 'price': self.price}
20+
return {'name': self.name, 'price': self.price}
2121

2222
@classmethod
2323
def find_by_name(cls, name):

section6/video_code/models/store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def __init__(self, name):
1313
self.name = name
1414

1515
def json(self):
16-
return {'id': self.id, 'name': self.name, 'items': [item.json() for item in self.items.all()]}
16+
return {'name': self.name, 'items': [item.json() for item in self.items.all()]}
1717

1818
@classmethod
1919
def find_by_name(cls, name):

0 commit comments

Comments
 (0)
0