tortoise / tortoise/tortoise-orm

tortoise.contrib.test.initializer relies on PGDATABASE env var even if db_url is provided

Open
#1,973 0 comments 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
When supplying a db_url argument to tortoise.contrib.test.initializer which includes the database name
(postgres://user:pass@$host:port/db), it does not respect the DB and instead uses the username supplied as DB name

To Reproduce
Reproducing with:

pytest 8.4.1
tortoise 0.25.1
asyncpg 0.30.0
pytest-asyncio 1.0.0
# conftest.py
@pytest.fixture(scope="session", autouse=True)
def initialize_tests(request):
    db_url = os.environ.get("TEST_DATABASE_URL")

    os.environ["PGDATABASE"] = "dbname"  # <-- removing this should raise:
    # tortoise.exceptions.DBConnectionError: Can't establish connection to default database. Verify environment PGDATABASE

    initializer(["foo.bar"], db_url=db_url)

    request.addfinalizer(finalizer)

Expected behavior
The DB name in the URL should be respected

Additional context
nil

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 at tortoise.contrib.test.initializer and reproduce the issue using the conftest.py fixture and pytest setup shown. Verify that a db_url containing a database name works without PGDATABASE, and run the relevant pytest coverage to confirm the expected connection behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, python
Domain
databases, testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.