-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-redis.js
76 lines (64 loc) · 2.75 KB
/
test-redis.js
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
import { redisClient, connectRedis } from './config/redis.js';
async function testRedis() {
try {
// Connect to Redis
await connectRedis();
console.log('\n----- Basic Operations -----');
// Test 1: String operations
console.log('\nTest 1: String Operations');
await redisClient.set('test:string', 'Hello from Redis!');
const stringValue = await redisClient.get('test:string');
console.log('String Test:', stringValue === 'Hello from Redis!' ? 'PASSED ✓' : 'FAILED ✗');
// Test 2: Expiration
console.log('\nTest 2: Expiration');
await redisClient.setEx('test:expiry', 5, 'This will expire in 5 seconds');
const ttl = await redisClient.ttl('test:expiry');
console.log(`TTL test: ${ttl} seconds remaining`);
console.log('Expiration Test:', ttl > 0 && ttl <= 5 ? 'PASSED ✓' : 'FAILED ✗');
// Test 3: JSON data
console.log('\nTest 3: JSON Data');
const user = {
id: '12345',
name: 'Test User',
email: '[email protected]',
roles: ['user', 'admin']
};
await redisClient.set('test:user', JSON.stringify(user));
const userJson = await redisClient.get('test:user');
const parsedUser = JSON.parse(userJson);
console.log('JSON Test:', parsedUser.name === 'Test User' ? 'PASSED ✓' : 'FAILED ✗');
// Test 4: Increment
console.log('\nTest 4: Increment');
await redisClient.set('test:counter', 10);
const newValue = await redisClient.incrBy('test:counter', 5);
console.log('Increment Test:', newValue === 15 ? 'PASSED ✓' : 'FAILED ✗');
// Test 5: Lists
console.log('\nTest 5: Lists');
await redisClient.del('test:list');
await redisClient.rPush('test:list', ['item1', 'item2', 'item3']);
const listLength = await redisClient.lLen('test:list');
const listItems = await redisClient.lRange('test:list', 0, -1);
console.log('List Items:', listItems);
console.log('List Test:', listLength === 3 ? 'PASSED ✓' : 'FAILED ✗');
// Test 6: Hash
console.log('\nTest 6: Hash');
await redisClient.hSet('test:hash', {
field1: 'value1',
field2: 'value2',
field3: 'value3'
});
const hashValue = await redisClient.hGetAll('test:hash');
console.log('Hash Values:', hashValue);
console.log('Hash Test:', hashValue.field2 === 'value2' ? 'PASSED ✓' : 'FAILED ✗');
// Clean up
console.log('\nCleaning up test keys...');
await redisClient.del('test:string', 'test:expiry', 'test:user', 'test:counter', 'test:list', 'test:hash');
console.log('All test keys removed');
// Disconnect
await redisClient.quit();
console.log('\nTest completed successfully!');
} catch (error) {
console.error('Redis test failed:', error);
}
}
testRedis();