If you haven't heard of it yet, FastAPI is a micro-framewok that allows developers to make full use of modern Python."Micro" here, means that rathe...
For further actions, you may consider blocking this person and/or reporting abuse
was struglying setting up pytest and your post helped a lot. Thanks for that!
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
Briliant!
Thanks :D
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
post
tests, 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 views
fromFastAPI-RESTful
to 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
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 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!
Where does the value of
database_exists
come from?sqlalchemy-utils.readthedocs.io/en...
you need to install this requirement sqlalchemy_utils
Thank you for this post. Is it normal that the
transaction
variable 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 :)