Skip to content

Commit b810198

Browse files
Merge pull request #295 from tillahoffmann/types
Add type annotations and improve documentation.
2 parents 49d2516 + 69900da commit b810198

File tree

27 files changed

+585
-670
lines changed

27 files changed

+585
-670
lines changed

arangodb/testcontainers/arangodb/__init__.py

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,26 @@ class ArangoDbContainer(DbContainer):
1212
"""
1313
ArangoDB container.
1414
15-
Example
16-
-------
17-
The example will spin up a ArangoDB container.
18-
You may use the :code:`get_connection_url()` method which returns a arangoclient-compatible url
19-
in format :code:`scheme://host:port`. As of now, only a single host is supported (over HTTP).
15+
Example:
2016
21-
.. doctest::
17+
This example spins up an ArangoDB container. You may use the :code:`get_connection_url()`
18+
method which returns a arangoclient-compatible url in format :code:`scheme://host:port`. As
19+
of now, only a single host is supported (over HTTP).
2220
23-
>>> from testcontainers.arangodb import ArangoDbContainer
24-
>>> from arango import ArangoClient
21+
.. doctest::
2522
26-
>>> with ArangoDbContainer("arangodb:3.9.1") as arango:
27-
... client = ArangoClient(hosts=arango.get_connection_url())
28-
...
29-
... # Connect
30-
... sys_db = client.db(username="root", password="passwd")
31-
...
32-
... # Create a new database named "test".
33-
... sys_db.create_database("test")
34-
True
23+
>>> from testcontainers.arangodb import ArangoDbContainer
24+
>>> from arango import ArangoClient
25+
26+
>>> with ArangoDbContainer("arangodb:3.9.1") as arango:
27+
... client = ArangoClient(hosts=arango.get_connection_url())
28+
...
29+
... # Connect
30+
... sys_db = client.db(username="root", password="passwd")
31+
...
32+
... # Create a new database named "test".
33+
... sys_db.create_database("test")
34+
True
3535
"""
3636

3737
def __init__(self,
@@ -40,7 +40,7 @@ def __init__(self,
4040
arango_root_password: str = "passwd",
4141
arango_no_auth: typing.Optional[bool] = None,
4242
arango_random_root_password: typing.Optional[bool] = None,
43-
**kwargs):
43+
**kwargs) -> None:
4444
"""
4545
Args:
4646
image: Actual docker image/tag to pull.
@@ -69,23 +69,16 @@ def __init__(self,
6969
else arango_random_root_password
7070
))
7171

72-
def _configure(self):
72+
def _configure(self) -> None:
7373
self.with_env("ARANGO_ROOT_PASSWORD", self.arango_root_password)
7474
if self.arango_no_auth:
7575
self.with_env("ARANGO_NO_AUTH", "1")
7676
if self.arango_random_root_password:
7777
self.with_env("ARANGO_RANDOM_ROOT_PASSWORD", "1")
7878

79-
def get_connection_url(self):
80-
# for now, single host over HTTP
81-
scheme = "http"
79+
def get_connection_url(self) -> str:
8280
port = self.get_exposed_port(self.port_to_expose)
83-
url = f"{scheme}://{self.get_container_host_ip()}:{port}"
84-
85-
return url
81+
return f"http://{self.get_container_host_ip()}:{port}"
8682

87-
def _connect(self):
88-
wait_for_logs(
89-
self,
90-
predicate="is ready for business",
91-
timeout=MAX_TRIES)
83+
def _connect(self) -> None:
84+
wait_for_logs(self, predicate="is ready for business", timeout=MAX_TRIES)

azurite/testcontainers/azurite/__init__.py

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# under the License.
1313
import os
1414
import socket
15+
from typing import Iterable, Optional
1516

1617
from testcontainers.core.container import DockerContainer
1718
from testcontainers.core.waiting_utils import wait_container_is_ready
@@ -24,19 +25,19 @@ class AzuriteContainer(DockerContainer):
2425
:code:`get_connection_string` can be used to create a client for Blob service, Queue service
2526
and Table service.
2627
27-
Example
28-
-------
29-
.. doctest::
28+
Example:
3029
31-
>>> from testcontainers.azurite import AzuriteContainer
32-
>>> from azure.storage.blob import BlobServiceClient
30+
.. doctest::
3331
34-
>>> with AzuriteContainer() as azurite_container:
35-
... connection_string = azurite_container.get_connection_string()
36-
... client = BlobServiceClient.from_connection_string(
37-
... connection_string,
38-
... api_version="2019-12-12"
39-
... )
32+
>>> from testcontainers.azurite import AzuriteContainer
33+
>>> from azure.storage.blob import BlobServiceClient
34+
35+
>>> with AzuriteContainer() as azurite_container:
36+
... connection_string = azurite_container.get_connection_string()
37+
... client = BlobServiceClient.from_connection_string(
38+
... connection_string,
39+
... api_version="2019-12-12"
40+
... )
4041
"""
4142

4243
_AZURITE_ACCOUNT_NAME = os.environ.get("AZURITE_ACCOUNT_NAME", "devstoreaccount1")
@@ -48,21 +49,14 @@ class AzuriteContainer(DockerContainer):
4849
_QUEUE_SERVICE_PORT = 10_001
4950
_TABLE_SERVICE_PORT = 10_002
5051

51-
def __init__(
52-
self,
53-
image="mcr.microsoft.com/azure-storage/azurite:latest",
54-
ports_to_expose=None,
55-
**kwargs
56-
):
52+
def __init__(self, image: str = "mcr.microsoft.com/azure-storage/azurite:latest",
53+
ports_to_expose: Optional[Iterable[int]] = None, **kwargs) -> None:
5754
""" Constructs an AzuriteContainer.
5855
59-
Parameters
60-
----------
61-
image: str
62-
Expects an image with tag.
63-
ports_to_expose: List[int]
64-
Expects a list with port numbers to expose.
65-
kwargs
56+
Args:
57+
image: Expects an image with tag.
58+
ports_to_expose: List with port numbers to expose.
59+
**kwargs: Keyword arguments passed to super class.
6660
"""
6761
super().__init__(image=image, **kwargs)
6862

@@ -80,7 +74,7 @@ def __init__(
8074
self.with_env("AZURITE_ACCOUNTS",
8175
f"{self._AZURITE_ACCOUNT_NAME}:{self._AZURITE_ACCOUNT_KEY}")
8276

83-
def get_connection_string(self):
77+
def get_connection_string(self) -> str:
8478
host_ip = self.get_container_host_ip()
8579
connection_string = f"DefaultEndpointsProtocol=http;" \
8680
f"AccountName={self._AZURITE_ACCOUNT_NAME};" \
@@ -103,13 +97,13 @@ def get_connection_string(self):
10397

10498
return connection_string
10599

106-
def start(self):
100+
def start(self) -> 'AzuriteContainer':
107101
super().start()
108102
self._connect()
109103
return self
110104

111105
@wait_container_is_ready(OSError)
112-
def _connect(self):
106+
def _connect(self) -> None:
113107
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
114108
s.connect((self.get_container_host_ip(),
115109
int(self.get_exposed_port(next(iter(self.ports))))))

clickhouse/testcontainers/clickhouse/__init__.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# License for the specific language governing permissions and limitations
1212
# under the License.
1313
import os
14+
from typing import Optional
1415

1516
import clickhouse_driver
1617
from clickhouse_driver.errors import Error
@@ -23,20 +24,20 @@ class ClickHouseContainer(DbContainer):
2324
"""
2425
ClickHouse database container.
2526
26-
Example
27-
-------
28-
The example spins up a ClickHouse database and connects to it
29-
using the :code:`clickhouse-driver`.
27+
Example:
3028
31-
.. doctest::
29+
The example spins up a ClickHouse database and connects to it using the
30+
:code:`clickhouse-driver`.
3231
33-
>>> import clickhouse_driver
34-
>>> from testcontainers.clickhouse import ClickHouseContainer
32+
.. doctest::
3533
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',)]
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',)]
4041
"""
4142

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

4647
def __init__(
4748
self,
48-
image="clickhouse/clickhouse-server:latest",
49-
port=9000,
50-
user=None,
51-
password=None,
52-
dbname=None
53-
):
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:
5455
super().__init__(image=image)
5556

5657
self.CLICKHOUSE_USER = user or self.CLICKHOUSE_USER
@@ -60,16 +61,16 @@ def __init__(
6061
self.with_exposed_ports(self.port_to_expose)
6162

6263
@wait_container_is_ready(Error, EOFError)
63-
def _connect(self):
64+
def _connect(self) -> None:
6465
with clickhouse_driver.Client.from_url(self.get_connection_url()) as client:
6566
client.execute("SELECT version()")
6667

67-
def _configure(self):
68+
def _configure(self) -> None:
6869
self.with_env("CLICKHOUSE_USER", self.CLICKHOUSE_USER)
6970
self.with_env("CLICKHOUSE_PASSWORD", self.CLICKHOUSE_PASSWORD)
7071
self.with_env("CLICKHOUSE_DB", self.CLICKHOUSE_DB)
7172

72-
def get_connection_url(self, host=None):
73+
def get_connection_url(self, host: Optional[str] = None) -> str:
7374
return self._create_connection_url(
7475
dialect="clickhouse",
7576
username=self.CLICKHOUSE_USER,

0 commit comments

Comments
 (0)