From cb1fcb7ed6f659c9e98d98c2270c16ee4b6b4544 Mon Sep 17 00:00:00 2001 From: chenfeng Date: Wed, 25 Nov 2020 17:04:55 +0800 Subject: [PATCH] test: add hivesql unit test --- test/parser/hive/lexer.test.ts | 14 +++++++++----- test/parser/hive/listener.test.ts | 27 ++++++++++++++++++++------- test/parser/hive/syntax.test.ts | 6 +++++- test/parser/hive/visitor.test.ts | 7 ++++--- 4 files changed, 38 insertions(+), 16 deletions(-) diff --git a/test/parser/hive/lexer.test.ts b/test/parser/hive/lexer.test.ts index 423bdb9..78a002d 100644 --- a/test/parser/hive/lexer.test.ts +++ b/test/parser/hive/lexer.test.ts @@ -2,11 +2,15 @@ import SQLParser from '../../../src/parser/hive'; describe('HiveSQL Lexer tests', () => { const parser = new SQLParser(); - // select id,name,sex from user1; - const sql = 'SELECT * FROM t1'; - const tokens = parser.getAllTokens(sql); + test('select token counts', () => { + const sql = 'SELECT * FROM t1'; + const tokens = parser.getAllTokens(sql); + expect(tokens.length).toBe(4); + }); - test('token counts', () => { - expect(tokens.length).toBe(12); + test('select token counts', () => { + const sql = 'show create table_name;'; + const tokens = parser.getAllTokens(sql); + expect(tokens.length).toBe(4); }); }); diff --git a/test/parser/hive/listener.test.ts b/test/parser/hive/listener.test.ts index 23ff3e0..ac92674 100644 --- a/test/parser/hive/listener.test.ts +++ b/test/parser/hive/listener.test.ts @@ -1,17 +1,16 @@ import SQLParser, { HiveSqlListener } from '../../../src/parser/hive'; describe('Hive SQL Listener Tests', () => { - const expectTableName = 'user1'; - const sql = `select id,name,sex from ${expectTableName};`; 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 = ''; class MyListener extends HiveSqlListener { - enterTableName(ctx): void { - result = ctx.getText().toLowerCase(); + enterSelect_list(ctx): void { + result = ctx.getText(); } } const listenTableName: any = new MyListener(); @@ -19,4 +18,18 @@ describe('Hive SQL Listener Tests', () => { await parser.listen(listenTableName, parserTree); 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'); + }); }); diff --git a/test/parser/hive/syntax.test.ts b/test/parser/hive/syntax.test.ts index 2ef6f36..3df530b 100644 --- a/test/parser/hive/syntax.test.ts +++ b/test/parser/hive/syntax.test.ts @@ -7,10 +7,14 @@ describe('Hive SQL Syntax Tests', () => { const result = parser.validate(sql); 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', () => { const sql = 'SELECT add ABC from Where ;' const result = parser.validate(sql); - console.log(result) expect(result.length).toBe(2); expect(result[0].message).toBe(`no viable alternative at input 'SELECTaddABCfromWhere'`) expect(result[1].message).toBe(`mismatched input 'Where' expecting `) diff --git a/test/parser/hive/visitor.test.ts b/test/parser/hive/visitor.test.ts index f4d2cec..57ecf9b 100644 --- a/test/parser/hive/visitor.test.ts +++ b/test/parser/hive/visitor.test.ts @@ -1,15 +1,15 @@ import SQLParser, { HiveSqlVisitor } from '../../../src/parser/hive'; describe('Generic SQL Visitor Tests', () => { - const expectTableName = 'user1'; - const sql = `select id,name,sex from ${expectTableName};`; + const expectTableName = 'dm_gis.dlv_addr_tc_count'; + const sql = `select citycode,tc,inc_day from ${expectTableName} where inc_day='20190501' limit 100;`; const parser = new SQLParser(); const parserTree = parser.parse(sql, (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', () => { let result = ''; @@ -19,6 +19,7 @@ describe('Generic SQL Visitor Tests', () => { super.visitTable_name(ctx); } } + const visitor: any = new MyVisitor(); visitor.visit(parserTree);