-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
118 lines (97 loc) · 3.05 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
var pgCamelCase = require('./index');
var assert = require('assert');
//------------------------------------------------------------
function getMockedPg() {
function Query() {}
Query.prototype.handleRowDescription = function(input){
return input;
};
return { Query : Query };
}
//------------------------------------------------------------
describe('camelCase()', function(){
var fix = require('./fixtures/snake2camel.json');
it('should camel-case', function(){
fix.forEach(function(mapping){
var input = mapping[0];
var expected = mapping[1];
assert.strictEqual(expected, pgCamelCase.camelCase(input));
});
});
});
//------------------------------------------------------------
describe('inject()', function(){
var input = {fields : [
{ name : 'some_snake_case' } ,
{ name : 'no snake case' } ,
{ name : 'someCamelCase' }
]};
var after = {fields : [
{ name : 'someSnakeCase' } ,
{ name : 'no snake case' } ,
{ name : 'someCamelCase' }
]};
it('should inject camel-casing and return proper revert-function', function(){
var pg = getMockedPg();
var callRowDesc = function(input){
var q = new pg.Query();
return q.handleRowDescription(input);
};
assert.deepEqual(input, callRowDesc(input));
var restore = pgCamelCase.inject(pg);
assert.deepEqual(after, callRowDesc(input));
restore();
assert.deepEqual(input, callRowDesc(input));
});
});
//------------------------------------------------------------
describe('test against real server', function(){
var envKey = 'TEST_PG_CONNECTION';
var pg;
if (!Object.hasOwnProperty.call(process.env, envKey)) {
return;
} else{
try { pg = require('pg'); } catch(_1) {
try { pg = require('pg.js'); } catch(_2) {
return;
}
}
}
function buildQuery(fieldNames) {
return 'select ' + fieldNames.map(function(name, idx){
return String(idx).concat(' as "').concat(name).concat('"');
}).join(', ');
}
function queryAndTest(client, stmt, expected, cb){
client.query(stmt, function(err, res){
assert.ifError(err);
var fieldNames = res.fields.map(function(field){
return field.name;
});
assert.deepEqual(fieldNames, expected);
expected.forEach(function(name, idx){
assert.strictEqual(idx, res.rows[0][name]);
});
cb();
});
}
it('should return camel-cased fields', function(testDone){
var fieldNames = [ 'some_snake_case', 'no snake case', 'someCamelCase' ];
var ccFieldNames = [ 'someSnakeCase', 'no snake case', 'someCamelCase' ];
var stmt = buildQuery(fieldNames);
pg.connect(process.env[envKey], function(err, client, clientDone){
assert.ifError(err);
var query = queryAndTest.bind(null, client, stmt);
query(fieldNames, function(){
var restore = pgCamelCase.inject(pg);
query(ccFieldNames, function() {
restore();
query(fieldNames, function(){
clientDone();
testDone();
});
});
});
});
});
});