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 25, 2024
1 parent a94e7fc commit f6b1a3d
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
116 changes: 116 additions & 0 deletions test/parser/spark/suggestion/completeAfterSyntaxError.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
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 Error Statement', () => {
const spark = new SparkSQL();

const keywordResult = [
'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 and in multiline', () => {
const pos: CaretPosition = {
lineNumber: 3,
column: 2,
};
const keywords = spark.getSuggestionAtCaretPosition(syntaxSql, pos)?.keywords;
expect(keywords).toMatchUnorderedArrary(keywordResult);
});

test('Syntax error but end with semi and in single line', () => {
const pos: CaretPosition = {
lineNumber: 5,
column: 20,
};
const keywords = spark.getSuggestionAtCaretPosition(syntaxSql, pos)?.keywords;
expect(keywords).toMatchUnorderedArrary(keywordResult);
});

test('Syntax error but start with keyword and in multiline', () => {
const pos: CaretPosition = {
lineNumber: 10,
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 and in single line', () => {
const pos: CaretPosition = {
lineNumber: 12,
column: 30,
};
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');
});
});
12 changes: 12 additions & 0 deletions test/parser/spark/suggestion/fixtures/completeAfterSyntaxError.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- error syntax but end with semi, should suggestion keywords which can start a single statement
SELECT FROM tb1;
I

SELECT FROM tb1; I


-- error syntax but start with keyword, should suggestion token at caret position
SELECT FROM tb2
INSERT INTO

SELECT FROM tb2 INSERT INTO

0 comments on commit f6b1a3d

Please sign in to comment.