Skip to content

Commit 6dbb330

Browse files
committed
Remove extra validation + Improve testing
1 parent ee5ab80 commit 6dbb330

File tree

2 files changed

+12
-26
lines changed

2 files changed

+12
-26
lines changed

modules/generic/testcontainers/generic/sql.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,30 +67,16 @@ def _create_connection_url(
6767
if self._container is None:
6868
raise ContainerStartException("Container has not been started")
6969

70-
# Validate required parameters
71-
if not dialect:
72-
raise ValueError("Database dialect is required")
73-
if not username:
74-
raise ValueError("Database username is required")
75-
if port is None:
76-
raise ValueError("Database port is required")
77-
7870
host = host or self.get_container_host_ip()
7971
exposed_port = self.get_exposed_port(port)
80-
81-
# Safely quote password to handle special characters
8272
quoted_password = quote(password, safe="")
8373
quoted_username = quote(username, safe="")
84-
85-
# Build base URL
8674
url = f"{dialect}://{quoted_username}:{quoted_password}@{host}:{exposed_port}"
8775

88-
# Add database name if provided
8976
if dbname:
9077
quoted_dbname = quote(dbname, safe="")
9178
url = f"{url}/{quoted_dbname}"
9279

93-
# Add query parameters if provided
9480
if query_params:
9581
query_string = urlencode(query_params)
9682
url = f"{url}?{query_string}"

modules/generic/tests/test_sql.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
from unittest.mock import patch
23

34
from testcontainers.core.exceptions import ContainerStartException
45
from testcontainers.generic.sql import SqlContainer
@@ -96,21 +97,20 @@ def test_connection_url_with_query_params(self):
9697
assert "ssl=require" in url
9798
assert "timeout=30" in url
9899

99-
def test_connection_url_validation_errors(self):
100+
def test_connection_url_type_errors(self):
101+
"""Test that _create_connection_url raises TypeError with invalid types"""
100102
container = SimpleSqlContainer()
101-
container._container = type("MockContainer", (), {})()
102-
103-
# Test missing dialect
104-
with pytest.raises(ValueError, match="Database dialect is required"):
105-
container._create_connection_url("", "user", "pass", port=5432)
103+
container._container = type("MockContainer", (), {"id": "test-id"})()
106104

107-
# Test missing username
108-
with pytest.raises(ValueError, match="Database username is required"):
109-
container._create_connection_url("postgresql", "", "pass", port=5432)
105+
# Mock get_exposed_port to simulate what happens with None port
106+
with patch.object(container, "get_exposed_port") as mock_get_port:
107+
# Simulate the TypeError that would occur when int(None) is called
108+
mock_get_port.side_effect = TypeError(
109+
"int() argument must be a string, a bytes-like object or a real number, not 'NoneType'"
110+
)
110111

111-
# Test missing port
112-
with pytest.raises(ValueError, match="Database port is required"):
113-
container._create_connection_url("postgresql", "user", "pass", port=None)
112+
with pytest.raises(TypeError, match="int\\(\\) argument must be a string"):
113+
container._create_connection_url("postgresql", "user", "pass", port=None)
114114

115115
def test_connection_url_container_not_started(self):
116116
container = SimpleSqlContainer()

0 commit comments

Comments
 (0)