-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4783647
commit c70755d
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|