So this is a nice way to decouple your business logic from the request execution path. (How to put it an other way, endpoints that are depending on it ) ) This also means you would have keep track of which endpoint eventually end up using the session. Without it you would have to repeat this every time you use the session. For example in the post, I'm using it to close the session automatically after processing the resquest. 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. Automatic dependency resolution, that guarantee that each dependency is executed only once per request.This because it comes with a bunch of really useful features including but not limited to: However using fastapi dependency injection system is the recommended way to do it. Then you can monkeypatch it using the monkeypatch fixture provided by pytest Because of python import cache, in most cases this would essentially create a singleton. You could probably just create a db module to instantiate the session. ![]() Well, it's not aboslutely necessary to do it. To to that, create a file called test_database.py to write our tests, and add the following code to it : Since our endpoint receives its session by dependency injection, we can use Dependency overrides to replace it with a session pointing to a test Database. We also want them to be deterministic and reusable, let's see how to do exactly that !įirst let's try the method described in the official documentation. We need to make sure that our tests don't affect the main database. That's great! But our goal here is to test the endpoint automatically with some unit tests. We can check that our endpoint works thanks to the Swagger available on the /docs endpoint. This is important to note as it'll help us easily test our endpoint as we'll see in the next section. Notice that we pass the SQLAlchemy Session as a dependency to our endpoint. Let's configure it in a file called database.py:Įnter fullscreen mode Exit fullscreen mode SQLAlchemy works by creating a declarative base, which allows us to describe our Database tables as Python classes that inherits it. I personnaly prefer to use docker-compose to run a Fastapi image as well as a Postgres database next to it. Simply clone the repo and call docker-compose up to launch everything.įirst things first, we need something to test ! We'll create a small project with an endpoint to create an "Item" with a title and a description and store it in the database. ![]() The code presented in this article is fully available on Github. That's what we'll be covering in this article ! But if like me, you come from Django, you might still struggle to configure everything so that each test works in isolation, and leaves the database as it was before running. There is a guide to help you set it up, and a tutorial wich gives some indications on how to go about testing it. ![]() SQLAlchemy is the one documented by Fast API. ![]() You'll have to choose and install an ORM. For example, if you want to use a relational database, However it does mean that you have to set up yourself some things that usually comes out of the box in other frameworks. Everything else is up to you which allows you to tailor you architecture to perfectly fit your needs. 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 rather than trying to cover every use case, it focuses on doing a single thing extremely well: giving you the tools to build a fast (duh) API.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |