Skip to content

Commit

Permalink
test: add unit test for JSON typeCast wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
jyoonPro-bagel committed Oct 28, 2022
1 parent 565f13e commit f33437f
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions test/unit/parsers/test-text-parser.js
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();

0 comments on commit f33437f

Please sign in to comment.