Skip to content

Commit

Permalink
test: complete after error syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxy0551 committed Jul 30, 2024
1 parent a94e7fc commit 56e4d0d
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
106 changes: 106 additions & 0 deletions test/parser/spark/suggestion/completeAfterSyntaxError.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import fs from 'fs';
import path from 'path';
import { SparkSQL } from 'src/parser/spark';
import { CaretPosition, EntityContextType } from 'src/parser/common/types';

const syntaxSql = fs.readFileSync(
path.join(__dirname, 'fixtures', 'completeAfterSyntaxError.sql'),
'utf-8'
);

describe('SparkSQL Complete After Syntax Error', () => {
const spark = new SparkSQL();

test('Syntax error but end with semi, should suggest keywords which can start a single statement', () => {
const sql1 = `SELECT FROM tb1;\nI`;
const pos: CaretPosition = {
lineNumber: 2,
column: 2,
};
const keywords = spark.getSuggestionAtCaretPosition(sql1, pos)?.keywords;
expect(keywords).toMatchUnorderedArrary([
'WITH',
'SELECT',
'MAP',
'REDUCE',
'FROM',
'TABLE',
'VALUES',
'INSERT',
'DELETE',
'UPDATE',
'MERGE',
'USE',
'SET',
'CREATE',
'ALTER',
'DROP',
'SHOW',
'REPLACE',
'ANALYZE',
'DECLARE',
'EXPLAIN',
'DESC',
'DESCRIBE',
'COMMENT',
'REFRESH',
'CACHE',
'UNCACHE',
'CLEAR',
'LOAD',
'TRUNCATE',
'MSCK',
'REPAIR',
'ADD',
'LIST',
'RESET',
'OPTIMIZE',
'GRANT',
'REVOKE',
'EXPORT',
'IMPORT',
'LOCK',
'UNLOCK',
'START',
'COMMIT',
'ROLLBACK',
'DFS',
]);
});

test('Syntax error but end with semi, should suggest token', () => {
const pos: CaretPosition = {
lineNumber: 3,
column: 13,
};
const suggestion = spark.getSuggestionAtCaretPosition(syntaxSql, pos);
expect(suggestion).not.toBeUndefined();

// syntax
const syntaxes = suggestion?.syntax;
expect(syntaxes.length).toBe(1);
expect(syntaxes[0].syntaxContextType).toBe(EntityContextType.TABLE);

const keywords = suggestion?.keywords;
expect(keywords.length).toBe(1);
expect(keywords[0]).toBe('TABLE');
});

test('Syntax error but start with keyword, should suggest token', () => {
const pos: CaretPosition = {
lineNumber: 9,
column: 13,
};
const suggestion = spark.getSuggestionAtCaretPosition(syntaxSql, pos);
expect(suggestion).not.toBeUndefined();

// syntax
const syntaxes = suggestion?.syntax;
expect(syntaxes.length).toBe(1);
expect(syntaxes[0].syntaxContextType).toBe(EntityContextType.TABLE);

const keywords = suggestion?.keywords;
expect(keywords.length).toBe(1);
expect(keywords[0]).toBe('TABLE');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- error syntax but end with semi
SELECT FROM tb2;
INSERT INTO



-- error syntax but start with keyword
SELECT FROM tb3
INSERT INTO

0 comments on commit 56e4d0d

Please sign in to comment.