-
-
Notifications
You must be signed in to change notification settings - Fork 390
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
Add proper error messages and a doc page dedicated for testing #1611
Comments
OK, I found Anyway, I got back to a place where I try to create an empty test case, this time armed with initialzier and finalizer methods. I have this:
On the DB I ran
I also created an empty DB with name test_mydb When I run the above, I'm getting
|
I made it! Apparently, one can't use postgres for the test database. When using default which apparently is The final version which is working:
|
Hi! Sad to see you struggling like that Good that you made it work with sqlite, although I think it should work with postgres too. In our internal testcase we use this pytest fixture:
And it works with all databases that in our testcase If you have ideas how to improve documentation - I will be glad to accept PR with improvements on this part |
My frustration continues, but I'm closing this. I'll create e new thread with full example. |
It's a very nice library but extremely bad at handling initialization errors and almost no documentation on how to initialize things for unit tests (the only thing is a simple fastapi example using pytest).
Consider someone installing this lib and writing their first unit test. It may look like this:
This test will not pass. It will raise
KeyError: 'apps'
. The message is very confusing. It should return a human-friendly message saying something like "Tortoise ORM was not initialized. You need to call Tortoise.init first", pointing the user to the right direction.But OK, we happen to be an experienced django dev, right? We understand it's something like trying to call manage.py without DJANGO_SETTINGS_MODULE pointing to a correct settings, right? OK, so we call init(). Let's say we do this:
What is the expected behaviour here? Welll it will still not pass. The error will be
AttributeError: 'EmptyTest' object has no attribute '_transaction'
. Wait, what? What transaction? What is it about? But luckily, we vaguely remembered tutorial saying something about "importance of cleaning up". We're GUESSING (coz we don't know that from the error message) that maybe we're missing close_connections() on teardown:And only after all that guessing game, we finally made an empty test which is actually passing.
OK. The test is passing so let's now try to call fastapi endpoint from it. We have a websocket endpoint which makes a simple DB get_or_create query and automatically accepts webscocket connection. We tested it from Postman. It works 100%. The DB connection string is correct, everything is correct. Everything works so nicely with Tortoise & Fastapi when we're making websocket connection from Postman. So we want to do that programmatically. So we write the following test:
We run the test. We're getting
RuntimeError: Event loop is closed
. Hmm... wait, maybe it's our fault. Our Fastapi app has a lifespan function where we runTortoise.init()
on startup andclose_connections()
on shutdown (BTW, this is not documented anywhere, the documentation is completely wrong on how to actually initialize the TortoiseORM with FastAPI - see my other ticket . OK, so maybe the Fastapi test client is already calling the lifespan function and doing the initialization and close_connection for us and this is causing the error? Let's remove startUp and tearDown functions we wrote earlier, it's now kind of similar to what's in the FastAPI example page, just the test client, making websocket connection.So we remove the setUP and tearDown classes from the test. We run the test.
We're getting
KeyError: 'apps'
error again. Aghrrrr!!!!!!!!!!I have an absurd situation where I have a perfectly working functionality of my websocket FastAPI endpoint, with TortoiseORM making queries successfully, but I have zero clue on how to write a goddamn 1 line unit test.
The text was updated successfully, but these errors were encountered: