Skip to content

Commit cb26059

Browse files
DOC-4423: add TCEs for various command pages (#2885)
* DOC-4423: add TCEs for various command pages * fix problematic doctest from redis docs for ACL DELUSER: Delete all the specified ACL users and terminate all the connections that are authenticated with such users Nodejs complains: Warning: Detected unsettled top-level await which in turn gives the error code 13 in the workflow await client.auth({ username: 'test-user' ...}); // deleting the user that is currently logged in -> bad await client.sendCommand(['ACL', 'DELUSER', 'test-user']); await client.quit() fix: authenticate with the default user before deleting the test-user --------- Co-authored-by: Nikolay Karadzhov <[email protected]>
1 parent 001087f commit cb26059

File tree

5 files changed

+306
-1
lines changed

5 files changed

+306
-1
lines changed

doctests/cmds-cnxmgmt.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// EXAMPLE: cmds_cnxmgmt
2+
// REMOVE_START
3+
import assert from "node:assert";
4+
// REMOVE_END
5+
6+
// HIDE_START
7+
import { createClient } from 'redis';
8+
9+
const client = createClient();
10+
await client.connect().catch(console.error);
11+
// HIDE_END
12+
13+
// STEP_START auth1
14+
// REMOVE_START
15+
await client.sendCommand(['CONFIG', 'SET', 'requirepass', 'temp_pass']);
16+
// REMOVE_END
17+
const res1 = await client.auth({ password: 'temp_pass' });
18+
console.log(res1); // OK
19+
20+
const res2 = await client.auth({ username: 'default', password: 'temp_pass' });
21+
console.log(res2); // OK
22+
23+
// REMOVE_START
24+
assert.equal(res1, "OK");
25+
assert.equal(res2, "OK");
26+
await client.sendCommand(['CONFIG', 'SET', 'requirepass', '']);
27+
// REMOVE_END
28+
// STEP_END
29+
30+
// STEP_START auth2
31+
// REMOVE_START
32+
await client.sendCommand([
33+
'ACL', 'SETUSER', 'test-user',
34+
'on', '>strong_password', '+acl'
35+
]);
36+
// REMOVE_END
37+
const res3 = await client.auth({ username: 'test-user', password: 'strong_password' });
38+
console.log(res3); // OK
39+
40+
// REMOVE_START
41+
assert.equal(res3, "OK");
42+
await client.auth({ username: 'default', password: '' })
43+
await client.sendCommand(['ACL', 'DELUSER', 'test-user']);
44+
// REMOVE_END
45+
// STEP_END
46+
47+
// HIDE_START
48+
await client.quit();
49+
// HIDE_END

doctests/cmds-hash.js

+39-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,45 @@ await client.del('myhash')
6565
// REMOVE_END
6666
// STEP_END
6767

68+
// STEP_START hgetall
69+
const res10 = await client.hSet(
70+
'myhash',
71+
{
72+
'field1': 'Hello',
73+
'field2': 'World'
74+
}
75+
)
76+
77+
const res11 = await client.hGetAll('myhash')
78+
console.log(res11) // [Object: null prototype] { field1: 'Hello', field2: 'World' }
79+
80+
// REMOVE_START
81+
assert.deepEqual(res11, {
82+
field1: 'Hello',
83+
field2: 'World'
84+
});
85+
await client.del('myhash')
86+
// REMOVE_END
87+
// STEP_END
88+
89+
// STEP_START hvals
90+
const res12 = await client.hSet(
91+
'myhash',
92+
{
93+
'field1': 'Hello',
94+
'field2': 'World'
95+
}
96+
)
97+
98+
const res13 = await client.hVals('myhash')
99+
console.log(res13) // [ 'Hello', 'World' ]
100+
101+
// REMOVE_START
102+
assert.deepEqual(res13, [ 'Hello', 'World' ]);
103+
await client.del('myhash')
104+
// REMOVE_END
105+
// STEP_END
106+
68107
// HIDE_START
69108
await client.quit();
70109
// HIDE_END
71-

doctests/cmds-list.js

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// EXAMPLE: cmds_list
2+
// HIDE_START
3+
import assert from 'node:assert';
4+
import { createClient } from 'redis';
5+
6+
const client = createClient();
7+
await client.connect().catch(console.error);
8+
// HIDE_END
9+
10+
// STEP_START lpush
11+
const res1 = await client.lPush('mylist', 'world');
12+
console.log(res1); // 1
13+
14+
const res2 = await client.lPush('mylist', 'hello');
15+
console.log(res2); // 2
16+
17+
const res3 = await client.lRange('mylist', 0, -1);
18+
console.log(res3); // [ 'hello', 'world' ]
19+
20+
// REMOVE_START
21+
assert.deepEqual(res3, [ 'hello', 'world' ]);
22+
await client.del('mylist');
23+
// REMOVE_END
24+
// STEP_END
25+
26+
// STEP_START lrange
27+
const res4 = await client.rPush('mylist', 'one');
28+
console.log(res4); // 1
29+
30+
const res5 = await client.rPush('mylist', 'two');
31+
console.log(res5); // 2
32+
33+
const res6 = await client.rPush('mylist', 'three');
34+
console.log(res6); // 3
35+
36+
const res7 = await client.lRange('mylist', 0, 0);
37+
console.log(res7); // [ 'one' ]
38+
39+
const res8 = await client.lRange('mylist', -3, 2);
40+
console.log(res8); // [ 'one', 'two', 'three' ]
41+
42+
const res9 = await client.lRange('mylist', -100, 100);
43+
console.log(res9); // [ 'one', 'two', 'three' ]
44+
45+
const res10 = await client.lRange('mylist', 5, 10);
46+
console.log(res10); // []
47+
48+
// REMOVE_START
49+
assert.deepEqual(res7, [ 'one' ]);
50+
assert.deepEqual(res8, [ 'one', 'two', 'three' ]);
51+
assert.deepEqual(res9, [ 'one', 'two', 'three' ]);
52+
assert.deepEqual(res10, []);
53+
await client.del('mylist');
54+
// REMOVE_END
55+
// STEP_END
56+
57+
// STEP_START llen
58+
const res11 = await client.lPush('mylist', 'World');
59+
console.log(res11); // 1
60+
61+
const res12 = await client.lPush('mylist', 'Hello');
62+
console.log(res12); // 2
63+
64+
const res13 = await client.lLen('mylist');
65+
console.log(res13); // 2
66+
67+
// REMOVE_START
68+
assert.equal(res13, 2);
69+
await client.del('mylist');
70+
// REMOVE_END
71+
// STEP_END
72+
73+
// STEP_START rpush
74+
const res14 = await client.rPush('mylist', 'hello');
75+
console.log(res14); // 1
76+
77+
const res15 = await client.rPush('mylist', 'world');
78+
console.log(res15); // 2
79+
80+
const res16 = await client.lRange('mylist', 0, -1);
81+
console.log(res16); // [ 'hello', 'world' ]
82+
83+
// REMOVE_START
84+
assert.deepEqual(res16, [ 'hello', 'world' ]);
85+
await client.del('mylist');
86+
// REMOVE_END
87+
// STEP_END
88+
89+
// STEP_START lpop
90+
const res17 = await client.rPush('mylist', ["one", "two", "three", "four", "five"]);
91+
console.log(res17); // 5
92+
93+
const res18 = await client.lPop('mylist');
94+
console.log(res18); // 'one'
95+
96+
const res19 = await client.lPopCount('mylist', 2);
97+
console.log(res19); // [ 'two', 'three' ]
98+
99+
const res20 = await client.lRange('mylist', 0, -1);
100+
console.log(res20); // [ 'four', 'five' ]
101+
102+
// REMOVE_START
103+
assert.deepEqual(res20, [ 'four', 'five' ]);
104+
await client.del('mylist');
105+
// REMOVE_END
106+
// STEP_END
107+
108+
// STEP_START rpop
109+
const res21 = await client.rPush('mylist', ["one", "two", "three", "four", "five"]);
110+
console.log(res21); // 5
111+
112+
const res22 = await client.rPop('mylist');
113+
console.log(res22); // 'five'
114+
115+
const res23 = await client.rPopCount('mylist', 2);
116+
console.log(res23); // [ 'four', 'three' ]
117+
118+
const res24 = await client.lRange('mylist', 0, -1);
119+
console.log(res24); // [ 'one', 'two' ]
120+
121+
// REMOVE_START
122+
assert.deepEqual(res24, [ 'one', 'two' ]);
123+
await client.del('mylist');
124+
// REMOVE_END
125+
// STEP_END
126+
127+
// HIDE_START
128+
await client.quit();
129+
// HIDE_END

doctests/cmds-servermgmt.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// EXAMPLE: cmds_servermgmt
2+
// REMOVE_START
3+
import assert from 'node:assert';
4+
// REMOVE_END
5+
6+
// HIDE_START
7+
import { createClient } from 'redis';
8+
9+
const client = createClient();
10+
await client.connect().catch(console.error);
11+
// HIDE_END
12+
13+
// STEP_START flushall
14+
// REMOVE_START
15+
await client.set('foo', '1');
16+
await client.set('bar', '2');
17+
await client.set('baz', '3');
18+
// REMOVE_END
19+
const res1 = await client.flushAll('SYNC'); // or ASYNC
20+
console.log(res1); // OK
21+
22+
const res2 = await client.keys('*');
23+
console.log(res2); // []
24+
25+
// REMOVE_START
26+
assert.equal(res1, 'OK');
27+
assert.deepEqual(res2, []);
28+
// REMOVE_END
29+
// STEP_END
30+
31+
// STEP_START info
32+
const res3 = await client.info();
33+
console.log(res3)
34+
// # Server
35+
// redis_version:7.4.0
36+
// redis_git_sha1:c9d29f6a
37+
// redis_git_dirty:0
38+
// redis_build_id:4c367a16e3f9616
39+
// redis_mode:standalone
40+
// ...
41+
// STEP_END
42+
43+
// HIDE_START
44+
await client.quit();
45+
// HIDE_END

doctests/cmds-set.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// EXAMPLE: cmds_set
2+
// REMOVE_START
3+
import assert from 'node:assert';
4+
// REMOVE_END
5+
6+
// HIDE_START
7+
import { createClient } from 'redis';
8+
9+
const client = createClient();
10+
await client.connect().catch(console.error);
11+
// HIDE_END
12+
13+
// STEP_START sadd
14+
const res1 = await client.sAdd('myset', ['Hello', 'World']);
15+
console.log(res1); // 2
16+
17+
const res2 = await client.sAdd('myset', ['World']);
18+
console.log(res2); // 0
19+
20+
const res3 = await client.sMembers('myset')
21+
console.log(res3); // ['Hello', 'World']
22+
23+
// REMOVE_START
24+
assert.deepEqual(res3, ['Hello', 'World']);
25+
await client.del('myset');
26+
// REMOVE_END
27+
// STEP_END
28+
29+
// STEP_START smembers
30+
const res4 = await client.sAdd('myset', ['Hello', 'World']);
31+
console.log(res4); // 2
32+
33+
const res5 = await client.sMembers('myset')
34+
console.log(res5); // ['Hello', 'World']
35+
36+
// REMOVE_START
37+
assert.deepEqual(res5, ['Hello', 'World']);
38+
await client.del('myset');
39+
// REMOVE_END
40+
// STEP_END
41+
42+
// HIDE_START
43+
await client.quit();
44+
// HIDE_END

0 commit comments

Comments
 (0)