|
| 1 | +const assert = require('assert') |
| 2 | +const Cursor = require('../') |
| 3 | +const pg = require('pg') |
| 4 | + |
| 5 | +const text = 'SELECT generate_series as num FROM generate_series(0, 5)' |
| 6 | + |
| 7 | +describe('cursor using promises', function () { |
| 8 | + beforeEach(function (done) { |
| 9 | + const client = (this.client = new pg.Client()) |
| 10 | + client.connect(done) |
| 11 | + |
| 12 | + this.pgCursor = function (text, values) { |
| 13 | + return client.query(new Cursor(text, values || [])) |
| 14 | + } |
| 15 | + }) |
| 16 | + |
| 17 | + afterEach(function () { |
| 18 | + this.client.end() |
| 19 | + }) |
| 20 | + |
| 21 | + it('resolve with result', function (done) { |
| 22 | + const cursor = this.pgCursor(text) |
| 23 | + cursor |
| 24 | + .read(6) |
| 25 | + .then((res) => assert.strictEqual(res.length, 6)) |
| 26 | + .error((err) => assert.ifError(err)) |
| 27 | + .finally(() => done()) |
| 28 | + }) |
| 29 | + |
| 30 | + it('reject with error', function (done) { |
| 31 | + const cursor = this.pgCursor('select asdfasdf') |
| 32 | + cursor.read(1).error((err) => { |
| 33 | + assert(err) |
| 34 | + done() |
| 35 | + }) |
| 36 | + }) |
| 37 | + |
| 38 | + it('read multiple times', async function (done) { |
| 39 | + const cursor = this.pgCursor(text) |
| 40 | + let res |
| 41 | + |
| 42 | + try { |
| 43 | + res = await cursor.read(2) |
| 44 | + assert.strictEqual(res.length, 2) |
| 45 | + |
| 46 | + res = await cursor.read(3) |
| 47 | + assert.strictEqual(res.length, 3) |
| 48 | + |
| 49 | + res = await cursor.read(1) |
| 50 | + assert.strictEqual(res.length, 1) |
| 51 | + |
| 52 | + res = await cursor.read(1) |
| 53 | + assert.strictEqual(res.length, 0) |
| 54 | + } catch (err) { |
| 55 | + assert.ifError(err) |
| 56 | + } finally { |
| 57 | + done() |
| 58 | + } |
| 59 | + }) |
| 60 | +}) |
0 commit comments