Ming Linfeng
Ming Linfeng

Reputation: 21

How to properly configure pytest with FastAPI and Tortoise ORM?

I am trying to configure the tests. According to the tortoise orm documentation I create this test configuration file:

import pytest
from fastapi.testclient import TestClient
from tortoise.contrib.test import finalizer, initializer

import app.main as main
from app.core.config import settings

@pytest.fixture(scope="session", autouse=True)
def initialize_tests(request):
    db_url = "postgres://USERNAME_HERE:[email protected]:5432/test"
    initializer(
        [
            "app.models",
        ],
        db_url=db_url,
        app_label="models"
    )
    print("initialize_tests")
    request.add_finaliser(finalizer)

@pytest.fixture(scope="session")
def client():
    app = main.create_application()
    with TestClient(app) as client:
        print("client")
        yield client

And the test file looks like this:

def test_get(client):
    response = client.get("/v1/url/")
    assert response.status_code == 200

I try to run the tests, but I get this error:

asyncpg.exceptions._base.InterfaceError: cannot perform operation: another operation is in progress

I have found that some users don't use initializer and finalizer and do everything manually. Testing in FastAPI using Tortoise-ORM https://stackoverflow.com/a/66907531 But that doesn't look like the clear solution. Question: Is there a way to make the tests work using initializer and finalizer?

Upvotes: 2

Views: 689

Answers (2)

Adrian Makridenko
Adrian Makridenko

Reputation: 17

@pytest_asyncio.fixture(scope="session", autouse=True)
async def init_test_db():
    db_url = "sqlite://:memory:"
    await Tortoise.init(db_url=db_url, modules={"modules": ["app.models"]})
    await Tortoise.generate_schemas()
    yield
    await Tortoise._drop_databases()

I did it like this

Upvotes: 1

Faisal P
Faisal P

Reputation: 11

import pytest
from fastapi.testclient import TestClient
from tortoise import Tortoise
from tortoise.contrib.fastapi import register_tortoise
from main import app

@pytest.fixture(scope="module")
def test_client():
    # Register Tortoise with a test database
    register_tortoise(
        app,
        db_url="sqlite://:memory:",  # In-memory test database
        modules={"models": ["src.models"]},  # Path to your models
        generate_schemas=True,  # Automatically generate schemas for testing
        add_exception_handlers=True,  # Include exception handlers
    )
    with TestClient(app) as c:
        yield c

this works for me :)

Upvotes: 1

Related Questions