Skip to content

Commit fd7b10b

Browse files
authored
TCEs for select command pages (#2824)
* TCEs for select command pages * Preemptive modifications from previous reviews * Update to previous commit
1 parent ebd2037 commit fd7b10b

File tree

4 files changed

+408
-0
lines changed

4 files changed

+408
-0
lines changed

doctests/cmds-generic.js

+195
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
// EXAMPLE: cmds_generic
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 del
14+
const delRes1 = await client.set('key1', 'Hello');
15+
console.log(delRes1); // OK
16+
17+
const delRes2 = await client.set('key2', 'World');
18+
console.log(delRes2); // OK
19+
20+
const delRes3 = await client.del(['key1', 'key2', 'key3']);
21+
console.log(delRes3); // 2
22+
// REMOVE_START
23+
assert.equal(delRes3, 2);
24+
// REMOVE_END
25+
// STEP_END
26+
27+
// STEP_START expire
28+
const expireRes1 = await client.set('mykey', 'Hello');
29+
console.log(expireRes1); // OK
30+
31+
const expireRes2 = await client.expire('mykey', 10);
32+
console.log(expireRes2); // true
33+
34+
const expireRes3 = await client.ttl('mykey');
35+
console.log(expireRes3); // 10
36+
// REMOVE_START
37+
assert.equal(expireRes3, 10);
38+
// REMOVE_END
39+
40+
const expireRes4 = await client.set('mykey', 'Hello World');
41+
console.log(expireRes4); // OK
42+
43+
const expireRes5 = await client.ttl('mykey');
44+
console.log(expireRes5); // -1
45+
// REMOVE_START
46+
assert.equal(expireRes5, -1);
47+
// REMOVE_END
48+
49+
const expireRes6 = await client.expire('mykey', 10, "XX");
50+
console.log(expireRes6); // false
51+
// REMOVE_START
52+
assert.equal(expireRes6, false)
53+
// REMOVE_END
54+
55+
const expireRes7 = await client.ttl('mykey');
56+
console.log(expireRes7); // -1
57+
// REMOVE_START
58+
assert.equal(expireRes7, -1);
59+
// REMOVE_END
60+
61+
const expireRes8 = await client.expire('mykey', 10, "NX");
62+
console.log(expireRes8); // true
63+
// REMOVE_START
64+
assert.equal(expireRes8, true);
65+
// REMOVE_END
66+
67+
const expireRes9 = await client.ttl('mykey');
68+
console.log(expireRes9); // 10
69+
// REMOVE_START
70+
assert.equal(expireRes9, 10);
71+
await client.del('mykey');
72+
// REMOVE_END
73+
// STEP_END
74+
75+
// STEP_START ttl
76+
const ttlRes1 = await client.set('mykey', 'Hello');
77+
console.log(ttlRes1); // OK
78+
79+
const ttlRes2 = await client.expire('mykey', 10);
80+
console.log(ttlRes2); // true
81+
82+
const ttlRes3 = await client.ttl('mykey');
83+
console.log(ttlRes3); // 10
84+
// REMOVE_START
85+
assert.equal(ttlRes3, 10);
86+
await client.del('mykey');
87+
// REMOVE_END
88+
// STEP_END
89+
90+
// STEP_START scan1
91+
const scan1Res1 = await client.sAdd('myset', ['1', '2', '3', 'foo', 'foobar', 'feelsgood']);
92+
console.log(scan1Res1); // 6
93+
94+
const scan1Res2 = [];
95+
for await (const value of client.sScanIterator('myset', { MATCH: 'f*' })) {
96+
scan1Res2.push(value);
97+
}
98+
console.log(scan1Res2); // ['foo', 'foobar', 'feelsgood']
99+
// REMOVE_START
100+
console.assert(scan1Res2.sort().toString() === ['foo', 'foobar', 'feelsgood'].sort().toString());
101+
await client.del('myset');
102+
// REMOVE_END
103+
// STEP_END
104+
105+
// STEP_START scan2
106+
// REMOVE_START
107+
for (let i = 1; i <= 1000; i++) {
108+
await client.set(`key:${i}`, i);
109+
}
110+
// REMOVE_END
111+
let cursor = '0';
112+
let scanResult;
113+
114+
scanResult = await client.scan(cursor, { MATCH: '*11*' });
115+
console.log(scanResult.cursor, scanResult.keys);
116+
117+
scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*' });
118+
console.log(scanResult.cursor, scanResult.keys);
119+
120+
scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*' });
121+
console.log(scanResult.cursor, scanResult.keys);
122+
123+
scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*' });
124+
console.log(scanResult.cursor, scanResult.keys);
125+
126+
scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*', COUNT: 1000 });
127+
console.log(scanResult.cursor, scanResult.keys);
128+
// REMOVE_START
129+
console.assert(scanResult.keys.length === 18);
130+
cursor = '0';
131+
const prefix = 'key:*';
132+
while (cursor !== 0) {
133+
scanResult = await client.scan(cursor, { MATCH: prefix, COUNT: 1000 });
134+
console.log(scanResult.cursor, scanResult.keys);
135+
cursor = scanResult.cursor;
136+
const keys = scanResult.keys;
137+
if (keys.length) {
138+
await client.del(keys);
139+
}
140+
}
141+
// REMOVE_END
142+
// STEP_END
143+
144+
// STEP_START scan3
145+
const scan3Res1 = await client.geoAdd('geokey', { longitude: 0, latitude: 0, member: 'value' });
146+
console.log(scan3Res1); // 1
147+
148+
const scan3Res2 = await client.zAdd('zkey', [{ score: 1000, value: 'value' }]);
149+
console.log(scan3Res2); // 1
150+
151+
const scan3Res3 = await client.type('geokey');
152+
console.log(scan3Res3); // zset
153+
// REMOVE_START
154+
console.assert(scan3Res3 === 'zset');
155+
// REMOVE_END
156+
157+
const scan3Res4 = await client.type('zkey');
158+
console.log(scan3Res4); // zset
159+
// REMOVE_START
160+
console.assert(scan3Res4 === 'zset');
161+
// REMOVE_END
162+
163+
const scan3Res5 = await client.scan('0', { TYPE: 'zset' });
164+
console.log(scan3Res5.keys); // ['zkey', 'geokey']
165+
// REMOVE_START
166+
console.assert(scan3Res5.keys.sort().toString() === ['zkey', 'geokey'].sort().toString());
167+
await client.del(['geokey', 'zkey']);
168+
// REMOVE_END
169+
// STEP_END
170+
171+
// STEP_START scan4
172+
const scan4Res1 = await client.hSet('myhash', { a: 1, b: 2 });
173+
console.log(scan4Res1); // 2
174+
175+
const scan4Res2 = await client.hScan('myhash', 0);
176+
console.log(scan4Res2.tuples); // [{field: 'a', value: '1'}, {field: 'b', value: '2'}]
177+
// REMOVE_START
178+
assert.deepEqual(scan4Res2.tuples, [
179+
{ field: 'a', value: '1' },
180+
{ field: 'b', value: '2' }
181+
]);
182+
// REMOVE_END
183+
184+
const scan4Res3 = await client.hScan('myhash', 0, { COUNT: 10 });
185+
const items = scan4Res3.tuples.map((item) => item.field)
186+
console.log(items); // ['a', 'b']
187+
// REMOVE_START
188+
assert.deepEqual(items, ['a', 'b'])
189+
await client.del('myhash');
190+
// REMOVE_END
191+
// STEP_END
192+
193+
// HIDE_START
194+
await client.quit();
195+
// HIDE_END

doctests/cmds-hash.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// EXAMPLE: cmds_hash
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 hset
11+
const res1 = await client.hSet('myhash', 'field1', 'Hello')
12+
console.log(res1) // 1
13+
14+
const res2 = await client.hGet('myhash', 'field1')
15+
console.log(res2) // Hello
16+
17+
const res3 = await client.hSet(
18+
'myhash',
19+
{
20+
'field2': 'Hi',
21+
'field3': 'World'
22+
}
23+
)
24+
console.log(res3) // 2
25+
26+
const res4 = await client.hGet('myhash', 'field2')
27+
console.log(res4) // Hi
28+
29+
const res5 = await client.hGet('myhash', 'field3')
30+
console.log(res5) // World
31+
32+
const res6 = await client.hGetAll('myhash')
33+
console.log(res6)
34+
35+
// REMOVE_START
36+
assert.equal(res1, 1);
37+
assert.equal(res2, 'Hello');
38+
assert.equal(res3, 2);
39+
assert.equal(res4, 'Hi');
40+
assert.equal(res5, 'World');
41+
assert.deepEqual(res6, {
42+
field1: 'Hello',
43+
field2: 'Hi',
44+
field3: 'World'
45+
});
46+
await client.del('myhash')
47+
// REMOVE_END
48+
// STEP_END
49+
50+
// STEP_START hget
51+
const res7 = await client.hSet('myhash', 'field1', 'foo')
52+
console.log(res7) // 1
53+
54+
const res8 = await client.hGet('myhash', 'field1')
55+
console.log(res8) // foo
56+
57+
const res9 = await client.hGet('myhash', 'field2')
58+
console.log(res9) // foo
59+
60+
// REMOVE_START
61+
assert.equal(res7, 1);
62+
assert.equal(res8, 'foo');
63+
assert.equal(res9, null);
64+
await client.del('myhash')
65+
// REMOVE_END
66+
// STEP_END
67+
68+
// HIDE_START
69+
await client.quit();
70+
// HIDE_END
71+

doctests/cmds-sorted-set.js

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// EXAMPLE: cmds_sorted_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+
client.on('error', err => console.log('Redis Client Error', err));
11+
await client.connect().catch(console.error);
12+
// HIDE_END
13+
14+
// STEP_START zadd
15+
const val1 = await client.zAdd("myzset", [{ value: 'one', score: 1 }]);
16+
console.log(val1);
17+
// returns 1
18+
19+
const val2 = await client.zAdd("myzset", [{ value: 'uno', score: 1 }]);
20+
console.log(val2);
21+
// returns 1
22+
23+
const val3 = await client.zAdd("myzset", [{ value: 'two', score: 2 }, { value: 'three', score: 3 }]);
24+
console.log(val3);
25+
// returns 2
26+
27+
const val4 = await client.zRangeWithScores("myzset", 0, -1);
28+
console.log(val4);
29+
// returns [{value: 'one', score: 1}, {value: 'uno', score: 1}, {value: 'two', score: 2}, {value: 'three', score: 3} ]
30+
31+
// REMOVE_START
32+
assert.equal(val1, 1);
33+
assert.equal(val2, 1);
34+
assert.equal(val3, 2);
35+
assert.deepEqual(val4, [
36+
{ value: 'one', score: 1 },
37+
{ value: 'uno', score: 1 },
38+
{ value: 'two', score: 2 },
39+
{ value: 'three', score: 3 }
40+
]);
41+
await client.del('myzset');
42+
// REMOVE_END
43+
// STEP_END
44+
45+
// STEP_START zrange1
46+
const val5 = await client.zAdd("myzset", [
47+
{ value: 'one', score: 1 },
48+
{ value: 'two', score: 2 },
49+
{ value: 'three', score: 3 }
50+
]);
51+
console.log(val5);
52+
// returns 3
53+
54+
const val6 = await client.zRange('myzset', 0, -1);
55+
console.log(val6);
56+
// returns ['one', 'two', 'three']
57+
// REMOVE_START
58+
console.assert(JSON.stringify(val6) === JSON.stringify(['one', 'two', 'three']));
59+
// REMOVE_END
60+
61+
const val7 = await client.zRange('myzset', 2, 3);
62+
console.log(val7);
63+
// returns ['three']
64+
// REMOVE_START
65+
console.assert(JSON.stringify(val7) === JSON.stringify(['three']));
66+
// REMOVE_END
67+
68+
const val8 = await client.zRange('myzset', -2, -1);
69+
console.log(val8);
70+
// returns ['two', 'three']
71+
// REMOVE_START
72+
console.assert(JSON.stringify(val8) === JSON.stringify(['two', 'three']));
73+
await client.del('myzset');
74+
// REMOVE_END
75+
// STEP_END
76+
77+
// STEP_START zrange2
78+
const val9 = await client.zAdd("myzset", [
79+
{ value: 'one', score: 1 },
80+
{ value: 'two', score: 2 },
81+
{ value: 'three', score: 3 }
82+
]);
83+
console.log(val9);
84+
// returns 3
85+
86+
const val10 = await client.zRangeWithScores('myzset', 0, 1);
87+
console.log(val10);
88+
// returns [{value: 'one', score: 1}, {value: 'two', score: 2}]
89+
// REMOVE_START
90+
console.assert(JSON.stringify(val10) === JSON.stringify([{value: 'one', score: 1}, {value: 'two', score: 2}]));
91+
await client.del('myzset');
92+
// REMOVE_END
93+
// STEP_END
94+
95+
// STEP_START zrange3
96+
const val11 = await client.zAdd("myzset", [
97+
{ value: 'one', score: 1 },
98+
{ value: 'two', score: 2 },
99+
{ value: 'three', score: 3 }
100+
]);
101+
console.log(val11);
102+
// returns 3
103+
104+
const val12 = await client.zRange('myzset', 2, 3, { BY: 'SCORE', LIMIT: { offset: 1, count: 1 } });
105+
console.log(val12);
106+
// >>> ['three']
107+
// REMOVE_START
108+
console.assert(JSON.stringify(val12) === JSON.stringify(['three']));
109+
await client.del('myzset');
110+
// REMOVE_END
111+
// STEP_END
112+
113+
// HIDE_START
114+
await client.quit();
115+
// HIDE_END

0 commit comments

Comments
 (0)