-
-
Notifications
You must be signed in to change notification settings - Fork 355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to run unit tests using tortoise-orm initializer() with a postgres database #1639
Comments
Does your environment has |
no |
I think to understand error you first need to understand what exactly initialiser doing:
Your error occurs because it's connection to default db failed, as it not exists. For most of postgres clients default db equals to user name. If your user was So you have few options to workaround:
Note that default database can't be equal to database you want to run your tests against, as you can't create/delete database you are already connected to |
Thanks for the explanation @abondar . I have changed the user to Can you explain the 4th option? "do not use initializer/finalizer at all, and manage database yourself" My fastapi application is set up using register_tortoise() function and I create a TestClient for it to run unit tests. But when any of the routes being tested read/write using a tortoise Model, I get this error:
This is the config dict I am passing to register_tortoise:
Router definition:
These problems are all faced during unit test runs only. When I run the application and test each endpoint via swagger manually everything works perfectly with no changes in code or configuration. However, I cannot run manual tests every time so it is imperative for me to get unit tests running or else I will have to switch back to other ORM. |
Can you try changing your database in to something like That how we run tests internally and it works, at least here
In 0.21 version we introduced new way to register Tortoise with fastapi - Here how we use it in example There is also simple test configuration for fast api in example But overall error you are getting indicates that for some reason tortoise wasn't properly initialised, because if it was - For diagnostics - you can check in debugger if
It just means that you create database and migrate it to needed state beforehand, using some external tools or manually, and then you run tests |
Thanks for the pointers! The fix I made is different though so I will try to explain here (maybe it will help someone else using Fastapi with tortoise-orm and trying to write unit tests like me) Finally got it to work changing the way I used the FastApi TestClient.
This gave me the exception:
Now I switched to using it so that startup and shutdown events get triggered as described in https://fastapi.tiangolo.com/advanced/testing-events/
This helped and my tests are running now. They run fine whether I use RegisterTortoise with lifespan or register_tortoise but with the deprecation at FastApi, the better approach is to use RegisterTortoise with lifespan as mentioned by @abondar
|
Describe the bug
We are unable to test using the initializer() when using a postgres db.
When initializer is called
initializer(['src.models'], db_url="asyncpg://your_postgres_user:your_postgres_password@localhost:5432/your_postgres_db")
the config generated by the tortoise code is
Here the connection name is also "models" (same as app_name which is "models" by default)
This results in an exception:
To Reproduce
Just run this code in python prompt:
Expected behavior
No exception. I have specified a complete db_url so I do not expect any environment variable to be used for this.
Additional context
This is the result of the reproduction along with complete stack trace.
Here if you see with_db=False is hard-coded.
So in
create_connection
the value ofself._template["database"]
is always None. That seems to be the reason for the exception insidecreate_pool
. Please check.The text was updated successfully, but these errors were encountered: