Skip to content

Commit

Permalink
fix(clickhouse): clickhouse waiting (testcontainers#428)
Browse files Browse the repository at this point in the history
im not actually sure if this fixes it but does: make clickhouse waiting
look more like java-tc

many other discussions about removing dependency on sqlalchemy + drivers

---------

Co-authored-by: Bálint Bartha <[email protected]>
  • Loading branch information
2 people authored and bstrausser committed Mar 9, 2024
1 parent 2f75b21 commit 8c95554
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions modules/clickhouse/testcontainers/clickhouse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
# under the License.
import os
from typing import Optional

import clickhouse_driver
from clickhouse_driver.errors import Error
from urllib.error import HTTPError, URLError
from urllib.request import urlopen

from testcontainers.core.generic import DbContainer
from testcontainers.core.utils import raise_for_deprecated_parameter
Expand Down Expand Up @@ -48,7 +47,7 @@ def __init__(
username: Optional[str] = None,
password: Optional[str] = None,
dbname: Optional[str] = None,
**kwargs
**kwargs,
) -> None:
raise_for_deprecated_parameter(kwargs, "user", "username")
super().__init__(image=image, **kwargs)
Expand All @@ -57,11 +56,14 @@ def __init__(
self.dbname = dbname or os.environ.get("CLICKHOUSE_DB", "test")
self.port = port
self.with_exposed_ports(self.port)
self.with_exposed_ports(8123)

@wait_container_is_ready(Error, EOFError)
@wait_container_is_ready(HTTPError, URLError)
def _connect(self) -> None:
with clickhouse_driver.Client.from_url(self.get_connection_url()) as client:
client.execute("SELECT version()")
# noinspection HttpUrlsUsage
url = f"http://{self.get_container_host_ip()}:{self.get_exposed_port(8123)}"
with urlopen(url) as r:
assert b"Ok" in r.read()

def _configure(self) -> None:
self.with_env("CLICKHOUSE_USER", self.username)
Expand Down

0 comments on commit 8c95554

Please sign in to comment.