Skip to content

Commit

Permalink
add more test case
Browse files Browse the repository at this point in the history
  • Loading branch information
invisal committed Nov 21, 2024
1 parent 5cd7682 commit 9ed6e2b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tests/connections/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
73 changes: 73 additions & 0 deletions tests/units/placeholder.test.ts
Original file line number Diff line number Diff line change
@@ -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],
});
});

0 comments on commit 9ed6e2b

Please sign in to comment.