π»Lead Software Engineer at an Eco-Responsible Company β»οΈ |
π Tech Blogger & π οΈ App Builder |
π Always Thriving to Share more Knowledge
The issue with this setup is that the same database session is used for all calls made within a test, meanwhile in a normal workflow a new session is created for each call. This makes tests fail for cases that otherwise work if tested with curl.
Iβm not sure how to make this work yet.
π»Lead Software Engineer at an Eco-Responsible Company β»οΈ |
π Tech Blogger & π οΈ App Builder |
π Always Thriving to Share more Knowledge
in my case, sqlachemy complains with a duplicate key id error. If i check the db, the items_seq_id is in fact not updated. Works fine if i don't put the 'items' fixture in argument of the test though...
Do you see any reason why ? Did you never run into such issue ?
π»Lead Software Engineer at an Eco-Responsible Company β»οΈ |
π Tech Blogger & π οΈ App Builder |
π Always Thriving to Share more Knowledge
Well, it's not aboslutely necessary to do it. You could probably just create a db module to instantiate the session. Because of python import cache, in most cases this would essentially create a singleton. Then you can monkeypatch it using the monkeypatch fixture provided by pytest
However using fastapi dependency injection system is the recommended way to do it. This because it comes with a bunch of really useful features including but not limited to:
Automatic dependency resolution, that guarantee that each dependency is executed only once per request. So if you want to do some request-dependant processing while you instantiate the session, you can use a dependency, and go on about your day.
Pre / Post processing. For example in the post, I'm using it to close the session automatically after processing the resquest. Without it you would have to repeat this every time you use the session. This also means you would have keep track of which endpoint eventually end up using the session. (How to put it an other way, endpoints that are depending on it ;) )
So this is a nice way to decouple your business logic from the request execution path. Without it you'd be force to organize you code around it. This is basically what makes FastAPI a framework and not a library!
π»Lead Software Engineer at an Eco-Responsible Company β»οΈ |
π Tech Blogger & π οΈ App Builder |
π Always Thriving to Share more Knowledge
Nope you're right it's never being used ! Sometimes I test different configurations when writing a post, and it's hard keeping the code sample exactly in sync whit what I'm writing. This is a part of my workflow I need to improve :D
The transaction variable should actually be used for the rollback instead of the session.
Briliant!
Thanks :D
The issue with this setup is that the same database session is used for all calls made within a test, meanwhile in a normal workflow a new session is created for each call. This makes tests fail for cases that otherwise work if tested with
curl.Iβm not sure how to make this work yet.
Edit: I ended up re-creating the database for each test: stackoverflow.com/a/76013246/735926
was struglying setting up pytest and your post helped a lot. Thanks for that!
Thank you! Brilliant job! Did my week!
Thanks Artur ! Knowing that I helped someone on their programming journey always makes my day :D
Hi JB,
I have an issue with this method, it is that for some reason, it doesn't work with
posttests, if you use the 'items' fixture in argumentfor exemple, let's say you write this test:
in my case, sqlachemy complains with a duplicate key id error. If i check the db, the items_seq_id is in fact not updated. Works fine if i don't put the 'items' fixture in argument of the test though...
Do you see any reason why ? Did you never run into such issue ?
a bit cumbersome to pass in ALL the methods the
db: Session = Depends(get_db)parameter.You can also use
class based viewsfromFastAPI-RESTfulto solve thisfastapi-restful.netlify.app/user-g...
Starting with FastAPI 0.95 you can greatly simplify this:
Well, it's not aboslutely necessary to do it. You could probably just create a
dbmodule to instantiate the session. Because of python import cache, in most cases this would essentially create a singleton. Then you can monkeypatch it using the monkeypatch fixture provided by pytestHowever using fastapi dependency injection system is the recommended way to do it. This because it comes with a bunch of really useful features including but not limited to:
So this is a nice way to decouple your business logic from the request execution path. Without it you'd be force to organize you code around it. This is basically what makes FastAPI a framework and not a library!
Thank you for this post. Is it normal that the
transactionvariable intransaction = connection.begin()is never used?Nope you're right it's never being used ! Sometimes I test different configurations when writing a post, and it's hard keeping the code sample exactly in sync whit what I'm writing. This is a part of my workflow I need to improve :D
The transaction variable should actually be used for the rollback instead of the session.
Thanks for pointing it out, I'll correct it :)
Where does the value of
database_existscome from?sqlalchemy-utils.readthedocs.io/en...
you need to install this requirement sqlalchemy_utils