2023-12-13 11:33:47 +08:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
2024-01-19 21:10:00 +08:00
|
|
|
import PostgresSQL from 'src/parser/pgsql';
|
|
|
|
import { CaretPosition, SyntaxContextType } from 'src/parser/common/basic-parser-types';
|
2023-12-13 11:33:47 +08:00
|
|
|
|
|
|
|
const syntaxSql = fs.readFileSync(
|
|
|
|
path.join(__dirname, 'fixtures', 'multipleStatement.sql'),
|
|
|
|
'utf-8'
|
|
|
|
);
|
|
|
|
|
|
|
|
describe('PgSQL Multiple Statements Syntax Suggestion', () => {
|
2023-12-19 19:22:27 +08:00
|
|
|
const parser = new PostgresSQL();
|
2023-12-13 11:33:47 +08:00
|
|
|
|
|
|
|
test('Create table ', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 1,
|
|
|
|
column: 14,
|
|
|
|
};
|
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax;
|
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE_CREATE
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
|
|
|
|
});
|
|
|
|
|
2024-02-05 16:24:14 +08:00
|
|
|
test('Select from table or view', () => {
|
2023-12-13 11:33:47 +08:00
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 9,
|
|
|
|
column: 18,
|
|
|
|
};
|
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax;
|
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE
|
|
|
|
);
|
2024-02-05 16:24:14 +08:00
|
|
|
const suggestionVw = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.VIEW
|
|
|
|
);
|
2023-12-13 11:33:47 +08:00
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.']);
|
2024-02-05 16:24:14 +08:00
|
|
|
expect(suggestionVw).not.toBeUndefined();
|
|
|
|
expect(suggestionVw?.wordRanges.map((token) => token.text)).toEqual(['db', '.']);
|
2023-12-13 11:33:47 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Insert into table ', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 15,
|
|
|
|
column: 13,
|
|
|
|
};
|
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax;
|
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Insert into select from table ', () => {
|
|
|
|
const pos: CaretPosition = {
|
|
|
|
lineNumber: 21,
|
|
|
|
column: 65,
|
|
|
|
};
|
|
|
|
const syntaxes = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax;
|
|
|
|
const suggestion = syntaxes?.find(
|
|
|
|
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(suggestion).not.toBeUndefined();
|
|
|
|
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.']);
|
|
|
|
});
|
|
|
|
});
|