Skip to content

Commit

Permalink
basic sqlalchemy + flask + pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
marksibrahim committed Aug 26, 2017
1 parent 4783647 commit c70755d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
45 changes: 45 additions & 0 deletions flask-bootstrap/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from flask import Flask
from flask import jsonify
from flask_sqlalchemy import SQLAlchemy


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


class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
email = db.Column(db.String(120), unique=True)

def __init__(self, username, email):
self.username = username
self.email = email

def __repr__(self):
return '<User %r>' % self.username

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


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

@app.route('/print_users')
def print_users():
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.run()
23 changes: 23 additions & 0 deletions flask-bootstrap/test_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import app
import pytest


@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


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


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


0 comments on commit c70755d

Please sign in to comment.