8000 Refactored a bunch of stuff! · tecladocode/testing-python-apps@6bf8caf · GitHub
[go: up one dir, main page]

Skip to content

Commit 6bf8caf

Browse files
committed
Refactored a bunch of stuff!
1 parent 8dc441e commit 6bf8caf

File tree

23 files changed

+30
-358
lines changed

23 files changed

+30
-358
lines changed

section4/video_code/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from flask import Flask
1+
from flask import Flask, jsonify
22

33

44
app = Flask(__name__)
55

66

77
@app.route('/')
88
def home():
9-
return render_template('home.html')
9+
return jsonify({'message': 'Hello, world!'})
1010

1111

1212
if __name__ == '__main__':

section4/video_code/requirements.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
11
Flask
2-
Flask-RESTful
3-
Flask-JWT
4-
Flask-SQLAlchemy
5-
uwsgi
6-
psycopg2
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
BaseTest
3+
4+
This class should be the parent class to each system test.
5+
It gives each test a Flask test client that we can use.
6+
"""
7+
8+
from unittest import TestCase
9+
from app import app
10+
11+
12+
class BaseTest(TestCase):
13+
def setUp(self):
14+
self.app.testing = True
15+
self.app = app.test_client
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from tests.system.base_test import BaseTest
2+
import json
3+
4+
5+
class TestHome(BaseTest):
6+
def test_home(self):
7+
with self.app() as c:
8+
r = c.get('/')
9+
self.assertEqual(r.status_code, 200)
10+
self.assertEqual(json.loads(r.get_data()), {'message': 'Hello, world!'})

section5/video_code/app.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,18 @@
22

33
from flask import Flask
44
from flask_restful import Api
5-
from flask_jwt import JWT
65

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

128
app = Flask(__name__)
139

1410
app.config['DEBUG'] = True
1511

1612
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', 'sqlite:///data.db')
1713
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
18-
app.secret_key = 'jose'
1914
api = Api(app)
2015

21-
jwt = JWT(app, authenticate, identity) # /auth
22-
23-
api.add_resource(Store, '/store/<string:name>')
2416
api.add_resource(Item, '/item/<string:name>')
25-
api.add_resource(ItemList, '/items')
26-
api.add_resource(StoreList, '/stores')
27-
28-
api.add_resource(UserRegister, '/register')
2917

3018
if __name__ == '__main__':
3119
from db import db

section5/video_code/models/item.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,9 @@ class ItemModel(db.Model):
88
name = db.Column(db.String(80))
99
price = db.Column(db.Float(precision=2))
1010

11-
store_id = db.Column(db.Integer, db.ForeignKey('stores.id'))
12-
store = db.relationship('StoreModel')
13-
14-
def __init__(self, name, price, store_id):
11+
def __init__(self, name, price):
1512
self.name = name
1613
self.price = price
17-
self.store_id = store_id
1814

1915
def json(self):
2016
return {'name': self.name, 'price': self.price}

section5/video_code/models/store.py

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

section5/video_code/models/user.py

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

section5/video_code/resources/__init__.py

Whitespace-only changes.

section5/video_code/resources/item.py

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

section5/video_code/resources/store.py

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

section5/video_code/resources/user.py

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

section5/video_code/security.py

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

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,15 @@
55

66
class ItemTest(TestCase):
77
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.
12-
item = ItemModel('test', 19.99, 1)
8+
item = ItemModel('test', 19.99)
139

1410
self.assertEqual(item.name, 'test',
1511
"The name of the item after creation does not equal the constructor argument.")
1612
self.assertEqual(item.price, 19.99,
1713
"The price of the item after creation does not equal the constructor argument.")
18-
self.assertEqual(item.store_id, 1,
19-
"The store_id of the item after creation does not equal the constructor argument.")
20-
self.assertIsNone(item.store, "The item's store was not None even though the store was not created.")
2114

2215
def test_item_json(self):
23-
item = ItemModel('test', 19.99, 1)
16+
item = ItemModel('test', 19.99)
2417
expected = {
2518
'name': 'test',
2619
'price': 19.99

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

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

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

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

section6/video_code/app.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
from flask import Flask
44
from flask_restful import Api
5-
from flask_jwt import JWT
65

7-
from security import authenticate, identity
8-
from resources.user import UserRegister
96
from resources.item import Item, ItemList
107
from resources.store import Store, StoreList
118

@@ -15,17 +12,14 @@
1512

1613
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', 'sqlite:///data.db')
1714
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
18-
app.secret_key = 'jose'
1915
api = Api(app)
2016

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

2318
api.add_resource(Store, '/store/<string:name>')
2419
api.add_resource(Item, '/item/<string:name>')
2520
api.add_resource(ItemList, '/items')
2621
api.add_resource(StoreList, '/stores')
2722

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

3024
if __name__ == '__main__':
3125
from db import db

0 commit comments

Comments
 (0)
0