Skip to content

Commit

Permalink
Added test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
julesyan committed Sep 23, 2024
1 parent 6c3ee29 commit 04b43eb
Showing 1 changed file with 168 additions and 68 deletions.
236 changes: 168 additions & 68 deletions src/language/sql/tests/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,112 +19,135 @@ const optionsLower: FormatOptions = {
spaceBetweenStatements: true
}

const optionsNoNewLine: FormatOptions = {
indentWidth: 4,
keywordCase: 'lower',
identifierCase: 'lower',
newLineLists: false,
spaceBetweenStatements: true
}


// Edit an assertion and save to see HMR in action
test('Clause new line - upper', () => {
const sql = `select * from sample`;
const formatted = formatSql(sql, optionsUpper);
expect(formatted).toBe(`SELECT
*
FROM
SAMPLE;`);
expect(formatted).toBe([
`SELECT`,
` *`,
`FROM`,
` SAMPLE;`,
].join(`\n`));
});

test('Clause new line - lower', () => {
const sql = `select * from sample`;
const formatted = formatSql(sql, optionsLower);
expect(formatted).toBe(`select
*
from
sample;`);
expect(formatted).toBe([
`select`,
` *`,
`from`,
` sample;`,
].join(`\n`));
});

test('Two clause statements', () => {
const sql = `select * from sample;\nselect * from sample;`;
const formatted = formatSql(sql, optionsUpper);
expect(formatted).toBe(`SELECT
*
FROM
SAMPLE;
SELECT
*
FROM
SAMPLE;`);
expect(formatted).toBe([
`SELECT`,
` *`,
`FROM`,
` SAMPLE;`,
`SELECT`,
` *`,
`FROM`,
` SAMPLE;`,
].join(`\n`));
});

test('Simple multi clause', () => {
const sql = `select * from sample limit 1`;
const formatted = formatSql(sql, optionsUpper);
expect(formatted).toBe(
`SELECT
*
FROM
SAMPLE
LIMIT
1;`);
expect(formatted).toBe([
`SELECT`,
` *`,
`FROM`,
` SAMPLE`,
`LIMIT`,
` 1;`,
].join(`\n`));
});

test('Brackets', () => {
const sql = `SELECT * FROM SAMPLE(RESET_STATISTICS => 'NO', JOB_NAME_FILTER => '*ALL', DETAILED_INFO => 'NONE') WHERE UPPER(JOB_NAME) LIKE '%QNAVMNSRV%' ORDER BY JOB_NAME_SHORT, JOB_NUMBER;`;
const formatted = formatSql(sql, optionsUpper);
expect(formatted).toBe(`SELECT
*
FROM
SAMPLE(
RESET_STATISTICS => 'NO',
JOB_NAME_FILTER => '*ALL',
DETAILED_INFO => 'NONE'
)
WHERE
UPPER(JOB_NAME) LIKE '%QNAVMNSRV%'
ORDER BY
JOB_NAME_SHORT,
JOB_NUMBER;`);
expect(formatted).toBe([
`SELECT`,
` *`,
`FROM`,
` SAMPLE(`,
` RESET_STATISTICS => 'NO',`,
` JOB_NAME_FILTER => '*ALL',`,
` DETAILED_INFO => 'NONE'`,
` )`,
`WHERE`,
` UPPER(JOB_NAME) LIKE '%QNAVMNSRV%'`,
`ORDER BY`,
` JOB_NAME_SHORT,`,
` JOB_NUMBER;`,
].join(`\n`));
});

test('Select with columns', () => {
const sql = `SELECT ONE, TWO, THREE FROM SAMPLE2`;
const formatted = formatSql(sql, optionsUpper);
expect(formatted).toBe(`SELECT
ONE,
TWO,
THREE
FROM
SAMPLE2;`);
expect(formatted).toBe([
`SELECT`,
` ONE,`,
` TWO,`,
` THREE`,
`FROM`,
` SAMPLE2;`
].join(`\n`));
});

test('Nested Select', () => {
const sql = `SELECT * FROM SAMPLE ( SELECT ONE, TWO, THREE FROM SAMPLE2 ) WHERE UPPER(JOB_NAME) LIKE '%QNAVMNSRV%' ORDER BY JOB_NAME_SHORT, JOB_NUMBER;`;
const formatted = formatSql(sql, optionsUpper);
expect(formatted).toBe(`SELECT
*
FROM
SAMPLE (
SELECT
ONE,
TWO,
THREE
FROM
SAMPLE2
)
WHERE
UPPER(JOB_NAME) LIKE '%QNAVMNSRV%'
ORDER BY
JOB_NAME_SHORT,
JOB_NUMBER;`);
expect(formatted).toBe([
`SELECT`,
` *`,
`FROM`,
` SAMPLE (`,
` SELECT`,
` ONE,`,
` TWO,`,
` THREE`,
` FROM`,
` SAMPLE2`,
` )`,
`WHERE`,
` UPPER(JOB_NAME) LIKE '%QNAVMNSRV%'`,
`ORDER BY`,
` JOB_NAME_SHORT,`,
` JOB_NUMBER;`
].join(`\n`));
});

test('Alter Table to Add Materialized Query (from ACS)', () => {
const sql = `ALTER TABLE table1 ADD MATERIALIZED QUERY (SELECT int_col, varchar_col FROM table3) DATA INITIALLY IMMEDIATE REFRESH DEFERRED MAINTAINED BY USER ENABLE QUERY OPTIMIZATION;`;
const formatted = formatSql(sql, optionsUpper);
expect(formatted).toBe(`ALTER
TABLE TABLE1 ADD MATERIALIZED QUERY (
SELECT
INT_COL,
VARCHAR_COL
FROM
TABLE3
) DATA INITIALLY IMMEDIATE REFRESH DEFERRED MAINTAINED BY USER ENABLE QUERY OPTIMIZATION;`);
expect(formatted).toBe([
`ALTER`,
` TABLE TABLE1 ADD MATERIALIZED QUERY (`,
` SELECT`,
` INT_COL,`,
` VARCHAR_COL`,
` FROM`,
` TABLE3`,
` ) DATA INITIALLY IMMEDIATE REFRESH DEFERRED MAINTAINED BY USER ENABLE QUERY OPTIMIZATION;`,
].join(`\n`));
});

test(`CREATE FUNCTION: with single parameter`, () => {
Expand Down Expand Up @@ -193,7 +216,84 @@ test(`CREATE PROCEDURE: with complex body`, () => {
test('Active jobs (from Nav)', () => {
const sql = `SELECT * FROM TABLE ( QSYS2.ACTIVE_JOB_INFO( RESET_STATISTICS => 'NO', SUBSYSTEM_LIST_FILTER => '', JOB_NAME_FILTER => '*ALL', CURRENT_USER_LIST_FILTER => '', DETAILED_INFO => 'NONE' ) ) ORDER BY SUBSYSTEM, RUN_PRIORITY, JOB_NAME_SHORT, JOB_NUMBER LIMIT 100 OFFSET 0`;
const formatted = formatSql(sql, optionsUpper);
// console.log('*************');
expect(formatted).toBe([
`SELECT`,
` *`,
`FROM`,
` TABLE (`,
` QSYS2.ACTIVE_JOB_INFO(`,
` RESET_STATISTICS => 'NO',`,
` SUBSYSTEM_LIST_FILTER => '',`,
` JOB_NAME_FILTER => '*ALL',`,
` CURRENT_USER_LIST_FILTER => '',`,
` DETAILED_INFO => 'NONE'`,
` )`,
` )`,
`ORDER BY`,
` SUBSYSTEM,`,
` RUN_PRIORITY,`,
` JOB_NAME_SHORT,`,
` JOB_NUMBER`,
`LIMIT`,
` 100`,
`OFFSET`,
` 0;`,
].join(`\n`));
});

test('Select WITH', () => {
const sql = `WITH A AS ( SELECT * FROM TABLE ( QSYS2.IFS_OBJECT_STATISTICS( START_PATH_NAME => '/QIBM/ProdData/HTTPA/admin/www/', SUBTREE_DIRECTORIES => 'NO', OBJECT_TYPE_LIST => '', OMIT_LIST => '', IGNORE_ERRORS => 'YES' ) ) ) SELECT * FROM A WHERE UPPER(PATH_NAME) LIKE '%HTML%' ORDER BY UPPER(PATH_NAME) ASC LIMIT 500 OFFSET 0`;
const formatted = formatSql(sql, optionsUpper);
expect(formatted).toBe([
`WITH`,
` A AS (`,
` SELECT`,
` *`,
` FROM`,
` TABLE (`,
` QSYS2.IFS_OBJECT_STATISTICS(`,
` START_PATH_NAME => '/QIBM/PRODDATA/HTTPA/ADMIN/WWW/',`,
` SUBTREE_DIRECTORIES => 'NO',`,
` OBJECT_TYPE_LIST => '',`,
` OMIT_LIST => '',`,
` IGNORE_ERRORS => 'YES'`,
` )`,
` )`,
` )`,
`SELECT`,
` *`,
`FROM`,
` A`,
`WHERE`,
` UPPER(PATH_NAME) LIKE '%HTML%'`,
`ORDER BY`,
` UPPER(PATH_NAME) ASC`,
`LIMIT`,
` 500`,
`OFFSET`,
` 0;`,
].join(`\n`));
});

test('Create and Insert', () => {
const sql = [
`CREATE TABLE emp(name VARCHAR(100) CCSID 1208, id int);`,
`INSERT INTO emp VALUES ('name', 1);`
].join(`\n`);
const formatted = formatSql(sql, optionsUpper);
// console.log("*******");
// console.log(formatted);
// console.log('*************');
// console.log("*******");
expect(formatted).toBe([
`CREATE TABLE EMP (`,
` NAME VARCHAR(100) CCSID 1208,`,
` ID INT`,
`);`,
`INSERT INTO`,
` EMP`,
`VALUES(`,
` 'NAME',`,
` 1`,
`);`,
].join(`\n`));
});

0 comments on commit 04b43eb

Please sign in to comment.