-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
82d8672
commit d8f953f
Showing
6 changed files
with
163 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,59 @@ | ||
import { assertEquals, assertThrows, replaceParams } from "../deps.ts"; | ||
import { assertEquals, assertThrows, replaceParams } from '../deps.ts'; | ||
const { test } = Deno; | ||
|
||
test("testReplaceDate", function () { | ||
const date = new Date("2019-01-01 12:12:12"); | ||
assertEquals(replaceParams("?", [date]), `"2019-01-01 12:12:12.000"`); | ||
test('testReplaceDate', function () { | ||
const date = new Date('2019-01-01 12:12:12'); | ||
assertEquals(replaceParams('?', [date]), `"2019-01-01 12:12:12.000"`); | ||
|
||
const dateWithMS = new Date("2020-07-04T12:51:53.728"); | ||
assertEquals(replaceParams("?", [dateWithMS]), `"2020-07-04 12:51:53.728"`); | ||
const dateWithMS = new Date('2020-07-04T12:51:53.728'); | ||
assertEquals(replaceParams('?', [dateWithMS]), `"2020-07-04 12:51:53.728"`); | ||
}); | ||
|
||
test("testIdReplace", async function () { | ||
test('testIdReplace', function () { | ||
assertEquals( | ||
replaceParams(`'??' "??" ?? ?`, ["a", "b"]), | ||
`'??' "??" \`a\` "b"`, | ||
replaceParams(`'??' "??" ?? ?`, ['a', 'b']), | ||
`'??' "??" \`a\` "b"` | ||
); | ||
|
||
assertEquals(replaceParams("?? ?", null), "?? ?"); | ||
assertEquals(replaceParams("?? ?", []), "?? ?"); | ||
assertEquals(replaceParams("?? ?", [null, null]), "`` NULL"); | ||
assertEquals(replaceParams('?? ?', null), '?? ?'); | ||
assertEquals(replaceParams('?? ?', []), '?? ?'); | ||
assertEquals(replaceParams('?? ?', [null, null]), '`` NULL'); | ||
|
||
assertEquals(replaceParams("??", ["user.id"]), "`user`.`id`"); | ||
assertEquals(replaceParams("??", ["user.*"]), "`user`.*"); | ||
assertEquals(replaceParams('??', ['user.id']), '`user`.`id`'); | ||
assertEquals(replaceParams('??', ['user.*']), '`user`.*'); | ||
assertEquals( | ||
replaceParams("??", ["user.id as user_id"]), | ||
"`user`.`id` AS `user_id`", | ||
replaceParams('??', ['user.id as user_id']), | ||
'`user`.`id` AS `user_id`' | ||
); | ||
|
||
assertEquals(replaceParams("?? ?", ["id", "val"]), '`id` "val"'); | ||
assertEquals(replaceParams("??", ["id"]), "`id`"); | ||
assertEquals(replaceParams("??", [1]), "`1`"); | ||
assertEquals(replaceParams("??", [true]), "`true`"); | ||
assertEquals(replaceParams("?", ["string"]), `"string"`); | ||
assertEquals(replaceParams("?", ["str\\ing"]), `"str\\\\ing"`); | ||
assertEquals(replaceParams("?", [123]), `123`); | ||
assertEquals(replaceParams("?", [`"test"`]), '"\\"test\\""'); | ||
assertEquals(replaceParams("?", [`\\"test"`]), '"\\\\\\"test\\""'); | ||
assertEquals(replaceParams("?", [["a", "b", "c", "d"]]), '("a","b","c","d")'); | ||
assertEquals(replaceParams("?", [[1, 2, 3, 4]]), "(1,2,3,4)"); | ||
assertEquals(replaceParams("??", [["a", "b", "c"]]), "(`a`,`b`,`c`)"); | ||
|
||
let keys: string[] = ["a", "b", "c"]; | ||
assertEquals(replaceParams("??", [keys]), "(`a`,`b`,`c`)"); | ||
assertEquals(replaceParams('?? ?', ['id', 'val']), '`id` "val"'); | ||
assertEquals(replaceParams('??', ['id']), '`id`'); | ||
assertEquals(replaceParams('??', [1]), '`1`'); | ||
assertEquals(replaceParams('??', [true]), '`true`'); | ||
assertEquals(replaceParams('?', ['string']), `"string"`); | ||
assertEquals(replaceParams('?', ['str\\ing']), `"str\\\\ing"`); | ||
assertEquals(replaceParams('?', [123]), `123`); | ||
assertEquals(replaceParams('?', [`"test"`]), '"\\"test\\""'); | ||
assertEquals(replaceParams('?', [`\\"test"`]), '"\\\\\\"test\\""'); | ||
assertEquals(replaceParams('?', [['a', 'b', 'c', 'd']]), '("a","b","c","d")'); | ||
assertEquals(replaceParams('?', [[1, 2, 3, 4]]), '(1,2,3,4)'); | ||
assertEquals(replaceParams('??', [['a', 'b', 'c']]), '(`a`,`b`,`c`)'); | ||
|
||
const keys: string[] = ['a', 'b', 'c']; | ||
assertEquals(replaceParams('??', [keys]), '(`a`,`b`,`c`)'); | ||
assertEquals( | ||
replaceParams("??", [Object.keys({ a: 1, b: 1, c: 1 })]), | ||
"(`a`,`b`,`c`)", | ||
replaceParams('??', [Object.keys({ a: 1, b: 1, c: 1 })]), | ||
'(`a`,`b`,`c`)' | ||
); | ||
|
||
const query = replaceParams( | ||
`select ??, ?? from ?? where ?? = ? and ?? = ? and is_admin = ?`, | ||
["name", "email", "users", "id", 1, "name", "manyuanrong", true], | ||
['name', 'email', 'users', 'id', 1, 'name', 'manyuanrong', true] | ||
); | ||
assertEquals( | ||
query, | ||
'select `name`, `email` from `users` where `id` = 1 and `name` = "manyuanrong" and is_admin = true', | ||
'select `name`, `email` from `users` where `id` = 1 and `name` = "manyuanrong" and is_admin = true' | ||
); | ||
|
||
assertThrows(() => replaceParams("?", [{}]), Error); | ||
assertThrows(() => replaceParams('?', [{}]), Error); | ||
}); |
Oops, something went wrong.