bring-your-own-promise.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict'
  2. const co = require('co')
  3. const expect = require('expect.js')
  4. const describe = require('mocha').describe
  5. const it = require('mocha').it
  6. const BluebirdPromise = require('bluebird')
  7. const Pool = require('../')
  8. const checkType = (promise) => {
  9. expect(promise).to.be.a(BluebirdPromise)
  10. return promise.catch((e) => undefined)
  11. }
  12. describe('Bring your own promise', function () {
  13. it(
  14. 'uses supplied promise for operations',
  15. co.wrap(function* () {
  16. const pool = new Pool({ Promise: BluebirdPromise })
  17. const client1 = yield checkType(pool.connect())
  18. client1.release()
  19. yield checkType(pool.query('SELECT NOW()'))
  20. const client2 = yield checkType(pool.connect())
  21. // TODO - make sure pg supports BYOP as well
  22. client2.release()
  23. yield checkType(pool.end())
  24. })
  25. )
  26. it(
  27. 'uses promises in errors',
  28. co.wrap(function* () {
  29. const pool = new Pool({ Promise: BluebirdPromise, port: 48484 })
  30. yield checkType(pool.connect())
  31. yield checkType(pool.end())
  32. yield checkType(pool.connect())
  33. yield checkType(pool.query())
  34. yield checkType(pool.end())
  35. })
  36. )
  37. })