index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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', 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('fetch 6 when asking for 10', function (done) {
  17. const cursor = this.pgCursor(text)
  18. cursor.read(10, function (err, res) {
  19. assert.ifError(err)
  20. assert.strictEqual(res.length, 6)
  21. done()
  22. })
  23. })
  24. it('end before reading to end', function (done) {
  25. const cursor = this.pgCursor(text)
  26. cursor.read(3, function (err, res) {
  27. assert.ifError(err)
  28. assert.strictEqual(res.length, 3)
  29. done()
  30. })
  31. })
  32. it('callback with error', function (done) {
  33. const cursor = this.pgCursor('select asdfasdf')
  34. cursor.read(1, function (err) {
  35. assert(err)
  36. done()
  37. })
  38. })
  39. it('read a partial chunk of data', function (done) {
  40. const cursor = this.pgCursor(text)
  41. cursor.read(2, function (err, res) {
  42. assert.ifError(err)
  43. assert.strictEqual(res.length, 2)
  44. cursor.read(3, function (err, res) {
  45. assert(!err)
  46. assert.strictEqual(res.length, 3)
  47. cursor.read(1, function (err, res) {
  48. assert(!err)
  49. assert.strictEqual(res.length, 1)
  50. cursor.read(1, function (err, res) {
  51. assert(!err)
  52. assert.ifError(err)
  53. assert.strictEqual(res.length, 0)
  54. done()
  55. })
  56. })
  57. })
  58. })
  59. })
  60. it('read return length 0 past the end', function (done) {
  61. const cursor = this.pgCursor(text)
  62. cursor.read(2, function (err) {
  63. assert(!err)
  64. cursor.read(100, function (err, res) {
  65. assert(!err)
  66. assert.strictEqual(res.length, 4)
  67. cursor.read(100, function (err, res) {
  68. assert(!err)
  69. assert.strictEqual(res.length, 0)
  70. done()
  71. })
  72. })
  73. })
  74. })
  75. it('read huge result', function (done) {
  76. this.timeout(10000)
  77. const text = 'SELECT generate_series as num FROM generate_series(0, 100000)'
  78. const values = []
  79. const cursor = this.pgCursor(text, values)
  80. let count = 0
  81. const read = function () {
  82. cursor.read(100, function (err, rows) {
  83. if (err) return done(err)
  84. if (!rows.length) {
  85. assert.strictEqual(count, 100001)
  86. return done()
  87. }
  88. count += rows.length
  89. if (count % 10000 === 0) {
  90. // console.log(count)
  91. }
  92. setImmediate(read)
  93. })
  94. }
  95. read()
  96. })
  97. it('normalizes parameter values', function (done) {
  98. const text = 'SELECT $1::json me'
  99. const values = [{ name: 'brian' }]
  100. const cursor = this.pgCursor(text, values)
  101. cursor.read(1, function (err, rows) {
  102. if (err) return done(err)
  103. assert.strictEqual(rows[0].me.name, 'brian')
  104. cursor.read(1, function (err, rows) {
  105. assert(!err)
  106. assert.strictEqual(rows.length, 0)
  107. done()
  108. })
  109. })
  110. })
  111. it('returns result along with rows', function (done) {
  112. const cursor = this.pgCursor(text)
  113. cursor.read(1, function (err, rows, result) {
  114. assert.ifError(err)
  115. assert.strictEqual(rows.length, 1)
  116. assert.strictEqual(rows, result.rows)
  117. assert.deepStrictEqual(
  118. result.fields.map((f) => f.name),
  119. ['num']
  120. )
  121. done()
  122. })
  123. })
  124. it('emits row events', function (done) {
  125. const cursor = this.pgCursor(text)
  126. cursor.read(10)
  127. cursor.on('row', (row, result) => result.addRow(row))
  128. cursor.on('end', (result) => {
  129. assert.strictEqual(result.rows.length, 6)
  130. done()
  131. })
  132. })
  133. it('emits row events when cursor is closed manually', function (done) {
  134. const cursor = this.pgCursor(text)
  135. cursor.on('row', (row, result) => result.addRow(row))
  136. cursor.on('end', (result) => {
  137. assert.strictEqual(result.rows.length, 3)
  138. done()
  139. })
  140. cursor.read(3, () => cursor.close())
  141. })
  142. it('emits error events', function (done) {
  143. const cursor = this.pgCursor('select asdfasdf')
  144. cursor.on('error', function (err) {
  145. assert(err)
  146. done()
  147. })
  148. })
  149. it('returns rowCount on insert', function (done) {
  150. const pgCursor = this.pgCursor
  151. this.client
  152. .query('CREATE TEMPORARY TABLE pg_cursor_test (foo VARCHAR(1), bar VARCHAR(1))')
  153. .then(function () {
  154. const cursor = pgCursor('insert into pg_cursor_test values($1, $2)', ['a', 'b'])
  155. cursor.read(1, function (err, rows, result) {
  156. assert.ifError(err)
  157. assert.strictEqual(rows.length, 0)
  158. assert.strictEqual(result.rowCount, 1)
  159. done()
  160. })
  161. })
  162. .catch(done)
  163. })
  164. })