feat: add ErrorStrategy(#230)
* refactor: rename errorHandler to errorListener * feat: add ErrorStrategy to mark context exceptions * test: errorStrategy unit tests
This commit is contained in:
		
							
								
								
									
										65
									
								
								test/parser/spark/errorStrategy.test.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								test/parser/spark/errorStrategy.test.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,65 @@
 | 
			
		||||
import SparkSQL from '../../../src/parser/spark';
 | 
			
		||||
import { SparkSqlSplitListener } from '../../../src/parser/spark';
 | 
			
		||||
import { SparkSqlParserListener } from '../../../src/lib/spark/SparkSqlParserListener';
 | 
			
		||||
 | 
			
		||||
const validSQL1 = `INSERT INTO country_page_view
 | 
			
		||||
VALUES ('Chinese', 'mumiao', 18),
 | 
			
		||||
    ('Amercian', 'georage', 22);`;
 | 
			
		||||
const validSQL2 = 'SELECT * FROM tb;';
 | 
			
		||||
const inValidSQL = 'CREATE TABLE;';
 | 
			
		||||
 | 
			
		||||
describe('SparkSQL ErrorStrategy test', () => {
 | 
			
		||||
    const sparkSQL = new SparkSQL();
 | 
			
		||||
 | 
			
		||||
    // TODO: handle unexpected case
 | 
			
		||||
    // test('begin inValid', () => {
 | 
			
		||||
    //     const sql = [inValidSQL, validSQL1, validSQL2].join('\n');
 | 
			
		||||
    //     // parse with empty errorListener
 | 
			
		||||
    //     const parseTree = sparkSQL.parse(sql, () => {});
 | 
			
		||||
    //     const splitListener = new SparkSqlSplitListener();
 | 
			
		||||
    //     sparkSQL.listen(splitListener as SparkSqlParserListener, parseTree);
 | 
			
		||||
 | 
			
		||||
    //     const statementCount = splitListener.statementsContext.length;
 | 
			
		||||
    //     splitListener.statementsContext.map((item, index) => {
 | 
			
		||||
    //         if(index !== statementCount-1 && index !== statementCount - 2) {
 | 
			
		||||
    //             expect(item.exception).not.toBe(undefined);
 | 
			
		||||
    //         } else {
 | 
			
		||||
    //             expect(item.exception).toBe(undefined);
 | 
			
		||||
    //         }
 | 
			
		||||
    //     })
 | 
			
		||||
    // });
 | 
			
		||||
 | 
			
		||||
    // TODO: handle unexpected case
 | 
			
		||||
    // test('middle inValid', () => {
 | 
			
		||||
    //     const sql = [validSQL1, inValidSQL, validSQL2].join('\n');
 | 
			
		||||
    //     // parse with empty errorListener
 | 
			
		||||
    //     const parseTree = sparkSQL.parse(sql, () => {});
 | 
			
		||||
    //     const splitListener = new SparkSqlSplitListener();
 | 
			
		||||
    //     sparkSQL.listen(splitListener as SparkSqlParserListener, parseTree);
 | 
			
		||||
 | 
			
		||||
    //     const statementCount = splitListener.statementsContext.length;
 | 
			
		||||
    //     splitListener.statementsContext.map((item, index) => {
 | 
			
		||||
    //         if(index !== statementCount-1 && index !== 0) {
 | 
			
		||||
    //             expect(item.exception).not.toBe(undefined);
 | 
			
		||||
    //         } else {
 | 
			
		||||
    //             expect(item.exception).toBe(undefined);
 | 
			
		||||
    //         }
 | 
			
		||||
    //     })
 | 
			
		||||
    // });
 | 
			
		||||
 | 
			
		||||
    test('end inValid', () => {
 | 
			
		||||
        const sql = [validSQL1, validSQL2, inValidSQL].join('\n');
 | 
			
		||||
        // parse with empty errorListener
 | 
			
		||||
        const parseTree = sparkSQL.parse(sql, () => {});
 | 
			
		||||
        const splitListener = new SparkSqlSplitListener();
 | 
			
		||||
        sparkSQL.listen(splitListener as SparkSqlParserListener, parseTree);
 | 
			
		||||
 | 
			
		||||
        splitListener.statementsContext.map((item, index) => {
 | 
			
		||||
            if (index !== 0 && index !== 1) {
 | 
			
		||||
                expect(item.exception).not.toBe(undefined);
 | 
			
		||||
            } else {
 | 
			
		||||
                expect(item.exception).toBe(undefined);
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
    });
 | 
			
		||||
});
 | 
			
		||||
		Reference in New Issue
	
	Block a user