mm_mongodb 1.4.8 → 1.4.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +15 -0
- package/README.md +32 -2
- package/index.js +2969 -1274
- package/package.json +2 -2
- package/test.js +297 -64
- package/test_events.js +0 -139
- package/test_redis_api.js +0 -135
- package/test_redis_compatibility.js +0 -155
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
// Redis兼容性API测试文件
|
|
2
|
-
const { MongoDB, mongodb_admin } = require('./index');
|
|
3
|
-
|
|
4
|
-
async function runRedisCompatibilityTests() {
|
|
5
|
-
console.log('开始测试Redis兼容API...');
|
|
6
|
-
console.log('正在连接数据库...');
|
|
7
|
-
|
|
8
|
-
let cache = null;
|
|
9
|
-
const testKeys = [];
|
|
10
|
-
|
|
11
|
-
try {
|
|
12
|
-
// 获取缓存实例
|
|
13
|
-
cache = mongodb_admin('cache');
|
|
14
|
-
|
|
15
|
-
console.log('数据库连接成功!\n');
|
|
16
|
-
|
|
17
|
-
// 核心缓存方法测试
|
|
18
|
-
console.log('=== 测试核心缓存方法 ===');
|
|
19
|
-
const setResult = await cache.set('test_key', 'test_value');
|
|
20
|
-
console.log('set结果:', setResult);
|
|
21
|
-
testKeys.push('test_key');
|
|
22
|
-
|
|
23
|
-
const getResult = await cache.get('test_key');
|
|
24
|
-
console.log('get结果:', getResult);
|
|
25
|
-
|
|
26
|
-
const existsResult = await cache.exists('test_key');
|
|
27
|
-
console.log('exists结果:', existsResult);
|
|
28
|
-
console.log('');
|
|
29
|
-
|
|
30
|
-
// 哈希表操作测试
|
|
31
|
-
console.log('=== 测试哈希表操作 ===');
|
|
32
|
-
await cache.hset('test_hash', 'field1', 'value1');
|
|
33
|
-
testKeys.push('test_hash');
|
|
34
|
-
|
|
35
|
-
const hgetResult = await cache.hget('test_hash', 'field1');
|
|
36
|
-
console.log('hget结果:', hgetResult);
|
|
37
|
-
|
|
38
|
-
const hgetallResult = await cache.hgetall('test_hash');
|
|
39
|
-
console.log('hgetall结果:', hgetallResult);
|
|
40
|
-
|
|
41
|
-
const hmgetResult = await cache.hmget('test_hash', ['field1', 'field2']);
|
|
42
|
-
console.log('hmget结果:', hmgetResult);
|
|
43
|
-
|
|
44
|
-
const hkeysResult = await cache.hkeys('test_hash');
|
|
45
|
-
console.log('hkeys结果:', hkeysResult);
|
|
46
|
-
|
|
47
|
-
const hvalsResult = await cache.hvals('test_hash');
|
|
48
|
-
console.log('hvals结果:', hvalsResult);
|
|
49
|
-
|
|
50
|
-
const hlenResult = await cache.hlen('test_hash');
|
|
51
|
-
console.log('hlen结果:', hlenResult);
|
|
52
|
-
console.log('');
|
|
53
|
-
|
|
54
|
-
// 列表操作测试
|
|
55
|
-
console.log('=== 测试列表操作 ===');
|
|
56
|
-
await cache.lpush('test_list', 'item1');
|
|
57
|
-
testKeys.push('test_list');
|
|
58
|
-
|
|
59
|
-
const llenResult = await cache.llen('test_list');
|
|
60
|
-
console.log('llen结果:', llenResult);
|
|
61
|
-
|
|
62
|
-
const lrangeResult = await cache.lrange('test_list', 0, -1);
|
|
63
|
-
console.log('lrange结果:', lrangeResult);
|
|
64
|
-
|
|
65
|
-
const lindexResult = await cache.lindex('test_list', 1);
|
|
66
|
-
console.log('lindex结果:', lindexResult);
|
|
67
|
-
console.log('');
|
|
68
|
-
|
|
69
|
-
// 集合操作测试
|
|
70
|
-
console.log('=== 测试集合操作 ===');
|
|
71
|
-
await cache.saddMulti('test_set', ['member1', 'member2']);
|
|
72
|
-
testKeys.push('test_set');
|
|
73
|
-
|
|
74
|
-
// 使用mget获取集合内容,然后手动去重模拟smembers
|
|
75
|
-
const setContent = await cache.get('test_set');
|
|
76
|
-
const uniqueMembers = Array.from(new Set(setContent.map(m => JSON.stringify(m)))).map(s => JSON.parse(s));
|
|
77
|
-
console.log('集合成员结果:', uniqueMembers);
|
|
78
|
-
|
|
79
|
-
const scardResult = await cache.scard('test_set');
|
|
80
|
-
console.log('scard结果:', scardResult);
|
|
81
|
-
|
|
82
|
-
const sismemberResult = await cache.sismember('test_set', 'member1');
|
|
83
|
-
console.log('sismember结果:', sismemberResult);
|
|
84
|
-
console.log('');
|
|
85
|
-
|
|
86
|
-
// 有序集合操作测试
|
|
87
|
-
console.log('=== 测试有序集合操作 ===');
|
|
88
|
-
await cache.zadd('test_zset', 1, 'zmember1');
|
|
89
|
-
testKeys.push('test_zset');
|
|
90
|
-
|
|
91
|
-
const zcardResult = await cache.zcard('test_zset');
|
|
92
|
-
console.log('zcard结果:', zcardResult);
|
|
93
|
-
|
|
94
|
-
const zscoreResult = await cache.zscore('test_zset', 'zmember1');
|
|
95
|
-
console.log('zscore结果:', zscoreResult);
|
|
96
|
-
|
|
97
|
-
const zrangebyscoreResult = await cache.zrangebyscore('test_zset', 0, 10);
|
|
98
|
-
console.log('zrangebyscore结果:', zrangebyscoreResult);
|
|
99
|
-
console.log('');
|
|
100
|
-
|
|
101
|
-
// 批量操作测试
|
|
102
|
-
console.log('=== 测试批量操作 ===');
|
|
103
|
-
const batchData = {
|
|
104
|
-
'batch_key1': 'batch_value1',
|
|
105
|
-
'batch_key2': 'batch_value2'
|
|
106
|
-
};
|
|
107
|
-
const msetResult = await cache.mset(batchData);
|
|
108
|
-
console.log('mset结果:', msetResult);
|
|
109
|
-
testKeys.push('batch_key1', 'batch_key2');
|
|
110
|
-
|
|
111
|
-
const mgetResult = await cache.mget(['batch_key1', 'batch_key2']);
|
|
112
|
-
console.log('mget结果:', mgetResult);
|
|
113
|
-
console.log('');
|
|
114
|
-
|
|
115
|
-
// 清理测试数据
|
|
116
|
-
console.log('=== 清理测试数据 ===');
|
|
117
|
-
for (const key of testKeys) {
|
|
118
|
-
await cache.del(key);
|
|
119
|
-
}
|
|
120
|
-
console.log('测试数据清理完成');
|
|
121
|
-
console.log('');
|
|
122
|
-
|
|
123
|
-
// 所有测试通过
|
|
124
|
-
console.log('✅ 所有Redis兼容API测试完成!');
|
|
125
|
-
|
|
126
|
-
// 关闭连接
|
|
127
|
-
await cache.close();
|
|
128
|
-
process.exit(0);
|
|
129
|
-
|
|
130
|
-
} catch (error) {
|
|
131
|
-
console.error('测试过程中出错:', error);
|
|
132
|
-
|
|
133
|
-
// 清理测试数据
|
|
134
|
-
if (cache && testKeys.length > 0) {
|
|
135
|
-
try {
|
|
136
|
-
console.log('尝试清理测试数据...');
|
|
137
|
-
for (const key of testKeys) {
|
|
138
|
-
await cache.del(key);
|
|
139
|
-
}
|
|
140
|
-
} catch (e) {
|
|
141
|
-
console.error('清理数据时出错:', e);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// 关闭连接
|
|
146
|
-
if (cache) {
|
|
147
|
-
await cache.close();
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
process.exit(1);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
// 运行测试
|
|
155
|
-
runRedisCompatibilityTests();
|