promises.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. const assert = require('assert')
  2. const Cursor = require('../')
  3. const pg = require('pg')
  4. const text = 'SELECT generate_series as num FROM generate_series(0, 5)'
  5. describe('cursor using promises', function () {
  6. beforeEach(function (done) {
  7. const client = (this.client = new pg.Client())
  8. client.connect(done)
  9. this.pgCursor = function (text, values) {
  10. return client.query(new Cursor(text, values || []))
  11. }
  12. })
  13. afterEach(function () {
  14. this.client.end()
  15. })
  16. it('resolve with result', async function () {
  17. const cursor = this.pgCursor(text)
  18. const res = await cursor.read(6)
  19. assert.strictEqual(res.length, 6)
  20. })
  21. it('reject with error', function (done) {
  22. const cursor = this.pgCursor('select asdfasdf')
  23. cursor.read(1).catch((err) => {
  24. assert(err)
  25. done()
  26. })
  27. })
  28. it('read multiple times', async function () {
  29. const cursor = this.pgCursor(text)
  30. let res
  31. res = await cursor.read(2)
  32. assert.strictEqual(res.length, 2)
  33. res = await cursor.read(3)
  34. assert.strictEqual(res.length, 3)
  35. res = await cursor.read(1)
  36. assert.strictEqual(res.length, 1)
  37. res = await cursor.read(1)
  38. assert.strictEqual(res.length, 0)
  39. })
  40. })