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 · 1 comment
Open

The odmantic engine do not change database in tests #57

hmtrii opened this issue Nov 20, 2024 · 1 comment

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

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

2 participants