Add test for how to set search path (#2700)

Also refactor a few tests a bit to slowly clean up some of the old style.
This commit is contained in:
Brian C 2022-05-12 22:00:00 -05:00 committed by GitHub
parent 3ca56027d3
commit 28ac2a17bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 51 additions and 62 deletions

View File

@ -1,7 +1,6 @@
'use strict'
var helper = require('./test-helper')
var pg = helper.pg
var sink
const suite = new helper.Suite()
var testForTypeCoercion = function (type) {

View File

@ -1,10 +1,41 @@
'use strict'
var helper = require('./test-helper')
const helper = require('../test-helper')
const assert = require('assert')
helper.testPoolSize(1)
const suite = new helper.Suite()
helper.testPoolSize(2)
const testPoolSize = function (max) {
suite.testAsync(`test ${max} queries executed on a pool rapidly`, () => {
const pool = new helper.pg.Pool({ max: 10 })
helper.testPoolSize(40)
let count = 0
helper.testPoolSize(200)
return new Promise((resolve) => {
for (var i = 0; i < max; i++) {
pool.connect(function (err, client, release) {
assert(!err)
client.query('SELECT * FROM NOW()')
client.query('select generate_series(0, 25)', function (err, result) {
assert.strictEqual(result.rows.length, 26)
})
client.query('SELECT * FROM NOW()', (err) => {
assert(!err)
release()
if (++count === max) {
resolve()
pool.end()
}
})
})
}
})
})
}
testPoolSize(1)
testPoolSize(2)
testPoolSize(40)
testPoolSize(200)

View File

@ -1,31 +1,4 @@
'use strict'
var helper = require('./../test-helper')
const suite = new helper.Suite()
helper.testPoolSize = function (max) {
suite.test(`test ${max} queries executed on a pool rapidly`, (cb) => {
const pool = new helper.pg.Pool({ max: 10 })
var sink = new helper.Sink(max, function () {
pool.end(cb)
})
for (var i = 0; i < max; i++) {
pool.connect(function (err, client, done) {
assert(!err)
client.query('SELECT * FROM NOW()')
client.query('select generate_series(0, 25)', function (err, result) {
assert.equal(result.rows.length, 26)
})
var query = client.query('SELECT * FROM NOW()', (err) => {
assert(!err)
sink.add()
done()
})
})
}
})
}
module.exports = Object.assign({}, helper, { suite: suite })
module.exports = helper

View File

@ -0,0 +1,14 @@
const helper = require('../test-helper')
const suite = new helper.Suite()
suite.testAsync('it sets search_path on connection', async () => {
const client = new helper.pg.Client({
options: '--search_path=foo',
})
await client.connect()
const { rows } = await client.query('SHOW search_path')
assert.strictEqual(rows.length, 1)
assert.strictEqual(rows[0].search_path, 'foo')
await client.end()
})

View File

@ -183,33 +183,6 @@ process.on('uncaughtException', function (err) {
process.exit(255)
})
var Sink = function (expected, timeout, callback) {
var defaultTimeout = 5000
if (typeof timeout === 'function') {
callback = timeout
timeout = defaultTimeout
}
timeout = timeout || defaultTimeout
var internalCount = 0
var kill = function () {
assert.ok(false, 'Did not reach expected ' + expected + ' with an idle timeout of ' + timeout)
}
var killTimeout = setTimeout(kill, timeout)
return {
add: function (count) {
count = count || 1
internalCount += count
clearTimeout(killTimeout)
if (internalCount < expected) {
killTimeout = setTimeout(kill, timeout)
} else {
assert.equal(internalCount, expected)
callback()
}
},
}
}
var getTimezoneOffset = Date.prototype.getTimezoneOffset
var setTimezoneOffset = function (minutesOffset) {
@ -231,7 +204,6 @@ const rejection = (promise) =>
)
module.exports = {
Sink: Sink,
Suite: Suite,
pg: require('./../lib/'),
args: args,