tortoise / tortoise/tortoise-orm

Connection closes if save method was triggered in pytest.

Open
#1,558 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

Describe the bug
Whenever I'm testing my API built with Litestar and Tortoise ORM, the connection closes if there's an interac

To Reproduce

    async def test_verify_email(client, test_user: User):
        otp = "111111"
    
        # Verify that the email verification fails with an invalid otp
        response = await client.post(
            f"{BASE_URL_PATH}/verify-email", json={"email": test_user.email, "otp": otp}
        )
        assert response.status_code == 404
        assert response.json() == {
            "status": "failure",
            "code": ErrorCode.INCORRECT_OTP,
            "message": "Incorrect Otp",
        }
        # Verify that the email verification succeeds with a valid otp
        otp = await Otp.create(user_id=test_user.id)
    
        response = await client.post(
            f"{BASE_URL_PATH}/verify-email",
            json={"email": test_user.email, "otp": otp.code},
        )
        assert response.status_code == 200
        assert response.json() == {
            "status": "success",
            "message": "Account verification successful",
        }

The test_user is a fixture that creates a test user. But once it does the test fails with connection closed error at this line:

response = await client.post(
    f"{BASE_URL_PATH}/verify-email", json={"email": test_user.email, "otp": otp}
)

If the client and test_user parameter are interchanged like this: test_user: User, client, test still fails with the same error but in this line: Otp.create(user_id=test_user.id). So whichever goes first, it closes the connection unexpectedly.

Here's my db config:

MODELS = [
    "app.db.models.base",
    "app.db.models.general",
    "app.db.models.accounts",
]

TORTOISE_ORM = {
    "connections": {"default": settings.TORTOISE_DATABASE_URL},
    "apps": {
        "models": {
            "models": MODELS
            + [
                "aerich.models",
            ],
            "default_connection": "default",
        },
    },
}


@asynccontextmanager
async def lifespan(app: Litestar):
    await Tortoise.init(config=TORTOISE_ORM)
    yield
    await connections.close_all()

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the pytest reproduction and the Litestar lifespan shown in the issue, especially Tortoise.init(config=TORTOISE_ORM) and connections.close_all(). Trace which fixture or request causes the connection to close, then verify the reproduction no longer fails at client.post or Otp.create and that lifecycle cleanup still occurs.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
databases, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.