|
| 1 | +import { Sql, SqlSplitListener } from 'src/parser/sql'; |
| 2 | +import { SqlParserListener } from 'src/lib/sql/SqlParserListener'; |
| 3 | + |
| 4 | +const validSQL1 = `INSERT INTO country_page_view |
| 5 | +VALUES ('Chinese', 'mumiao', 18), |
| 6 | + ('Amercian', 'georage', 22);`; |
| 7 | +const validSQL2 = 'SELECT * FROM tb;'; |
| 8 | +const inValidSQL = 'CREATE TABLE;'; |
| 9 | + |
| 10 | +describe('Sql ErrorStrategy test', () => { |
| 11 | + const sql = new Sql(); |
| 12 | + |
| 13 | + // TODO: handle unexpected case |
| 14 | + // test('begin inValid', () => { |
| 15 | + // const sqlText = [inValidSQL, validSQL1, validSQL2].join('\n'); |
| 16 | + // // parse with empty errorListener |
| 17 | + // const parseTree = sql.parse(sqlText, () => {}); |
| 18 | + // const splitListener = new SqlSplitListener(); |
| 19 | + // sql.listen(splitListener as SqlParserListener, parseTree); |
| 20 | + |
| 21 | + // const statementCount = splitListener.statementsContext.length; |
| 22 | + // splitListener.statementsContext.map((item, index) => { |
| 23 | + // if (index !== statementCount - 1 && index !== statementCount - 2) { |
| 24 | + // expect(item.exception).not.toBe(null); |
| 25 | + // } else { |
| 26 | + // expect(item.exception).toBe(null); |
| 27 | + // } |
| 28 | + // }); |
| 29 | + // }); |
| 30 | + |
| 31 | + // TODO: handle unexpected case |
| 32 | + // test('middle inValid', () => { |
| 33 | + // const sqlText = [validSQL1, inValidSQL, validSQL2].join('\n'); |
| 34 | + // // parse with empty errorListener |
| 35 | + // const parseTree = sql.parse(sqlText, () => {}); |
| 36 | + // const splitListener = new SqlSplitListener(); |
| 37 | + // sql.listen(splitListener as SqlParserListener, parseTree); |
| 38 | + |
| 39 | + // const statementCount = splitListener.statementsContext.length; |
| 40 | + // splitListener.statementsContext.map((item, index) => { |
| 41 | + // if (index !== statementCount - 1 && index !== 0) { |
| 42 | + // expect(item.exception).not.toBe(null); |
| 43 | + // } else { |
| 44 | + // expect(item.exception).toBe(null); |
| 45 | + // } |
| 46 | + // }); |
| 47 | + // }); |
| 48 | + |
| 49 | + test('end inValid', () => { |
| 50 | + const sqlText = [validSQL1, validSQL2, inValidSQL].join('\n'); |
| 51 | + // parse with empty errorListener |
| 52 | + const parseTree = sql.parse(sqlText, () => {}); |
| 53 | + const splitListener = new SqlSplitListener(); |
| 54 | + sql.listen(splitListener as SqlParserListener, parseTree); |
| 55 | + |
| 56 | + splitListener.statementsContext.map((item, index) => { |
| 57 | + if (index !== 0 && index !== 1) { |
| 58 | + expect(item.exception).not.toBe(null); |
| 59 | + } else { |
| 60 | + expect(item.exception).toBe(null); |
| 61 | + } |
| 62 | + }); |
| 63 | + }); |
| 64 | +}); |
0 commit comments