55 lines
1.6 KiB
JavaScript
55 lines
1.6 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
|
|
import {
|
|
DEFAULT_EXPORT_LIMITS,
|
|
MYSQL2_EXPORTER_METADATA,
|
|
SchemaExportError,
|
|
assertMysql57ServerMetadata,
|
|
buildConnectionOptions,
|
|
quoteIdentifier,
|
|
} from './mysql2-schema-exporter.mjs';
|
|
|
|
const config = Object.freeze({
|
|
MYSQL_HOST: '127.0.0.1',
|
|
MYSQL_PORT: '3306',
|
|
MYSQL_DATABASE: 'a_share_analysis_dev',
|
|
MYSQL_USERNAME: 'schema_reader',
|
|
MYSQL_PASSWORD: 'not-for-logs',
|
|
});
|
|
|
|
test('连接配置固定单连接、utf8mb4、东八区且禁用多语句', () => {
|
|
const options = buildConnectionOptions(config);
|
|
assert.deepEqual(options, {
|
|
host: '127.0.0.1',
|
|
port: 3306,
|
|
user: 'schema_reader',
|
|
password: 'not-for-logs',
|
|
database: 'a_share_analysis_dev',
|
|
charset: 'utf8mb4',
|
|
timezone: '+08:00',
|
|
connectTimeout: 5_000,
|
|
multipleStatements: false,
|
|
enableKeepAlive: true,
|
|
});
|
|
});
|
|
|
|
test('标识符转义反引号并拒绝空值和控制字符', () => {
|
|
assert.equal(quoteIdentifier('market`view'), '`market``view`');
|
|
for (const value of ['', 'line\nbreak', 'zero\0byte']) {
|
|
assert.throws(() => quoteIdentifier(value), SchemaExportError);
|
|
}
|
|
});
|
|
|
|
test('只接受 MySQL 5.7.x 与固定字符规则', () => {
|
|
const valid = {
|
|
serverVersion: '5.7.42-log',
|
|
databaseCharset: 'utf8mb4',
|
|
databaseCollation: 'utf8mb4_general_ci',
|
|
};
|
|
assert.doesNotThrow(() => assertMysql57ServerMetadata(valid));
|
|
for (const serverVersion of ['5.6.51', '8.0.40', '10.6.18-MariaDB']) {
|
|
assert.throws(() => assertMysql57ServerMetadata({ ...valid, serverVersion }));
|
|
}
|
|
});
|