dbgate/integration-tests/tools.js

64 lines
1.4 KiB
JavaScript
Raw Normal View History

2021-05-27 08:28:52 +00:00
global.DBGATE_TOOLS = require('dbgate-tools');
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;
}
}
2021-05-27 11:50:49 +00:00
const testWrapper = body => async (label, ...other) => {
const engine = other[other.length - 1];
2021-05-27 11:17:25 +00:00
const driver = requireEngineDriver(engine.connection);
2021-05-27 11:07:58 +00:00
const conn = await connect(engine, randomDbName());
try {
2021-05-27 11:50:49 +00:00
await body(conn, driver, ...other);
2021-05-27 11:07:58 +00:00
} finally {
await driver.close(conn);
}
};
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
};