Skip to content

Commit

Permalink
Add test for bigint casting
Browse files Browse the repository at this point in the history
  • Loading branch information
dhensby committed Apr 28, 2022
1 parent 6d5e44c commit 5c27016
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 36 deletions.
3 changes: 3 additions & 0 deletions test/cleanup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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]')

24 changes: 24 additions & 0 deletions test/common/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,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()
Expand Down
38 changes: 20 additions & 18 deletions test/msnodesqlv8/msnodesqlv8.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 () {
Expand Down Expand Up @@ -90,6 +90,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))

Expand Down Expand Up @@ -244,7 +246,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)
Expand Down
4 changes: 4 additions & 0 deletions test/prepare.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 20 additions & 18 deletions test/tedious/tedious.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
}
Expand All @@ -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', () => {
Expand Down Expand Up @@ -99,6 +99,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))
Expand Down Expand Up @@ -344,7 +346,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)
Expand Down

0 comments on commit 5c27016

Please sign in to comment.