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

96 lines
3.6 KiB
TypeScript
Raw Normal View History

2019-09-25 15:43:36 +08:00
import * as dtSqlParser from '../src';
import { SyntaxResult } from '../src/core/sqlSyntaxParser';
2019-05-31 15:58:34 +08:00
const parser = dtSqlParser.parser;
const filter = dtSqlParser.filter;
2019-09-25 15:43:36 +08:00
const flinksqlParser = dtSqlParser.flinksqlParser;
2019-05-31 15:58:34 +08:00
describe('complete test', () => {
describe('hive', () => {
test('complete result', () => {
const sql = 'select id,name from user ';
2019-09-25 15:43:36 +08:00
const result = parser.parserSql([sql, ''], dtSqlParser.parser.sqlType.Hive);
2019-05-31 15:58:34 +08:00
expect(result.locations).toBeInstanceOf(Array);
expect(result.suggestKeywords).toBeInstanceOf(Array);
});
test('empty result', () => {
const sql = 'i';
2019-09-25 15:43:36 +08:00
const result = parser.parserSql([sql, ''], dtSqlParser.parser.sqlType.Hive);
2019-05-31 15:58:34 +08:00
expect(result.locations).toBeInstanceOf(Array);
expect(result.locations).toHaveLength(0);
expect(result.suggestKeywords).toBeInstanceOf(Array);
})
})
})
describe('syntax test', () => {
describe('hive', () => {
test('no error', () => {
const sql = 'select id,name from user ';
2019-09-25 15:43:36 +08:00
const result = parser.parseSyntax([sql, ''], dtSqlParser.parser.sqlType.Hive);
2019-05-31 15:58:34 +08:00
expect(result).toBe(false);
});
test('select table should not be null', () => {
const sql = 'select id,name from ';
2019-09-25 15:43:36 +08:00
const result = parser.parseSyntax([sql, ''], dtSqlParser.parser.sqlType.Hive) as SyntaxResult;
2019-05-31 15:58:34 +08:00
expect(result.loc).toEqual({
first_line: 1,
last_line: 1,
first_column: 20,
last_column: 20
})
});
test('sql contains the wrong keyword', () => {
const sql = `create table if not 1exists ods_order_header (
order_header_id string comment 'order id'
,order_date bigint comment 'order date'
)comment 'order table'
PARTITIONED BY (ds string);`;
2019-09-25 15:43:36 +08:00
const result = parser.parseSyntax([sql, ''], dtSqlParser.parser.sqlType.Hive) as SyntaxResult;
2019-05-31 15:58:34 +08:00
expect(result.text).toBe('1exists');
expect(result.loc).toEqual({
first_line: 1,
last_line: 1,
first_column: 20,
last_column: 27
})
});
})
2019-09-25 15:43:36 +08:00
describe('flinksql', () => {
test('no error', () => {
const sql = `select id from user.id;`;
const result = flinksqlParser(sql);
expect(result).toBeNull();
});
test('empty sql', () => {
const sql = ``;
const result = flinksqlParser(sql);
expect(result).toBeNull();
});
2019-09-25 16:48:19 +08:00
test('sql comment', () => {
const sql = `-- name asf
-- type FlinkSQL
-- author admin@dtstack.com
-- create time 2019-06-12 18:11:05
-- desc `;
const result = flinksqlParser(sql);
expect(result).toBeNull();
});
2019-09-25 17:04:43 +08:00
test('position', () => {
const sql = `selec`;
const result = flinksqlParser(sql);
expect(result.token.start).toBe(0);
expect(result.token.stop).toBe(4);
});
2019-09-25 15:43:36 +08:00
test('syntax error', () => {
const sql = 'select id from user.id; \nselect id from us*er.id; \nselect id from *user.id;';
const result = flinksqlParser(sql);
expect(result).toMatchObject({
line: 2,
column: 17,
});
expect(result.token.start).toBe(42);
expect(result.token.stop).toBe(42);
})
})
2019-05-31 15:58:34 +08:00
})