close.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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, 50)'
  5. describe('close', function () {
  6. beforeEach(function (done) {
  7. const client = (this.client = new pg.Client())
  8. client.connect(done)
  9. })
  10. this.afterEach(function (done) {
  11. this.client.end(done)
  12. })
  13. it('can close a finished cursor without a callback', function (done) {
  14. const cursor = new Cursor(text)
  15. this.client.query(cursor)
  16. this.client.query('SELECT NOW()', done)
  17. cursor.read(100, function (err) {
  18. assert.ifError(err)
  19. cursor.close()
  20. })
  21. })
  22. it('can close a finished cursor a promise', function (done) {
  23. const cursor = new Cursor(text)
  24. this.client.query(cursor)
  25. cursor.read(100, (err) => {
  26. assert.ifError(err)
  27. cursor.close().then(() => {
  28. this.client.query('SELECT NOW()', done)
  29. })
  30. })
  31. })
  32. it('closes cursor early', function (done) {
  33. const cursor = new Cursor(text)
  34. this.client.query(cursor)
  35. this.client.query('SELECT NOW()', done)
  36. cursor.read(25, function (err) {
  37. assert.ifError(err)
  38. cursor.close()
  39. })
  40. })
  41. it('works with callback style', function (done) {
  42. const cursor = new Cursor(text)
  43. const client = this.client
  44. client.query(cursor)
  45. cursor.read(25, function (err, rows) {
  46. assert.ifError(err)
  47. assert.strictEqual(rows.length, 25)
  48. cursor.close(function (err) {
  49. assert.ifError(err)
  50. client.query('SELECT NOW()', done)
  51. })
  52. })
  53. })
  54. it('is a no-op to "close" the cursor before submitting it', function (done) {
  55. const cursor = new Cursor(text)
  56. cursor.close(done)
  57. })
  58. })