forked from brianc/node-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromises.js
60 lines (49 loc) · 1.37 KB
/
promises.js
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
51
52
53
54
55
56
57
58
59
60
const assert = require('assert')
const Cursor = require('../')
const pg = require('pg')
const text = 'SELECT generate_series as num FROM generate_series(0, 5)'
describe('cursor using promises', function () {
beforeEach(function (done) {
const client = (this.client = new pg.Client())
client.connect(done)
this.pgCursor = function (text, values) {
return client.query(new Cursor(text, values || []))
}
})
afterEach(function () {
this.client.end()
})
it('resolve with result', function (done) {
const cursor = this.pgCursor(text)
cursor
.read(6)
.then((res) => assert.strictEqual(res.length, 6))
.error((err) => assert.ifError(err))
.finally(() => done())
})
it('reject with error', function (done) {
const cursor = this.pgCursor('select asdfasdf')
cursor.read(1).error((err) => {
assert(err)
done()
})
})
it('read multiple times', async function (done) {
const cursor = this.pgCursor(text)
let res
try {
res = await cursor.read(2)
assert.strictEqual(res.length, 2)
res = await cursor.read(3)
assert.strictEqual(res.length, 3)
res = await cursor.read(1)
assert.strictEqual(res.length, 1)
res = await cursor.read(1)
assert.strictEqual(res.length, 0)
} catch (err) {
assert.ifError(err)
} finally {
done()
}
})
})