Skip to content

Commit 49ce5a5

Browse files
authored
fix: Add container Trino (#642)
Resolve #641
1 parent 0ce4fec commit 49ce5a5

File tree

5 files changed

+105
-2
lines changed

5 files changed

+105
-2
lines changed

modules/trino/README.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.. autoclass:: testcontainers.trino.TrinoContainer
2+
.. title:: testcontainers.trino.TrinoContainer
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#
2+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
3+
# not use this file except in compliance with the License. You may obtain
4+
# a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11+
# License for the specific language governing permissions and limitations
12+
# under the License.
13+
import re
14+
15+
from testcontainers.core.config import testcontainers_config as c
16+
from testcontainers.core.generic import DbContainer
17+
from testcontainers.core.waiting_utils import wait_container_is_ready, wait_for_logs
18+
from trino.dbapi import connect
19+
20+
21+
class TrinoContainer(DbContainer):
22+
def __init__(
23+
self,
24+
image="trinodb/trino:latest",
25+
user: str = "test",
26+
port: int = 8080,
27+
**kwargs,
28+
):
29+
super().__init__(image=image, **kwargs)
30+
self.user = user
31+
self.port = port
32+
self.with_exposed_ports(self.port)
33+
34+
@wait_container_is_ready()
35+
def _connect(self) -> None:
36+
wait_for_logs(
37+
self,
38+
re.compile(".*======== SERVER STARTED ========.*", re.MULTILINE).search,
39+
c.max_tries,
40+
c.sleep_time,
41+
)
42+
conn = connect(
43+
host=self.get_container_host_ip(),
44+
port=self.get_exposed_port(self.port),
45+
user=self.user,
46+
)
47+
cur = conn.cursor()
48+
cur.execute("SELECT 1")
49+
cur.fetchall()
50+
conn.close()
51+
52+
def get_connection_url(self):
53+
return f"trino://{self.user}@{self.get_container_host_ip()}:{self.port}"
54+
55+
def _configure(self):
56+
pass

modules/trino/tests/test_trino.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from testcontainers.trino import TrinoContainer
2+
from trino.dbapi import connect
3+
4+
5+
def test_docker_run_trino():
6+
container = TrinoContainer("trinodb/trino:451")
7+
with container as trino:
8+
conn = connect(
9+
host=trino.get_container_host_ip(),
10+
port=trino.get_exposed_port(trino.port),
11+
user="test",
12+
)
13+
cur = conn.cursor()
14+
cur.execute("SELECT version()")
15+
rows = cur.fetchall()
16+
assert rows[0][0] == "451"
17+
conn.close()

poetry.lock

Lines changed: 27 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ packages = [
6565
{ include = "testcontainers", from = "modules/registry" },
6666
{ include = "testcontainers", from = "modules/sftp" },
6767
{ include = "testcontainers", from = "modules/selenium" },
68+
{ include = "testcontainers", from = "modules/trino" },
6869
{ include = "testcontainers", from = "modules/vault" },
6970
{ include = "testcontainers", from = "modules/weaviate" },
7071
]
@@ -111,6 +112,7 @@ bcrypt = { version = "*", optional = true }
111112
httpx = { version = "*", optional = true }
112113
azure-cosmos = { version = "*", optional = true }
113114
cryptography = { version = "*", optional = true }
115+
trino = { version = "*", optional = true }
114116

115117
[tool.poetry.extras]
116118
arangodb = ["python-arango"]
@@ -153,6 +155,7 @@ sftp = ["cryptography"]
153155
vault = []
154156
weaviate = ["weaviate-client"]
155157
chroma = ["chromadb-client"]
158+
trino = ["trino"]
156159

157160
[tool.poetry.group.dev.dependencies]
158161
mypy = "1.7.1"

0 commit comments

Comments
 (0)