dbgate/integration-tests/tools.js

70 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2024-08-20 10:18:55 +00:00
global.DBGATE_PACKAGES = {
'dbgate-tools': require('dbgate-tools'),
'dbgate-sqltree': require('dbgate-sqltree'),
};
2021-05-27 08:28:52 +00:00
const requireEngineDriver = require('dbgate-api/src/utility/requireEngineDriver');
const crypto = require('crypto');
function randomDbName() {
const generatedKey = crypto.randomBytes(6);
const newKey = generatedKey.toString('hex');
return `db${newKey}`;
}
2021-05-27 11:07:58 +00:00
function extractConnection(engine) {
2021-05-27 08:28:52 +00:00
const { connection } = engine;
2021-05-27 11:07:58 +00:00
if (process.env.LOCALTEST && engine.local) {
return {
...connection,
...engine.local,
};
}
return connection;
}
async function connect(engine, database) {
const connection = extractConnection(engine);
2021-05-27 08:28:52 +00:00
const driver = requireEngineDriver(connection);
if (engine.generateDbFile) {
const conn = await driver.connect({
...connection,
databaseFile: `dbtemp/${database}`,
});
return conn;
} else {
const conn = await driver.connect(connection);
await driver.query(conn, `CREATE DATABASE ${database}`);
await driver.close(conn);
const res = await driver.connect({
...connection,
database,
});
return res;
}
}
2024-08-20 10:18:55 +00:00
const testWrapper =
body =>
async (label, ...other) => {
const engine = other[other.length - 1];
const driver = requireEngineDriver(engine.connection);
const conn = await connect(engine, randomDbName());
try {
await body(conn, driver, ...other);
} finally {
await driver.close(conn);
}
};
2021-05-27 11:07:58 +00:00
2021-05-27 08:28:52 +00:00
module.exports = {
randomDbName,
connect,
2021-05-27 11:07:58 +00:00
extractConnection,
testWrapper,
2021-05-27 08:28:52 +00:00
};