FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix(postgres): get_connection_url(driver=None) should return postgres://... by oliverlambson · Pull Request #588 · testcontainers/testcontainers-python · GitHub

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

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL