Skip to content

Commit 0a37f88

Browse files
committed
Add CassandraContainer
1 parent ade144e commit 0a37f88

File tree

7 files changed

+156
-3
lines changed

7 files changed

+156
-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: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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
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+
32+
>>> with CassandraContainer("cassandra:latest") as cassandra:
33+
... session = cassandra.cluster.connect()
34+
... result = session.execute("SELECT release_version FROM system.local;")
35+
"""
36+
37+
def __init__(
38+
self,
39+
image: str = "cassandra:latest",
40+
port: int = 9042,
41+
**kwargs
42+
) -> None:
43+
super().__init__(image=image, **kwargs)
44+
self.port = port
45+
self.with_exposed_ports(self.port)
46+
self.cluster: Optional[Cluster] = None
47+
48+
@wait_container_is_ready(NoHostAvailable)
49+
def _connect(self):
50+
self.cluster = Cluster([self.get_container_host_ip()],
51+
port=self.get_exposed_port(self.port))
52+
self.cluster.connect()
53+
54+
def start(self) -> "CassandraContainer":
55+
super().start()
56+
self._connect()
57+
return self
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from testcontainers.cassandra import CassandraContainer
2+
3+
4+
def test_docker_run_cassandra():
5+
with CassandraContainer("cassandra:4.1.4") as cassandra:
6+
session = cassandra.cluster.connect()
7+
result = session.execute("SELECT release_version FROM system.local;")
8+
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" },
@@ -81,10 +82,12 @@ cx_Oracle = { version = "*", optional = true }
8182
pika = { version = "*", optional = true }
8283
redis = { version = "*", optional = true }
8384
selenium = { version = "*", optional = true }
85+
cassandra-driver = { version = "*", optional = true }
8486

8587
[tool.poetry.extras]
8688
arangodb = ["python-arango"]
8789
azurite = ["azure-storage-blob"]
90+
cassandra = ["cassandra-driver"]
8891
clickhouse = ["clickhouse-driver"]
8992
elasticsearch = []
9093
google = ["google-cloud-pubsub"]
@@ -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)