I'm using Flask-SQLAlchemy. My app is created with an app_factory as suggested in docs and my db connection is created with flask-sqlalchemy; both in a main.py file. I have a manage.py file where flask-script is configured. I'm trying to set up flask-testing with it and facing the following problem: In flask-testing docs, it says that I should create a new app in setup, configured for testing. That is reasonable. The problem is my db instance is created in my main.py file and imported from other modules from there. As I'm creating a app in my TestCase.setUp, I suppose my db instance will be imported misconfigured. So, has anyone faced this problem? How do you solve it? Tips? main.py -> app_factory is there; app is created there; db connection instance is created there with flask-sqlalchemy; manage.py -> has flask-script; imports tests modules. tests -> has BaseTestCase. code example <http://dpaste.org/RwaJA/> -- "A arrogância é a arma dos fracos." =========================== Italo Moreira Campelo Maia Bacharel em Ciência da Computação - UECE Desenvolvedor WEB e Desktop (Java, Python, Lua) Coordenador do Pug-CE ----------------------------------------------------- http://www.italomaia.com/ http://twitter.com/italomaia/ http://eusouolobomau.blogspot.com/ ----------------------------------------------------- Turtle Linux 9.10 - http://tiny.cc/blogturtle910 Turtle Linux 10.10 - http://bit.ly/cEw4ET ===========================
On Sep 23, 2011, at 8:27 PM, Italo Maia wrote:
> In flask-testing docs, it says that I should create a new app in setup,
configured for testing. That is reasonable. The problem is my db instance
is created in my main.py file and imported from other modules from there.
As I'm creating a app in my TestCase.setUp, I suppose my db instance will
be imported misconfigured. So, has anyone faced this problem? How do you
solve it? Tips?
Are you linking the database to the application at module level? Consider
doing the binding later, in a factory function. That way, before the
factory function is invoked, you have a chance to set different
configuration values for the database.
http://packages.python.org/Flask-SQLAlchemy/contexts.html?highlight=init_app
Cheers,
-- Alex
Alex, I took your tip. Things "seem" to be working. I think I'll create another thread regarding some flask-sqlalchemy behavior. As Craig said he keeps his connection in the g thread local, I wonder how to use the db from flaskext-sqlalchemy and best practices regarding the request start and end. Here is a sample on how my code looks right now: http://dpaste.org/anMEB/ 2011/9/23 Alex Morega <alex@grep.ro> > > On Sep 23, 2011, at 8:27 PM, Italo Maia wrote: > > > In flask-testing docs, it says that I should create a new app in setup, > configured for testing. That is reasonable. The problem is my db instance is > created in my main.py file and imported from other modules from there. As > I'm creating a app in my TestCase.setUp, I suppose my db instance will be > imported misconfigured. So, has anyone faced this problem? How do you solve > it? Tips? > > Are you linking the database to the application at module level? Consider > doing the binding later, in a factory function. That way, before the factory > function is invoked, you have a chance to set different configuration values > for the database. > > > http://packages.python.org/Flask-SQLAlchemy/contexts.html?highlight=init_app > > Cheers, > -- Alex > > -- "A arrogância é a arma dos fracos." =========================== Italo Moreira Campelo Maia Bacharel em Ciência da Computação - UECE Desenvolvedor WEB e Desktop (Java, Python, Lua) Coordenador do Pug-CE ----------------------------------------------------- http://www.italomaia.com/ http://twitter.com/italomaia/ http://eusouolobomau.blogspot.com/ ----------------------------------------------------- Turtle Linux 9.10 - http://tiny.cc/blogturtle910 Turtle Linux 10.10 - http://bit.ly/cEw4ET ===========================
So far, the following architecture works for me: > db created with flaskext.sqlalchemy > db instance created in a separated database.py, not initialized. > db instance imported from within app_factory and configured there (in a main.py file) > app.db points to configured db instance (have no idea if this is a bad decision) So, if i want access to the db session from within a request, I just use current_app.db.session . Other nice consequence of this is that I can import a model from a blueprint models.py without circular import. 2011/9/23 Italo Maia <italo.maia@gmail.com> > Alex, I took your tip. Things "seem" to be working. I think I'll create > another thread regarding some flask-sqlalchemy behavior. > As Craig said he keeps his connection in the g thread local, I wonder how > to use the db from flaskext-sqlalchemy and best practices regarding the > request start and end. > > Here is a sample on how my code looks right now: http://dpaste.org/anMEB/ > > > 2011/9/23 Alex Morega <alex@grep.ro> > >> >> On Sep 23, 2011, at 8:27 PM, Italo Maia wrote: >> >> > In flask-testing docs, it says that I should create a new app in setup, >> configured for testing. That is reasonable. The problem is my db instance is >> created in my main.py file and imported from other modules from there. As >> I'm creating a app in my TestCase.setUp, I suppose my db instance will be >> imported misconfigured. So, has anyone faced this problem? How do you solve >> it? Tips? >> >> Are you linking the database to the application at module level? Consider >> doing the binding later, in a factory function. That way, before the factory >> function is invoked, you have a chance to set different configuration values >> for the database. >> >> >> http://packages.python.org/Flask-SQLAlchemy/contexts.html?highlight=init_app >> >> Cheers, >> -- Alex >> >> > > > -- > "A arrogância é a arma dos fracos." > > =========================== > Italo Moreira Campelo Maia > Bacharel em Ciência da Computação - UECE > Desenvolvedor WEB e Desktop (Java, Python, Lua) > Coordenador do Pug-CE > ----------------------------------------------------- > http://www.italomaia.com/ > http://twitter.com/italomaia/ > http://eusouolobomau.blogspot.com/ > ----------------------------------------------------- > Turtle Linux 9.10 - http://tiny.cc/blogturtle910 > Turtle Linux 10.10 - http://bit.ly/cEw4ET > =========================== > -- "A arrogância é a arma dos fracos." =========================== Italo Moreira Campelo Maia Bacharel em Ciência da Computação - UECE Desenvolvedor WEB e Desktop (Java, Python, Lua) Coordenador do Pug-CE ----------------------------------------------------- http://www.italomaia.com/ http://twitter.com/italomaia/ http://eusouolobomau.blogspot.com/ ----------------------------------------------------- Turtle Linux 9.10 - http://tiny.cc/blogturtle910 Turtle Linux 10.10 - http://bit.ly/cEw4ET ===========================
I use connection pooling and put a connection in the thread local variable g at the start of a request using before_request() Craig Younkins On Fri, Sep 23, 2011 at 1:27 PM, Italo Maia <italo.maia@gmail.com> wrote: > I'm using Flask-SQLAlchemy. My app is created with an app_factory as > suggested in docs and my db connection is created with flask-sqlalchemy; > both in a main.py file. > > I have a manage.py file where flask-script is configured. I'm trying to set > up flask-testing with it and facing the following problem: > > In flask-testing docs, it says that I should create a new app in setup, > configured for testing. That is reasonable. The problem is my db instance is > created in my main.py file and imported from other modules from there. As > I'm creating a app in my TestCase.setUp, I suppose my db instance will be > imported misconfigured. So, has anyone faced this problem? How do you solve > it? Tips? > > main.py -> app_factory is there; app is created there; db connection > instance is created there with flask-sqlalchemy; > manage.py -> has flask-script; imports tests modules. > tests -> has BaseTestCase. > > code example <http://dpaste.org/RwaJA/> > > > -- > "A arrogância é a arma dos fracos." > > =========================== > Italo Moreira Campelo Maia > Bacharel em Ciência da Computação - UECE > Desenvolvedor WEB e Desktop (Java, Python, Lua) > Coordenador do Pug-CE > ----------------------------------------------------- > http://www.italomaia.com/ > http://twitter.com/italomaia/ > http://eusouolobomau.blogspot.com/ > ----------------------------------------------------- > Turtle Linux 9.10 - http://tiny.cc/blogturtle910 > Turtle Linux 10.10 - http://bit.ly/cEw4ET > =========================== >