Skip to content

Commit 01d6c18

Browse files
authoredJun 28, 2024··
fix(postgres): get_connection_url(driver=None) should return postgres://... (#588)
Fixes #587
1 parent a962147 commit 01d6c18

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed
 

‎modules/postgres/testcontainers/postgres/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def get_connection_url(self, host: Optional[str] = None, driver: Optional[str] =
7979
driver. The optional driver argument to :code:`get_connection_url` overwrites the constructor
8080
set value. Pass :code:`driver=None` to get URLs without a driver.
8181
"""
82-
driver_str = self.driver if driver is _UNSET else f"+{driver}"
82+
driver_str = "" if driver is None else self.driver if driver is _UNSET else f"+{driver}"
8383
return super()._create_connection_url(
8484
dialect=f"postgresql{driver_str}",
8585
username=self.username,

‎modules/postgres/tests/test_postgres.py

+24
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,27 @@ def test_show_how_to_initialize_db_via_initdb_dir():
9797
result = result.fetchall()
9898
assert len(result) == 1
9999
assert result[0] == (1, "sally", "sells seashells")
100+
101+
102+
def test_none_driver_urls():
103+
user = "root"
104+
password = "pass"
105+
kwargs = {
106+
"username": user,
107+
"password": password,
108+
}
109+
with PostgresContainer("postgres:16-alpine", driver=None, **kwargs) as container:
110+
port = container.get_exposed_port(5432)
111+
host = container.get_container_host_ip()
112+
expected_url = f"postgresql://{user}:{password}@{host}:{port}/test"
113+
114+
url = container.get_connection_url()
115+
assert url == expected_url
116+
117+
with PostgresContainer("postgres:16-alpine", **kwargs) as container:
118+
port = container.get_exposed_port(5432)
119+
host = container.get_container_host_ip()
120+
expected_url = f"postgresql://{user}:{password}@{host}:{port}/test"
121+
122+
url = container.get_connection_url(driver=None)
123+
assert url == expected_url

0 commit comments

Comments
 (0)
Please sign in to comment.