Skip to content

Commit

Permalink
working pytest + flask + sqlalchemy
Browse files Browse the repository at this point in the history
  • Loading branch information
marksibrahim committed Aug 27, 2017
1 parent c70755d commit 78ef6f0
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 21 deletions.
38 changes: 31 additions & 7 deletions flask-bootstrap/app.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
from flask import Flask
from flask import jsonify
from flask import Blueprint
from flask_sqlalchemy import SQLAlchemy


app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
db = SQLAlchemy(app)
db = SQLAlchemy()

# Blueprints define the routes the routes of the app, without creating one
# this way you can separate app creation and bind blueprints to many apps!
# there's a register blueprints method on the app object
bp = Blueprint("vim_labs", __name__)


def create_app(config=None):
"""
Returns a Flask app bound to the sqlalchemy db
Args:
config (dict): override Flask app configs
"""
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config.update(config or {})
db.init_app(app)
return app


class User(db.Model):
Expand All @@ -20,26 +39,31 @@ def __init__(self, username, email):
def __repr__(self):
return '<User %r>' % self.username

@app.route('/')

@bp.route('/')
def hello_world():
return 'Hello, World!'


@app.route('/add_user')
@bp.route('/add_user')
def add_user():
"""Adds guest user to db"""
guest = User('guest', '[email protected]')
db.session.add(guest)
db.session.commit()
return 'added guest'

@app.route('/print_users')
@bp.route('/print_users')
def print_users():
"""Returns json of all users in db"""
users = User.query.all()
users_dict = {}
for user in users:
users_dict[str(user)] = True
return jsonify(users_dict)

if __name__ == "__main__":
db.create_all()
app = create_app()
app.register_blueprint(bp)
db.create_all(app=app)
app.run()
40 changes: 26 additions & 14 deletions flask-bootstrap/test_app.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
import app
import pytest
from app import create_app
from app import db
from app import bp


@pytest.fixture(scope='session')
def test_app():
"""Returns a Flask app for testing"""
app.app.testing = True
an_app = app.app.test_client()
app.db.create_all()
yield an_app
@pytest.fixture
def app(request):
"""
Returns a Flask app for testing
Following https://github.com/pallets/flask/blob/master/examples/flaskr/tests/test_flaskr.py
"""
app = create_app({"TESTING": True})

with app.app_context():
db.init_app(app)
db.create_all(app=app)
app.register_blueprint(bp)
yield app

def test_hello(test_app):
result = test_app.get('/')
assert b"Hello" in result.data

@pytest.fixture
def client(request, app):
a_client = app.test_client()
return a_client

def test_print_users(test_app):
result = test_app.get('/print_users')
assert b"{}" in result.data

def test_hello(client):
result = client.get('/')
assert b"Hello" in result.data


def test_print_users(client):
result = client.get('/print_users')
assert b"{}" in result.data

0 comments on commit 78ef6f0

Please sign in to comment.