test: add hivesql unit test
This commit is contained in:
parent
2aab4b6ca7
commit
cb1fcb7ed6
@ -2,11 +2,15 @@ import SQLParser from '../../../src/parser/hive';
|
|||||||
|
|
||||||
describe('HiveSQL Lexer tests', () => {
|
describe('HiveSQL Lexer tests', () => {
|
||||||
const parser = new SQLParser();
|
const parser = new SQLParser();
|
||||||
// select id,name,sex from user1;
|
test('select token counts', () => {
|
||||||
const sql = 'SELECT * FROM t1';
|
const sql = 'SELECT * FROM t1';
|
||||||
const tokens = parser.getAllTokens(sql);
|
const tokens = parser.getAllTokens(sql);
|
||||||
|
expect(tokens.length).toBe(4);
|
||||||
|
});
|
||||||
|
|
||||||
test('token counts', () => {
|
test('select token counts', () => {
|
||||||
expect(tokens.length).toBe(12);
|
const sql = 'show create table_name;';
|
||||||
|
const tokens = parser.getAllTokens(sql);
|
||||||
|
expect(tokens.length).toBe(4);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -1,17 +1,16 @@
|
|||||||
import SQLParser, { HiveSqlListener } from '../../../src/parser/hive';
|
import SQLParser, { HiveSqlListener } from '../../../src/parser/hive';
|
||||||
|
|
||||||
describe('Hive SQL Listener Tests', () => {
|
describe('Hive SQL Listener Tests', () => {
|
||||||
const expectTableName = 'user1';
|
|
||||||
const sql = `select id,name,sex from ${expectTableName};`;
|
|
||||||
const parser = new SQLParser();
|
const parser = new SQLParser();
|
||||||
|
test('Listener enterSelectList', async () => {
|
||||||
|
const expectTableName = 'userName';
|
||||||
|
const sql = `select ${expectTableName} from user1 where inc_day='20190601' limit 1000;`;
|
||||||
const parserTree = parser.parse(sql);
|
const parserTree = parser.parse(sql);
|
||||||
|
|
||||||
test('Listener enterTableName', async () => {
|
|
||||||
let result = '';
|
let result = '';
|
||||||
class MyListener extends HiveSqlListener {
|
class MyListener extends HiveSqlListener {
|
||||||
enterTableName(ctx): void {
|
enterSelect_list(ctx): void {
|
||||||
result = ctx.getText().toLowerCase();
|
result = ctx.getText();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const listenTableName: any = new MyListener();
|
const listenTableName: any = new MyListener();
|
||||||
@ -19,4 +18,18 @@ describe('Hive SQL Listener Tests', () => {
|
|||||||
await parser.listen(listenTableName, parserTree);
|
await parser.listen(listenTableName, parserTree);
|
||||||
expect(result).toBe(expectTableName);
|
expect(result).toBe(expectTableName);
|
||||||
});
|
});
|
||||||
|
test('Listener enterCreateTable', async () => {
|
||||||
|
const sql = `drop table table_name;`;
|
||||||
|
const parserTree = parser.parse(sql);
|
||||||
|
let result = '';
|
||||||
|
class MyListener extends HiveSqlListener {
|
||||||
|
enterDrop_stmt(ctx): void {
|
||||||
|
result = ctx.getText();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const listenTableName: any = new MyListener();
|
||||||
|
|
||||||
|
await parser.listen(listenTableName, parserTree);
|
||||||
|
expect(result).toBe('droptabletable_name');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -7,10 +7,14 @@ describe('Hive SQL Syntax Tests', () => {
|
|||||||
const result = parser.validate(sql);
|
const result = parser.validate(sql);
|
||||||
expect(result.length).toBe(0);
|
expect(result.length).toBe(0);
|
||||||
});
|
});
|
||||||
|
test('Create Table Statement', () => {
|
||||||
|
const sql = `alter table dm_gis.table_name add if not exists partition (inc_day = '20190601');`;
|
||||||
|
const result = parser.validate(sql);
|
||||||
|
expect(result.length).toBe(0);
|
||||||
|
});
|
||||||
test('Wrong Select Statement', () => {
|
test('Wrong Select Statement', () => {
|
||||||
const sql = 'SELECT add ABC from Where ;'
|
const sql = 'SELECT add ABC from Where ;'
|
||||||
const result = parser.validate(sql);
|
const result = parser.validate(sql);
|
||||||
console.log(result)
|
|
||||||
expect(result.length).toBe(2);
|
expect(result.length).toBe(2);
|
||||||
expect(result[0].message).toBe(`no viable alternative at input 'SELECTaddABCfromWhere'`)
|
expect(result[0].message).toBe(`no viable alternative at input 'SELECTaddABCfromWhere'`)
|
||||||
expect(result[1].message).toBe(`mismatched input 'Where' expecting <EOF>`)
|
expect(result[1].message).toBe(`mismatched input 'Where' expecting <EOF>`)
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
import SQLParser, { HiveSqlVisitor } from '../../../src/parser/hive';
|
import SQLParser, { HiveSqlVisitor } from '../../../src/parser/hive';
|
||||||
|
|
||||||
describe('Generic SQL Visitor Tests', () => {
|
describe('Generic SQL Visitor Tests', () => {
|
||||||
const expectTableName = 'user1';
|
const expectTableName = 'dm_gis.dlv_addr_tc_count';
|
||||||
const sql = `select id,name,sex from ${expectTableName};`;
|
const sql = `select citycode,tc,inc_day from ${expectTableName} where inc_day='20190501' limit 100;`;
|
||||||
const parser = new SQLParser();
|
const parser = new SQLParser();
|
||||||
|
|
||||||
const parserTree = parser.parse(sql, (error) => {
|
const parserTree = parser.parse(sql, (error) => {
|
||||||
console.log('Parse error:', error);
|
console.log('Parse error:', error);
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log('Parser tree string:', parser.toString(parserTree));
|
// console.log('Parser tree string:', parser.toString(parserTree));
|
||||||
|
|
||||||
test('Visitor visitTableName', () => {
|
test('Visitor visitTableName', () => {
|
||||||
let result = '';
|
let result = '';
|
||||||
@ -19,6 +19,7 @@ describe('Generic SQL Visitor Tests', () => {
|
|||||||
super.visitTable_name(ctx);
|
super.visitTable_name(ctx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const visitor: any = new MyVisitor();
|
const visitor: any = new MyVisitor();
|
||||||
visitor.visit(parserTree);
|
visitor.visit(parserTree);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user