Skip to content

Cassandra

Install

1
npm install @testcontainers/cassandra --save-dev

Examples

These examples use the following libraries:

Choose an image from the container registry and substitute IMAGE.

Execute a query

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
await using container = await new CassandraContainer(IMAGE).start();

const client = new Client({
  contactPoints: [container.getContactPoint()],
  localDataCenter: container.getDatacenter(),
  keyspace: "system",
});
await client.connect();

const result = await client.execute("SELECT release_version FROM system.local");
expect(result.rows[0].release_version).toBe(ImageName.fromString(IMAGE).tag);

await client.shutdown();

With credentials

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
const username = "testUser";
const password = "testPassword";

await using container = await new CassandraContainer(IMAGE).withUsername(username).withPassword(password).start();

const client = new Client({
  contactPoints: [container.getContactPoint()],
  localDataCenter: container.getDatacenter(),
  credentials: { username, password },
  keyspace: "system",
});

With datacenter/rack

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
const customDataCenter = "customDC";
const customRack = "customRack";

await using container = await new CassandraContainer(IMAGE)
  .withDatacenter(customDataCenter)
  .withRack(customRack)
  .start();

const client = new Client({
  contactPoints: [container.getContactPoint()],
  localDataCenter: container.getDatacenter(),
});
await client.connect();

const result = await client.execute("SELECT data_center, rack FROM system.local");
expect(result.rows[0].data_center).toBe(customDataCenter);
expect(result.rows[0].rack).toBe(customRack);

await client.shutdown();