range.test.js
10 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
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
'use strict';
const chai = require('chai'),
expect = chai.expect,
Support = require('../../support'),
DataTypes = require('../../../../lib/data-types'),
dialect = Support.getTestDialect(),
range = require('../../../../lib/dialects/postgres/range');
if (dialect.match(/^postgres/)) {
// Don't try to load pg until we know we're running on postgres.
const pg = require('pg');
describe('[POSTGRES Specific] range datatype', () => {
describe('stringify', () => {
it('should handle empty objects correctly', () => {
expect(range.stringify([])).to.equal('empty');
});
it('should handle null as empty bound', () => {
expect(range.stringify([null, 1])).to.equal('[,1)');
expect(range.stringify([1, null])).to.equal('[1,)');
expect(range.stringify([null, null])).to.equal('[,)');
});
it('should handle Infinity/-Infinity as infinity/-infinity bounds', () => {
expect(range.stringify([Infinity, 1])).to.equal('[infinity,1)');
expect(range.stringify([1, Infinity])).to.equal('[1,infinity)');
expect(range.stringify([-Infinity, 1])).to.equal('[-infinity,1)');
expect(range.stringify([1, -Infinity])).to.equal('[1,-infinity)');
expect(range.stringify([-Infinity, Infinity])).to.equal('[-infinity,infinity)');
});
it('should throw error when array length is no 0 or 2', () => {
expect(() => { range.stringify([1]); }).to.throw();
expect(() => { range.stringify([1, 2, 3]); }).to.throw();
});
it('should throw error when non-array parameter is passed', () => {
expect(() => { range.stringify({}); }).to.throw();
expect(() => { range.stringify('test'); }).to.throw();
expect(() => { range.stringify(undefined); }).to.throw();
});
it('should handle array of objects with `inclusive` and `value` properties', () => {
expect(range.stringify([{ inclusive: true, value: 0 }, { value: 1 }])).to.equal('[0,1)');
expect(range.stringify([{ inclusive: true, value: 0 }, { inclusive: true, value: 1 }])).to.equal('[0,1]');
expect(range.stringify([{ inclusive: false, value: 0 }, 1])).to.equal('(0,1)');
expect(range.stringify([0, { inclusive: true, value: 1 }])).to.equal('[0,1]');
});
it('should handle date values', () => {
const Range = new DataTypes.postgres.RANGE(DataTypes.DATE);
expect(Range.stringify([new Date(Date.UTC(2000, 1, 1)),
new Date(Date.UTC(2000, 1, 2))], { timezone: '+02:00' })).to.equal('\'["2000-02-01 02:00:00.000 +02:00","2000-02-02 02:00:00.000 +02:00")\'');
});
});
describe('stringify value', () => {
it('should stringify integer values with appropriate casting', () => {
const Range = new DataTypes.postgres.RANGE(DataTypes.INTEGER);
expect(Range.stringify(1)).to.equal('\'1\'::int4');
});
it('should stringify bigint values with appropriate casting', () => {
const Range = new DataTypes.postgres.RANGE(DataTypes.BIGINT);
expect(Range.stringify(1)).to.equal('\'1\'::int8');
});
it('should stringify numeric values with appropriate casting', () => {
const Range = new DataTypes.postgres.RANGE(DataTypes.DECIMAL);
expect(Range.stringify(1.1)).to.equal('\'1.1\'::numeric');
});
it('should stringify dateonly values with appropriate casting', () => {
const Range = new DataTypes.postgres.RANGE(DataTypes.DATEONLY);
expect(Range.stringify(new Date(Date.UTC(2000, 1, 1)))).to.include('::date');
});
it('should stringify date values with appropriate casting', () => {
const Range = new DataTypes.postgres.RANGE(DataTypes.DATE);
expect(Range.stringify(new Date(Date.UTC(2000, 1, 1)), { timezone: '+02:00' })).to.equal('\'2000-02-01 02:00:00.000 +02:00\'::timestamptz');
});
describe('with null range bounds', () => {
const infiniteRange = [null, null];
const infiniteRangeSQL = "'[,)'";
it('should stringify integer range to infinite range', () => {
const Range = new DataTypes.postgres.RANGE(DataTypes.INTEGER);
expect(Range.stringify(infiniteRange)).to.equal(infiniteRangeSQL);
});
it('should stringify bigint range to infinite range', () => {
const Range = new DataTypes.postgres.RANGE(DataTypes.BIGINT);
expect(Range.stringify(infiniteRange)).to.equal(infiniteRangeSQL);
});
it('should stringify numeric range to infinite range', () => {
const Range = new DataTypes.postgres.RANGE(DataTypes.DECIMAL);
expect(Range.stringify(infiniteRange)).to.equal(infiniteRangeSQL);
});
it('should stringify dateonly ranges appropriately', () => {
const Range = new DataTypes.postgres.RANGE(DataTypes.DATEONLY);
expect(Range.stringify(infiniteRange)).to.equal(infiniteRangeSQL);
});
it('should be stringified to appropriate unbounded postgres range', () => {
const Range = new DataTypes.postgres.RANGE(DataTypes.DATEONLY);
expect(Range.stringify(infiniteRange)).to.equal(infiniteRangeSQL);
});
it('should stringify date values with appropriate casting', () => {
const Range = new DataTypes.postgres.RANGE(DataTypes.DATE);
expect(Range.stringify(infiniteRange, { timezone: '+02:00' })).to.equal(infiniteRangeSQL);
});
});
describe('with infinite range bounds', () => {
const infiniteRange = [-Infinity, Infinity];
const infiniteRangeSQL = "'[-infinity,infinity)'";
it('should stringify integer range to infinite range', () => {
const Range = new DataTypes.postgres.RANGE(DataTypes.INTEGER);
expect(Range.stringify(infiniteRange)).to.equal(infiniteRangeSQL);
});
it('should stringify bigint range to infinite range', () => {
const Range = new DataTypes.postgres.RANGE(DataTypes.BIGINT);
expect(Range.stringify(infiniteRange)).to.equal(infiniteRangeSQL);
});
it('should stringify numeric range to infinite range', () => {
const Range = new DataTypes.postgres.RANGE(DataTypes.DECIMAL);
expect(Range.stringify(infiniteRange)).to.equal(infiniteRangeSQL);
});
it('should stringify dateonly ranges appropriately', () => {
const Range = new DataTypes.postgres.RANGE(DataTypes.DATEONLY);
expect(Range.stringify(infiniteRange)).to.equal(infiniteRangeSQL);
});
it('should be stringified to appropriate unbounded postgres range', () => {
const Range = new DataTypes.postgres.RANGE(DataTypes.DATEONLY);
expect(Range.stringify(infiniteRange)).to.equal(infiniteRangeSQL);
});
it('should stringify date values with appropriate casting', () => {
const Range = new DataTypes.postgres.RANGE(DataTypes.DATE);
expect(Range.stringify(infiniteRange, { timezone: '+02:00' })).to.equal(infiniteRangeSQL);
});
});
});
describe('parse', () => {
it('should handle a null object correctly', () => {
expect(range.parse(null)).to.equal(null);
});
it('should handle empty range string correctly', () => {
expect(range.parse('empty')).to.deep.equal([]);
});
it('should handle empty bounds correctly', () => {
expect(range.parse('(1,)', DataTypes.postgres.INTEGER.parse)).to.deep.equal([{ value: 1, inclusive: false }, { value: null, inclusive: false }]);
expect(range.parse('(,1)', DataTypes.postgres.INTEGER.parse)).to.deep.equal([{ value: null, inclusive: false }, { value: 1, inclusive: false }]);
expect(range.parse('(,)', DataTypes.postgres.INTEGER.parse)).to.deep.equal([{ value: null, inclusive: false }, { value: null, inclusive: false }]);
});
it('should handle infinity/-infinity bounds correctly', () => {
expect(range.parse('(infinity,1)', DataTypes.postgres.INTEGER.parse)).to.deep.equal([{ value: Infinity, inclusive: false }, { value: 1, inclusive: false }]);
expect(range.parse('(1,infinity)', DataTypes.postgres.INTEGER.parse)).to.deep.equal([{ value: 1, inclusive: false }, { value: Infinity, inclusive: false }]);
expect(range.parse('(-infinity,1)', DataTypes.postgres.INTEGER.parse)).to.deep.equal([{ value: -Infinity, inclusive: false }, { value: 1, inclusive: false }]);
expect(range.parse('(1,-infinity)', DataTypes.postgres.INTEGER.parse)).to.deep.equal([{ value: 1, inclusive: false }, { value: -Infinity, inclusive: false }]);
expect(range.parse('(-infinity,infinity)', DataTypes.postgres.INTEGER.parse)).to.deep.equal([{ value: -Infinity, inclusive: false }, { value: Infinity, inclusive: false }]);
});
it('should return raw value if not range is returned', () => {
expect(range.parse('some_non_array')).to.deep.equal('some_non_array');
});
it('should handle native postgres timestamp format', async () => {
// Make sure nameOidMap is loaded
const connection = await Support.sequelize.connectionManager.getConnection();
Support.sequelize.connectionManager.releaseConnection(connection);
const tsName = DataTypes.postgres.DATE.types.postgres[0],
tsOid = Support.sequelize.connectionManager.nameOidMap[tsName].oid,
parser = pg.types.getTypeParser(tsOid);
expect(range.parse('(2016-01-01 08:00:00-04,)', parser)[0].value.toISOString()).to.equal('2016-01-01T12:00:00.000Z');
});
});
describe('stringify and parse', () => {
it('should stringify then parse back the same structure', () => {
const testRange = [{ value: 5, inclusive: true }, { value: 10, inclusive: true }];
const Range = new DataTypes.postgres.RANGE(DataTypes.INTEGER);
let stringified = Range.stringify(testRange, {});
stringified = stringified.substr(1, stringified.length - 2); // Remove the escaping ticks
expect(DataTypes.postgres.RANGE.parse(stringified, { parser: DataTypes.postgres.INTEGER.parse })).to.deep.equal(testRange);
});
});
});
}