From 3847c1fd8dd457b4d16f77352723c8f3d7cd5353 Mon Sep 17 00:00:00 2001 From: Taylor Bantle Date: Fri, 15 Dec 2023 13:35:08 -0800 Subject: [PATCH] web: Sql builder tests: --- web/hooks/useSqlBuilder/tests/mysql.test.tsx | 26 +++++++++++-------- .../useSqlBuilder/tests/postgres.test.tsx | 23 +++++++++------- web/package.json | 2 +- web/yarn.lock | 10 +++---- 4 files changed, 35 insertions(+), 26 deletions(-) diff --git a/web/hooks/useSqlBuilder/tests/mysql.test.tsx b/web/hooks/useSqlBuilder/tests/mysql.test.tsx index ff6636ea..aa36bda1 100644 --- a/web/hooks/useSqlBuilder/tests/mysql.test.tsx +++ b/web/hooks/useSqlBuilder/tests/mysql.test.tsx @@ -327,7 +327,6 @@ describe("test deleteFromTable", () => { td.nameSingleQuoteColValue, ], }, - // TODO: Is this the right escaping? expectedQuery: `DELETE FROM \`test-table\` WHERE \`id\` = '1' AND \`pk2\` = '2' AND \`name\` = 'Taylor\\'s chair'`, }, ]; @@ -342,7 +341,7 @@ describe("test deleteFromTable", () => { }); }); -describe("test updateTableQuery", () => { +describe("test updateTableQuery and updateTableMakeNullQuery", () => { const tests: Array<{ desc: string; tableName: string; @@ -351,6 +350,7 @@ describe("test updateTableQuery", () => { columns: ColumnForDataTableFragment[]; row: RowForDataTableFragment; expectedQuery: string; + expectedNullQuery: string; }> = [ { desc: "one pk", @@ -360,6 +360,7 @@ describe("test updateTableQuery", () => { columns: [td.idPKColumn, td.nameColumn], row: { columnValues: [td.idColValue, td.nameColValue] }, expectedQuery: `UPDATE \`test-table\` SET \`name\` = 'New Name' WHERE \`id\` = '1'`, + expectedNullQuery: `UPDATE \`test-table\` SET \`name\` = NULL WHERE \`id\` = '1'`, }, { desc: "two pks", @@ -369,6 +370,7 @@ describe("test updateTableQuery", () => { columns: [td.idPKColumn, td.pkPKColumn, td.nameColumn], row: { columnValues: [td.idColValue, td.idTwoColValue, td.nameColValue] }, expectedQuery: `UPDATE \`test-table\` SET \`name\` = 'New Name' WHERE \`id\` = '1' AND \`pk2\` = '2'`, + expectedNullQuery: `UPDATE \`test-table\` SET \`name\` = NULL WHERE \`id\` = '1' AND \`pk2\` = '2'`, }, { desc: "three pks with single quote val", @@ -388,26 +390,28 @@ describe("test updateTableQuery", () => { ], }, expectedQuery: `UPDATE \`test-table\` SET \`name\` = 'New Name' WHERE \`id\` = '1' AND \`pk2\` = '2' AND \`name\` = 'Taylor\\'s chair'`, + expectedNullQuery: `UPDATE \`test-table\` SET \`name\` = NULL WHERE \`id\` = '1' AND \`pk2\` = '2' AND \`name\` = 'Taylor\\'s chair'`, }, ]; tests.forEach(test => { it(test.desc, async () => { - const { updateTableQuery } = await renderUseSqlBuilder(); + const { updateTableQuery, updateTableMakeNullQuery } = + await renderUseSqlBuilder(); + const cols = toPKCols(test.row, test.columns); + expect( - updateTableQuery( - test.tableName, - test.currentCol, - test.newVal, - toPKCols(test.row, test.columns), - ), + updateTableQuery(test.tableName, test.currentCol, test.newVal, cols), ).toEqual(test.expectedQuery); + + expect( + updateTableMakeNullQuery(test.tableName, test.currentCol, cols), + ).toEqual(test.expectedNullQuery); }); }); }); -describe("test updateTableMakeNullQuery", () => {}); - +// TODO: Add tests describe("test convertToSqlWithNewCols", () => {}); describe("test convertToSqlWithNewColNames", () => {}); diff --git a/web/hooks/useSqlBuilder/tests/postgres.test.tsx b/web/hooks/useSqlBuilder/tests/postgres.test.tsx index 77d61d14..94b98ff3 100644 --- a/web/hooks/useSqlBuilder/tests/postgres.test.tsx +++ b/web/hooks/useSqlBuilder/tests/postgres.test.tsx @@ -331,7 +331,6 @@ describe("test deleteFromTable", () => { td.nameSingleQuoteColValue, ], }, - // TODO: Is this the right escaping? expectedQuery: `DELETE FROM "test-table" WHERE "id" = '1' AND "pk2" = '2' AND "name" = 'Taylor''s chair'`, }, ]; @@ -346,7 +345,7 @@ describe("test deleteFromTable", () => { }); }); -describe("test updateTableQuery", () => { +describe("test updateTableQuery and updateTableMakeNullQuery", () => { const tests: Array<{ desc: string; tableName: string; @@ -355,6 +354,7 @@ describe("test updateTableQuery", () => { columns: ColumnForDataTableFragment[]; row: RowForDataTableFragment; expectedQuery: string; + expectedNullQuery: string; }> = [ { desc: "one pk", @@ -364,6 +364,7 @@ describe("test updateTableQuery", () => { columns: [td.idPKColumn, td.nameColumn], row: { columnValues: [td.idColValue, td.nameColValue] }, expectedQuery: `UPDATE "test-table" SET "name" = 'New Name' WHERE "id" = '1'`, + expectedNullQuery: `UPDATE "test-table" SET "name" = NULL WHERE "id" = '1'`, }, { desc: "two pks", @@ -373,6 +374,7 @@ describe("test updateTableQuery", () => { columns: [td.idPKColumn, td.pkPKColumn, td.nameColumn], row: { columnValues: [td.idColValue, td.idTwoColValue, td.nameColValue] }, expectedQuery: `UPDATE "test-table" SET "name" = 'New Name' WHERE "id" = '1' AND "pk2" = '2'`, + expectedNullQuery: `UPDATE "test-table" SET "name" = NULL WHERE "id" = '1' AND "pk2" = '2'`, }, { desc: "three pks with single quote val", @@ -392,20 +394,23 @@ describe("test updateTableQuery", () => { ], }, expectedQuery: `UPDATE "test-table" SET "name" = 'New Name' WHERE "id" = '1' AND "pk2" = '2' AND "name" = 'Taylor''s chair'`, + expectedNullQuery: `UPDATE "test-table" SET "name" = NULL WHERE "id" = '1' AND "pk2" = '2' AND "name" = 'Taylor''s chair'`, }, ]; tests.forEach(test => { it(test.desc, async () => { - const { updateTableQuery } = await renderUseSqlBuilderForPG(); + const { updateTableQuery, updateTableMakeNullQuery } = + await renderUseSqlBuilderForPG(); + const cols = toPKCols(test.row, test.columns); + expect( - updateTableQuery( - test.tableName, - test.currentCol, - test.newVal, - toPKCols(test.row, test.columns), - ), + updateTableQuery(test.tableName, test.currentCol, test.newVal, cols), ).toEqual(test.expectedQuery); + + expect( + updateTableMakeNullQuery(test.tableName, test.currentCol, cols), + ).toEqual(test.expectedNullQuery); }); }); }); diff --git a/web/package.json b/web/package.json index 0ffa55fe..a238d406 100644 --- a/web/package.json +++ b/web/package.json @@ -37,7 +37,7 @@ "lodash": "^4.17.21", "next": "^14.0.1", "next-useragent": "^2.8.0", - "node-sql-parser": "^4.12.0", + "node-sql-parser": "^4.15.0", "react": "^18.2.0", "react-ace": "^10.1.0", "react-copy-to-clipboard": "^5.1.0", diff --git a/web/yarn.lock b/web/yarn.lock index 6a8461aa..d675e774 100644 --- a/web/yarn.lock +++ b/web/yarn.lock @@ -1337,7 +1337,7 @@ __metadata: lodash: ^4.17.21 next: ^14.0.1 next-useragent: ^2.8.0 - node-sql-parser: ^4.12.0 + node-sql-parser: ^4.15.0 postcss: ^8.4.31 postcss-preset-env: ^9.1.4 prettier: ^3.1.0 @@ -10012,12 +10012,12 @@ __metadata: languageName: node linkType: hard -"node-sql-parser@npm:^4.12.0": - version: 4.12.0 - resolution: "node-sql-parser@npm:4.12.0" +"node-sql-parser@npm:^4.15.0": + version: 4.15.0 + resolution: "node-sql-parser@npm:4.15.0" dependencies: big-integer: ^1.6.48 - checksum: 1cc8d84267b69b1ec80d9bb22f6a2665cd3b1f6bc56c45da6f931886415c5ac13dcbcf93fa2fb04a9f33e08c9a03f1f70aca4250576385839917dab6ee80093f + checksum: 0e8bb210e8e6b10a135c81fe7d26f37f085e0e909dd4a87afc1266b915abd036084bb4a5d97a263384781bfcef44d33bfb0b0d6448e78fa8ebbc1c6da05cb0ea languageName: node linkType: hard