Skip to content

CockroachDB Module

CockroachDB is a cloud-native, postgresql compatible, distributed SQL database designed to build, scale, and manage modern, data-intensive applications.

Install

npm install @testcontainers/cockroachdb --save-dev

Examples

it("should connect and return a query result", async () => {
  const container = await new CockroachDbContainer().start();

  const client = new Client({
    host: container.getHost(),
    port: container.getPort(),
    database: container.getDatabase(),
    user: container.getUsername(),
    ssl: false,
  });

  await client.connect();

  const result = await client.query("SELECT 1");
  expect(result.rows[0]).toEqual({ "?column?": "1" });

  await client.end();
  await container.stop();
});
it("should work with database URI", async () => {
  const container = await new CockroachDbContainer().start();

  const client = new Client({
    connectionString: container.getConnectionUri(),
  });
  await client.connect();

  const result = await client.query("SELECT 1");
  expect(result.rows[0]).toEqual({ "?column?": "1" });

  await client.end();
  await container.stop();
});
it("should set database", async () => {
  const container = await new CockroachDbContainer().withDatabase("custom_database").start();

  const client = new Client({
    host: container.getHost(),
    port: container.getPort(),
    database: container.getDatabase(),
    user: container.getUsername(),
  });
  await client.connect();

  const result = await client.query("SELECT current_database()");
  expect(result.rows[0]).toEqual({ current_database: "custom_database" });

  await client.end();
  await container.stop();
});
it("should set username", async () => {
  const container = await new CockroachDbContainer().withUsername("custom_username").start();

  const client = new Client({
    host: container.getHost(),
    port: container.getPort(),
    database: container.getDatabase(),
    user: container.getUsername(),
  });
  await client.connect();

  const result = await client.query("SELECT current_user");
  expect(result.rows[0]).toEqual({ current_user: "custom_username" });

  await client.end();
  await container.stop();
});