mirror of
https://github.com/brianc/node-postgres.git
synced 2025-12-08 20:16:25 +00:00
I didn't do much to "modernize" the pg-native codebase other than running it through the standard eslint --fix that is applied to the rest of the code. There's some easy opportunities there to update it to es6 and so on...it still uses some pretty antiquated coding styles in places. This PR re-introduces the native tests on node v20, and updates test matrix to drop unsupported versions of node & add in node v22.
84 lines
2.3 KiB
JavaScript
84 lines
2.3 KiB
JavaScript
var Client = require('../')
|
|
var assert = require('assert')
|
|
|
|
describe('query sync', function (done) {
|
|
before(function () {
|
|
this.client = Client()
|
|
this.client.connectSync()
|
|
})
|
|
|
|
after(function (done) {
|
|
this.client.end(done)
|
|
})
|
|
|
|
it('simple query works', function () {
|
|
var rows = this.client.querySync('SELECT NOW() AS the_time')
|
|
assert.equal(rows.length, 1)
|
|
assert.equal(rows[0].the_time.getFullYear(), new Date().getFullYear())
|
|
})
|
|
|
|
it('parameterized query works', function () {
|
|
var rows = this.client.querySync('SELECT $1::text AS name', ['Brian'])
|
|
assert.equal(rows.length, 1)
|
|
assert.equal(rows[0].name, 'Brian')
|
|
})
|
|
|
|
it('throws when second argument is not an array', function () {
|
|
assert.throws(() => {
|
|
this.client.querySync('SELECT $1::text AS name', 'Brian')
|
|
})
|
|
assert.throws(() => {
|
|
this.client.prepareSync('test-failure', 'SELECT $1::text as name', 1)
|
|
|
|
this.client.executeSync('test-failure', 'Brian')
|
|
})
|
|
})
|
|
|
|
it('prepared statement works', function () {
|
|
this.client.prepareSync('test', 'SELECT $1::text as name', 1)
|
|
|
|
var rows = this.client.executeSync('test', ['Brian'])
|
|
assert.equal(rows.length, 1)
|
|
assert.equal(rows[0].name, 'Brian')
|
|
|
|
var rows2 = this.client.executeSync('test', ['Aaron'])
|
|
assert.equal(rows2.length, 1)
|
|
assert.equal(rows2[0].name, 'Aaron')
|
|
})
|
|
|
|
it('prepare throws exception on error', function () {
|
|
assert.throws(
|
|
function () {
|
|
this.client.prepareSync('blah', 'I LIKE TO PARTY!!!', 0)
|
|
}.bind(this)
|
|
)
|
|
})
|
|
|
|
it('throws exception on executing improperly', function () {
|
|
assert.throws(function () {
|
|
// wrong number of parameters
|
|
this.client.executeSync('test', [])
|
|
})
|
|
})
|
|
|
|
it('throws exception on error', function () {
|
|
assert.throws(
|
|
function () {
|
|
this.client.querySync('SELECT ASLKJASLKJF')
|
|
}.bind(this)
|
|
)
|
|
})
|
|
|
|
it('is still usable after an error', function () {
|
|
var rows = this.client.querySync('SELECT NOW()')
|
|
assert(rows, 'should have returned rows')
|
|
assert.equal(rows.length, 1)
|
|
})
|
|
|
|
it('supports empty query', function () {
|
|
var rows = this.client.querySync('')
|
|
assert(rows, 'should return rows')
|
|
assert.equal(rows.length, 0, 'should return no rows')
|
|
})
|
|
})
|