Skip to content

Commit c9a2dd0

Browse files
authored
Enable using HTTP port
1 parent 4d2b49e commit c9a2dd0

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

clickhouse/testcontainers/clickhouse/__init__.py

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
1010
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1111
# License for the specific language governing permissions and limitations
12-
# under the License.
13-
import os
14-
from typing import Optional
12+
# under the License.import os
1513

14+
import clickhouse_connect
1615
import clickhouse_driver
17-
from clickhouse_driver.errors import Error
16+
from clickhouse_connect.driver.exceptions import Error
1817

1918
from testcontainers.core.generic import DbContainer
2019
from testcontainers.core.waiting_utils import wait_container_is_ready
@@ -24,20 +23,20 @@ class ClickHouseContainer(DbContainer):
2423
"""
2524
ClickHouse database container.
2625
27-
Example:
26+
Example
27+
-------
28+
The example spins up a ClickHouse database and connects to it
29+
using the :code:`clickhouse-driver`.
2830
29-
The example spins up a ClickHouse database and connects to it using the
30-
:code:`clickhouse-driver`.
31+
.. doctest::
3132
32-
.. doctest::
33+
>>> import clickhouse_driver
34+
>>> from testcontainers.clickhouse import ClickHouseContainer
3335
34-
>>> import clickhouse_driver
35-
>>> from testcontainers.clickhouse import ClickHouseContainer
36-
37-
>>> with ClickHouseContainer("clickhouse/clickhouse-server:21.8") as clickhouse:
38-
... client = clickhouse_driver.Client.from_url(clickhouse.get_connection_url())
39-
... client.execute("select 'working'")
40-
[('working',)]
36+
>>> with ClickHouseContainer("clickhouse/clickhouse-server:21.8") as clickhouse:
37+
... client = clickhouse_driver.Client.from_url(clickhouse.get_connection_url())
38+
... client.execute("select 'working'")
39+
[('working',)]
4140
"""
4241

4342
CLICKHOUSE_USER = os.environ.get("CLICKHOUSE_USER", "test")
@@ -46,12 +45,12 @@ class ClickHouseContainer(DbContainer):
4645

4746
def __init__(
4847
self,
49-
image: str = "clickhouse/clickhouse-server:latest",
50-
port: int = 9000,
51-
user: Optional[str] = None,
52-
password: Optional[str] = None,
53-
dbname: Optional[str] = None
54-
) -> None:
48+
image="clickhouse/clickhouse-server:latest",
49+
port=9000,
50+
user=None,
51+
password=None,
52+
dbname=None
53+
):
5554
super().__init__(image=image)
5655

5756
self.CLICKHOUSE_USER = user or self.CLICKHOUSE_USER
@@ -61,16 +60,21 @@ def __init__(
6160
self.with_exposed_ports(self.port_to_expose)
6261

6362
@wait_container_is_ready(Error, EOFError)
64-
def _connect(self) -> None:
65-
with clickhouse_driver.Client.from_url(self.get_connection_url()) as client:
66-
client.execute("SELECT version()")
63+
def _connect(self):
64+
if self.port_to_expose == 8123:
65+
with clickhouse_connect.get_client(dsn=self.get_connection_url()) as client:
66+
client.command("SELECT version()")
67+
else:
68+
with clickhouse_driver.Client.from_url(self.get_connection_url()) as client:
69+
client.execute("SELECT version()")
70+
6771

68-
def _configure(self) -> None:
72+
def _configure(self):
6973
self.with_env("CLICKHOUSE_USER", self.CLICKHOUSE_USER)
7074
self.with_env("CLICKHOUSE_PASSWORD", self.CLICKHOUSE_PASSWORD)
7175
self.with_env("CLICKHOUSE_DB", self.CLICKHOUSE_DB)
7276

73-
def get_connection_url(self, host: Optional[str] = None) -> str:
77+
def get_connection_url(self, host=None):
7478
return self._create_connection_url(
7579
dialect="clickhouse",
7680
username=self.CLICKHOUSE_USER,

0 commit comments

Comments
 (0)