不要怂,就是干,撸起袖子干!

Commit b66f9ddc by Michael Yates Committed by Sushant

refactor(range): use standard format & observe inclusive range bounds (#9364)

1 parent bd6fa212
......@@ -220,13 +220,6 @@ When supplying ranges as values you can choose from the following APIs:
Timeline.create({ range: [new Date(Date.UTC(2016, 0, 1)), new Date(Date.UTC(2016, 1, 1))] });
// control inclusion
const range = [new Date(Date.UTC(2016, 0, 1)), new Date(Date.UTC(2016, 1, 1))];
range.inclusive = false; // '()'
range.inclusive = [false, true]; // '(]'
range.inclusive = true; // '[]'
range.inclusive = [true, false]; // '[)'
// or as a single expression
const range = [
{ value: new Date(Date.UTC(2016, 0, 1)), inclusive: false },
{ value: new Date(Date.UTC(2016, 1, 1)), inclusive: true },
......@@ -248,12 +241,10 @@ receive:
```js
// stored value: ("2016-01-01 00:00:00+00:00", "2016-02-01 00:00:00+00:00"]
range // [Date, Date]
range.inclusive // [false, true]
range // [{ value: Date, inclusive: false }, { value: Date, inclusive: true }]
```
Make sure you turn that into a serializable format before serialization since array
extra properties will not be serialized.
You will need to call reload after updating an instance with a range type or use `returning: true` option.
**Special Cases**
......
......@@ -571,10 +571,6 @@ RANGE.prototype.toCastType = function toCastType() {
return pgRangeCastTypes[this._subtype.toLowerCase()];
};
RANGE.prototype.validate = function validate(value) {
if (_.isPlainObject(value) && value.inclusive) {
value = value.inclusive;
}
if (!_.isArray(value)) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid range', value));
}
......
......@@ -538,7 +538,15 @@ module.exports = BaseTypes => {
return "'" + this.options.subtype.stringify(values, options) + "'::" +
this.toCastType();
}
const valuesStringified = values.map(value => {
const valueInclusivity = [true, false];
const valuesStringified = values.map((value, index) => {
if (_.isObject(value) && value.hasOwnProperty('value')) {
if (value.hasOwnProperty('inclusive')) {
valueInclusivity[index] = value.inclusive;
}
value = value.value;
}
if (_.includes([null, -Infinity, Infinity], value)) {
// Pass through "unbounded" bounds unchanged
return value;
......@@ -550,7 +558,7 @@ module.exports = BaseTypes => {
});
// Array.map does not preserve extra array properties
valuesStringified.inclusive = values.inclusive;
valuesStringified.inclusive = valueInclusivity;
return '\'' + range.stringify(valuesStringified) + '\'';
};
......
......@@ -56,9 +56,7 @@ exports.stringify = stringify;
function parse(value, parser) {
if (value === null) return null;
if (value === 'empty') {
const empty = [];
empty.inclusive = [];
return empty;
return [];
}
let result = value
......@@ -67,9 +65,12 @@ function parse(value, parser) {
if (result.length !== 2) return value;
result = result.map(value => parseRangeBound(value, parser));
result.inclusive = [value[0] === '[', value[value.length - 1] === ']'];
result = result.map((item, index) => {
return {
value: parseRangeBound(item, parser),
inclusive: index === 0 ? value[0] === '[' : value[value.length - 1] === ']'
};
});
return result;
}
......
......@@ -208,12 +208,10 @@ function mapWhereFieldNames(attributes, Model) {
}
if (Array.isArray(attributes[attribute])) {
attributes[attribute] = attributes[attribute].map(where => {
_.forEach(attributes[attribute], (where, index) => {
if (_.isPlainObject(where)) {
return mapWhereFieldNames(where, Model);
attributes[attribute][index] = mapWhereFieldNames(where, Model);
}
return where;
});
}
......
......@@ -8,6 +8,7 @@ const chai = require('chai'),
_ = require('lodash'),
moment = require('moment'),
current = Support.sequelize,
Op = current.Op,
uuid = require('uuid'),
DataTypes = require('../../lib/data-types'),
dialect = Support.getTestDialect(),
......@@ -436,8 +437,108 @@ describe(Support.getTestDialectTeaser('DataTypes'), () => {
.then(() => Model.create({ interval: [1, 4] }) )
.then(() => Model.findAll() )
.spread(m => {
expect(m.interval[0]).to.be.eql(1);
expect(m.interval[1]).to.be.eql(4);
expect(m.interval[0].value).to.be.eql(1);
expect(m.interval[1].value).to.be.eql(4);
});
});
}
if (current.dialect.supports.RANGE) {
it('should allow date ranges to be generated with default bounds inclusion #8176', function() {
const Model = this.sequelize.define('M', {
interval: {
type: Sequelize.RANGE(Sequelize.DATE),
allowNull: false,
unique: true
}
});
const testDate1 = new Date();
const testDate2 = new Date(testDate1.getTime() + 10000);
const testDateRange = [testDate1, testDate2];
return Model.sync({ force: true })
.then(() => Model.create({ interval: testDateRange }))
.then(() => Model.findOne())
.then(m => {
expect(m).to.exist;
expect(m.interval[0].value).to.be.eql(testDate1);
expect(m.interval[1].value).to.be.eql(testDate2);
expect(m.interval[0].inclusive).to.be.eql(true);
expect(m.interval[1].inclusive).to.be.eql(false);
});
});
it('should allow date ranges to be generated using a single range expression to define bounds inclusion #8176', function() {
const Model = this.sequelize.define('M', {
interval: {
type: Sequelize.RANGE(Sequelize.DATE),
allowNull: false,
unique: true
}
});
const testDate1 = new Date();
const testDate2 = new Date(testDate1.getTime() + 10000);
const testDateRange = [{ value: testDate1, inclusive: false }, { value: testDate2, inclusive: true }];
return Model.sync({ force: true })
.then(() => Model.create({ interval: testDateRange }))
.then(() => Model.findOne())
.then(m => {
expect(m).to.exist;
expect(m.interval[0].value).to.be.eql(testDate1);
expect(m.interval[1].value).to.be.eql(testDate2);
expect(m.interval[0].inclusive).to.be.eql(false);
expect(m.interval[1].inclusive).to.be.eql(true);
});
});
it('should allow date ranges to be generated using a composite range expression #8176', function() {
const Model = this.sequelize.define('M', {
interval: {
type: Sequelize.RANGE(Sequelize.DATE),
allowNull: false,
unique: true
}
});
const testDate1 = new Date();
const testDate2 = new Date(testDate1.getTime() + 10000);
const testDateRange = [testDate1, { value: testDate2, inclusive: true }];
return Model.sync({ force: true })
.then(() => Model.create({ interval: testDateRange }))
.then(() => Model.findOne())
.then(m => {
expect(m).to.exist;
expect(m.interval[0].value).to.be.eql(testDate1);
expect(m.interval[1].value).to.be.eql(testDate2);
expect(m.interval[0].inclusive).to.be.eql(true);
expect(m.interval[1].inclusive).to.be.eql(true);
});
});
it('should correctly return ranges when using predicates that define bounds inclusion #8176', function() {
const Model = this.sequelize.define('M', {
interval: {
type: Sequelize.RANGE(Sequelize.DATE),
allowNull: false,
unique: true
}
});
const testDate1 = new Date();
const testDate2 = new Date(testDate1.getTime() + 10000);
const testDateRange = [testDate1, testDate2];
const dateRangePredicate = [{ value: testDate1, inclusive: true }, { value: testDate1, inclusive: true }];
return Model.sync({ force: true })
.then(() => Model.create({ interval: testDateRange }))
.then(() => Model.findOne({
where: {
interval: { [Op.overlap]: dateRangePredicate }
}
}))
.then(m => {
expect(m).to.exist;
});
});
}
......
......@@ -764,21 +764,25 @@ if (dialect.match(/^postgres/)) {
// Check to see if the default value for a range field works
expect(newUser.acceptable_marks.length).to.equal(2);
expect(newUser.acceptable_marks[0]).to.equal('0.65'); // lower bound
expect(newUser.acceptable_marks[1]).to.equal('1'); // upper bound
expect(newUser.acceptable_marks.inclusive).to.deep.equal([true, false]); // inclusive, exclusive
expect(newUser.course_period[0] instanceof Date).to.be.ok; // lower bound
expect(newUser.course_period[1] instanceof Date).to.be.ok; // upper bound
expect(newUser.course_period[0]).to.equalTime(period[0]); // lower bound
expect(newUser.course_period[1]).to.equalTime(period[1]); // upper bound
expect(newUser.course_period.inclusive).to.deep.equal([true, false]); // inclusive, exclusive
expect(newUser.acceptable_marks[0].value).to.equal('0.65'); // lower bound
expect(newUser.acceptable_marks[1].value).to.equal('1'); // upper bound
expect(newUser.acceptable_marks[0].inclusive).to.deep.equal(true); // inclusive
expect(newUser.acceptable_marks[1].inclusive).to.deep.equal(false); // exclusive
expect(newUser.course_period[0].value instanceof Date).to.be.ok; // lower bound
expect(newUser.course_period[1].value instanceof Date).to.be.ok; // upper bound
expect(newUser.course_period[0].value).to.equalTime(period[0]); // lower bound
expect(newUser.course_period[1].value).to.equalTime(period[1]); // upper bound
expect(newUser.course_period[0].inclusive).to.deep.equal(true); // inclusive
expect(newUser.course_period[1].inclusive).to.deep.equal(false); // exclusive
// Check to see if updating a range field works
return newUser.updateAttributes({ acceptable_marks: [0.8, 0.9] }).then(() => {
expect(newUser.acceptable_marks.length).to.equal(2);
expect(newUser.acceptable_marks[0]).to.equal(0.8); // lower bound
expect(newUser.acceptable_marks[1]).to.equal(0.9); // upper bound
});
return newUser.updateAttributes({ acceptable_marks: [0.8, 0.9] })
.then(() => newUser.reload()) // Ensure the acceptable_marks array is loaded with the complete range definition
.then(() => {
expect(newUser.acceptable_marks.length).to.equal(2);
expect(newUser.acceptable_marks[0].value).to.equal('0.8'); // lower bound
expect(newUser.acceptable_marks[1].value).to.equal('0.9'); // upper bound
});
});
});
......@@ -797,15 +801,15 @@ if (dialect.match(/^postgres/)) {
return User.findById(1).then(user => {
expect(user.holidays.length).to.equal(2);
expect(user.holidays[0].length).to.equal(2);
expect(user.holidays[0][0] instanceof Date).to.be.ok;
expect(user.holidays[0][1] instanceof Date).to.be.ok;
expect(user.holidays[0][0]).to.equalTime(holidays[0][0]);
expect(user.holidays[0][1]).to.equalTime(holidays[0][1]);
expect(user.holidays[0][0].value instanceof Date).to.be.ok;
expect(user.holidays[0][1].value instanceof Date).to.be.ok;
expect(user.holidays[0][0].value).to.equalTime(holidays[0][0]);
expect(user.holidays[0][1].value).to.equalTime(holidays[0][1]);
expect(user.holidays[1].length).to.equal(2);
expect(user.holidays[1][0] instanceof Date).to.be.ok;
expect(user.holidays[1][1] instanceof Date).to.be.ok;
expect(user.holidays[1][0]).to.equalTime(holidays[1][0]);
expect(user.holidays[1][1]).to.equalTime(holidays[1][1]);
expect(user.holidays[1][0].value instanceof Date).to.be.ok;
expect(user.holidays[1][1].value instanceof Date).to.be.ok;
expect(user.holidays[1][0].value).to.equalTime(holidays[1][0]);
expect(user.holidays[1][1].value).to.equalTime(holidays[1][1]);
});
});
});
......@@ -820,11 +824,12 @@ if (dialect.match(/^postgres/)) {
course_period: period
}]).then(() => {
return User.findById(1).then(user => {
expect(user.course_period[0] instanceof Date).to.be.ok;
expect(user.course_period[1] instanceof Date).to.be.ok;
expect(user.course_period[0]).to.equalTime(period[0]); // lower bound
expect(user.course_period[1]).to.equalTime(period[1]); // upper bound
expect(user.course_period.inclusive).to.deep.equal([true, false]); // inclusive, exclusive
expect(user.course_period[0].value instanceof Date).to.be.ok;
expect(user.course_period[1].value instanceof Date).to.be.ok;
expect(user.course_period[0].value).to.equalTime(period[0]); // lower bound
expect(user.course_period[1].value).to.equalTime(period[1]); // upper bound
expect(user.course_period[0].inclusive).to.deep.equal(true); // inclusive
expect(user.course_period[1].inclusive).to.deep.equal(false); // exclusive
});
});
});
......@@ -836,25 +841,29 @@ if (dialect.match(/^postgres/)) {
return User.create({ username: 'user', email: ['foo@bar.com'], course_period: period }).then(newUser => {
// Check to see if the default value for a range field works
expect(newUser.acceptable_marks.length).to.equal(2);
expect(newUser.acceptable_marks[0]).to.equal('0.65'); // lower bound
expect(newUser.acceptable_marks[1]).to.equal('1'); // upper bound
expect(newUser.acceptable_marks.inclusive).to.deep.equal([true, false]); // inclusive, exclusive
expect(newUser.course_period[0] instanceof Date).to.be.ok;
expect(newUser.course_period[1] instanceof Date).to.be.ok;
expect(newUser.course_period[0]).to.equalTime(period[0]); // lower bound
expect(newUser.course_period[1]).to.equalTime(period[1]); // upper bound
expect(newUser.course_period.inclusive).to.deep.equal([true, false]); // inclusive, exclusive
expect(newUser.acceptable_marks[0].value).to.equal('0.65'); // lower bound
expect(newUser.acceptable_marks[1].value).to.equal('1'); // upper bound
expect(newUser.acceptable_marks[0].inclusive).to.deep.equal(true); // inclusive
expect(newUser.acceptable_marks[1].inclusive).to.deep.equal(false); // exclusive
expect(newUser.course_period[0].value instanceof Date).to.be.ok;
expect(newUser.course_period[1].value instanceof Date).to.be.ok;
expect(newUser.course_period[0].value).to.equalTime(period[0]); // lower bound
expect(newUser.course_period[1].value).to.equalTime(period[1]); // upper bound
expect(newUser.course_period[0].inclusive).to.deep.equal(true); // inclusive
expect(newUser.course_period[1].inclusive).to.deep.equal(false); // exclusive
const period2 = [new Date(2015, 1, 1), new Date(2015, 10, 30)];
// Check to see if updating a range field works
return User.update({ course_period: period2 }, { where: newUser.where() }).then(() => {
return newUser.reload().then(() => {
expect(newUser.course_period[0] instanceof Date).to.be.ok;
expect(newUser.course_period[1] instanceof Date).to.be.ok;
expect(newUser.course_period[0]).to.equalTime(period2[0]); // lower bound
expect(newUser.course_period[1]).to.equalTime(period2[1]); // upper bound
expect(newUser.course_period.inclusive).to.deep.equal([true, false]); // inclusive, exclusive
expect(newUser.course_period[0].value instanceof Date).to.be.ok;
expect(newUser.course_period[1].value instanceof Date).to.be.ok;
expect(newUser.course_period[0].value).to.equalTime(period2[0]); // lower bound
expect(newUser.course_period[1].value).to.equalTime(period2[1]); // upper bound
expect(newUser.course_period[0].inclusive).to.deep.equal(true); // inclusive
expect(newUser.course_period[1].inclusive).to.deep.equal(false); // exclusive
});
});
});
......@@ -873,11 +882,12 @@ if (dialect.match(/^postgres/)) {
return User.update({ course_period: period }, { where: oldUser.where(), returning: true })
.spread((count, users) => {
expect(count).to.equal(1);
expect(users[0].course_period[0] instanceof Date).to.be.ok;
expect(users[0].course_period[1] instanceof Date).to.be.ok;
expect(users[0].course_period[0]).to.equalTime(period[0]); // lower bound
expect(users[0].course_period[1]).to.equalTime(period[1]); // upper bound
expect(users[0].course_period.inclusive).to.deep.equal([true, false]); // inclusive, exclusive
expect(users[0].course_period[0].value instanceof Date).to.be.ok;
expect(users[0].course_period[1].value instanceof Date).to.be.ok;
expect(users[0].course_period[0].value).to.equalTime(period[0]); // lower bound
expect(users[0].course_period[1].value).to.equalTime(period[1]); // upper bound
expect(users[0].course_period[0].inclusive).to.deep.equal(true); // inclusive
expect(users[0].course_period[1].inclusive).to.deep.equal(false); // exclusive
});
});
});
......@@ -885,8 +895,7 @@ if (dialect.match(/^postgres/)) {
it('should read range correctly', function() {
const User = this.User;
const course_period = [new Date(2015, 1, 1), new Date(2015, 10, 30)];
course_period.inclusive = [false, false];
const course_period = [{ value: new Date(2015, 1, 1), inclusive: false }, { value: new Date(2015, 10, 30), inclusive: false }];
const data = { username: 'user', email: ['foo@bar.com'], course_period };
......@@ -903,13 +912,9 @@ if (dialect.match(/^postgres/)) {
it('should read range array correctly', function() {
const User = this.User;
const holidays = [
[new Date(2015, 3, 1, 10), new Date(2015, 3, 15)],
[new Date(2015, 8, 1), new Date(2015, 9, 15)]
[{ value: new Date(2015, 3, 1, 10), inclusive: true}, { value: new Date(2015, 3, 15), inclusive: true }],
[{ value: new Date(2015, 8, 1), inclusive: true }, { value: new Date(2015, 9, 15), inclusive: true }]
];
holidays[0].inclusive = [true, true];
holidays[1].inclusive = [true, true];
const data = { username: 'user', email: ['foo@bar.com'], holidays };
return User.create(data)
......@@ -938,12 +943,14 @@ if (dialect.match(/^postgres/)) {
return User.findAll({ order: ['username'] });
})
.then(users => {
expect(users[0].course_period[0]).to.equalTime(periods[0][0]); // lower bound
expect(users[0].course_period[1]).to.equalTime(periods[0][1]); // upper bound
expect(users[0].course_period.inclusive).to.deep.equal([true, false]); // inclusive, exclusive
expect(users[1].course_period[0]).to.equalTime(periods[1][0]); // lower bound
expect(users[1].course_period[1]).to.equalTime(periods[1][1]); // upper bound
expect(users[1].course_period.inclusive).to.deep.equal([true, false]); // inclusive, exclusive
expect(users[0].course_period[0].value).to.equalTime(periods[0][0]); // lower bound
expect(users[0].course_period[1].value).to.equalTime(periods[0][1]); // upper bound
expect(users[0].course_period[0].inclusive).to.deep.equal(true); // inclusive
expect(users[0].course_period[1].inclusive).to.deep.equal(false); // exclusive
expect(users[1].course_period[0].value).to.equalTime(periods[1][0]); // lower bound
expect(users[1].course_period[1].value).to.equalTime(periods[1][1]); // upper bound
expect(users[1].course_period[0].inclusive).to.deep.equal(true); // inclusive
expect(users[1].course_period[1].inclusive).to.deep.equal(false); // exclusive
});
});
......@@ -974,8 +981,8 @@ if (dialect.match(/^postgres/)) {
expect(user.hasOwnProperty('holidayDates')).to.be.ok;
expect(user.holidayDates.length).to.equal(1);
expect(user.holidayDates[0].period.length).to.equal(2);
expect(user.holidayDates[0].period[0]).to.equalTime(period[0]);
expect(user.holidayDates[0].period[1]).to.equalTime(period[1]);
expect(user.holidayDates[0].period[0].value).to.equalTime(period[0]);
expect(user.holidayDates[0].period[1].value).to.equalTime(period[1]);
});
});
});
......
......@@ -5,9 +5,8 @@ const chai = require('chai'),
Support = require(__dirname + '/../../support'),
DataTypes = require(__dirname + '/../../../../lib/data-types'),
dialect = Support.getTestDialect(),
range = require('../../../../lib/dialects/postgres/range'),
_ = require('lodash');
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');
......@@ -50,25 +49,6 @@ if (dialect.match(/^postgres/)) {
expect(range.stringify([0, { inclusive: true, value: 1 }])).to.equal('[0,1]');
});
it('should handle inclusive property of input array properly', () => {
const testRange = [1, 2];
testRange.inclusive = [true, false];
expect(range.stringify(testRange)).to.equal('[1,2)');
testRange.inclusive = [false, true];
expect(range.stringify(testRange)).to.equal('(1,2]');
testRange.inclusive = [true, true];
expect(range.stringify(testRange)).to.equal('[1,2]');
testRange.inclusive = true;
expect(range.stringify(testRange)).to.equal('[1,2]');
testRange.inclusive = false;
expect(range.stringify(testRange)).to.equal('(1,2)');
});
it('should handle date values', () => {
const Range = new DataTypes.postgres.RANGE(DataTypes.DATE);
expect(Range.stringify([new Date(Date.UTC(2000, 1, 1)),
......@@ -183,21 +163,21 @@ if (dialect.match(/^postgres/)) {
});
it('should handle empty range string correctly', () => {
expect(range.parse('empty')).to.deep.equal(_.extend([], { inclusive: [] }));
expect(range.parse('empty')).to.deep.equal([]);
});
it('should handle empty bounds correctly', () => {
expect(range.parse('(1,)', DataTypes.postgres.INTEGER.parse)).to.deep.equal(_.extend([1, null], { inclusive: [false, false] }));
expect(range.parse('(,1)', DataTypes.postgres.INTEGER.parse)).to.deep.equal(_.extend([null, 1], { inclusive: [false, false] }));
expect(range.parse('(,)', DataTypes.postgres.INTEGER.parse)).to.deep.equal(_.extend([null, null], { inclusive: [false, false] }));
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(_.extend([Infinity, 1], { inclusive: [false, false] }));
expect(range.parse('(1,infinity)', DataTypes.postgres.INTEGER.parse)).to.deep.equal(_.extend([1, Infinity], { inclusive: [false, false] }));
expect(range.parse('(-infinity,1)', DataTypes.postgres.INTEGER.parse)).to.deep.equal(_.extend([-Infinity, 1], { inclusive: [false, false] }));
expect(range.parse('(1,-infinity)', DataTypes.postgres.INTEGER.parse)).to.deep.equal(_.extend([1, -Infinity], { inclusive: [false, false] }));
expect(range.parse('(-infinity,infinity)', DataTypes.postgres.INTEGER.parse)).to.deep.equal(_.extend([-Infinity, Infinity], { inclusive: [false, 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,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', () => {
......@@ -207,15 +187,13 @@ if (dialect.match(/^postgres/)) {
it('should handle native postgres timestamp format', () => {
const tsOid = DataTypes.postgres.DATE.types.postgres.oids[0],
parser = pg.types.getTypeParser(tsOid);
expect(range.parse('(2016-01-01 08:00:00-04,)', parser)[0].toISOString()).to.equal('2016-01-01T12:00:00.000Z');
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 = [5, 10];
testRange.inclusive = [true, true];
const testRange = [{ value: 5, inclusive: true }, { value: 10, inclusive: true }];
const Range = new DataTypes.postgres.RANGE(DataTypes.INTEGER);
let stringified = Range.stringify(testRange, {});
......
......@@ -1296,33 +1296,11 @@ suite(Support.getTestDialectTeaser('SQL'), () => {
}).to.throw(Sequelize.ValidationError, 'A range must be an array with two elements');
});
test('should throw an error if `value.inclusive` is invalid', () => {
const type = DataTypes.RANGE();
expect(() => {
type.validate({ inclusive: 'foobar' });
}).to.throw(Sequelize.ValidationError, '"foobar" is not a valid range');
});
test('should throw an error if `value.inclusive` is not an array with two elements', () => {
const type = DataTypes.RANGE();
expect(() => {
type.validate({ inclusive: [1] });
}).to.throw(Sequelize.ValidationError, 'A range must be an array with two elements');
});
test('should return `true` if `value` is a range', () => {
const type = DataTypes.RANGE();
expect(type.validate([1, 2])).to.equal(true);
});
test('should return `true` if `value.inclusive` is a range', () => {
const type = DataTypes.RANGE();
expect(type.validate({ inclusive: [1, 2] })).to.equal(true);
});
});
});
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!