dbgate/integration-tests/__tests__/query.spec.js

169 lines
5.0 KiB
JavaScript
Raw Normal View History

2021-05-27 18:49:20 +00:00
const engines = require('../engines');
2021-06-03 09:27:49 +00:00
const { splitQuery } = require('dbgate-query-splitter');
2021-05-27 18:49:20 +00:00
const { testWrapper } = require('../tools');
2024-09-13 11:09:33 +00:00
const initSql = [
'CREATE TABLE t1 (id int primary key)',
'INSERT INTO t1 (id) VALUES (1)',
'INSERT INTO t1 (id) VALUES (2)',
];
2021-05-27 18:49:20 +00:00
expect.extend({
dataRow(row, expected) {
for (const key in expected) {
if (row[key] != expected[key]) {
return {
pass: false,
message: () => `Different key: ${key}`,
};
}
}
return {
pass: true,
message: () => '',
};
},
});
class StreamHandler {
constructor(resolve) {
this.results = [];
this.resolve = resolve;
this.infoRows = [];
}
row(row) {
this.results[this.results.length - 1].rows.push(row);
}
recordset(columns) {
this.results.push({
columns,
rows: [],
});
}
done(result) {
this.resolve(this.results);
}
info(msg) {
this.infoRows.push(msg);
}
}
2021-06-03 09:27:49 +00:00
function executeStreamItem(driver, conn, sql) {
2021-05-27 18:49:20 +00:00
return new Promise(resolve => {
const handler = new StreamHandler(resolve);
driver.stream(conn, sql, handler);
});
}
2021-06-03 09:27:49 +00:00
async function executeStream(driver, conn, sql) {
const results = [];
2021-06-03 09:45:27 +00:00
for (const sqlItem of splitQuery(sql, driver.getQuerySplitterOptions('stream'))) {
2021-06-03 09:27:49 +00:00
const item = await executeStreamItem(driver, conn, sqlItem);
results.push(...item);
}
return results;
}
2021-05-27 18:49:20 +00:00
describe('Query', () => {
test.each(engines.map(engine => [engine.label, engine]))(
'Simple query - %s',
testWrapper(async (conn, driver, engine) => {
2024-09-13 11:09:33 +00:00
for (const sql of initSql) await driver.query(conn, sql, { discardResult: true });
2021-05-27 18:49:20 +00:00
const res = await driver.query(conn, 'SELECT id FROM t1 ORDER BY id');
expect(res.columns).toEqual([
expect.objectContaining({
columnName: 'id',
}),
]);
expect(res.rows).toEqual([
expect.dataRow({
id: 1,
}),
expect.dataRow({
id: 2,
}),
]);
})
);
test.each(engines.map(engine => [engine.label, engine]))(
'Simple stream query - %s',
testWrapper(async (conn, driver, engine) => {
2024-09-13 11:09:33 +00:00
for (const sql of initSql) await driver.query(conn, sql, { discardResult: true });
2021-05-27 18:49:20 +00:00
const results = await executeStream(driver, conn, 'SELECT id FROM t1 ORDER BY id');
expect(results.length).toEqual(1);
const res = results[0];
expect(res.columns).toEqual([expect.objectContaining({ columnName: 'id' })]);
expect(res.rows).toEqual([expect.dataRow({ id: 1 }), expect.dataRow({ id: 2 })]);
})
);
test.each(engines.map(engine => [engine.label, engine]))(
2021-05-28 13:10:02 +00:00
'More queries - %s',
2021-05-27 18:49:20 +00:00
testWrapper(async (conn, driver, engine) => {
2024-09-13 11:09:33 +00:00
for (const sql of initSql) await driver.query(conn, sql, { discardResult: true });
2021-05-27 18:49:20 +00:00
const results = await executeStream(
driver,
conn,
'SELECT id FROM t1 ORDER BY id; SELECT id FROM t1 ORDER BY id DESC'
);
expect(results.length).toEqual(2);
const res1 = results[0];
expect(res1.columns).toEqual([expect.objectContaining({ columnName: 'id' })]);
expect(res1.rows).toEqual([expect.dataRow({ id: 1 }), expect.dataRow({ id: 2 })]);
const res2 = results[1];
expect(res2.columns).toEqual([expect.objectContaining({ columnName: 'id' })]);
expect(res2.rows).toEqual([expect.dataRow({ id: 2 }), expect.dataRow({ id: 1 })]);
})
);
2021-05-28 13:10:02 +00:00
test.each(engines.map(engine => [engine.label, engine]))(
2021-06-03 10:33:02 +00:00
'Script - return data - %s',
2021-05-28 13:10:02 +00:00
testWrapper(async (conn, driver, engine) => {
const results = await executeStream(
driver,
conn,
2024-09-13 11:09:33 +00:00
'CREATE TABLE t1 (id int primary key); INSERT INTO t1 (id) VALUES (1); INSERT INTO t1 (id) VALUES (2); SELECT id FROM t1 ORDER BY id; '
2021-05-28 13:10:02 +00:00
);
expect(results.length).toEqual(1);
const res1 = results[0];
expect(res1.columns).toEqual([expect.objectContaining({ columnName: 'id' })]);
expect(res1.rows).toEqual([expect.dataRow({ id: 1 }), expect.dataRow({ id: 2 })]);
})
);
2021-06-03 10:33:02 +00:00
test.each(engines.map(engine => [engine.label, engine]))(
'Script - no data - %s',
testWrapper(async (conn, driver, engine) => {
const results = await executeStream(
driver,
conn,
'CREATE TABLE t1 (id int); INSERT INTO t1 (id) VALUES (1); INSERT INTO t1 (id) VALUES (2) '
);
expect(results.length).toEqual(0);
})
);
2024-09-13 11:09:33 +00:00
test.each(engines.filter(x => !x.skipDataModifications).map(engine => [engine.label, engine]))(
2021-05-30 07:01:00 +00:00
'Save data query - %s',
testWrapper(async (conn, driver, engine) => {
2024-09-13 11:09:33 +00:00
for (const sql of initSql) await driver.query(conn, sql, { discardResult: true });
2021-05-30 07:01:00 +00:00
2021-06-03 09:27:49 +00:00
await driver.script(
2021-05-30 07:01:00 +00:00
conn,
2024-09-13 11:09:33 +00:00
'INSERT INTO t1 (id) VALUES (3);INSERT INTO t1 (id) VALUES (4);UPDATE t1 SET id=10 WHERE id=1;DELETE FROM t1 WHERE id=2;',
{ discardResult: true }
2021-05-30 07:01:00 +00:00
);
const res = await driver.query(conn, 'SELECT COUNT(*) AS cnt FROM t1');
2021-06-03 10:33:02 +00:00
// console.log(res);
2021-05-30 07:01:00 +00:00
expect(res.rows[0].cnt == 3).toBeTruthy();
})
);
2021-05-27 18:49:20 +00:00
});