forked from Suor/pg-bricks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
103 lines (85 loc) · 2.88 KB
/
test.js
File metadata and controls
103 lines (85 loc) · 2.88 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
var assert = require('assert');
var pf = require('point-free');
var pg = require('./index').configure('postgres://postgres@localhost/pg_bricks');
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.query.bind(pgsu, 'drop database if exists pg_bricks', []),
pgsu.query.bind(pgsu, 'create database pg_bricks', []),
pg.query.bind(pg, 'create table item (id serial, title text, price int)', []),
pg.insert('item', INITIAL).run
)(function (err, res) {
done(err);
})
})
it('should run query', function (done) {
pg.query('select 42 as x', [], function (err, res) {
assert.ifError(err);
assert.equal(res.command, 'SELECT');
assert.deepEqual(res.rows, [{x: 42}]);
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)
})
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 return EventEmitter', function (done) {
var query = pg.select('title', 'price').from('item').where({price: 10}).run();
query.on('row', function (row) {
assert.deepEqual(row, {"title": "apple", "price": 10})
});
query.on('end', function () {
done();
})
})
it('should pipe', function (done) {
var query = pg.query('select title, price from item where price = 10');
var store = new StoreStream();
query.pipe(store);
query.on('end', function () {
assert.deepEqual(store._store, [{"title": "apple", "price": 10}])
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);
};