-
Notifications
You must be signed in to change notification settings - Fork 296
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for Cassandra and Scylla
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/
- Loading branch information
Showing
8 changed files
with
124 additions
and
1 deletion.
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
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
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
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
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,40 @@ | ||
from testcontainers.core.generic import DockerContainer | ||
from testcontainers.core.waiting_utils import wait_container_is_ready | ||
|
||
|
||
class CassandraContainer(DockerContainer): | ||
""" | ||
Cassandra database container. | ||
Example | ||
------- | ||
:: | ||
with CassandraContainer() as cassandra: | ||
cluster = cassandra.get_cluster() | ||
with cluster.connect() as session: | ||
session.execute( | ||
"CREATE KEYSPACE keyspace1 WITH replication = " | ||
"{'class': 'SimpleStrategy', 'replication_factor': '1'};") | ||
""" | ||
def __init__(self, image="rinscy/cassandra:latest", ports_to_expose=[9042]): | ||
super(CassandraContainer, self).__init__(image) | ||
self.ports_to_expose = ports_to_expose | ||
self.with_exposed_ports(*self.ports_to_expose) | ||
|
||
@wait_container_is_ready() | ||
def _connect(self): | ||
cluster = self.get_cluster() | ||
cluster.connect() | ||
|
||
def start(self): | ||
super(CassandraContainer, self).start() | ||
self._connect() | ||
return self | ||
|
||
def get_cluster(self, **kwargs): | ||
from cassandra.cluster import Cluster | ||
hostname = self.get_container_host_ip() | ||
port = self.get_exposed_port(9042) | ||
return Cluster(contact_points=[hostname], port=port, **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,41 @@ | ||
from testcontainers.core.generic import DockerContainer | ||
from testcontainers.core.waiting_utils import wait_container_is_ready | ||
|
||
|
||
class ScyllaContainer(DockerContainer): | ||
""" | ||
Scylla database container. | ||
Example | ||
------- | ||
:: | ||
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'};") | ||
""" | ||
def __init__(self, image="scylladb/scylla:4.6.rc1", ports_to_expose=[9042]): | ||
super(ScyllaContainer, self).__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): | ||
cluster = self.get_cluster() | ||
cluster.connect() | ||
|
||
def start(self): | ||
super(ScyllaContainer, self).start() | ||
self._connect() | ||
return self | ||
|
||
def get_cluster(self, **kwargs): | ||
from cassandra.cluster import Cluster | ||
hostname = self.get_container_host_ip() | ||
port = self.get_exposed_port(9042) | ||
return Cluster(contact_points=[hostname], port=port, **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.cassandra import CassandraContainer | ||
|
||
|
||
def test_docker_run_cassandra(): | ||
with CassandraContainer() as cassandra: | ||
cluster = cassandra.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 |
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 |