Skip to content
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

The odmantic engine do not change database in tests #57

Open
hmtrii opened this issue Nov 20, 2024 · 4 comments
Open

The odmantic engine do not change database in tests #57

hmtrii opened this issue Nov 20, 2024 · 4 comments

Comments

@hmtrii
Copy link

hmtrii commented Nov 20, 2024

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.

@Jibola
Copy link
Collaborator

Jibola commented 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

@pytest_asyncio.fixture(scope="session")
async def db() -> Generator:
    db = MongoDatabase()
    print(db.name) # <----- This new line
    _MongoClientSingleton.instance.mongo_client.get_io_loop = asyncio.get_event_loop
    await init_db(db)
    yield db

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:

@pytest_asyncio.fixture(scope="session")
async def db() -> Generator:
    db = _MongoClientSingleton()[TEST_DATABASE]
    print(db.name) # <----- This new line
    _MongoClientSingleton.instance.mongo_client.get_io_loop = asyncio.get_event_loop
    await init_db(db)
    yield db

@whikwon
Copy link

whikwon commented Feb 14, 2025

@Jibola It appears that since CRUDBase holds self.engine, the call to MongoDatabase() occurs before settings.MONGO_DATABASE is set to TEST_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
Copy link
Collaborator

Jibola commented 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:

  • Would codifying the code reference above suffice?
  • Would exposing a function that changes the reference of the CRUDBase help?

@whikwon
Copy link

whikwon commented 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants