-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtest.js
273 lines (240 loc) · 9.2 KB
/
test.js
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
var assert = require('assert');
var pf = require('point-free');
var pg = require('./index').configure('postgres://postgres@localhost/pg_bricks');
if (process.env.PGBRICKS_TEST_NATIVE) {
pg = pg.native;
}
var INITIAL = [
{title: 'apple', price: 10},
{title: 'orange', price: 20},
]
describe('pg-bricks', function () {
before(function (done) {
// Create test database and fill a test data
var pgsu = require('./index').configure('postgres://postgres@localhost/postgres');
pf.serial(
pgsu.raw('drop database if exists pg_bricks').run,
pgsu.raw('create database pg_bricks').run,
pg.raw('create table item (id serial, title text, price int)').run,
pg.insert('item', INITIAL).run
)(function (err, res) {
done(err);
})
})
it('should run query', function (done) {
pg.raw('select 42 as x').run(function (err, res) {
assert.ifError(err);
assert.equal(res.command, 'SELECT');
assert.deepEqual(res.rows, [{x: 42}]);
done();
})
})
it('should run query from client', function (done) {
pg.run(function (client, callback) {
client.raw('select 42 as x').run(function (err, res) {
assert.ifError(err);
assert.deepEqual(res.rows, [{x: 42}]);
done();
})
}, done)
})
it('should support sql-bricks', function (done) {
pf.waterfall(
pg.select('title', 'price').from('item').run,
function (res, callback) {
assert.deepEqual(res.rows, INITIAL);
done();
}
)(done)
})
describe('Accessors', function () {
it('should provide .rows', function (done) {
pf.waterfall(
pg.select('title', 'price').from('item').rows,
function (rows, callback) {
assert.deepEqual(rows, INITIAL);
done();
}
)(done)
})
it('should provide .col', function (done) {
pf.waterfall(
pg.select('title').from('item').col,
function (col, callback) {
assert.deepEqual(col, ['apple', 'orange']);
done();
}
)(done)
})
it('should provide .val', function (done) {
pf.waterfall(
pg.select('price').from('item').where({title: 'apple'}).val,
function (price, callback) {
assert.equal(price, 10);
done();
}
)(done)
})
it('should provide .val on .raw', function (done) {
pf.waterfall(
pg.raw('select price from item where title = $1', ['apple']).val,
function (price, callback) {
assert.equal(price, 10);
done();
}
)(done)
})
})
describe('Promises', function () {
it('should return promise', function () {
var res = pg.raw('select 42 as x').run();
assert(res instanceof Promise);
})
it('should work with raw', function () {
return pg.raw('select 42 as x').run().then(function (data) {
assert.deepEqual(data.rows, [{x: 42}])
})
})
it('should support sql-bricks', function () {
return pg.raw('select 42 as x').run().then(function (data) {
assert.deepEqual(data.rows, [{x: 42}])
})
})
it('should work with accessors', function () {
return pg.select('title,price').from('item').where({title: 'apple'}).row()
.then(function (row) {
assert.deepEqual(row, {title: 'apple', price: 10});
})
})
})
describe('Enclosures', function () {
it('should manage client', function (done) {
var idle = pg._pool.idleCount;
pg.run(function (client, callback) {
assert.equal(pg._pool.idleCount, idle - 1);
callback()
}, function (err) {
assert.equal(pg._pool.idleCount, idle);
done(err);
})
})
it('should wrap in transaction', function (done) {
pg.transaction(function (client, callback) {
pf.serial(
client.update('item', {price: 42}).where('title', 'apple').run,
// Check that change is not visible outside yet
pf.waterfall(
pg.select('price').from('item').where('title', 'apple').val,
function (price, callback) {
assert.equal(price, 10);
callback()
}
),
// Return price back to not screw up remaining tests
client.update('item', {price: 10}).where('title', 'apple').run
)(callback)
}, done)
})
it('should rollback on error', function (done) {
pg.transaction(function (client, callback) {
pf.serial(
client.update('item', {price: 42}).where('title', 'apple').run,
function (callback) { callback(new Error('Intended rollback')) }
)(callback)
}, function (err) {
assert.equal(err.message, 'Intended rollback')
pg.select('price').from('item').where('title', 'apple').val(
function (err, price) {
assert.equal(price, 10);
done();
})
})
})
it('should work with promises', function () {
var idle = pg._pool.idleCount;
return pg.run(function (client) {
assert.equal(pg._pool.idleCount, idle - 1);
return client.raw('select 42 as x').val();
}).then(function (val) {
assert.equal(val, 42);
assert.equal(pg._pool.idleCount, idle);
})
})
it('should transact with promises', function () {
return pg.transaction(function (client) {
return client.raw('select 42 as x').val();
}).then(function (val) {
assert.equal(val, 42);
})
})
})
var usingNative = process.env.PGBRICKS_TEST_NATIVE || process.env.NODE_PG_FORCE_NATIVE;
(usingNative ? describe.skip : describe)('Streaming', function () {
it('should return EventEmitter', function (done) {
var query = pg.select('title', 'price').from('item').where({price: 10}).stream();
query.on('error', done);
query.on('data', function (row) {
assert.deepEqual(row, {"title": "apple", "price": 10})
});
query.on('end', function () {
done();
})
})
it('should pipe', function () {
var query = pg.raw('select title, price from item where price = $1', [10]).stream();
return slurp(query).then(function (data) {
assert.deepEqual(data, [{"title": "apple", "price": 10}])
})
})
it('should pipe from client', function (done) {
pg.run(function (client, callback) {
var query = client.raw('select title, price from item where price = 10').stream();
slurp(query).then(function (data) {
assert.deepEqual(data, [{"title": "apple", "price": 10}])
done();
}).catch(done);
}, done)
})
it('should error out', function (done) {
var stream = pg.raw('select no_col from item').stream();
stream.on('error', function (err) {
assert.equal(err.message, 'column "no_col" does not exist')
done()
})
stream.on('data', function () {
throw Error('Unexpected data from broken query')
})
})
it('should error out from client', function (done) {
pg.run(function (client, callback) {
var stream = client.raw('select no_col from item').stream();
stream.on('error', function (err) {
assert.equal(err.message, 'column "no_col" does not exist')
callback()
})
stream.on('data', function () {
throw Error('Unexpected data from broken query')
})
}, done)
})
})
})
// Helper stream
var stream = require('stream')
var util = require('util');
util.inherits(StoreStream, stream.Writable);
function StoreStream(options) {
stream.Writable.call(this, options);
this._store = [];
}
StoreStream.prototype.write = function (chunk, encoding, callback) {
this._store.push(chunk);
};
function slurp(stream) {
var store = new StoreStream();
stream.pipe(store);
return new Promise(function (resolve, reject) {
stream.on('error', reject)
stream.on('end', function () {resolve(store._store)})
})
}