mm_mysql 2.4.1 → 2.4.3
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/db.js +152 -223
- package/index.js +19 -24
- package/package.json +4 -4
- package/sql.js +381 -499
- package/test2.js +381 -0
package/test2.js
ADDED
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MySQL错误处理测试文件
|
|
3
|
+
* @file test2.js
|
|
4
|
+
* @description 测试Mysql类的错误日志输出功能
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const { Mysql } = require('./index.js');
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 错误测试类
|
|
11
|
+
* @class ErrorTestMysql
|
|
12
|
+
*/
|
|
13
|
+
class ErrorTestMysql {
|
|
14
|
+
/**
|
|
15
|
+
* 构造函数
|
|
16
|
+
* @param {object} config - 数据库配置
|
|
17
|
+
*/
|
|
18
|
+
constructor(config) {
|
|
19
|
+
this.config = {
|
|
20
|
+
host: '127.0.0.1',
|
|
21
|
+
port: 3306,
|
|
22
|
+
user: 'root',
|
|
23
|
+
password: 'Asd159357',
|
|
24
|
+
database: 'test_db',
|
|
25
|
+
debug: true,
|
|
26
|
+
...config || {}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
this._mysql = null;
|
|
30
|
+
this._test_results = [];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 初始化测试
|
|
35
|
+
* @returns {Promise<boolean>}
|
|
36
|
+
*/
|
|
37
|
+
async _init() {
|
|
38
|
+
try {
|
|
39
|
+
this._mysql = new Mysql(this.config);
|
|
40
|
+
await this._mysql.open();
|
|
41
|
+
console.log('=== 错误处理测试初始化完成 ===');
|
|
42
|
+
return true;
|
|
43
|
+
} catch (error) {
|
|
44
|
+
console.error('初始化失败:', error.message);
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 记录测试结果
|
|
51
|
+
* @param {string} test_name - 测试名称
|
|
52
|
+
* @param {boolean} success - 是否成功
|
|
53
|
+
* @param {string} message - 测试信息
|
|
54
|
+
*/
|
|
55
|
+
_recordResult(test_name, success, message) {
|
|
56
|
+
this._test_results.push({
|
|
57
|
+
test_name,
|
|
58
|
+
success,
|
|
59
|
+
message,
|
|
60
|
+
timestamp: new Date().toISOString()
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const status = success ? '✓' : '✗';
|
|
64
|
+
console.log(`${status} ${test_name}: ${message}`);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 测试无效表查询错误
|
|
69
|
+
* @returns {Promise<boolean>}
|
|
70
|
+
*/
|
|
71
|
+
async testInvalidTable() {
|
|
72
|
+
try {
|
|
73
|
+
console.log('\n--- 测试1: 查询不存在的表 ---');
|
|
74
|
+
|
|
75
|
+
// 执行无效SQL
|
|
76
|
+
const invalid_sql = 'SELECT * FROM non_existent_table_xyz';
|
|
77
|
+
const result = await this._mysql.run(invalid_sql);
|
|
78
|
+
|
|
79
|
+
// 检查错误信息
|
|
80
|
+
if (this._mysql.error) {
|
|
81
|
+
console.log('错误信息:', this._mysql.error);
|
|
82
|
+
this._recordResult('无效表查询', true, `成功捕获错误: ${this._mysql.error.message}`);
|
|
83
|
+
return true;
|
|
84
|
+
} else {
|
|
85
|
+
this._recordResult('无效表查询', false, '未捕获到预期的错误');
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
} catch (error) {
|
|
89
|
+
this._recordResult('无效表查询', false, `异常错误: ${error.message}`);
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* 测试无效SQL语法错误
|
|
96
|
+
* @returns {Promise<boolean>}
|
|
97
|
+
*/
|
|
98
|
+
async testInvalidSyntax() {
|
|
99
|
+
try {
|
|
100
|
+
console.log('\n--- 测试2: 无效SQL语法 ---');
|
|
101
|
+
|
|
102
|
+
// 执行语法错误的SQL
|
|
103
|
+
const invalid_sql = 'SELECT FROM test_users';
|
|
104
|
+
const result = await this._mysql.run(invalid_sql);
|
|
105
|
+
|
|
106
|
+
// 检查错误信息
|
|
107
|
+
if (this._mysql.error) {
|
|
108
|
+
console.log('错误信息:', this._mysql.error);
|
|
109
|
+
this._recordResult('无效SQL语法', true, `成功捕获错误: ${this._mysql.error.message}`);
|
|
110
|
+
return true;
|
|
111
|
+
} else {
|
|
112
|
+
this._recordResult('无效SQL语法', false, '未捕获到预期的错误');
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
} catch (error) {
|
|
116
|
+
this._recordResult('无效SQL语法', false, `异常错误: ${error.message}`);
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* 测试exec方法错误处理
|
|
123
|
+
* @returns {Promise<boolean>}
|
|
124
|
+
*/
|
|
125
|
+
async testExecError() {
|
|
126
|
+
try {
|
|
127
|
+
console.log('\n--- 测试3: exec方法错误处理 ---');
|
|
128
|
+
|
|
129
|
+
// 执行无效INSERT语句
|
|
130
|
+
const invalid_sql = 'INSERT INTO non_existent_table (name) VALUES (?)';
|
|
131
|
+
const result = await this._mysql.exec(invalid_sql, ['test']);
|
|
132
|
+
|
|
133
|
+
// 检查错误信息
|
|
134
|
+
if (this._mysql.error) {
|
|
135
|
+
console.log('错误信息:', this._mysql.error);
|
|
136
|
+
this._recordResult('exec方法错误', true, `成功捕获错误: ${this._mysql.error.message}`);
|
|
137
|
+
return true;
|
|
138
|
+
} else {
|
|
139
|
+
this._recordResult('exec方法错误', false, '未捕获到预期的错误');
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
} catch (error) {
|
|
143
|
+
this._recordResult('exec方法错误', false, `异常错误: ${error.message}`);
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* 测试DB类错误处理
|
|
150
|
+
* @returns {Promise<boolean>}
|
|
151
|
+
*/
|
|
152
|
+
async testDbClassError() {
|
|
153
|
+
try {
|
|
154
|
+
console.log('\n--- 测试4: DB类错误处理 ---');
|
|
155
|
+
|
|
156
|
+
// 获取DB实例并尝试操作不存在的表
|
|
157
|
+
const db = this._mysql.db().new('non_existent_table_db', 'id');
|
|
158
|
+
|
|
159
|
+
// 尝试获取数据
|
|
160
|
+
const result = await db.get({ id: 1 });
|
|
161
|
+
|
|
162
|
+
// 检查错误信息
|
|
163
|
+
if (this._mysql.error) {
|
|
164
|
+
console.log('错误信息:', this._mysql.error);
|
|
165
|
+
this._recordResult('DB类错误', true, `成功捕获错误: ${this._mysql.error.message}`);
|
|
166
|
+
return true;
|
|
167
|
+
} else {
|
|
168
|
+
this._recordResult('DB类错误', false, '未捕获到预期的错误');
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
} catch (error) {
|
|
172
|
+
this._recordResult('DB类错误', false, `异常错误: ${error.message}`);
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* 测试连接失败错误(使用错误配置)
|
|
179
|
+
* @returns {Promise<boolean>}
|
|
180
|
+
*/
|
|
181
|
+
async testConnectionError() {
|
|
182
|
+
try {
|
|
183
|
+
console.log('\n--- 测试5: 连接失败错误 ---');
|
|
184
|
+
|
|
185
|
+
// 使用错误配置创建连接
|
|
186
|
+
const bad_config = {
|
|
187
|
+
host: '127.0.0.1',
|
|
188
|
+
port: 3307, // 错误端口
|
|
189
|
+
user: 'root',
|
|
190
|
+
password: 'wrong_password',
|
|
191
|
+
database: 'test_db',
|
|
192
|
+
debug: true
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
const bad_mysql = new Mysql(bad_config);
|
|
196
|
+
const result = await bad_mysql.open();
|
|
197
|
+
|
|
198
|
+
// 连接应该失败
|
|
199
|
+
if (result === false) {
|
|
200
|
+
console.log('连接失败(预期行为)');
|
|
201
|
+
this._recordResult('连接失败错误', true, '成功检测到连接失败');
|
|
202
|
+
return true;
|
|
203
|
+
} else {
|
|
204
|
+
this._recordResult('连接失败错误', false, '连接意外成功,测试失败');
|
|
205
|
+
await bad_mysql.close();
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
} catch (error) {
|
|
209
|
+
console.log('捕获到异常:', error.message);
|
|
210
|
+
this._recordResult('连接失败错误', true, `成功捕获连接异常: ${error.message}`);
|
|
211
|
+
return true;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* 测试参数验证错误
|
|
217
|
+
* @returns {Promise<boolean>}
|
|
218
|
+
*/
|
|
219
|
+
async testParameterValidation() {
|
|
220
|
+
try {
|
|
221
|
+
console.log('\n--- 测试6: 参数验证错误 ---');
|
|
222
|
+
|
|
223
|
+
// 测试无效参数类型
|
|
224
|
+
let error_count = 0;
|
|
225
|
+
|
|
226
|
+
try {
|
|
227
|
+
await this._mysql.run(null);
|
|
228
|
+
} catch (error) {
|
|
229
|
+
console.log('参数验证错误1:', error.message);
|
|
230
|
+
error_count++;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
try {
|
|
234
|
+
await this._mysql.run('SELECT 1', 'invalid');
|
|
235
|
+
} catch (error) {
|
|
236
|
+
console.log('参数验证错误2:', error.message);
|
|
237
|
+
error_count++;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (error_count === 2) {
|
|
241
|
+
this._recordResult('参数验证错误', true, '成功捕获2个参数验证错误');
|
|
242
|
+
return true;
|
|
243
|
+
} else {
|
|
244
|
+
this._recordResult('参数验证错误', false, `只捕获到 ${error_count} 个错误,预期2个`);
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
247
|
+
} catch (error) {
|
|
248
|
+
this._recordResult('参数验证错误', false, `异常错误: ${error.message}`);
|
|
249
|
+
return false;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* 运行所有错误测试
|
|
255
|
+
* @returns {Promise<object>}
|
|
256
|
+
*/
|
|
257
|
+
async runAllTests() {
|
|
258
|
+
console.log('\n=== 开始运行MySQL错误处理测试 ===\n');
|
|
259
|
+
|
|
260
|
+
// 初始化测试
|
|
261
|
+
const init_success = await this._init();
|
|
262
|
+
if (!init_success) {
|
|
263
|
+
console.log('测试初始化失败');
|
|
264
|
+
return this._getTestSummary();
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// 定义测试用例
|
|
268
|
+
const test_cases = [
|
|
269
|
+
{ name: '无效表查询', method: 'testInvalidTable' },
|
|
270
|
+
{ name: '无效SQL语法', method: 'testInvalidSyntax' },
|
|
271
|
+
{ name: 'exec方法错误', method: 'testExecError' },
|
|
272
|
+
{ name: 'DB类错误', method: 'testDbClassError' },
|
|
273
|
+
{ name: '连接失败错误', method: 'testConnectionError' },
|
|
274
|
+
{ name: '参数验证错误', method: 'testParameterValidation' }
|
|
275
|
+
];
|
|
276
|
+
|
|
277
|
+
// 执行测试
|
|
278
|
+
for (const test_case of test_cases) {
|
|
279
|
+
try {
|
|
280
|
+
console.log(`执行测试: ${test_case.name}`);
|
|
281
|
+
await this[test_case.method]();
|
|
282
|
+
} catch (error) {
|
|
283
|
+
console.error(`测试异常: ${test_case.name}`, error);
|
|
284
|
+
this._recordResult(test_case.name, false, `测试执行异常: ${error.message}`);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// 关闭连接
|
|
289
|
+
if (this._mysql) {
|
|
290
|
+
await this._mysql.close();
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
return this._getTestSummary();
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* 获取测试摘要
|
|
298
|
+
* @returns {object}
|
|
299
|
+
*/
|
|
300
|
+
_getTestSummary() {
|
|
301
|
+
const total = this._test_results.length;
|
|
302
|
+
const passed = this._test_results.filter(r => r.success).length;
|
|
303
|
+
const failed = total - passed;
|
|
304
|
+
|
|
305
|
+
console.log('\n=== 测试结果摘要 ===');
|
|
306
|
+
console.log(`总测试数: ${total}`);
|
|
307
|
+
console.log(`通过: ${passed}`);
|
|
308
|
+
console.log(`失败: ${failed}`);
|
|
309
|
+
console.log(`通过率: ${((passed / total) * 100).toFixed(1)}%`);
|
|
310
|
+
|
|
311
|
+
// 显示失败的测试
|
|
312
|
+
const failed_tests = this._test_results.filter(r => !r.success);
|
|
313
|
+
if (failed_tests.length > 0) {
|
|
314
|
+
console.log('\n失败的测试:');
|
|
315
|
+
failed_tests.forEach(test => {
|
|
316
|
+
console.log(` ✗ ${test.test_name}: ${test.message}`);
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
return {
|
|
321
|
+
total,
|
|
322
|
+
passed,
|
|
323
|
+
failed,
|
|
324
|
+
results: this._test_results
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* 主测试函数
|
|
330
|
+
* @returns {Promise<void>}
|
|
331
|
+
*/
|
|
332
|
+
async main() {
|
|
333
|
+
try {
|
|
334
|
+
const summary = await this.runAllTests();
|
|
335
|
+
|
|
336
|
+
if (summary.failed > 0) {
|
|
337
|
+
console.log('\n❌ 部分测试失败');
|
|
338
|
+
process.exit(1);
|
|
339
|
+
} else {
|
|
340
|
+
console.log('\n✅ 所有测试通过!');
|
|
341
|
+
process.exit(0);
|
|
342
|
+
}
|
|
343
|
+
} catch (error) {
|
|
344
|
+
console.error('测试执行异常:', error);
|
|
345
|
+
process.exit(1);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// 模块导出
|
|
351
|
+
exports.ErrorTestMysql = ErrorTestMysql;
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* 快速测试函数
|
|
355
|
+
* @param {object} config - 数据库配置
|
|
356
|
+
*/
|
|
357
|
+
const quick_test = async function(config) {
|
|
358
|
+
const tester = new ErrorTestMysql(config);
|
|
359
|
+
await tester.main();
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
// 如果直接运行此文件,执行测试
|
|
363
|
+
if (require.main === module) {
|
|
364
|
+
const test_config = {
|
|
365
|
+
host: process.env.DB_HOST || '127.0.0.1',
|
|
366
|
+
port: parseInt(process.env.DB_PORT) || 3306,
|
|
367
|
+
user: process.env.DB_USER || 'root',
|
|
368
|
+
password: process.env.DB_PASSWORD || 'Asd159357',
|
|
369
|
+
database: process.env.DB_NAME || 'test_db',
|
|
370
|
+
debug: process.env.DEBUG === 'true'
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
console.log('使用配置:', {
|
|
374
|
+
...test_config,
|
|
375
|
+
password: '***'
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
quick_test(test_config);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
exports.quick_test = quick_test;
|