Skip to content

Commit

Permalink
fix(postgres): get_connection_url(driver=None) should return postgres…
Browse files Browse the repository at this point in the history
…://... (#588)

Fixes #587
  • Loading branch information
oliverlambson authored Jun 28, 2024
1 parent a962147 commit 01d6c18
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion modules/postgres/testcontainers/postgres/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def get_connection_url(self, host: Optional[str] = None, driver: Optional[str] =
driver. The optional driver argument to :code:`get_connection_url` overwrites the constructor
set value. Pass :code:`driver=None` to get URLs without a driver.
"""
driver_str = self.driver if driver is _UNSET else f"+{driver}"
driver_str = "" if driver is None else self.driver if driver is _UNSET else f"+{driver}"
return super()._create_connection_url(
dialect=f"postgresql{driver_str}",
username=self.username,
Expand Down
24 changes: 24 additions & 0 deletions modules/postgres/tests/test_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,27 @@ def test_show_how_to_initialize_db_via_initdb_dir():
result = result.fetchall()
assert len(result) == 1
assert result[0] == (1, "sally", "sells seashells")


def test_none_driver_urls():
user = "root"
password = "pass"
kwargs = {
"username": user,
"password": password,
}
with PostgresContainer("postgres:16-alpine", driver=None, **kwargs) as container:
port = container.get_exposed_port(5432)
host = container.get_container_host_ip()
expected_url = f"postgresql://{user}:{password}@{host}:{port}/test"

url = container.get_connection_url()
assert url == expected_url

with PostgresContainer("postgres:16-alpine", **kwargs) as container:
port = container.get_exposed_port(5432)
host = container.get_container_host_ip()
expected_url = f"postgresql://{user}:{password}@{host}:{port}/test"

url = container.get_connection_url(driver=None)
assert url == expected_url

0 comments on commit 01d6c18

Please sign in to comment.