├── .gitignore ├── LICENSE ├── README.md ├── pytest_flask_sqlalchemy ├── __init__.py └── plugin.py └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | dist/ 3 | .idea/ 4 | *.pyc 5 | *.egg-info 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 ESSS 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pytest-flask-sqlalchemy 2 | 3 | [DEPRECATED] Check [jeancochrane/pytest-flask-sqlalchemy](https://github.com/jeancochrane/pytest-flask-sqlalchemy) for a more robust implementation of a Flask-SQLAlchemy plugin 4 | -------------------------------------------------------------------------------- /pytest_flask_sqlalchemy/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ESSS/pytest-flask-sqlalchemy/d9467dbf02e78de52d88228a40cbe7c48cb1972d/pytest_flask_sqlalchemy/__init__.py -------------------------------------------------------------------------------- /pytest_flask_sqlalchemy/plugin.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | 4 | @pytest.fixture(scope='session') 5 | def db_engine(flask_app): 6 | """ 7 | Setup the database for a test session and drop all tables after the 8 | session ends. It is not intended to be used on tests functions, 9 | use `db_session` instead. 10 | """ 11 | db = flask_app.extensions['sqlalchemy'].db 12 | with flask_app.app_context(): 13 | db.create_all() 14 | yield db.engine 15 | db.drop_all() 16 | 17 | 18 | @pytest.fixture() 19 | def db_session(flask_app, db_engine, request): 20 | """ 21 | Creates a new database transaction for the test and roll it back 22 | when the test is completed 23 | """ 24 | db = flask_app.extensions['sqlalchemy'].db 25 | connection = db_engine.connect() 26 | transaction = connection.begin() 27 | 28 | options = dict(bind=connection, binds={}) 29 | session = db.create_scoped_session(options=options) 30 | 31 | db.session = session 32 | 33 | def finalize(): 34 | db.session.close() 35 | transaction.rollback() 36 | connection.close() 37 | 38 | request.addfinalizer(finalize) 39 | return session 40 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | 3 | setup( 4 | name="pytest-flask-sqlalchemy", 5 | packages = ['pytest_flask_sqlalchemy'], 6 | license='MIT', 7 | description='pytest plugin with sqlalchemy related fixtures', 8 | version='0.1.0', 9 | author='ESSS', 10 | author_email='igor@esss.co', 11 | url='http://github.com/ESSS/pytest-flask-sqlalchemy/', 12 | py_modules=['pytest_flask_sqlalchemy'], 13 | entry_points = { 14 | 'pytest11': [ 15 | 'flask_sqlalchemy = pytest_flask_sqlalchemy.plugin', 16 | ] 17 | }, 18 | classifiers=[ 19 | "Framework :: Pytest", 20 | ], 21 | ) --------------------------------------------------------------------------------