Skip to content

Commit 67eb244

Browse files
Redis
1 parent d10e91c commit 67eb244

File tree

2 files changed

+60
-56
lines changed

2 files changed

+60
-56
lines changed

docs/modules/redis.md

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
# Redis Module
2-
3-
[Redis](https://redis.io/) The open source, in-memory data store used by millions of developers as a database, cache, streaming engine, and message broker.
1+
# Redis
42

53
## Install
64

@@ -10,38 +8,46 @@ npm install @testcontainers/redis --save-dev
108

119
## Examples
1210

13-
<!--codeinclude-->
11+
These examples use the following libraries:
1412

15-
[Start container:](../../packages/modules/redis/src/redis-container.test.ts) inside_block:startContainer
13+
- [redis](https://www.npmjs.com/package/redis)
1614

17-
<!--/codeinclude-->
15+
npm install redis
1816

19-
<!--codeinclude-->
17+
Choose an image from the [container registry](https://hub.docker.com/_/redis) and substitute `IMAGE`.
2018

21-
[Connect redis client to container:](../../packages/modules/redis/src/redis-container.test.ts) inside_block:simpleConnect
22-
23-
<!--/codeinclude-->
19+
### Set and get a value
2420

2521
<!--codeinclude-->
26-
27-
[Start container with password authentication:](../../packages/modules/redis/src/redis-container.test.ts) inside_block:startWithCredentials
28-
22+
[](../../packages/modules/redis/src/redis-container.test.ts) inside_block:redisStartContainer
2923
<!--/codeinclude-->
3024

31-
<!--codeinclude-->
32-
33-
[Define volume for persistent/predefined data:](../../packages/modules/redis/src/redis-container.test.ts) inside_block:persistentData
25+
### With password
3426

27+
<!--codeinclude-->
28+
[](../../packages/modules/redis/src/redis-container.test.ts) inside_block:redisStartWithCredentials
3529
<!--/codeinclude-->
3630

31+
### With persistent data
32+
3733
<!--codeinclude-->
34+
[](../../packages/modules/redis/src/redis-container.test.ts) inside_block:persistentData
35+
<!--/codeinclude-->
3836

39-
[Start container with redis/redis-stack-server image:](../../packages/modules/redis/src/redis-container.test.ts) inside_block:startWithRedisStack
37+
### With predefined data
4038

39+
<!--codeinclude-->
40+
[](../../packages/modules/redis/src/redis-container.test.ts) inside_block:withPredefinedData
4141
<!--/codeinclude-->
4242

43+
### Redis stack
44+
4345
<!--codeinclude-->
46+
[](../../packages/modules/redis/src/redis-container.test.ts) inside_block:startWithRedisStack
47+
<!--/codeinclude-->
4448

45-
[Execute a command inside the container:](../../packages/modules/redis/src/redis-container.test.ts) inside_block:executeCommand
49+
### Execute a command inside the container
4650

51+
<!--codeinclude-->
52+
[](../../packages/modules/redis/src/redis-container.test.ts) inside_block:executeCommand
4753
<!--/codeinclude-->

packages/modules/redis/src/redis-container.test.ts

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,64 +3,69 @@ import os from "os";
33
import path from "path";
44
import { createClient } from "redis";
55
import { getImage } from "../../../testcontainers/src/utils/test-helper";
6-
import { RedisContainer, StartedRedisContainer } from "./redis-container";
6+
import { RedisContainer } from "./redis-container";
77

88
const IMAGE = getImage(__dirname);
99

1010
describe("RedisContainer", { timeout: 240_000 }, () => {
11-
// startContainer {
1211
it("should connect and execute set-get", async () => {
12+
// redisStartContainer {
1313
await using container = await new RedisContainer(IMAGE).start();
1414

15-
const client = await connectTo(container);
15+
const client = createClient({ url: container.getConnectionUrl() });
16+
await client.connect();
1617

1718
await client.set("key", "val");
1819
expect(await client.get("key")).toBe("val");
1920

2021
client.destroy();
22+
// }
2123
});
22-
// }
2324

2425
it("should connect with password and execute set-get", async () => {
2526
await using container = await new RedisContainer(IMAGE).withPassword("test").start();
2627

27-
const client = await connectTo(container);
28+
const client = createClient({ url: container.getConnectionUrl() });
29+
await client.connect();
2830

2931
await client.set("key", "val");
3032
expect(await client.get("key")).toBe("val");
3133

3234
client.destroy();
3335
});
3436

35-
// persistentData {
3637
it("should reconnect with volume and persistence data", async () => {
38+
// persistentData {
3739
const sourcePath = fs.mkdtempSync(path.join(os.tmpdir(), "redis-"));
40+
3841
await using container = await new RedisContainer(IMAGE).withPassword("test").withPersistence(sourcePath).start();
39-
let client = await connectTo(container);
4042

43+
let client = createClient({ url: container.getConnectionUrl() });
44+
await client.connect();
4145
await client.set("key", "val");
4246
client.destroy();
47+
4348
await container.restart();
44-
client = await connectTo(container);
49+
client = createClient({ url: container.getConnectionUrl() });
50+
await client.connect();
51+
4552
expect(await client.get("key")).toBe("val");
4653

4754
client.destroy();
48-
try {
49-
fs.rmSync(sourcePath, { force: true, recursive: true });
50-
} catch (e) {
51-
//Ignore clean up, when have no access on fs.
52-
console.log(e);
53-
}
55+
fs.rmSync(sourcePath, { force: true, recursive: true });
56+
// }
5457
});
55-
// }
5658

57-
// initial data import {
5859
it("should load initial data and can read it", async () => {
60+
// withPredefinedData {
5961
await using container = await new RedisContainer(IMAGE)
6062
.withPassword("test")
6163
.withInitialData(path.join(__dirname, "initData.redis"))
6264
.start();
63-
const client = await connectTo(container);
65+
66+
const client = createClient({ url: container.getConnectionUrl() });
67+
await client.connect();
68+
6469
const user = {
6570
first_name: "David",
6671
last_name: "Bloom",
@@ -69,58 +74,51 @@ describe("RedisContainer", { timeout: 240_000 }, () => {
6974
expect(await client.get("user:002")).toBe(JSON.stringify(user));
7075

7176
client.destroy();
77+
// }
7278
});
73-
// }
7479

75-
// startWithCredentials {
7680
it("should start with credentials and login", async () => {
81+
// redisStartWithCredentials {
7782
const password = "testPassword";
7883

79-
// Test authentication
8084
await using container = await new RedisContainer(IMAGE).withPassword(password).start();
85+
8186
expect(container.getConnectionUrl()).toEqual(`redis://:${password}@${container.getHost()}:${container.getPort()}`);
87+
// }
8288

83-
const client = await connectTo(container);
89+
const client = createClient({ url: container.getConnectionUrl() });
90+
await client.connect();
8491

8592
await client.set("key", "val");
8693
expect(await client.get("key")).toBe("val");
8794

8895
client.destroy();
8996
});
90-
// }
9197

92-
// executeCommand {
9398
it("should execute container cmd and return the result", async () => {
99+
// executeCommand {
94100
await using container = await new RedisContainer(IMAGE).start();
95101

96102
const queryResult = await container.executeCliCmd("info", ["clients"]);
103+
97104
expect(queryResult).toEqual(expect.stringContaining("connected_clients:1"));
105+
// }
98106
});
99-
// }
100107

101-
// startWithRedisStack {
102108
it("should start with redis-stack-server and json module", async () => {
109+
// startWithRedisStack {
103110
await using container = await new RedisContainer("redis/redis-stack-server:7.4.0-v4")
104111
.withPassword("testPassword")
105112
.start();
106-
const client = await connectTo(container);
113+
114+
const client = createClient({ url: container.getConnectionUrl() });
115+
await client.connect();
107116

108117
await client.json.set("key", "$", { name: "test" });
109118
const result = await client.json.get("key");
110119
expect(result).toEqual({ name: "test" });
111120

112121
client.destroy();
122+
// }
113123
});
114-
// }
115-
116-
// simpleConnect {
117-
async function connectTo(container: StartedRedisContainer) {
118-
const client = createClient({
119-
url: container.getConnectionUrl(),
120-
});
121-
await client.connect();
122-
expect(client.isOpen).toBeTruthy();
123-
return client;
124-
}
125-
// }
126124
});

0 commit comments

Comments
 (0)