|
| 1 | +import { ImpalaSQL } from 'src/parser/impala'; |
| 2 | +import { CaretPosition, EntityContextType } from 'src/parser/common/types'; |
| 3 | + |
| 4 | +describe('ImpalaSQL Complete After Syntax Error', () => { |
| 5 | + const impala = new ImpalaSQL(); |
| 6 | + |
| 7 | + const sql1 = `SELECT FROM tb2;\nINSERT INTO `; |
| 8 | + const sql2 = `SELECT FROM tb3;\nCREATE TABLE `; |
| 9 | + const sql3 = `SELECT FROM t1;\nSL`; |
| 10 | + |
| 11 | + test('Syntax error but end with semi, should suggest tableName', () => { |
| 12 | + const pos: CaretPosition = { |
| 13 | + lineNumber: 2, |
| 14 | + column: 13, |
| 15 | + }; |
| 16 | + const suggestion = impala.getSuggestionAtCaretPosition(sql1, pos); |
| 17 | + expect(suggestion).not.toBeUndefined(); |
| 18 | + |
| 19 | + // syntax |
| 20 | + const syntaxes = suggestion?.syntax; |
| 21 | + expect(syntaxes.length).toBe(1); |
| 22 | + expect(syntaxes[0].syntaxContextType).toBe(EntityContextType.TABLE); |
| 23 | + |
| 24 | + // keyword |
| 25 | + const keywords = suggestion?.keywords; |
| 26 | + expect(keywords.length).toBe(1); |
| 27 | + expect(keywords[0]).toBe('TABLE'); |
| 28 | + }); |
| 29 | + |
| 30 | + test('Syntax error but end with semi, should suggest tableNameCreate', () => { |
| 31 | + const pos: CaretPosition = { |
| 32 | + lineNumber: 2, |
| 33 | + column: 14, |
| 34 | + }; |
| 35 | + const suggestion = impala.getSuggestionAtCaretPosition(sql2, pos); |
| 36 | + expect(suggestion).not.toBeUndefined(); |
| 37 | + |
| 38 | + // syntax |
| 39 | + const syntaxes = suggestion?.syntax; |
| 40 | + expect(syntaxes.length).toBe(1); |
| 41 | + expect(syntaxes[0].syntaxContextType).toBe(EntityContextType.TABLE_CREATE); |
| 42 | + |
| 43 | + // keyword |
| 44 | + const keywords = suggestion?.keywords; |
| 45 | + expect(keywords).toMatchUnorderedArray(['IF', 'IF NOT EXISTS']); |
| 46 | + }); |
| 47 | + |
| 48 | + test('Syntax error but end with semi, should suggest filter token', () => { |
| 49 | + const pos: CaretPosition = { |
| 50 | + lineNumber: 2, |
| 51 | + column: 2, |
| 52 | + }; |
| 53 | + const suggestion = impala.getSuggestionAtCaretPosition(sql3, pos); |
| 54 | + expect(suggestion).not.toBeUndefined(); |
| 55 | + |
| 56 | + // syntax |
| 57 | + const syntaxes = suggestion?.syntax; |
| 58 | + expect(syntaxes.length).toBe(0); |
| 59 | + |
| 60 | + // keyword |
| 61 | + const filterKeywords = suggestion?.keywords?.filter( |
| 62 | + (item) => item.startsWith('S') && /S(?=.*L)/.test(item) |
| 63 | + ); |
| 64 | + expect(filterKeywords).toMatchUnorderedArray(['SELECT']); |
| 65 | + }); |
| 66 | +}); |
0 commit comments