Skip to content

Commit b02894b

Browse files
committed
feat(mysql): add mysql expression column unit test
1 parent ca4d102 commit b02894b

File tree

3 files changed

+130
-1
lines changed

3 files changed

+130
-1
lines changed

test/parser/mysql/errorListener.test.ts

+17
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const sql1 = `SHOW CREATE TABLE`;
55
const sql2 = `CREATE DATABASE `;
66
const sql3 = `SHOW CREATE DATABASE IF NOT EXIsST aaa aaa`;
77
const sql4 = `SELECT * froma aaa`;
8+
const sql5 = `SELECT user, MAX(salary) FROM users where `;
89

910
describe('MySQL validate invalid sql and test msg', () => {
1011
const mysql = new MySQL();
@@ -46,6 +47,14 @@ describe('MySQL validate invalid sql and test msg', () => {
4647
);
4748
});
4849

50+
test('validate unComplete sql5', () => {
51+
const errors = mysql.validate(sql5);
52+
expect(errors.length).toBe(1);
53+
expect(errors[0].message).toBe(
54+
`Statement is incomplete, expecting an existing function or an existing column or a keyword`
55+
);
56+
});
57+
4958
test('validate random text cn', () => {
5059
mysql.locale = 'zh_CN';
5160
const errors = mysql.validate(randomText);
@@ -77,4 +86,12 @@ describe('MySQL validate invalid sql and test msg', () => {
7786
expect(errors.length).toBe(1);
7887
expect(errors[0].message).toBe(`'froma' 在此位置无效,期望一个存在的column或者一个关键字`);
7988
});
89+
90+
test('validate unComplete sql5 cn', () => {
91+
const errors = mysql.validate(sql5);
92+
expect(errors.length).toBe(1);
93+
expect(errors[0].message).toBe(
94+
`语句不完整,期望一个存在的function或者一个存在的column或者一个关键字`
95+
);
96+
});
8097
});

test/parser/mysql/suggestion/fixtures/syntaxSuggestion.sql

+7-1
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,10 @@ SHOW CREATE TABLE tbl_name;
5454

5555
SHOW CREATE DATABASE IF NOT EXISTS db_name;
5656

57-
SHOW CREATE VIEW test.v;
57+
SHOW CREATE VIEW test.v;
58+
59+
SELECT user, MAX(salary) FROM users where age = 10 GROUP BY length(user) HAVING MAX(salary) > 10;
60+
61+
SELECT c.category_id FROM category c JOIN product p ON c.category_id = p.category_id;
62+
63+
SELECT score, CASE WHEN score >= 90 THEN 'A' ELSE 'F' END AS grade FROM students;

test/parser/mysql/suggestion/syntaxSuggestion.test.ts

+106
Original file line numberDiff line numberDiff line change
@@ -508,4 +508,110 @@ describe('MySQL Syntax Suggestion', () => {
508508
expect(suggestion).not.toBeUndefined();
509509
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['test', '.', 'v']);
510510
});
511+
512+
test('Select expression column', () => {
513+
const pos: CaretPosition = {
514+
lineNumber: 59,
515+
column: 24,
516+
};
517+
const syntaxes = mysql.getSuggestionAtCaretPosition(
518+
commentOtherLine(syntaxSql, pos.lineNumber),
519+
pos
520+
)?.syntax;
521+
const suggestion = syntaxes?.find(
522+
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
523+
);
524+
525+
expect(suggestion).not.toBeUndefined();
526+
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['salary']);
527+
});
528+
529+
test('Group by expression column', () => {
530+
const pos: CaretPosition = {
531+
lineNumber: 59,
532+
column: 72,
533+
};
534+
const syntaxes = mysql.getSuggestionAtCaretPosition(
535+
commentOtherLine(syntaxSql, pos.lineNumber),
536+
pos
537+
)?.syntax;
538+
const suggestion = syntaxes?.find(
539+
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
540+
);
541+
542+
expect(suggestion).not.toBeUndefined();
543+
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['user']);
544+
});
545+
546+
test('Having expression column', () => {
547+
const pos: CaretPosition = {
548+
lineNumber: 59,
549+
column: 91,
550+
};
551+
const syntaxes = mysql.getSuggestionAtCaretPosition(
552+
commentOtherLine(syntaxSql, pos.lineNumber),
553+
pos
554+
)?.syntax;
555+
const suggestion = syntaxes?.find(
556+
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
557+
);
558+
559+
expect(suggestion).not.toBeUndefined();
560+
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['salary']);
561+
});
562+
563+
test('Select expression column', () => {
564+
const pos: CaretPosition = {
565+
lineNumber: 59,
566+
column: 46,
567+
};
568+
const syntaxes = mysql.getSuggestionAtCaretPosition(
569+
commentOtherLine(syntaxSql, pos.lineNumber),
570+
pos
571+
)?.syntax;
572+
const suggestion = syntaxes?.find(
573+
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
574+
);
575+
576+
expect(suggestion).not.toBeUndefined();
577+
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['age']);
578+
});
579+
580+
test('Join expression column', () => {
581+
const pos: CaretPosition = {
582+
lineNumber: 61,
583+
column: 85,
584+
};
585+
const syntaxes = mysql.getSuggestionAtCaretPosition(
586+
commentOtherLine(syntaxSql, pos.lineNumber),
587+
pos
588+
)?.syntax;
589+
const suggestion = syntaxes?.find(
590+
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
591+
);
592+
593+
expect(suggestion).not.toBeUndefined();
594+
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([
595+
'p',
596+
'.',
597+
'category_id',
598+
]);
599+
});
600+
601+
test('Case expression column', () => {
602+
const pos: CaretPosition = {
603+
lineNumber: 63,
604+
column: 30,
605+
};
606+
const syntaxes = mysql.getSuggestionAtCaretPosition(
607+
commentOtherLine(syntaxSql, pos.lineNumber),
608+
pos
609+
)?.syntax;
610+
const suggestion = syntaxes?.find(
611+
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
612+
);
613+
614+
expect(suggestion).not.toBeUndefined();
615+
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['score']);
616+
});
511617
});

0 commit comments

Comments
 (0)