Skip to content

Commit a84db5f

Browse files
authored
Add async iterator tests (#62)
1 parent a756ee3 commit a84db5f

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,17 @@
3232
"eslint-plugin-standard": "^3.0.1",
3333
"mocha": "^6.2.2",
3434
"pg": "^7.5.0",
35+
"prettier": "^1.18.2",
3536
"stream-spec": "~0.3.5",
3637
"stream-tester": "0.0.5",
3738
"through": "~2.3.4"
3839
},
40+
"prettier": {
41+
"semi": false,
42+
"printWidth": 120,
43+
"trailingComma": "es5",
44+
"singleQuote": true
45+
},
3946
"dependencies": {
4047
"pg-cursor": "2.0.0"
4148
}

test/async-iterator.es6

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const QueryStream = require('../')
2+
const pg = require('pg')
3+
const assert = require('assert')
4+
5+
const queryText = 'SELECT * FROM generate_series(0, 200) num'
6+
describe('Async iterator', () => {
7+
it('works', async () => {
8+
const stream = new QueryStream(queryText, [])
9+
const client = new pg.Client()
10+
await client.connect()
11+
const query = client.query(stream)
12+
const rows = []
13+
for await (const row of query) {
14+
rows.push(row)
15+
}
16+
assert.equal(rows.length, 201)
17+
await client.end()
18+
})
19+
20+
it('can async iterate and then do a query afterwards', async () => {
21+
const stream = new QueryStream(queryText, [])
22+
const client = new pg.Client()
23+
await client.connect()
24+
const query = client.query(stream)
25+
const iteratorRows = []
26+
for await (const row of query) {
27+
iteratorRows.push(row)
28+
}
29+
assert.equal(iteratorRows.length, 201)
30+
const { rows } = await client.query('SELECT NOW()')
31+
assert.equal(rows.length, 1)
32+
await client.end()
33+
})
34+
35+
it('can async iterate multiple times with a pool', async () => {
36+
const pool = new pg.Pool({ max: 1 })
37+
38+
const allRows = []
39+
const run = async () => {
40+
// get the client
41+
const client = await pool.connect()
42+
// stream some rows
43+
const stream = new QueryStream(queryText, [])
44+
const iteratorRows = []
45+
client.query(stream)
46+
for await (const row of stream) {
47+
iteratorRows.push(row)
48+
allRows.push(row)
49+
}
50+
assert.equal(iteratorRows.length, 201)
51+
client.release()
52+
}
53+
await Promise.all([run(), run(), run()])
54+
assert.equal(allRows.length, 603)
55+
await pool.end()
56+
})
57+
})

test/async-iterator.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// only newer versions of node support async iterator
2+
if (!process.version.startsWith('v8')) {
3+
require('./async-iterator.es6')
4+
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,11 @@ prelude-ls@~1.1.2:
12171217
version "1.1.2"
12181218
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"
12191219

1220+
prettier@^1.18.2:
1221+
version "1.18.2"
1222+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.18.2.tgz#6823e7c5900017b4bd3acf46fe9ac4b4d7bda9ea"
1223+
integrity sha512-OeHeMc0JhFE9idD4ZdtNibzY0+TPHSpSSb9h8FqtP+YnoZZ1sl8Vc9b1sasjfymH3SonAF4QcA2+mzHPhMvIiw==
1224+
12201225
process-nextick-args@~1.0.6:
12211226
version "1.0.7"
12221227
resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-1.0.7.tgz#150e20b756590ad3f91093f25a4f2ad8bff30ba3"

0 commit comments

Comments
 (0)