2021-05-27 11:52:51 +00:00
|
|
|
const { testWrapper } = require('../tools');
|
2021-05-27 08:53:53 +00:00
|
|
|
const engines = require('../engines');
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
2024-09-13 13:55:33 +00:00
|
|
|
const initSql = ['CREATE TABLE t1 (id int primary key)', 'CREATE TABLE t2 (id int primary key)'];
|
2021-05-27 08:53:53 +00:00
|
|
|
|
|
|
|
function flatSource() {
|
|
|
|
return _.flatten(
|
2021-05-27 11:50:49 +00:00
|
|
|
engines.map(engine => (engine.objects || []).map(object => [engine.label, object.type, object, engine]))
|
2021-05-27 08:53:53 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const obj1Match = expect.objectContaining({
|
|
|
|
pureName: 'obj1',
|
|
|
|
});
|
2021-05-28 13:47:48 +00:00
|
|
|
const view1Match = expect.objectContaining({
|
|
|
|
pureName: 'obj1',
|
2021-05-30 08:34:52 +00:00
|
|
|
columns: expect.arrayContaining([
|
2021-05-28 13:47:48 +00:00
|
|
|
expect.objectContaining({
|
|
|
|
columnName: 'id',
|
|
|
|
}),
|
2021-05-30 08:34:52 +00:00
|
|
|
]),
|
2021-05-28 13:47:48 +00:00
|
|
|
});
|
2021-05-27 08:53:53 +00:00
|
|
|
|
|
|
|
describe('Object analyse', () => {
|
2021-05-27 11:17:25 +00:00
|
|
|
test.each(flatSource())(
|
2021-05-27 13:46:21 +00:00
|
|
|
'Full analysis - %s - %s',
|
2021-05-27 11:50:49 +00:00
|
|
|
testWrapper(async (conn, driver, type, object, engine) => {
|
2024-09-13 13:55:33 +00:00
|
|
|
for (const sql of initSql) await driver.query(conn, sql, { discardResult: true });
|
2021-05-27 11:17:25 +00:00
|
|
|
|
2024-09-13 13:55:33 +00:00
|
|
|
await driver.query(conn, object.create1, { discardResult: true });
|
2021-05-27 11:17:25 +00:00
|
|
|
const structure = await driver.analyseFull(conn);
|
|
|
|
|
|
|
|
expect(structure[type].length).toEqual(1);
|
2021-05-28 20:18:06 +00:00
|
|
|
expect(structure[type][0]).toEqual(type.includes('views') ? view1Match : obj1Match);
|
2021-05-27 11:17:25 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
test.each(flatSource())(
|
2021-05-27 13:46:21 +00:00
|
|
|
'Incremental analysis - add - %s - %s',
|
2021-05-27 11:50:49 +00:00
|
|
|
testWrapper(async (conn, driver, type, object, engine) => {
|
2024-09-13 13:55:33 +00:00
|
|
|
for (const sql of initSql) await driver.query(conn, sql, { discardResult: true });
|
2021-05-27 11:17:25 +00:00
|
|
|
|
2024-09-13 13:55:33 +00:00
|
|
|
await driver.query(conn, object.create2, { discardResult: true });
|
2021-05-27 11:17:25 +00:00
|
|
|
const structure1 = await driver.analyseFull(conn);
|
2024-09-13 13:55:33 +00:00
|
|
|
await driver.query(conn, object.create1, { discardResult: true });
|
2021-05-27 11:17:25 +00:00
|
|
|
const structure2 = await driver.analyseIncremental(conn, structure1);
|
|
|
|
|
|
|
|
expect(structure2[type].length).toEqual(2);
|
2021-05-28 20:18:06 +00:00
|
|
|
expect(structure2[type].find(x => x.pureName == 'obj1')).toEqual(type.includes('views') ? view1Match : obj1Match);
|
2021-05-27 11:17:25 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
test.each(flatSource())(
|
2021-05-27 13:46:21 +00:00
|
|
|
'Incremental analysis - drop - %s - %s',
|
2021-05-27 11:50:49 +00:00
|
|
|
testWrapper(async (conn, driver, type, object, engine) => {
|
2024-09-13 13:55:33 +00:00
|
|
|
for (const sql of initSql) await driver.query(conn, sql, { discardResult: true });
|
2021-05-27 11:17:25 +00:00
|
|
|
|
2024-09-13 13:55:33 +00:00
|
|
|
await driver.query(conn, object.create1, { discardResult: true });
|
|
|
|
await driver.query(conn, object.create2, { discardResult: true });
|
2021-05-27 11:17:25 +00:00
|
|
|
const structure1 = await driver.analyseFull(conn);
|
2024-09-13 13:55:33 +00:00
|
|
|
await driver.query(conn, object.drop2, { discardResult: true });
|
2021-05-27 11:17:25 +00:00
|
|
|
const structure2 = await driver.analyseIncremental(conn, structure1);
|
|
|
|
|
|
|
|
expect(structure2[type].length).toEqual(1);
|
2021-05-28 20:18:06 +00:00
|
|
|
expect(structure2[type][0]).toEqual(type.includes('views') ? view1Match : obj1Match);
|
2021-05-27 11:17:25 +00:00
|
|
|
})
|
|
|
|
);
|
2021-05-27 13:46:21 +00:00
|
|
|
|
|
|
|
test.each(flatSource())(
|
|
|
|
'Create SQL - add - %s - %s',
|
|
|
|
testWrapper(async (conn, driver, type, object, engine) => {
|
2024-09-13 13:55:33 +00:00
|
|
|
for (const sql of initSql) await driver.query(conn, sql, { discardResult: true });
|
2021-05-27 13:46:21 +00:00
|
|
|
|
2024-09-13 13:55:33 +00:00
|
|
|
await driver.query(conn, object.create1, { discardResult: true });
|
2021-05-27 13:46:21 +00:00
|
|
|
const structure1 = await driver.analyseFull(conn);
|
2024-09-13 13:55:33 +00:00
|
|
|
await driver.query(conn, object.drop1, { discardResult: true });
|
2021-05-27 13:46:21 +00:00
|
|
|
const structure2 = await driver.analyseIncremental(conn, structure1);
|
|
|
|
expect(structure2[type].length).toEqual(0);
|
|
|
|
|
2024-09-13 13:55:33 +00:00
|
|
|
await driver.query(conn, structure1[type][0].createSql, { discardResult: true });
|
2021-05-27 13:46:21 +00:00
|
|
|
|
|
|
|
const structure3 = await driver.analyseIncremental(conn, structure2);
|
|
|
|
|
|
|
|
expect(structure3[type].length).toEqual(1);
|
2021-05-28 20:18:06 +00:00
|
|
|
expect(structure3[type][0]).toEqual(type.includes('views') ? view1Match : obj1Match);
|
2021-05-27 13:46:21 +00:00
|
|
|
})
|
|
|
|
);
|
2021-05-27 08:53:53 +00:00
|
|
|
});
|