From e5b3f60a1621885b7fae76f00eee257ad92e2e72 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/cleanup.sql | 3 +++ test/common/tests.js | 24 +++++++++++++++++++++ test/msnodesqlv8/msnodesqlv8.js | 38 +++++++++++++++++---------------- test/prepare.sql | 4 ++++ test/tedious/tedious.js | 38 +++++++++++++++++---------------- 5 files changed, 71 insertions(+), 36 deletions(-) diff --git a/test/cleanup.sql b/test/cleanup.sql index 05b28409..a1ccf2f8 100644 --- a/test/cleanup.sql +++ b/test/cleanup.sql @@ -52,6 +52,9 @@ if exists (select * from sys.tables where name = 'bulk_table5') if exists (select * from sys.tables where name = 'rowsaffected_test') exec('drop table [dbo].[rowsaffected_test]') +if exists (select * from sys.tables where name = 'bignumbers') + exec('drop table [dbo].[bignumbers]') + if exists (select * from sys.tables where name = 'streaming') exec('drop table [dbo].[streaming]') diff --git a/test/common/tests.js b/test/common/tests.js index ed052871..eac4a93d 100644 --- a/test/common/tests.js +++ b/test/common/tests.js @@ -1276,6 +1276,30 @@ module.exports = (sql, driver) => { }).catch(done) }, + 'BigInt parameters' (done) { + const req = new TestRequest() + req.input('bignumber', sql.BigInt, '9223372036854775807') + req.query('INSERT INTO [dbo].[bignumbers] (bignumber) VALUES (@bignumber)') + .then(() => { + const req2 = new TestRequest() + return req2.query('SELECT * FROM [dbo].[bignumbers]') + }) + .then((result) => { + assert.strictEqual(result.recordsets.length, 1) + assert.strictEqual(result.recordset[0].bignumber, '9223372036854775807') + done() + }) + .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 058200a4..7cd8e381 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 () { @@ -91,6 +91,8 @@ 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 parameters', done => TESTS['BigInt parameters'](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)) @@ -245,7 +247,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/prepare.sql b/test/prepare.sql index 5a527ec5..84675056 100644 --- a/test/prepare.sql +++ b/test/prepare.sql @@ -145,6 +145,10 @@ exec('create table [dbo].[rowsaffected_test] ( a int not null )') +exec('create table [dbo].[bignumbers] ( + bignumber bigint +)') + ;with nums as ( select 0 AS n diff --git a/test/tedious/tedious.js b/test/tedious/tedious.js index 86c5f3f2..7439e48e 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', () => { @@ -100,6 +100,8 @@ 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 parameters', done => TESTS['BigInt parameters'](done)) + 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)) @@ -345,7 +347,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)