-
-
Notifications
You must be signed in to change notification settings - Fork 255
Expand file tree
/
Copy pathcassandra-container.test.ts
More file actions
133 lines (103 loc) · 4.22 KB
/
cassandra-container.test.ts
File metadata and controls
133 lines (103 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { Client } from "cassandra-driver";
import { ImageName } from "testcontainers";
import { getImage } from "../../../testcontainers/src/utils/test-helper";
import { CassandraContainer } from "./cassandra-container";
const IMAGE = getImage(__dirname);
describe("Cassandra", { timeout: 240_000 }, () => {
// connectWithDefaultCredentials {
it.concurrent("should connect and execute a query with default credentials", async () => {
const 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();
await container.stop();
});
// }
// connectWithCustomCredentials {
it.concurrent("should connect with custom username and password", async () => {
const username = "testUser";
const password = "testPassword";
const 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",
});
await client.connect();
const result = await client.execute("SELECT release_version FROM system.local");
expect(result.rows.length).toBeGreaterThan(0);
await client.shutdown();
await container.stop();
});
// }
// customDataSenterAndRack {
it.concurrent("should set datacenter and rack", async () => {
const customDataCenter = "customDC";
const customRack = "customRack";
const 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();
await container.stop();
});
// }
// createAndFetchData {
it.concurrent("should create keyspace, a table, insert data, and retrieve it", async () => {
const container = await new CassandraContainer(IMAGE).start();
const client = new Client({
contactPoints: [container.getContactPoint()],
localDataCenter: container.getDatacenter(),
});
await client.connect();
// Create the keyspace
await client.execute(`
CREATE KEYSPACE IF NOT EXISTS test_keyspace
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}
`);
await client.execute("USE test_keyspace");
// Create the table.
await client.execute(`
CREATE TABLE IF NOT EXISTS test_keyspace.users (
id UUID PRIMARY KEY,
name text
)
`);
// Insert a record
const id = "d002cd08-401a-47d6-92d7-bb4204d092f8"; // Fixed UUID for testing
const username = "Testy McTesterson";
client.execute("INSERT INTO test_keyspace.users (id, name) VALUES (?, ?)", [id, username]);
// Fetch and verify the record
const result = await client.execute("SELECT * FROM test_keyspace.users WHERE id = ?", [id], { prepare: true });
expect(result.rows[0].name).toBe(username);
await client.shutdown();
await container.stop();
});
// }
it.concurrent("should work with restarted container", async () => {
const container = await new CassandraContainer(IMAGE).start();
await container.restart();
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();
await container.stop();
});
});