Skip to content

Commit

Permalink
feat(mysql): add mysql expression column unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
LuckyFBB committed Oct 16, 2024
1 parent ca4d102 commit b02894b
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 1 deletion.
17 changes: 17 additions & 0 deletions test/parser/mysql/errorListener.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const sql1 = `SHOW CREATE TABLE`;
const sql2 = `CREATE DATABASE `;
const sql3 = `SHOW CREATE DATABASE IF NOT EXIsST aaa aaa`;
const sql4 = `SELECT * froma aaa`;
const sql5 = `SELECT user, MAX(salary) FROM users where `;

describe('MySQL validate invalid sql and test msg', () => {
const mysql = new MySQL();
Expand Down Expand Up @@ -46,6 +47,14 @@ describe('MySQL validate invalid sql and test msg', () => {
);
});

test('validate unComplete sql5', () => {
const errors = mysql.validate(sql5);
expect(errors.length).toBe(1);
expect(errors[0].message).toBe(
`Statement is incomplete, expecting an existing function or an existing column or a keyword`
);
});

test('validate random text cn', () => {
mysql.locale = 'zh_CN';
const errors = mysql.validate(randomText);
Expand Down Expand Up @@ -77,4 +86,12 @@ describe('MySQL validate invalid sql and test msg', () => {
expect(errors.length).toBe(1);
expect(errors[0].message).toBe(`'froma' 在此位置无效,期望一个存在的column或者一个关键字`);
});

test('validate unComplete sql5 cn', () => {
const errors = mysql.validate(sql5);
expect(errors.length).toBe(1);
expect(errors[0].message).toBe(
`语句不完整,期望一个存在的function或者一个存在的column或者一个关键字`
);
});
});
8 changes: 7 additions & 1 deletion test/parser/mysql/suggestion/fixtures/syntaxSuggestion.sql
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,10 @@ SHOW CREATE TABLE tbl_name;

SHOW CREATE DATABASE IF NOT EXISTS db_name;

SHOW CREATE VIEW test.v;
SHOW CREATE VIEW test.v;

SELECT user, MAX(salary) FROM users where age = 10 GROUP BY length(user) HAVING MAX(salary) > 10;

SELECT c.category_id FROM category c JOIN product p ON c.category_id = p.category_id;

SELECT score, CASE WHEN score >= 90 THEN 'A' ELSE 'F' END AS grade FROM students;
106 changes: 106 additions & 0 deletions test/parser/mysql/suggestion/syntaxSuggestion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,4 +508,110 @@ describe('MySQL Syntax Suggestion', () => {
expect(suggestion).not.toBeUndefined();
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['test', '.', 'v']);
});

test('Select expression column', () => {
const pos: CaretPosition = {
lineNumber: 59,
column: 24,
};
const syntaxes = mysql.getSuggestionAtCaretPosition(
commentOtherLine(syntaxSql, pos.lineNumber),
pos
)?.syntax;
const suggestion = syntaxes?.find(
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
);

expect(suggestion).not.toBeUndefined();
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['salary']);
});

test('Group by expression column', () => {
const pos: CaretPosition = {
lineNumber: 59,
column: 72,
};
const syntaxes = mysql.getSuggestionAtCaretPosition(
commentOtherLine(syntaxSql, pos.lineNumber),
pos
)?.syntax;
const suggestion = syntaxes?.find(
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
);

expect(suggestion).not.toBeUndefined();
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['user']);
});

test('Having expression column', () => {
const pos: CaretPosition = {
lineNumber: 59,
column: 91,
};
const syntaxes = mysql.getSuggestionAtCaretPosition(
commentOtherLine(syntaxSql, pos.lineNumber),
pos
)?.syntax;
const suggestion = syntaxes?.find(
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
);

expect(suggestion).not.toBeUndefined();
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['salary']);
});

test('Select expression column', () => {
const pos: CaretPosition = {
lineNumber: 59,
column: 46,
};
const syntaxes = mysql.getSuggestionAtCaretPosition(
commentOtherLine(syntaxSql, pos.lineNumber),
pos
)?.syntax;
const suggestion = syntaxes?.find(
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
);

expect(suggestion).not.toBeUndefined();
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['age']);
});

test('Join expression column', () => {
const pos: CaretPosition = {
lineNumber: 61,
column: 85,
};
const syntaxes = mysql.getSuggestionAtCaretPosition(
commentOtherLine(syntaxSql, pos.lineNumber),
pos
)?.syntax;
const suggestion = syntaxes?.find(
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
);

expect(suggestion).not.toBeUndefined();
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([
'p',
'.',
'category_id',
]);
});

test('Case expression column', () => {
const pos: CaretPosition = {
lineNumber: 63,
column: 30,
};
const syntaxes = mysql.getSuggestionAtCaretPosition(
commentOtherLine(syntaxSql, pos.lineNumber),
pos
)?.syntax;
const suggestion = syntaxes?.find(
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
);

expect(suggestion).not.toBeUndefined();
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['score']);
});
});

0 comments on commit b02894b

Please sign in to comment.