Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add CommonSuggestion and JoinsSuggestion types, migrate postgresql tests to typescript #105

Merged
merged 7 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/autocomplete/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ export interface ParseResult {
suggestColumnAliases?: ColumnAliasSuggestion[];
suggestCommonTableExpressions?: unknown;
suggestDatabases?: DatabasesSuggestion;
suggestFilters?: FiltersSuggestion;
suggestFilters?: CommonSuggestion;
suggestFunctions?: FunctionsSuggestion;
suggestValues?: ValuesSuggestion;
suggestGroupBys?: unknown;
suggestGroupBys?: CommonSuggestion;
suggestOrderBys?: CommonSuggestion;
suggestJoins?: JoinsSuggestion;
suggestIdentifiers?: IdentifierSuggestion[];
suggestTemplates?: boolean;
suggestEngines?: EnginesSuggestion;
Expand Down Expand Up @@ -116,11 +118,16 @@ export interface FunctionsSuggestion {
types?: string[];
}

export interface FiltersSuggestion {
export interface CommonSuggestion {
prefix?: string;
tables: Table[];
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Common suggestion looks too general. Why not just create a FiltersSuggestion, and make GroupBysSuggestion, OrderBysSuggestion aliases?

Or else we need a better naming


export interface JoinsSuggestion {
prependJoin?: boolean;
tables: Table[];
}

export interface ValuesSuggestion {
missingEndQuote?: boolean;
partialQuote?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {expect, test} from '@jest/globals';
import {
ColumnReference,
ColumnSuggestion,
CommonSuggestion,
DatabasesSuggestion,
FiltersSuggestion,
FunctionsSuggestion,
KeywordSuggestion,
ParserSyntaxError,
Expand Down Expand Up @@ -186,7 +186,7 @@ test('should suggest columns, functions, filters, keywords after WHERE', () => {
const functionsSuggestion: FunctionsSuggestion = {};
expect(parseResult.suggestFunctions).toEqual(functionsSuggestion);

const filtersSuggestion: FiltersSuggestion = {
const filtersSuggestion: CommonSuggestion = {
tables: [
{
identifierChain: [
Expand Down Expand Up @@ -286,7 +286,7 @@ test('should suggest columns, functions, filters after AND', () => {
const functionsSuggestion: FunctionsSuggestion = {};
expect(parseResult.suggestFunctions).toEqual(functionsSuggestion);

const filtersSuggestion: FiltersSuggestion = {
const filtersSuggestion: CommonSuggestion = {
tables: [
{
identifierChain: [
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {expect, test} from '@jest/globals';

import {KeywordSuggestion, ParserSyntaxError, parsePostgreSql} from '../../../../index';

test('should suggest values', () => {
const parseResult = parsePostgreSql(
'SELECT COUNT(*) AS test_count FROM test_table GROUP BY test_count LIMIT ',
'',
);

expect(parseResult.errors).toBeUndefined();

const suggestions: KeywordSuggestion[] = [
{value: '10', weight: 10000},
{value: '100', weight: 10000},
{value: '1000', weight: 10000},
{value: '10000', weight: 10000},
{value: '5000', weight: 10000},
];
expect(parseResult.suggestKeywords).toEqual(suggestions);
});

test('should contain LIMIT in suggestions', () => {
const parseResult = parsePostgreSql(
'SELECT COUNT(*) AS test_count FROM test_table GROUP BY test_count OFFSET 10 ',
'',
);

expect(parseResult.errors).toBeUndefined();

const suggestion: KeywordSuggestion = {value: 'LIMIT', weight: 2.2};
expect(parseResult.suggestKeywords).toContainEqual(suggestion);
});

test('should not allow to include offset after comma', () => {
const parseResult = parsePostgreSql(
'SELECT COUNT(*) AS test_count FROM test_table GROUP BY test_count LIMIT 100, 100 ',
'',
);

const error: Partial<ParserSyntaxError> = {
text: ',',
token: ',',
line: 0,
loc: {
first_line: 1,
last_line: 1,
first_column: 75,
last_column: 76,
},
};
expect(parseResult.errors).toContainEqual(expect.objectContaining(error));
});

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {expect, test} from '@jest/globals';

import {CommonSuggestion, KeywordSuggestion, parsePostgreSql} from '../../../../index';

test('should suggest OFFSET', () => {
const parseResult = parsePostgreSql('SELECT * FROM test_table LIMIT 100 ', '');

expect(parseResult.errors).toBeUndefined();

const suggestion: KeywordSuggestion = {value: 'OFFSET', weight: 2.2};
expect(parseResult.suggestKeywords).toContainEqual(suggestion);
});

test('should not throw errors with OFFSET statement', () => {
const parseResult = parsePostgreSql('SELECT * FROM test_table OFFSET 100;', '');

expect(parseResult.errors).toBeUndefined();
});

test('should not throw errors with LIMIT OFFSET statement', () => {
const parseResult = parsePostgreSql('SELECT * FROM test_table LIMIT 1 OFFSET 12;', '');

expect(parseResult.errors).toBeUndefined();
});

test('should not throw errors with OFFSET LIMIT statement', () => {
const parseResult = parsePostgreSql('SELECT * FROM test_table OFFSET 12 LIMIT 1;', '');

expect(parseResult.errors).toBeUndefined();
});

test('should not throw errors with LIMIT statement', () => {
const parseResult = parsePostgreSql('SELECT * FROM test_table LIMIT 1;', '');

expect(parseResult.errors).toBeUndefined();
});

test('should suggest OFFSET, LIMIT, GROUP BY, ORDER BY', () => {
const parseResult = parsePostgreSql('SELECT * FROM test_table WHERE test_column = 1 ', '');

expect(parseResult.errors).toBeUndefined();

const offsetSuggestion: KeywordSuggestion = {value: 'OFFSET', weight: 2.2};
expect(parseResult.suggestKeywords).toContainEqual(offsetSuggestion);

const limitSuggestion: KeywordSuggestion = {value: 'LIMIT', weight: 2.3};
expect(parseResult.suggestKeywords).toContainEqual(limitSuggestion);

const groupBysSuggestion: CommonSuggestion = {
prefix: 'GROUP BY',
tables: [{identifierChain: [{name: 'test_table'}]}],
};
expect(parseResult.suggestGroupBys).toEqual(groupBysSuggestion);

const orderBysSuggestion: CommonSuggestion = {
prefix: 'ORDER BY',
tables: [{identifierChain: [{name: 'test_table'}]}],
};
expect(parseResult.suggestOrderBys).toEqual(orderBysSuggestion);
});
Loading
Loading