Skip to content

Commit c90713f

Browse files
committed
Add CassandraContainer
1 parent ade144e commit c90713f

File tree

7 files changed

+155
-3
lines changed

7 files changed

+155
-3
lines changed

.github/settings.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ labels:
6363
- { name: '🐧 linux', color: '#3ED4D',, description: '' }
6464
- { name: '👀 requires attention', color: '#fef2c0', description: '' }
6565
- { name: '📖 documentation', color: '#d93f0b', description: '' }
66+
- { name: '📦 package: cassandra', color: '#0052CC', description: '' }
6667
- { name: '📦 package: clickhouse', color: '#0052CC', description: '' }
6768
- { name: '📦 package: compose', color: '#0052CC', description: '' }
6869
- { name: '📦 package: core', color: '#0052CC', description: '' }

INDEX.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ testcontainers-python facilitates the use of Docker containers for functional an
1717
core/README
1818
modules/arangodb/README
1919
modules/azurite/README
20+
modules/cassandra/README
2021
modules/clickhouse/README
2122
modules/elasticsearch/README
2223
modules/google/README

modules/cassandra/README.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.. autoclass:: testcontainers.cassandra.CassandraContainer
2+
.. title:: testcontainers.cassandra.CassandraContainer
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 os
14+
from typing import Optional
15+
16+
from cassandra.cluster import Cluster, NoHostAvailable
17+
18+
from testcontainers.core.container import DockerContainer
19+
from testcontainers.core.waiting_utils import wait_container_is_ready, wait_for_logs
20+
21+
22+
class CassandraContainer(DockerContainer):
23+
"""
24+
Cassandra database container.
25+
26+
Example:
27+
28+
.. doctest::
29+
30+
>>> from testcontainers.cassandra import CassandraContainer
31+
>>> from cassandra.cluster import Cluster
32+
33+
>>> with CassandraContainer("cassandra:latest") as cassandra:
34+
... cluster = Cluster(
35+
.... [cassandra.get_container_host_ip()],
36+
... port=cassandra.get_exposed_port(cassandra.port)
37+
... )
38+
... session = cluster.connect()
39+
... result = session.execute("SELECT release_version FROM system.local;")
40+
"""
41+
42+
def __init__(self, image: str = "cassandra:latest", port: int = 9042, **kwargs) -> None:
43+
super().__init__(image=image, **kwargs)
44+
self.port = port
45+
self.with_exposed_ports(self.port)
46+
47+
def _connect(self):
48+
wait_for_logs(self, "Startup complete")
49+
50+
def start(self) -> "CassandraContainer":
51+
super().start()
52+
self._connect()
53+
return self
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from cassandra.cluster import Cluster
2+
3+
from testcontainers.cassandra import CassandraContainer
4+
5+
6+
def test_docker_run_cassandra():
7+
with CassandraContainer("cassandra:4.1.4") as cassandra:
8+
cluster = Cluster([cassandra.get_container_host_ip()], port=cassandra.get_exposed_port(cassandra.port))
9+
session = cluster.connect()
10+
result = session.execute("SELECT release_version FROM system.local;")
11+
assert result.one().release_version == "4.1.4"

poetry.lock

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

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ packages = [
2929
{ include = "testcontainers", from = "core" },
3030
{ include = "testcontainers", from = "modules/arangodb" },
3131
{ include = "testcontainers", from = "modules/azurite" },
32+
{ include = "testcontainers", from = "modules/cassandra" },
3233
{ include = "testcontainers", from = "modules/clickhouse" },
3334
{ include = "testcontainers", from = "modules/elasticsearch" },
3435
{ include = "testcontainers", from = "modules/google" },
@@ -85,6 +86,7 @@ selenium = { version = "*", optional = true }
8586
[tool.poetry.extras]
8687
arangodb = ["python-arango"]
8788
azurite = ["azure-storage-blob"]
89+
cassandra = ["cassandra-driver"]
8890
clickhouse = ["clickhouse-driver"]
8991
elasticsearch = []
9092
google = ["google-cloud-pubsub"]
@@ -117,6 +119,7 @@ anyio = "^4.3.0"
117119
psycopg2-binary = "*"
118120
pg8000 = "*"
119121
sqlalchemy = "*"
122+
cassandra-driver = "*"
120123

121124

122125
[[tool.poetry.source]]
@@ -213,6 +216,7 @@ mypy_path = [
213216
"core",
214217
# "modules/arangodb",
215218
# "modules/azurite",
219+
# "modules/cassandra",
216220
# "modules/clickhouse",
217221
# "modules/elasticsearch",
218222
# "modules/google",

0 commit comments

Comments
 (0)