-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adding support for Cassandra and Scylla (#167)
This add the support for those cassandra based dbs and their drivers, cassandra-driver, scylla-driver Ref: https://cassandra.apache.org/ Ref: https://www.scylladb.com/ Ref: https://pypi.org/project/cassandra-driver/ Ref: https://pypi.org/project/scylla-driver/ --------- Co-authored-by: David Ankin <daveankin@gmail.com>
- Loading branch information
1 parent
d5de0aa
commit 2d8bc11
Showing
5 changed files
with
75 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.. autoclass:: testcontainers.scylla.ScyllaContainer | ||
.. title:: testcontainers.scylla.ScyllaContainer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from testcontainers.core.config import MAX_TRIES | ||
from testcontainers.core.generic import DockerContainer | ||
from testcontainers.core.waiting_utils import wait_container_is_ready, wait_for_logs | ||
|
||
|
||
class ScyllaContainer(DockerContainer): | ||
""" | ||
Scylla database container. | ||
Example | ||
------- | ||
.. doctest:: | ||
>>> from testcontainers.scylla import ScyllaContainer | ||
>>> with ScyllaContainer() as scylla: | ||
... cluster = scylla.get_cluster() | ||
... with cluster.connect() as session: | ||
... result = session.execute( | ||
... "CREATE KEYSPACE keyspace1 WITH replication " | ||
... "= {'class': 'SimpleStrategy', 'replication_factor': '1'};") | ||
""" | ||
|
||
def __init__(self, image="scylladb/scylla:latest", ports_to_expose=(9042,)): | ||
super().__init__(image) | ||
self.ports_to_expose = ports_to_expose | ||
self.with_exposed_ports(*self.ports_to_expose) | ||
self.with_command("--skip-wait-for-gossip-to-settle=0") | ||
|
||
@wait_container_is_ready() | ||
def _connect(self): | ||
wait_for_logs(self, predicate="Starting listening for CQL clients", timeout=MAX_TRIES) | ||
cluster = self.get_cluster() | ||
cluster.connect() | ||
|
||
def start(self): | ||
super().start() | ||
self._connect() | ||
return self | ||
|
||
def get_cluster(self, **kwargs): | ||
from cassandra.cluster import Cluster | ||
|
||
container = self.get_wrapped_container() | ||
container.reload() | ||
hostname = container.attrs["NetworkSettings"]["IPAddress"] | ||
return Cluster(contact_points=[hostname], **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from testcontainers.scylla import ScyllaContainer | ||
|
||
|
||
def test_docker_run_scylla(): | ||
with ScyllaContainer() as scylla: | ||
cluster = scylla.get_cluster() | ||
with cluster.connect() as session: | ||
session.execute( | ||
"CREATE KEYSPACE keyspace1 WITH replication = " | ||
"{'class': 'SimpleStrategy', 'replication_factor': '1'};" | ||
) | ||
session.execute("CREATE TABLE keyspace1.table1 (key1 int, key2 int, PRIMARY KEY (key1));") | ||
session.execute("INSERT INTO keyspace1.table1 (key1,key2) values (1,2);") | ||
|
||
response = session.execute("SELECT * FROM keyspace1.table1") | ||
|
||
assert response.one().key1 == 1 | ||
assert response.one().key2 == 2 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters