Skip to content

Commit

Permalink
web: Sql builder tests:
Browse files Browse the repository at this point in the history
  • Loading branch information
tbantle22 committed Dec 15, 2023
1 parent d892169 commit 3847c1f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 26 deletions.
26 changes: 15 additions & 11 deletions web/hooks/useSqlBuilder/tests/mysql.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'`,
},
];
Expand All @@ -342,7 +341,7 @@ describe("test deleteFromTable", () => {
});
});

describe("test updateTableQuery", () => {
describe("test updateTableQuery and updateTableMakeNullQuery", () => {
const tests: Array<{
desc: string;
tableName: string;
Expand All @@ -351,6 +350,7 @@ describe("test updateTableQuery", () => {
columns: ColumnForDataTableFragment[];
row: RowForDataTableFragment;
expectedQuery: string;
expectedNullQuery: string;
}> = [
{
desc: "one pk",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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", () => {});
Expand Down
23 changes: 14 additions & 9 deletions web/hooks/useSqlBuilder/tests/postgres.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'`,
},
];
Expand All @@ -346,7 +345,7 @@ describe("test deleteFromTable", () => {
});
});

describe("test updateTableQuery", () => {
describe("test updateTableQuery and updateTableMakeNullQuery", () => {
const tests: Array<{
desc: string;
tableName: string;
Expand All @@ -355,6 +354,7 @@ describe("test updateTableQuery", () => {
columns: ColumnForDataTableFragment[];
row: RowForDataTableFragment;
expectedQuery: string;
expectedNullQuery: string;
}> = [
{
desc: "one pk",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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);
});
});
});
2 changes: 1 addition & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 5 additions & 5 deletions web/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 3847c1f

Please sign in to comment.