Last active
July 10, 2018 22:38
-
-
Save AbdouSeck/fbdfc9f3ded29e5fd534297de9c8e2e6 to your computer and use it in GitHub Desktop.
Flask dummy app with unittest to provide a response to the stackoverflow question at https://stackoverflow.com/q/51252936/3135417
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
# application.py | |
from flask import Flask, render_template_string | |
from flask_sqlalchemy import SQLAlchemy | |
db = SQLAlchemy() | |
class User(db.Model): | |
""" | |
User model | |
""" | |
__tablename__ = 'users' | |
user_id = db.Column(db.Integer, primary_key=True) | |
first_name = db.Column(db.String(100)) | |
last_name = db.Column(db.String(100)) | |
age = db.Column(db.Integer) | |
def __init__(self, user_id, first, last, age): | |
self.user_id = int(user_id) | |
self.first_name = first | |
self.last_name = last | |
self.age = int(age) | |
@classmethod | |
def from_string(cls, user_info): | |
return cls(*user_info.split("_")[:4]) | |
app = Flask(__name__) | |
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test_database.db' | |
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False | |
app.config['DEBUG'] = True | |
db.init_app(app) | |
@app.route('/') | |
def index(): | |
msg = """hello! Welcome to the index page. Go to <a href="{{ url_for('welcome') }}">Welcome Page</a>""" | |
return render_template_string(msg) | |
@app.route('/add/<user_string>') | |
def add_user(user_string): | |
try: | |
user = User.from_string(user_string) | |
except Exception as excp: | |
print(excp) | |
error_message = """The info provided, {{ info }}, could not be parsed into a user""" | |
return render_template_string(error_message, info=user_string) | |
if User.query.filter_by(user_id=user.user_id).first(): | |
msg = """User ID {{ u.user_id }} with name {{ u.first_name + " " + u.last_name }} already exists.""" | |
else: | |
db.session.add(user) | |
db.session.commit() | |
msg = """User ID {{ u.user_id }} with name {{ u.first_name + " " + u.last_name }} got added to DB.""" | |
return render_template_string(msg, u=user) | |
@app.route('/welcome') | |
def welcome(): | |
msg = """This actually is the welcome page. <a href="{{ url_for('index') }}">Landing Page</a>""" | |
return render_template_string(msg) | |
if __name__ == '__main__': | |
app.run() | |
# test_app.py | |
import unittest | |
from application import app as application, db | |
TEST_DB = "sqlite:///unittest_database.db" | |
class BasicTests(unittest.TestCase): | |
def setUp(self): | |
application.app_context().push() | |
application.config['TESTING'] = True | |
application.config['DEBUG'] = False | |
application.config['SQLALCHEMY_DATABASE_URI'] = TEST_DB | |
self.client = application.test_client() | |
db.drop_all() | |
db.create_all() | |
self.assertEqual(application.debug, False) | |
def tearDown(self): | |
db.session.close() | |
db.drop_all() | |
def test_welcome_page(self): | |
response = self.client.get("/welcome", follow_redirects=True) | |
msg = b"""This actually is the welcome page.""" | |
self.assertEqual(response.status_code, 200) | |
self.assertIn(msg, response.data) | |
def test_add_user(self): | |
response = self.client.get('/add/1_Luke_Kenworthy_18', follow_redirects=True) | |
msg = b"""User ID 1 with name Luke Kenworthy got added to DB.""" | |
self.assertIn(msg, response.data) | |
def test_repeated_adds(self): | |
self.client.get('/add/1_Luke_Kenworthy_18', follow_redirects=True) | |
response = self.client.get('/add/1_Luke_Kenworthy_18', follow_redirects=True) | |
msg = b"""User ID 1 with name Luke Kenworthy already exists.""" | |
self.assertEqual(msg, response.data) | |
if __name__ == "__main__": | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment