Skip to content

Commit

Permalink
feat: add CommonSuggestion and JoinsSuggestion types, migrate postgre…
Browse files Browse the repository at this point in the history
…sql tests to typescript (#105)

* refactor: add CommonSuggestion and JoinsSuggestion types

* refactor: migrate limit_clause tests to typescript

* refactor: migrate offset_clause tests to typescript

* refactor: migrate order_by_clause tests to typescript

* refactor: migrate select_conditions tests to typescript

* refactor: migrate postgresql specific select tests to typescript

* refactor: rename CommonSuggestion

---------

Co-authored-by: robhovsepyan <[email protected]>
  • Loading branch information
roberthovsepyan and robhovsepyan authored Dec 5, 2023
1 parent 73bd7b8 commit 36b291c
Show file tree
Hide file tree
Showing 10 changed files with 795 additions and 383 deletions.
13 changes: 12 additions & 1 deletion src/autocomplete/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export interface ParseResult {
suggestFilters?: FiltersSuggestion;
suggestFunctions?: FunctionsSuggestion;
suggestValues?: ValuesSuggestion;
suggestGroupBys?: unknown;
suggestGroupBys?: GroupBysSuggestion;
suggestOrderBys?: OrderBysSuggestion;
suggestJoins?: JoinsSuggestion;
suggestIdentifiers?: IdentifierSuggestion[];
suggestTemplates?: boolean;
suggestEngines?: EnginesSuggestion;
Expand Down Expand Up @@ -121,6 +123,15 @@ export interface FiltersSuggestion {
tables: Table[];
}

export type GroupBysSuggestion = FiltersSuggestion;

export type OrderBysSuggestion = FiltersSuggestion;

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

export interface ValuesSuggestion {
missingEndQuote?: boolean;
partialQuote?: boolean;
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,65 @@
import {expect, test} from '@jest/globals';

import {
GroupBysSuggestion,
KeywordSuggestion,
OrderBysSuggestion,
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: GroupBysSuggestion = {
prefix: 'GROUP BY',
tables: [{identifierChain: [{name: 'test_table'}]}],
};
expect(parseResult.suggestGroupBys).toEqual(groupBysSuggestion);

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

0 comments on commit 36b291c

Please sign in to comment.