2021-07-11 11:04:04 +00:00
|
|
|
import {test} from 'uvu';
|
|
|
|
import * as assert from 'uvu/assert';
|
|
|
|
import {sql} from '../src/index';
|
|
|
|
|
|
|
|
test('should be able to parse select query', () => {
|
|
|
|
const query = sql`select * from 'users' where email = ${'example@mail.com'}`;
|
|
|
|
assert.equal(query.sql, 'select * from \'users\' where email = $1');
|
|
|
|
assert.equal(query.values, ['example@mail.com']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should be able to parse insert query', () => {
|
2021-07-13 06:40:15 +00:00
|
|
|
const query = sql`insert into users (name, age, profession) values (${'Jonas'}, ${21}, ${'jobless'})`;
|
2021-07-11 11:04:04 +00:00
|
|
|
assert.equal(query.sql, 'insert into users (name, age, profession) values ($1, $2, $3)');
|
2021-07-13 06:40:15 +00:00
|
|
|
assert.equal(query.values, ['Jonas', 21, 'jobless']);
|
2021-07-11 11:04:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should be able to parse to question marks', () => {
|
2021-07-13 06:40:15 +00:00
|
|
|
const query = sql`insert into users (name, age, profession) values (${'Jonas'}, ${21}, ${'jobless'})`.formatQuestion();
|
|
|
|
assert.equal(query.sql, 'insert into users (name, age, profession) values (?, ?, ?)');
|
|
|
|
assert.equal(query.values, ['Jonas', 21, 'jobless']);
|
2021-07-11 11:04:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should be able to parse to colons', () => {
|
2021-07-13 06:40:15 +00:00
|
|
|
const query = sql`insert into users (name, age, profession) values (${'Jonas'}, ${21}, ${'jobless'})`.formatColon();
|
|
|
|
assert.equal(query.sql, 'insert into users (name, age, profession) values (:1, :2, :3)');
|
|
|
|
assert.equal(query.values, ['Jonas', 21, 'jobless']);
|
2021-07-11 11:04:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should be able to parse to @p', () => {
|
2021-07-13 06:40:15 +00:00
|
|
|
const query = sql`insert into users (name, age, profession) values (${'Jonas'}, ${21}, ${'jobless'})`.formatAtP();
|
|
|
|
assert.equal(query.sql, 'insert into users (name, age, profession) values (@p1, @p2, @p3)');
|
|
|
|
assert.equal(query.values, ['Jonas', 21, 'jobless']);
|
2021-07-11 11:04:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test.run();
|