Skip to content

The odmantic engine do not change database in tests #57

Open
@hmtrii

Description

@hmtrii

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

Jibola commented on Nov 22, 2024

@Jibola
Collaborator

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

whikwon commented on Feb 14, 2025

@whikwon

@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

Jibola commented on Mar 7, 2025

@Jibola
Collaborator

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

whikwon commented on Mar 8, 2025

@whikwon

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @Jibola@whikwon@hmtrii

        Issue actions

          The odmantic engine do not change database in tests · Issue #57 · mongodb-labs/full-stack-fastapi-mongodb