lava-oushudb-dt-sql-parser/test/utils/index.test.ts

61 lines
1.6 KiB
TypeScript
Raw Normal View History

import { lexer, splitSql, cleanSql } from '../../src';
2021-05-11 13:33:03 +08:00
import { TokenType } from '../../src/utils/token';
2019-09-25 15:43:36 +08:00
describe('utils', () => {
test('split single sql', () => {
const sql = 'select id,name from user';
const result = splitSql(sql);
expect(result.length).toEqual(1);
});
test('split multiple sql', () => {
const sql = `-- a ;
select * from a;
/*
xxx
xxx
*/
select user from b`;
const result = splitSql(sql);
expect(result.length).toEqual(2);
});
test('lexer', () => {
const sql = `-- a ;
select * from a;
/*
xxx
xxx
*/
select user from b;`;
const result = lexer(sql);
expect(result.length).toEqual(4);
});
2021-05-11 13:33:03 +08:00
test('lexer for comments', () => {
const sql = `select * from a;--comments`;
const expected = `--comments`;
const result = lexer(sql);
const comments = result.find((token) =>
token.type === TokenType.Comment,
);
expect(comments.value).toEqual(expected);
});
test('cleanSql', () => {
const sql = `-- a ;
select * from a;
/*
xxx
xxx
*/
select user from b`;
const result = cleanSql(sql);
expect(result.indexOf('xxx')).toEqual(-1);
2020-08-28 13:29:47 +08:00
});
2021-05-11 13:39:42 +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
});