Open
Description
Hi there,
I used this repository to develop my own app. When I ran tests, the database of the Odmantic engine did not change to the test database. I saw the database name was changed to the test name at this code, but the singleton engine is created at this file.
Therefore, the crud methods in unit tests still use production database.
Activity
Jibola commentedon Nov 22, 2024
Hey, sorry to hear you're running into this issue.
Could you walk me through the steps you ran to execute your tests?
Additionally, when you run this test script, could you add
in the test framework code? This will confirm if the database name is viewed as "test" in the tests.
In the meantime, one workaround you can conduct to ensure the
test
database is being called is either calling the_MongoClientSingleton
object directly and yielding that in the asyncio fixture like below:whikwon commentedon Feb 14, 2025
@Jibola It appears that since
CRUDBase
holdsself.engine
, the call toMongoDatabase()
occurs beforesettings.MONGO_DATABASE
is set toTEST_DATABASE
in contest.py, which is causing an issue. How about having the CRUD methods always receive self.engine as an argument instead?In the full-stack-fastapi-template, the code is implemented in that way.
Were there any design considerations behind this approach?
Jibola commentedon Mar 7, 2025
Our design consideration was that to prevent redundant client construction in the tool.
I would definitely advise the workaround as stated above or:
whikwon commentedon Mar 8, 2025
Thank you for reply. I think since there’s no need to dynamically switch the DB during operation, it seems unnecessary to change the CRUDBase reference, and simply receiving the created engine as an argument should be sufficient.