Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(postgres): get_connection_url(driver=None) should return postgres://... #588

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather self.driver did not contain the leading +, then this could be simpler. But I imagine that could be a breaking change if people are relying on the internals of this class

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I think we can introduce a Breaking change at a later stage, as part of some larger changes to inheritance for DB containers. This only looks good for now, and I think people would expect that if you pass None, the driver should be empty.

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