forked from brianc/node-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple-query-tests.js
More file actions
91 lines (77 loc) · 2.87 KB
/
simple-query-tests.js
File metadata and controls
91 lines (77 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
'use strict'
const helper = require('./test-helper')
const Query = helper.pg.Query
const assert = require('assert')
const suite = new helper.Suite()
const test = suite.test.bind(suite)
// before running this test make sure you run the script create-test-tables
test('simple query interface', function () {
const client = helper.client()
const query = client.query(new Query('select name from person order by name collate "C"'))
client.on('drain', client.end.bind(client))
const rows = []
query.on('row', function (row, result) {
assert.ok(result)
rows.push(row['name'])
})
query.once('row', function (row) {
test('returned right columns', function () {
assert.deepStrictEqual(row, { name: row.name })
})
})
assert.emits(query, 'end', function () {
test('returned right number of rows', function () {
assert.lengthIs(rows, 26)
})
test('row ordering', function () {
assert.equal(rows[0], 'Aaron')
assert.equal(rows[25], 'Zanzabar')
})
})
})
test('prepared statements do not mutate params', function () {
const client = helper.client()
const params = [1]
const query = client.query(new Query('select name from person where $1 = 1 order by name collate "C"', params))
assert.deepEqual(params, [1])
client.on('drain', client.end.bind(client))
const rows = []
query.on('row', function (row, result) {
assert.ok(result)
rows.push(row)
})
query.on('end', function (result) {
assert.lengthIs(rows, 26, 'result returned wrong number of rows')
assert.lengthIs(rows, result.rowCount)
assert.equal(rows[0].name, 'Aaron')
assert.equal(rows[25].name, 'Zanzabar')
})
})
test('multiple simple queries', function () {
const client = helper.client()
client.query({ text: "create temp table bang(id serial, name varchar(5));insert into bang(name) VALUES('boom');" })
client.query("insert into bang(name) VALUES ('yes');")
const query = client.query(new Query('select name from bang'))
assert.emits(query, 'row', function (row) {
assert.equal(row['name'], 'boom')
assert.emits(query, 'row', function (row) {
assert.equal(row['name'], 'yes')
})
})
client.on('drain', client.end.bind(client))
})
test('multiple select statements', function () {
const client = helper.client()
client.query(
'create temp table boom(age integer); insert into boom(age) values(1); insert into boom(age) values(2); insert into boom(age) values(3)'
)
client.query({ text: "create temp table bang(name varchar(5)); insert into bang(name) values('zoom');" })
const result = client.query(new Query({ text: 'select age from boom where age < 2; select name from bang' }))
assert.emits(result, 'row', function (row) {
assert.strictEqual(row['age'], 1)
assert.emits(result, 'row', function (row) {
assert.strictEqual(row['name'], 'zoom')
})
})
client.on('drain', client.end.bind(client))
})