|
| 1 | +import { Client } from "cassandra-driver"; |
| 2 | +import { CassandraContainer } from "./cassandra-container"; |
| 3 | + |
| 4 | +describe("Cassandra", () => { |
| 5 | + jest.setTimeout(240_000); |
| 6 | + |
| 7 | + // connectWithDefaultCredentials { |
| 8 | + it("should connect and execute a query with default credentials", async () => { |
| 9 | + const container = await new CassandraContainer("cassandra:5.0.2").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 release_version FROM system.local"); |
| 20 | + expect(result.rows[0].release_version).toBe("5.0.2"); |
| 21 | + |
| 22 | + await client.shutdown(); |
| 23 | + await container.stop(); |
| 24 | + }); |
| 25 | + // } |
| 26 | + |
| 27 | + // connectWithCustomCredentials { |
| 28 | + it("should connect with custom username and password", async () => { |
| 29 | + const username = "testUser"; |
| 30 | + const password = "testPassword"; |
| 31 | + |
| 32 | + const container = await new CassandraContainer().withUsername(username).withPassword(password).start(); |
| 33 | + |
| 34 | + const client = new Client({ |
| 35 | + contactPoints: [container.getContactPoint()], |
| 36 | + localDataCenter: container.getDatacenter(), |
| 37 | + credentials: { username, password }, |
| 38 | + keyspace: "system", |
| 39 | + }); |
| 40 | + |
| 41 | + await client.connect(); |
| 42 | + |
| 43 | + const result = await client.execute("SELECT release_version FROM system.local"); |
| 44 | + expect(result.rows.length).toBeGreaterThan(0); |
| 45 | + |
| 46 | + await client.shutdown(); |
| 47 | + await container.stop(); |
| 48 | + }); |
| 49 | + // } |
| 50 | + |
| 51 | + // customDataSenterAndRack { |
| 52 | + it("should set datacenter and rack", async () => { |
| 53 | + const customDataCenter = "customDC"; |
| 54 | + const customRack = "customRack"; |
| 55 | + const container = await new CassandraContainer().withDatacenter(customDataCenter).withRack(customRack).start(); |
| 56 | + |
| 57 | + const client = new Client({ |
| 58 | + contactPoints: [container.getContactPoint()], |
| 59 | + localDataCenter: container.getDatacenter(), |
| 60 | + }); |
| 61 | + |
| 62 | + await client.connect(); |
| 63 | + const result = await client.execute("SELECT data_center, rack FROM system.local"); |
| 64 | + expect(result.rows[0].data_center).toBe(customDataCenter); |
| 65 | + expect(result.rows[0].rack).toBe(customRack); |
| 66 | + |
| 67 | + await client.shutdown(); |
| 68 | + await container.stop(); |
| 69 | + }); |
| 70 | + // } |
| 71 | + |
| 72 | + // createAndFetchData { |
| 73 | + it("should create keyspace, a table, insert data, and retrieve it", async () => { |
| 74 | + const container = await new CassandraContainer().start(); |
| 75 | + |
| 76 | + const client = new Client({ |
| 77 | + contactPoints: [container.getContactPoint()], |
| 78 | + localDataCenter: container.getDatacenter(), |
| 79 | + }); |
| 80 | + |
| 81 | + await client.connect(); |
| 82 | + |
| 83 | + // Create the keyspace |
| 84 | + await client.execute(` |
| 85 | + CREATE KEYSPACE IF NOT EXISTS test_keyspace |
| 86 | + WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'} |
| 87 | + `); |
| 88 | + |
| 89 | + await client.execute("USE test_keyspace"); |
| 90 | + |
| 91 | + // Create the table. |
| 92 | + await client.execute(` |
| 93 | + CREATE TABLE IF NOT EXISTS test_keyspace.users ( |
| 94 | + id UUID PRIMARY KEY, |
| 95 | + name text |
| 96 | + ) |
| 97 | + `); |
| 98 | + |
| 99 | + // Insert a record |
| 100 | + const id = "d002cd08-401a-47d6-92d7-bb4204d092f8"; // Fixed UUID for testing |
| 101 | + const username = "Testy McTesterson"; |
| 102 | + client.execute("INSERT INTO test_keyspace.users (id, name) VALUES (?, ?)", [id, username]); |
| 103 | + |
| 104 | + // Fetch and verify the record |
| 105 | + const result = await client.execute("SELECT * FROM test_keyspace.users WHERE id = ?", [id], { prepare: true }); |
| 106 | + expect(result.rows[0].name).toBe(username); |
| 107 | + |
| 108 | + await client.shutdown(); |
| 109 | + await container.stop(); |
| 110 | + }); |
| 111 | + // } |
| 112 | +}); |
0 commit comments