Last active
April 30, 2019 18:38
-
-
Save dpmccabe/6d3bed81c37817c38fb3f2b262d1393d to your computer and use it in GitHub Desktop.
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
from flask import Flask | |
def create_app(): | |
app = Flask('testapp') | |
# omitted for brevity: load app config | |
# configure database | |
from app.lib.db import init_engine, db_session | |
init_engine(app.config['DATABASE_URI']) | |
# close database session during teardown | |
@app.teardown_appcontext | |
def shutdown_session(exception=None): | |
db_session.remove() | |
from app.example import bp | |
app.register_blueprint(bp) | |
return app | |
if __name__ == "__main__": | |
app = create_app() | |
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
from sqlalchemy import create_engine | |
from sqlalchemy.orm import scoped_session, sessionmaker | |
from sqlalchemy.ext.declarative import declarative_base | |
engine = None | |
made_session = sessionmaker(autocommit=False) | |
db_session = scoped_session(made_session) | |
Base = declarative_base() | |
def init_engine(uri): | |
global made_session, engine, session, Base | |
engine = create_engine(uri, echo=True, pool_pre_ping=True) | |
db_session.remove() | |
made_session.configure(bind=engine) | |
Base.query = db_session.query_property() | |
def init_db(): | |
Base.metadata.create_all(bind=engine) |
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
from flask import Blueprint | |
from app.models.person import Person | |
from app.db import db_session | |
bp = Blueprint('example', __name__, url_prefix='/example') | |
@bp.route('/form', methods = ['POST']) | |
def test_form(): | |
return str(db_session.query(Person).count()) | |
@bp.route('/json', methods = ['POST']) | |
def test_json(): | |
return str(db_session.query(Person).count()) |
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
from sqlalchemy import Column, String | |
from app.lib.db import Base, db_session | |
class Person(Base): | |
__tablename__ = 'people' | |
id = Column(String, primary_key=True) |
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
import pytest | |
from app import create_app | |
@pytest.fixture(scope='session') | |
def client(): | |
"""A test instance of the app in testing mode""" | |
app = create_app() | |
client = app.test_client() | |
yield client |
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
import factory | |
from app.lib.db import db_session | |
from app.models.person import Person | |
class PersonFactory(factory.alchemy.SQLAlchemyModelFactory): | |
class Meta: | |
model = Person | |
sqlalchemy_session = db_session | |
id = factory.Faker('uuid4') |
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
from flask import json | |
from tests.people.person_factories import PersonFactory | |
class TestExample: | |
def test_form(self, client): | |
person = PersonFactory() | |
resp = client.post('/example/form', data={'foo': 'bar'}) | |
assert resp.data == b'1' | |
def test_json(self, client): | |
person = PersonFactory() | |
resp = client.post('/example/json', json={'foo': 'bar'}) | |
assert resp.data == b'1' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment