-
-
Notifications
You must be signed in to change notification settings - Fork 627
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add unit test for JSON typeCast wrapper
- Loading branch information
1 parent
565f13e
commit f33437f
Showing
1 changed file
with
38 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const common = require('../../common'); | ||
|
||
const typeCastWrapper = function (...args) { | ||
return function (field, next) { | ||
if (field.type === 'JSON') { | ||
return JSON.parse(field.string(...args)); | ||
} | ||
|
||
return next(); | ||
} | ||
} | ||
|
||
const connection = common.createConnection(); | ||
connection.query('CREATE TEMPORARY TABLE t (i JSON)'); | ||
connection.query('INSERT INTO t values(\'{ "test": "😀" }\')'); | ||
|
||
// JSON without encoding options - should result in unexpected behaviors | ||
connection.query({ | ||
sql: 'SELECT * FROM t', | ||
typeCast: typeCastWrapper() | ||
}, (err, rows) => { | ||
assert.ifError(err); | ||
assert.notEqual(rows[0].i.test, "😀"); | ||
}); | ||
|
||
// JSON with encoding explicitly set to utf8 | ||
connection.query({ | ||
sql: 'SELECT * FROM t', | ||
typeCast: typeCastWrapper("utf8") | ||
}, (err, rows) => { | ||
assert.ifError(err); | ||
assert.equal(rows[0].i.test, "😀"); | ||
}); | ||
|
||
connection.end(); |