-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathsimple-query-tests.js
More file actions
156 lines (135 loc) · 4.5 KB
/
simple-query-tests.js
File metadata and controls
156 lines (135 loc) · 4.5 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
'use strict'
const helper = require('./test-helper')
const Query = require('../../../lib/query')
const assert = require('assert')
const suite = new helper.Suite()
const test = suite.test.bind(suite)
test('executing query', function () {
test('queing query', function () {
test('when connection is ready', function () {
const client = helper.client()
assert.empty(client.connection.queries)
client.connection.emit('readyForQuery')
client.query('yes')
assert.lengthIs(client.connection.queries, 1)
assert.equal(client.connection.queries, 'yes')
})
test('when connection is not ready', function () {
const client = helper.client()
test('query is not sent', function () {
client.query('boom')
assert.empty(client.connection.queries)
})
test('sends query to connection once ready', function () {
assert.ok(client.connection.emit('readyForQuery'))
assert.lengthIs(client.connection.queries, 1)
assert.equal(client.connection.queries[0], 'boom')
})
})
test('multiple in the queue', function () {
const client = helper.client()
const connection = client.connection
const queries = connection.queries
client.query('one')
client.query('two')
client.query('three')
assert.empty(queries)
test('after one ready for query', function () {
connection.emit('readyForQuery')
assert.lengthIs(queries, 1)
assert.equal(queries[0], 'one')
})
test('after two ready for query', function () {
connection.emit('readyForQuery')
assert.lengthIs(queries, 2)
})
test('after a bunch more', function () {
connection.emit('readyForQuery')
connection.emit('readyForQuery')
connection.emit('readyForQuery')
assert.lengthIs(queries, 3)
assert.equal(queries[0], 'one')
assert.equal(queries[1], 'two')
assert.equal(queries[2], 'three')
})
})
})
test('query event binding and flow', function () {
const client = helper.client()
const con = client.connection
const query = client.query(new Query('whatever'))
test('has no queries sent before ready', function () {
assert.empty(con.queries)
})
test('sends query on readyForQuery event', function () {
con.emit('readyForQuery')
assert.lengthIs(con.queries, 1)
assert.equal(con.queries[0], 'whatever')
})
test('handles rowDescription message', function () {
const handled = con.emit('rowDescription', {
fields: [
{
name: 'boom',
},
],
})
assert.ok(handled, 'should have handlded rowDescription')
})
test('handles dataRow messages', function () {
assert.emits(query, 'row', function (row) {
assert.equal(row['boom'], 'hi')
})
const handled = con.emit('dataRow', { fields: ['hi'] })
assert.ok(handled, 'should have handled first data row message')
assert.emits(query, 'row', function (row) {
assert.equal(row['boom'], 'bye')
})
const handledAgain = con.emit('dataRow', { fields: ['bye'] })
assert.ok(handledAgain, 'should have handled seciond data row message')
})
// multiple command complete messages will be sent
// when multiple queries are in a simple command
test('handles command complete messages', function () {
con.emit('commandComplete', {
text: 'INSERT 31 1',
})
})
})
test('handles errors', function () {
const client = helper.client()
test('throws an error when config is null', function () {
try {
client.query(null, undefined)
} catch (error) {
assert.equal(
error.message,
'Client was passed a null or undefined query',
'Should have thrown an Error for null queries'
)
}
})
test('throws an error when config is undefined', function () {
try {
client.query()
} catch (error) {
assert.equal(
error.message,
'Client was passed a null or undefined query',
'Should have thrown an Error for null queries'
)
}
})
test('throws an error when callback is not a function', function () {
try {
client.query('SELECT $1', [1], 'notafunction')
} catch (error) {
assert.equal(
error.message,
'callback is not a function',
'Should have thrown an Error for non function callback'
)
}
})
})
})