librelist archives

« back to archive

Flask.before_first_request not called by test_request_context

Flask.before_first_request not called by test_request_context

From:
Alex Morega
Date:
2011-09-25 @ 20:32
Hi,

I'm using the new `before_first_request` hook to set up some database 
access, and this seems inconsistent: `test_request_context` does not 
trigger `try_trigger_before_first_request_functions`. Is this a design 
decision? My feeling is that testing and management scripts would be a bit
easier to write.

Best regards,
-- Alex

Re: [flask] Flask.before_first_request not called by test_request_context

From:
Armin Ronacher
Date:
2011-09-25 @ 20:42
Hi,

That is correct.  Our stance on this issue is that database connections 
should not be started up on each request but on first usage and then 
cached on the request context.  We do however guarantee that the 
teardown handlers are called when closing the test request context.

So do things like this:

def get_connection():
     con = getattr(g, 'db_con', None)
     if con is None:
         con = g.con = connect_to_database()
     return con

Why?  Many reasons:

1. You only connect when really necessary.  Note very page will need
    the database connection.
2. Makes it easier to test the code.
3. Easier to mock a different object on there.

Why don't we run before-request handlers when creating the test context? 
  First of all because it would be a waste.  Test contexts are created 
for a variety of reasons and for the majority of the use cases you don't 
want the handlers to be called.  Test request contexts are created on 
mass in testsuites for testing isolates parts of the system where 
databases are not involved.

Also before-request handlers do other things than setting up resources. 
  Consider a maintenance hook that wants to disable all requests while 
the application is in maintenance mode.  In that situation it will 
return a replacement page for each request.

For a general introduction to this sort of thing check the testing docs 
and the sqlite pattern which shows how to connect on demand.


Regards,
Armin

Re: [flask] Flask.before_first_request not called by test_request_context

From:
Alex Morega
Date:
2011-09-25 @ 21:14
On Sep 25, 2011, at 11:42 PM, Armin Ronacher wrote:

> That is correct.  Our stance on this issue is that database connections 
> should not be started up on each request but on first usage and then 
> cached on the request context.  We do however guarantee that the 
> teardown handlers are called when closing the test request context.

Thank you for the detailed answer, but I was asking about 
before_*first*_request. The database part isn't important, I can achieve 
the same result in different ways.

My point was: should `try_trigger_before_first_request_functions` be 
called from `test_request_context`? Since you would put one-time 
initialization code in there, it makes sense to run it before any code 
that expects a fully configured application.

Cheers,
-- Alex