2021-01-05 18:02:01 +08:00
|
|
|
import { lexer, splitSql, cleanSql } from '../../src';
|
|
|
|
|
2019-09-25 15:43:36 +08:00
|
|
|
describe('utils', () => {
|
2020-12-09 17:59:24 +08:00
|
|
|
test('split single sql', () => {
|
|
|
|
const sql = 'select id,name from user';
|
2021-01-05 18:02:01 +08:00
|
|
|
const result = splitSql(sql);
|
2020-12-09 17:59:24 +08:00
|
|
|
expect(result.length).toEqual(1);
|
|
|
|
});
|
|
|
|
test('split multiple sql', () => {
|
|
|
|
const sql = `-- a ;
|
|
|
|
select * from a;
|
|
|
|
/*
|
|
|
|
xxx
|
|
|
|
xxx
|
|
|
|
*/
|
|
|
|
select user from b`;
|
2021-01-05 18:02:01 +08:00
|
|
|
const result = splitSql(sql);
|
2020-12-09 17:59:24 +08:00
|
|
|
expect(result.length).toEqual(2);
|
|
|
|
});
|
|
|
|
test('lexer', () => {
|
|
|
|
const sql = `-- a ;
|
|
|
|
select * from a;
|
|
|
|
/*
|
|
|
|
xxx
|
|
|
|
xxx
|
|
|
|
*/
|
|
|
|
select user from b;`;
|
2021-01-05 18:02:01 +08:00
|
|
|
const result = lexer(sql);
|
2020-12-09 17:59:24 +08:00
|
|
|
expect(result.length).toEqual(4);
|
|
|
|
});
|
|
|
|
test('cleanSql', () => {
|
|
|
|
const sql = `-- a ;
|
|
|
|
select * from a;
|
|
|
|
/*
|
|
|
|
xxx
|
|
|
|
xxx
|
|
|
|
*/
|
|
|
|
select user from b`;
|
2021-01-05 18:02:01 +08:00
|
|
|
const result = cleanSql(sql);
|
2020-12-09 17:59:24 +08:00
|
|
|
expect(result.indexOf('xxx')).toEqual(-1);
|
2020-08-28 13:29:47 +08:00
|
|
|
});
|
2021-05-11 13:35:45 +08:00
|
|
|
|
|
|
|
test('clean SQL white spaces', () => {
|
|
|
|
const sql = `
|
|
|
|
select * from a; `;
|
|
|
|
const expected = 'select * from a;';
|
|
|
|
const result = cleanSql(sql);
|
|
|
|
expect(result).toEqual(expected);
|
|
|
|
});
|
2020-08-28 13:29:47 +08:00
|
|
|
});
|