forked from brianc/node-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype-coercion-tests.js
216 lines (195 loc) · 6.26 KB
/
type-coercion-tests.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
var helper = require(__dirname + '/test-helper');
var sink;
var testForTypeCoercion = function(type){
helper.pg.connect(helper.config, function(err, client, done) {
assert.isNull(err);
client.query("create temp table test_type(col " + type.name + ")", assert.calls(function(err, result) {
assert.isNull(err);
test("Coerces " + type.name, function() {
type.values.forEach(function(val) {
var insertQuery = client.query('insert into test_type(col) VALUES($1)',[val],assert.calls(function(err, result) {
assert.isNull(err);
}));
var query = client.query({
name: 'get type ' + type.name ,
text: 'select col from test_type'
});
query.on('error', function(err) {
console.log(err);
throw err;
});
assert.emits(query, 'row', function(row) {
var expected = val + " (" + typeof val + ")";
var returned = row.col + " (" + typeof row.col + ")";
assert.strictEqual(row.col, val, "expected " + type.name + " of " + expected + " but got " + returned);
}, "row should have been called for " + type.name + " of " + val);
client.query('delete from test_type');
});
client.query('drop table test_type', function() {
sink.add();
done();
});
})
}));
})
};
var types = [{
name: 'integer',
values: [-2147483648, -1, 0, 1, 2147483647, null]
},{
name: 'smallint',
values: [-32768, -1, 0, 1, 32767, null]
},{
name: 'bigint',
values: [
'-9223372036854775808',
'-9007199254740992',
'0',
'9007199254740992',
'72057594037928030',
'9223372036854775807',
null
]
},{
name: 'varchar(5)',
values: ['yo', '', 'zomg!', null]
},{
name: 'oid',
values: [0, 204410, null]
},{
name: 'bool',
values: [true, false, null]
},{
name: 'numeric',
values: [
'-12.34',
'0',
'12.34',
'-3141592653589793238462643383279502.1618033988749894848204586834365638',
'3141592653589793238462643383279502.1618033988749894848204586834365638',
null
]
},{
name: 'real',
values: [-101.3, -1.2, 0, 1.2, 101.1, null]
},{
name: 'double precision',
values: [-101.3, -1.2, 0, 1.2, 101.1, null]
},{
name: 'timestamptz',
values: [null]
},{
name: 'timestamp',
values: [null]
},{
name: 'timetz',
values: ['13:11:12.1234-05:30',null]
},{
name: 'time',
values: ['13:12:12.321', null]
}];
// ignore some tests in binary mode
if (helper.config.binary) {
types = types.filter(function(type) {
return !(type.name in {'real': 1, 'timetz':1, 'time':1, 'numeric': 1, 'bigint': 1});
});
}
var valueCount = 0;
types.forEach(function(type) {
valueCount += type.values.length;
})
sink = new helper.Sink(types.length + 1, function() {
helper.pg.end();
})
types.forEach(function(type) {
testForTypeCoercion(type)
});
test("timestampz round trip", function() {
var now = new Date();
var client = helper.client();
client.on('error', function(err) {
console.log(err);
client.end();
});
client.query("create temp table date_tests(name varchar(10), tstz timestamptz(3))");
client.query({
text: "insert into date_tests(name, tstz)VALUES($1, $2)",
name: 'add date',
values: ['now', now]
});
var result = client.query({
name: 'get date',
text: 'select * from date_tests where name = $1',
values: ['now']
});
assert.emits(result, 'row', function(row) {
var date = row.tstz;
assert.equal(date.getYear(),now.getYear());
assert.equal(date.getMonth(), now.getMonth());
assert.equal(date.getDate(), now.getDate());
assert.equal(date.getHours(), now.getHours());
assert.equal(date.getMinutes(), now.getMinutes());
assert.equal(date.getSeconds(), now.getSeconds());
test("milliseconds are equal", function() {
assert.equal(date.getMilliseconds(), now.getMilliseconds());
});
});
client.on('drain', client.end.bind(client));
});
if(!helper.config.binary) {
test('date range extremes', function() {
var client = helper.client();
client.on('error', function(err) {
console.log(err);
client.end();
});
// Set teh server timeszone to the same as used for the test,
// otherwise (if server's timezone is ahead of GMT) in
// textParsers.js::parseDate() the timezone offest is added to the date;
// in the case of "275760-09-13 00:00:00 GMT" the timevalue overflows.
client.query('SET TIMEZONE TO GMT', [], assert.success(function(res){
// PostgreSQL supports date range of 4713 BCE to 294276 CE
// http://www.postgresql.org/docs/9.2/static/datatype-datetime.html
// ECMAScript supports date range of Apr 20 271821 BCE to Sep 13 275760 CE
// http://ecma-international.org/ecma-262/5.1/#sec-15.9.1.1
client.query('SELECT $1::TIMESTAMPTZ as when', ["275760-09-13 00:00:00 GMT"], assert.success(function(res) {
assert.equal(res.rows[0].when.getFullYear(), 275760);
}));
client.query('SELECT $1::TIMESTAMPTZ as when', ["4713-12-31 12:31:59 BC GMT"], assert.success(function(res) {
assert.equal(res.rows[0].when.getFullYear(), -4713);
}));
client.query('SELECT $1::TIMESTAMPTZ as when', ["275760-09-13 00:00:00 -15:00"], assert.success(function(res) {
assert( isNaN(res.rows[0].when.getTime()) );
}));
client.on('drain', client.end.bind(client));
}));
});
}
helper.pg.connect(helper.config, assert.calls(function(err, client, done) {
assert.isNull(err);
client.query('select null as res;', assert.calls(function(err, res) {
assert.isNull(err);
assert.strictEqual(res.rows[0].res, null)
}))
client.query('select 7 <> $1 as res;',[null], function(err, res) {
assert.isNull(err);
assert.strictEqual(res.rows[0].res, null);
sink.add();
done();
})
}))
if(!helper.config.binary) {
test("postgres date type", function() {
var client = helper.client();
var testDate = new Date (2010, 9, 31);
client.on('error', function(err) {
console.log(err);
client.end();
});
client.query("SELECT $1::date", [testDate], assert.calls(function(err, result){
assert.isNull(err);
assert.strictEqual(result.rows[0].date.toString(), testDate.toString());
}));
client.on('drain', client.end.bind(client));
});
}