|
| 1 | +import { Client } from "cassandra-driver"; // Scylla uses Cassandra's driver in Node.js |
| 2 | +import { ScyllaContainer } from "./scylladb-container"; |
| 3 | + |
| 4 | +describe("ScyllaDB", () => { |
| 5 | + jest.setTimeout(240_000); |
| 6 | + |
| 7 | + // connectWithDefaultCredentials { |
| 8 | + it("should connect and execute a query", async () => { |
| 9 | + const container = await new ScyllaContainer("scylladb/scylla:6.2.0").start(); |
| 10 | + |
| 11 | + const client = new Client({ |
| 12 | + contactPoints: [container.getContactPoint()], |
| 13 | + localDataCenter: container.getDatacenter(), |
| 14 | + keyspace: "system", |
| 15 | + }); |
| 16 | + |
| 17 | + await client.connect(); |
| 18 | + |
| 19 | + const result = await client.execute("SELECT cql_version FROM system.local"); |
| 20 | + expect(result.rows[0].cql_version).toBe("3.3.1"); |
| 21 | + |
| 22 | + await client.shutdown(); |
| 23 | + await container.stop(); |
| 24 | + }); |
| 25 | + // } |
| 26 | + |
| 27 | + // createAndFetchData { |
| 28 | + it("should create keyspace, a table, insert data, and retrieve it", async () => { |
| 29 | + const container = await new ScyllaContainer().start(); |
| 30 | + |
| 31 | + const client = new Client({ |
| 32 | + contactPoints: [container.getContactPoint()], |
| 33 | + localDataCenter: container.getDatacenter(), |
| 34 | + }); |
| 35 | + |
| 36 | + await client.connect(); |
| 37 | + |
| 38 | + // Create the keyspace |
| 39 | + await client.execute(` |
| 40 | + CREATE KEYSPACE IF NOT EXISTS test_keyspace |
| 41 | + WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'} |
| 42 | + `); |
| 43 | + |
| 44 | + await client.execute("USE test_keyspace"); |
| 45 | + |
| 46 | + // Create the table. |
| 47 | + await client.execute(` |
| 48 | + CREATE TABLE IF NOT EXISTS test_keyspace.users ( |
| 49 | + id UUID PRIMARY KEY, |
| 50 | + name text |
| 51 | + ) |
| 52 | + `); |
| 53 | + |
| 54 | + // Insert a record |
| 55 | + const id = "d002cd08-401a-47d6-92d7-bb4204d092f8"; // Fixed UUID for testing |
| 56 | + const username = "Test McTestinson"; |
| 57 | + await client.execute("INSERT INTO test_keyspace.users (id, name) VALUES (?, ?)", [id, username]); |
| 58 | + |
| 59 | + // Fetch and verify the record |
| 60 | + const result = await client.execute("SELECT * FROM test_keyspace.users WHERE id = ?", [id], { prepare: true }); |
| 61 | + expect(result.rows[0].name).toEqual(username); |
| 62 | + |
| 63 | + await client.shutdown(); |
| 64 | + await container.stop(); |
| 65 | + }); |
| 66 | + // } |
| 67 | +}); |
0 commit comments