From 1fb3a94e2fdbc7edde51ae44a7310d59c483bbe4 Mon Sep 17 00:00:00 2001 From: Daniel Hensby Date: Tue, 19 Apr 2022 14:57:13 +0100 Subject: [PATCH] Add test for bigint casting --- test/common/tests.js | 8 +++++++ test/msnodesqlv8/msnodesqlv8.js | 37 +++++++++++++++++---------------- test/tedious/tedious.js | 37 +++++++++++++++++---------------- 3 files changed, 46 insertions(+), 36 deletions(-) diff --git a/test/common/tests.js b/test/common/tests.js index 9e215046..60d9f84c 100644 --- a/test/common/tests.js +++ b/test/common/tests.js @@ -1250,6 +1250,14 @@ module.exports = (sql, driver) => { }).catch(done) }, + 'BigInt casted types' (done) { + const req = new TestRequest() + req.query('SELECT cast(9223372036854775807 AS BigInt) as bignumber').then(result => { + assert.strictEqual(result.recordset[0].bignumber, '9223372036854775807') + done() + }).catch(done) + }, + 'dataLength type correction' (done) { sql.on('error', err => console.error(err)) const req = new TestRequest() diff --git a/test/msnodesqlv8/msnodesqlv8.js b/test/msnodesqlv8/msnodesqlv8.js index 96d766c7..533c621a 100644 --- a/test/msnodesqlv8/msnodesqlv8.js +++ b/test/msnodesqlv8/msnodesqlv8.js @@ -8,9 +8,10 @@ const sql = require('../../msnodesqlv8') const TESTS = require('../common/tests.js')(sql, 'msnodesqlv8') const TIMES = require('../common/times.js')(sql, 'msnodesqlv8') const versionHelper = require('../common/versionhelper') +const { readFileSync } = require('fs') const config = function () { - const cfg = JSON.parse(require('fs').readFileSync(join(__dirname, '../.mssql.json'))) + const cfg = JSON.parse(readFileSync(join(__dirname, '../.mssql.json'))) cfg.driver = 'msnodesqlv8' return cfg } @@ -19,23 +20,22 @@ let connection1 = null let connection2 = null describe('msnodesqlv8', function () { - before(done => - sql.connect(config(), function (err) { - if (err) return done(err) - - let req = new sql.Request() - req.batch(require('fs').readFileSync(join(__dirname, '../cleanup.sql'), 'utf8'), function (err) { - if (err) return done(err) - - req = new sql.Request() - req.batch(require('fs').readFileSync(join(__dirname, '../prepare.sql'), 'utf8'), function (err) { - if (err) return done(err) - - sql.close(done) + before(done => { + try { + sql.connect(config()) + .then(() => { + return new sql.Request().query(readFileSync(join(__dirname, '../cleanup.sql'), 'utf8')) }) - }) - }) - ) + .then(() => { + return new sql.Request().query(readFileSync(join(__dirname, '../prepare.sql'), 'utf8')) + }) + .catch(done) + .then(() => sql.close()) + .then(() => done()) + } catch (e) { + done(e) + } + }) afterEach(() => sql.valueHandler.clear()) describe('basic test suite', function () { @@ -90,6 +90,7 @@ describe('msnodesqlv8', function () { it('connection healthy works', done => TESTS['connection healthy works'](config(), done)) it('healthy connection goes bad', done => TESTS['healthy connection goes bad'](config(), done)) it('request timeout', done => TESTS['request timeout'](done)) + it('BigInt casted types', done => TESTS['BigInt casted types'](done)) it('dataLength type correction', done => TESTS['dataLength type correction'](done)) it('chunked xml support', done => TESTS['chunked xml support'](done)) @@ -244,7 +245,7 @@ describe('msnodesqlv8', function () { if (err) return done(err) const req = new sql.Request() - req.query(require('fs').readFileSync(join(__dirname, '../cleanup.sql'), 'utf8'), function (err) { + req.query(readFileSync(join(__dirname, '../cleanup.sql'), 'utf8'), function (err) { if (err) return done(err) sql.close(done) diff --git a/test/tedious/tedious.js b/test/tedious/tedious.js index 99caffbf..b12417bd 100644 --- a/test/tedious/tedious.js +++ b/test/tedious/tedious.js @@ -5,6 +5,7 @@ const sql = require('../../tedious.js') const assert = require('assert') const { join } = require('path') +const { readFileSync } = require('fs') const TESTS = require('../common/tests.js')(sql, 'tedious') const TIMES = require('../common/times.js')(sql, 'tedious') @@ -16,7 +17,7 @@ if (parseInt(process.version.match(/^v(\d+)\./)[1]) > 0) { } const config = function () { - const cfg = JSON.parse(require('fs').readFileSync(join(__dirname, '../.mssql.json'))) + const cfg = JSON.parse(readFileSync(join(__dirname, '../.mssql.json'))) cfg.driver = 'tedious' return cfg } @@ -25,23 +26,22 @@ let connection1 = null let connection2 = null describe('tedious', () => { - before(done => - sql.connect(config(), err => { - if (err) return done(err) - - let req = new sql.Request() - req.query(require('fs').readFileSync(join(__dirname, '../cleanup.sql'), 'utf8'), err => { - if (err) return done(err) - - req = new sql.Request() - req.query(require('fs').readFileSync(join(__dirname, '../prepare.sql'), 'utf8'), err => { - if (err) return done(err) - - sql.close(done) + before(done => { + try { + sql.connect(config()) + .then(() => { + return new sql.Request().query(readFileSync(join(__dirname, '../cleanup.sql'), 'utf8')) }) - }) - }) - ) + .then(() => { + return new sql.Request().query(readFileSync(join(__dirname, '../prepare.sql'), 'utf8')) + }) + .catch(done) + .then(() => sql.close()) + .then(() => done()) + } catch (e) { + done(e) + } + }) afterEach(() => sql.valueHandler.clear()) describe('basic test suite', () => { @@ -99,6 +99,7 @@ describe('tedious', () => { it('connection healthy works', done => TESTS['connection healthy works'](config(), done)) it('healthy connection goes bad', done => TESTS['healthy connection goes bad'](config(), done)) it('request timeout', done => TESTS['request timeout'](done, 'tedious', /Timeout: Request failed to complete in 1000ms/)) + it('BigInt casted types', done => TESTS['BigInt casted types'](done)) it('dataLength type correction', done => TESTS['dataLength type correction'](done)) it('type validation', done => TESTS['type validation']('query', done)) it('type validation (batch)', done => TESTS['type validation']('batch', done)) @@ -344,7 +345,7 @@ describe('tedious', () => { if (err) return done(err) const req = new sql.Request() - req.query(require('fs').readFileSync(join(__dirname, '../cleanup.sql'), 'utf8'), function (err) { + req.query(readFileSync(join(__dirname, '../cleanup.sql'), 'utf8'), function (err) { if (err) return done(err) sql.close(done)