forked from brianc/node-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery-as-promise-tests.js
More file actions
50 lines (45 loc) · 1.36 KB
/
Copy pathquery-as-promise-tests.js
File metadata and controls
50 lines (45 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
'use strict'
const bluebird = require('bluebird')
const helper = require('../test-helper')
const pg = helper.pg
const assert = require('assert')
const suite = new helper.Suite()
suite.test('promise API', (cb) => {
const pool = new pg.Pool()
pool.connect().then((client) => {
client
.query('SELECT $1::text as name', ['foo'])
.then(function (result) {
assert.equal(result.rows[0].name, 'foo')
return client
})
.then(function (client) {
client.query('ALKJSDF').catch(function (e) {
assert(e instanceof Error)
client.query('SELECT 1 as num').then(function (result) {
assert.equal(result.rows[0].num, 1)
client.release()
pool.end(cb)
})
})
})
})
})
suite.test('promise API with configurable promise type', (cb) => {
const client = new pg.Client({ Promise: bluebird })
const connectPromise = client.connect()
assert(connectPromise instanceof bluebird, 'Client connect() returns configured promise')
connectPromise
.then(() => {
const queryPromise = client.query('SELECT 1')
assert(queryPromise instanceof bluebird, 'Client query() returns configured promise')
return queryPromise.then(() => {
client.end(cb)
})
})
.catch((error) => {
process.nextTick(() => {
throw error
})
})
})