Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issues with using the application factory pattern #32

Open
sebastianelsner opened this issue Jul 17, 2020 · 1 comment
Open

issues with using the application factory pattern #32

sebastianelsner opened this issue Jul 17, 2020 · 1 comment

Comments

@sebastianelsner
Copy link

I am using the application factory pattern as described in the flask docs: https://flask.palletsprojects.com/en/1.1.x/patterns/appfactories/ , e.g.:

def create_app():
    app = Flask(__name__)

    from yourapplication.model import db
    db.init_app(app)

As described in the docs here, I would then do:

from yourapplication.model import db

@pytest.fixture(scope="session")
def app():
    app = create_app()
    return app

@pytest.fixture(scope="session")
def _db(app):
    db.init_app(app)
    return db

but this leads to a "RuntimeError: No application found...." in my test

_______________________ ERROR at setup of test_set_name ________________________

request = <SubRequest '_transaction' for <Function test_set_name>>
_db = <SQLAlchemy engine=None>
mocker = <pytest_mock.plugin.MockFixture object at 0x7f98a6919be0>

    @pytest.fixture(scope='function')
    def _transaction(request, _db, mocker):
        '''
        Create a transactional context for tests to run in.
        '''
        # Start a transaction
>       connection = _db.engine.connect()

.venv/lib64/python3.8/site-packages/pytest_flask_sqlalchemy/fixtures.py:31: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
.venv/lib64/python3.8/site-packages/flask_sqlalchemy/__init__.py:943: in engine
    return self.get_engine()
.venv/lib64/python3.8/site-packages/flask_sqlalchemy/__init__.py:952: in get_engine
    app = self.get_app(app)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <SQLAlchemy engine=None>, reference_app = None

    def get_app(self, reference_app=None):
        """Helper method that implements the logic to look up an
        application."""
    
        if reference_app is not None:
            return reference_app
    
        if current_app:
            return current_app._get_current_object()
    
        if self.app is not None:
            return self.app
    
>       raise RuntimeError(
            'No application found. Either work inside a view function or push'
            ' an application context. See'
            ' http://flask-sqlalchemy.pocoo.org/contexts/.'
        )
E       RuntimeError: No application found. Either work inside a view function or push an application context. See http://flask-sqlalchemy.pocoo.org/contexts/.

.venv/lib64/python3.8/site-packages/flask_sqlalchemy/__init__.py:987: RuntimeError

This also gives the same error:

@pytest.fixture(scope="session")
def _db(app):
    db = SQLAlchemy()
    db.init_app(app)
    return db

But this works:

@pytest.fixture(scope="session")
def _db(app):
    db = SQLAlchemy(app=app)
    db.init_app(app) 
    return db

The "db.init_app(app)" is double, but it shows, that this statement does not acutally do something bad.

How do I correctly use this and why is the "init_app" different?

@tytuseczek
Copy link

tytuseczek commented Dec 1, 2021

I have the same problem. But for me even the solution presented by @sebastianelsner does not work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants