Skip to content

Commit b778f2b

Browse files
committed
utils-tests: add unit tests for prepareValue
1 parent 5c49a4a commit b778f2b

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

test/test-helper.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,25 @@ var Sink = function(expected, timeout, callback) {
222222
}
223223
}
224224

225+
var getTimezoneOffset = Date.prototype.getTimezoneOffset;
226+
227+
var setTimezoneOffset = function(minutesOffset) {
228+
Date.prototype.getTimezoneOffset = function () { return minutesOffset; };
229+
}
230+
231+
var resetTimezoneOffset = function() {
232+
Date.prototype.getTimezoneOffset = getTimezoneOffset;
233+
}
225234

226235
module.exports = {
227236
Sink: Sink,
228237
pg: require(__dirname + '/../lib/'),
229238
args: args,
230239
config: args,
231240
sys: sys,
232-
Client: Client
241+
Client: Client,
242+
setTimezoneOffset: setTimezoneOffset,
243+
resetTimezoneOffset: resetTimezoneOffset
233244
};
234245

235246

test/unit/utils-tests.js

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require(__dirname + '/test-helper');
1+
var helper = require(__dirname + '/test-helper');
22
var utils = require(__dirname + "/../../lib/utils");
33
var defaults = require(__dirname + "/../../lib").defaults;
44

@@ -48,3 +48,59 @@ test('normalizing query configs', function() {
4848
config = utils.normalizeQueryConfig({text: 'TEXT', values: [10]}, callback)
4949
assert.deepEqual(config, {text: 'TEXT', values: [10], callback: callback})
5050
})
51+
52+
test('prepareValues: buffer prepared properly', function() {
53+
var buf = new Buffer("quack");
54+
var out = utils.prepareValue(buf);
55+
assert.strictEqual(buf, out);
56+
});
57+
58+
test('prepareValues: date prepared properly', function() {
59+
helper.setTimezoneOffset(-330);
60+
61+
var date = new Date(2014, 1, 1, 11, 11, 1, 7);
62+
var out = utils.prepareValue(date);
63+
assert.strictEqual(out, "2014-02-01T11:11:01.007+05:30");
64+
65+
helper.resetTimezoneOffset();
66+
});
67+
68+
test('prepareValues: undefined prepared properly', function() {
69+
var out = utils.prepareValue(void 0);
70+
assert.strictEqual(out, null);
71+
});
72+
73+
test('prepareValue: null prepared properly', function() {
74+
var out = utils.prepareValue(null);
75+
assert.strictEqual(out, null);
76+
});
77+
78+
test('prepareValue: true prepared properly', function() {
79+
var out = utils.prepareValue(true);
80+
assert.strictEqual(out, 'true');
81+
});
82+
83+
test('prepareValue: false prepared properly', function() {
84+
var out = utils.prepareValue(false);
85+
assert.strictEqual(out, 'false');
86+
});
87+
88+
test('prepareValue: number prepared properly', function () {
89+
var out = utils.prepareValue(3.042);
90+
assert.strictEqual(out, '3.042');
91+
});
92+
93+
test('prepareValue: string prepared properly', function() {
94+
var out = utils.prepareValue('big bad wolf');
95+
assert.strictEqual(out, 'big bad wolf');
96+
});
97+
98+
test('prepareValue: array prepared properly', function() {
99+
var out = utils.prepareValue([1, null, 3, undefined, [5, 6, "squ,awk"]]);
100+
assert.strictEqual(out, '{1,NULL,3,NULL,{5,6,"squ,awk"}}');
101+
});
102+
103+
test('prepareValue: arbitrary objects prepared properly', function() {
104+
var out = utils.prepareValue({ x: 42 });
105+
assert.strictEqual(out, '{"x":42}');
106+
});

0 commit comments

Comments
 (0)