From 9ed6e2be19160a43961eaa15f67259c9119b96a5 Mon Sep 17 00:00:00 2001 From: "Visal .In" Date: Thu, 21 Nov 2024 18:09:41 +0700 Subject: [PATCH] add more test case --- tests/connections/connection.test.ts | 2 +- tests/units/placeholder.test.ts | 73 ++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 tests/units/placeholder.test.ts diff --git a/tests/connections/connection.test.ts b/tests/connections/connection.test.ts index 421d50a..bc43424 100644 --- a/tests/connections/connection.test.ts +++ b/tests/connections/connection.test.ts @@ -53,7 +53,7 @@ describe('Database Connection', () => { }); test('Support positional placeholder', async () => { - if (process.env.CONNECTION_TYPE === 'mongo') return; + if (process.env.CONNECTION_TYPE === 'mongodb') return; const sql = process.env.CONNECTION_TYPE === 'mysql' diff --git a/tests/units/placeholder.test.ts b/tests/units/placeholder.test.ts new file mode 100644 index 0000000..b8aa61f --- /dev/null +++ b/tests/units/placeholder.test.ts @@ -0,0 +1,73 @@ +import { + namedPlaceholder, + toNumberedPlaceholders, +} from './../../src/utils/placeholder'; + +test('Positional placeholder', () => { + expect( + namedPlaceholder('SELECT * FROM users WHERE id = :id AND age > :age', { + id: 1, + age: 50, + }) + ).toEqual({ + query: 'SELECT * FROM users WHERE id = ? AND age > ?', + bindings: [1, 50], + }); +}); + +test('Positional placeholder inside the string should be ignored', () => { + expect( + namedPlaceholder( + 'SELECT * FROM users WHERE name = :name AND email = ":email"', + { + name: 'John', + } + ) + ).toEqual({ + query: 'SELECT * FROM users WHERE name = ? AND email = ":email"', + bindings: ['John'], + }); +}); + +test('Named placeholder to number placeholder', () => { + expect( + namedPlaceholder( + 'SELECT * FROM users WHERE id = :id AND age > :age', + { + id: 1, + age: 30, + }, + true + ) + ).toEqual({ + query: 'SELECT * FROM users WHERE id = $1 AND age > $2', + bindings: [1, 30], + }); +}); + +test('Named placeholder to number placeholder with string', () => { + expect( + namedPlaceholder( + 'SELECT * FROM users WHERE id = :id AND email = ":email"', + { + id: 1, + }, + true + ) + ).toEqual({ + query: 'SELECT * FROM users WHERE id = $1 AND email = ":email"', + bindings: [1], + }); +}); + +test('Convert positional placeholder to numbered placeholder', () => { + expect( + toNumberedPlaceholders( + `SELECT * FROM users WHERE id = ? AND email = '?' AND name = 'Outer""base' AND age > ?`, + [1, 30] + ) + ).toEqual({ + query: `SELECT * FROM users WHERE id = $1 AND email = '?' AND name = 'Outer""base' AND age > $2`, + bindings: [1, 30], + }); +});