From b5df699cac0cb5008e931da90ba9b1f483d78ebd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Weslley=20Ara=C3=BAjo?= <46850407+wellwelwel@users.noreply.github.com> Date: Thu, 30 May 2024 13:35:38 -0300 Subject: [PATCH] ci: add tests to JSON parsers (#2720) --- test/common.test.cjs | 2 + .../integration/parsers/json-parse.test.mjs | 39 ++++++++++++++++++ .../integration/parsers/json-string.test.mjs | 41 +++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 test/esm/integration/parsers/json-parse.test.mjs create mode 100644 test/esm/integration/parsers/json-string.test.mjs diff --git a/test/common.test.cjs b/test/common.test.cjs index e46510c0cc..f3cf230766 100644 --- a/test/common.test.cjs +++ b/test/common.test.cjs @@ -108,6 +108,7 @@ exports.createConnection = function (args) { connectTimeout: args && args.connectTimeout, nestTables: args && args.nestTables, ssl: (args && args.ssl) ?? config.ssl, + jsonStrings: args && args.jsonStrings, }; const conn = driver.createConnection(params); @@ -137,6 +138,7 @@ exports.getConfig = function (input) { connectionLimit: args && args.connectionLimit, maxIdle: args && args.maxIdle, idleTimeout: args && args.idleTimeout, + jsonStrings: args && args.jsonStrings, }; return params; }; diff --git a/test/esm/integration/parsers/json-parse.test.mjs b/test/esm/integration/parsers/json-parse.test.mjs new file mode 100644 index 0000000000..99a0690e99 --- /dev/null +++ b/test/esm/integration/parsers/json-parse.test.mjs @@ -0,0 +1,39 @@ +import { test, describe, assert } from 'poku'; +import { createRequire } from 'node:module'; + +const require = createRequire(import.meta.url); +const { + createConnection, + describeOptions, +} = require('../../../common.test.cjs'); + +describe('JSON Parser', describeOptions); + +const connection = createConnection().promise(); + +Promise.all([ + test(async () => { + const [result] = await connection.query( + `SELECT CAST('{"test": true}' AS JSON) AS json_result`, + ); + + assert.deepStrictEqual( + result[0].json_result, + { test: true }, + 'Ensure JSON return parsed (query)', + ); + }), + test(async () => { + const [result] = await connection.execute( + `SELECT CAST('{"test": true}' AS JSON) AS json_result`, + ); + + assert.deepStrictEqual( + result[0].json_result, + { test: true }, + 'Ensure JSON return parsed (execute)', + ); + }), +]).then(async () => { + await connection.end(); +}); diff --git a/test/esm/integration/parsers/json-string.test.mjs b/test/esm/integration/parsers/json-string.test.mjs new file mode 100644 index 0000000000..098777051e --- /dev/null +++ b/test/esm/integration/parsers/json-string.test.mjs @@ -0,0 +1,41 @@ +import { test, describe, assert } from 'poku'; +import { createRequire } from 'node:module'; + +const require = createRequire(import.meta.url); +const { + createConnection, + describeOptions, +} = require('../../../common.test.cjs'); + +describe('JSON String', describeOptions); + +const connection = createConnection({ + jsonStrings: true, +}).promise(); + +Promise.all([ + test(async () => { + const [result] = await connection.query( + `SELECT CAST('{"test": true}' AS JSON) AS json_result`, + ); + + assert.deepStrictEqual( + result[0].json_result, + '{"test": true}', + 'Ensure JSON return as string (query)', + ); + }), + test(async () => { + const [result] = await connection.execute( + `SELECT CAST('{"test": true}' AS JSON) AS json_result`, + ); + + assert.deepStrictEqual( + result[0].json_result, + '{"test": true}', + 'Ensure JSON return as string (execute)', + ); + }), +]).then(async () => { + await connection.end(); +});