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

Commit 3c19e974 by up9cloud Committed by Sushant

feat(data-types): add TINYINT, SMALLINT, MEDIUMINT (#8515)

1 parent 17b19d9c
...@@ -141,7 +141,7 @@ NUMBER.prototype.toSql = function toSql() { ...@@ -141,7 +141,7 @@ NUMBER.prototype.toSql = function toSql() {
NUMBER.prototype.validate = function(value) { NUMBER.prototype.validate = function(value) {
if (!Validator.isFloat(String(value))) { if (!Validator.isFloat(String(value))) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid number', value)); throw new sequelizeErrors.ValidationError(util.format('%j is not a valid ' + _.toLower(this.key), value));
} }
return true; return true;
...@@ -172,27 +172,47 @@ inherits(INTEGER, NUMBER); ...@@ -172,27 +172,47 @@ inherits(INTEGER, NUMBER);
INTEGER.prototype.key = INTEGER.key = 'INTEGER'; INTEGER.prototype.key = INTEGER.key = 'INTEGER';
INTEGER.prototype.validate = function validate(value) { INTEGER.prototype.validate = function validate(value) {
if (!Validator.isInt(String(value))) { if (!Validator.isInt(String(value))) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid integer', value)); throw new sequelizeErrors.ValidationError(util.format('%j is not a valid ' + _.toLower(this.key), value));
} }
return true; return true;
}; };
function TINYINT(length) {
const options = typeof length === 'object' && length || {length};
if (!(this instanceof TINYINT)) return new TINYINT(options);
NUMBER.call(this, options);
}
inherits(TINYINT, INTEGER);
TINYINT.prototype.key = TINYINT.key = 'TINYINT';
function SMALLINT(length) {
const options = typeof length === 'object' && length || {length};
if (!(this instanceof SMALLINT)) return new SMALLINT(options);
NUMBER.call(this, options);
}
inherits(SMALLINT, INTEGER);
SMALLINT.prototype.key = SMALLINT.key = 'SMALLINT';
function MEDIUMINT(length) {
const options = typeof length === 'object' && length || {length};
if (!(this instanceof MEDIUMINT)) return new MEDIUMINT(options);
NUMBER.call(this, options);
}
inherits(MEDIUMINT, INTEGER);
MEDIUMINT.prototype.key = MEDIUMINT.key = 'MEDIUMINT';
function BIGINT(length) { function BIGINT(length) {
const options = typeof length === 'object' && length || {length}; const options = typeof length === 'object' && length || {length};
if (!(this instanceof BIGINT)) return new BIGINT(options); if (!(this instanceof BIGINT)) return new BIGINT(options);
NUMBER.call(this, options); NUMBER.call(this, options);
} }
inherits(BIGINT, NUMBER); inherits(BIGINT, INTEGER);
BIGINT.prototype.key = BIGINT.key = 'BIGINT'; BIGINT.prototype.key = BIGINT.key = 'BIGINT';
BIGINT.prototype.validate = function validate(value) {
if (!Validator.isInt(String(value))) {
throw new sequelizeErrors.ValidationError(util.format('%j is not a valid bigint', value));
}
return true;
};
function FLOAT(length, decimals) { function FLOAT(length, decimals) {
const options = typeof length === 'object' && length || {length, decimals}; const options = typeof length === 'object' && length || {length, decimals};
...@@ -664,8 +684,8 @@ ARRAY.is = function is(obj, type) { ...@@ -664,8 +684,8 @@ ARRAY.is = function is(obj, type) {
const helpers = { const helpers = {
BINARY: [STRING, CHAR], BINARY: [STRING, CHAR],
UNSIGNED: [NUMBER, INTEGER, BIGINT, FLOAT, DOUBLE, REAL, DECIMAL], UNSIGNED: [NUMBER, TINYINT, SMALLINT, MEDIUMINT, INTEGER, BIGINT, FLOAT, DOUBLE, REAL, DECIMAL],
ZEROFILL: [NUMBER, INTEGER, BIGINT, FLOAT, DOUBLE, REAL, DECIMAL], ZEROFILL: [NUMBER, TINYINT, SMALLINT, MEDIUMINT, INTEGER, BIGINT, FLOAT, DOUBLE, REAL, DECIMAL],
PRECISION: [DECIMAL], PRECISION: [DECIMAL],
SCALE: [DECIMAL] SCALE: [DECIMAL]
}; };
...@@ -771,6 +791,9 @@ for (const helper of Object.keys(helpers)) { ...@@ -771,6 +791,9 @@ for (const helper of Object.keys(helpers)) {
* @property {function(length=255: integer)} STRING A variable length string * @property {function(length=255: integer)} STRING A variable length string
* @property {function(length=255: integer)} CHAR A fixed length string. * @property {function(length=255: integer)} CHAR A fixed length string.
* @property {function([length]: string)} TEXT An unlimited length text column. Available lengths: `tiny`, `medium`, `long` * @property {function([length]: string)} TEXT An unlimited length text column. Available lengths: `tiny`, `medium`, `long`
* @property {function(length: integer)} TINYINT A 8 bit integer.
* @property {function(length: integer)} SMALLINT A 16 bit integer.
* @property {function(length: integer)} MEDIUMINT A 24 bit integer.
* @property {function(length=255: integer)} INTEGER A 32 bit integer. * @property {function(length=255: integer)} INTEGER A 32 bit integer.
* @property {function(length: integer)} BIGINT A 64 bit integer. Note: an attribute defined as `BIGINT` will be treated like a `string` due this [feature from node-postgres](https://github.com/brianc/node-postgres/pull/353) to prevent precision loss. To have this attribute as a `number`, this is a possible [workaround](https://github.com/sequelize/sequelize/issues/2383#issuecomment-58006083). * @property {function(length: integer)} BIGINT A 64 bit integer. Note: an attribute defined as `BIGINT` will be treated like a `string` due this [feature from node-postgres](https://github.com/brianc/node-postgres/pull/353) to prevent precision loss. To have this attribute as a `number`, this is a possible [workaround](https://github.com/sequelize/sequelize/issues/2383#issuecomment-58006083).
* @property {function(length: integer, decimals: integer)} FLOAT Floating point number (4-byte precision). * @property {function(length: integer, decimals: integer)} FLOAT Floating point number (4-byte precision).
...@@ -875,6 +898,9 @@ const DataTypes = module.exports = { ...@@ -875,6 +898,9 @@ const DataTypes = module.exports = {
CHAR, CHAR,
TEXT, TEXT,
NUMBER, NUMBER,
TINYINT,
SMALLINT,
MEDIUMINT,
INTEGER, INTEGER,
BIGINT, BIGINT,
FLOAT, FLOAT,
......
...@@ -7,10 +7,19 @@ const inherits = require('../../utils/inherits'); ...@@ -7,10 +7,19 @@ const inherits = require('../../utils/inherits');
module.exports = BaseTypes => { module.exports = BaseTypes => {
const warn = BaseTypes.ABSTRACT.warn.bind(undefined, 'https://msdn.microsoft.com/en-us/library/ms187752%28v=sql.110%29.aspx'); const warn = BaseTypes.ABSTRACT.warn.bind(undefined, 'https://msdn.microsoft.com/en-us/library/ms187752%28v=sql.110%29.aspx');
/**
* types: [hex, ...]
* @see hex here https://github.com/tediousjs/tedious/blob/master/src/data-type.js
*/
BaseTypes.DATE.types.mssql = [43]; BaseTypes.DATE.types.mssql = [43];
BaseTypes.STRING.types.mssql = [231, 173]; BaseTypes.STRING.types.mssql = [231, 173];
BaseTypes.CHAR.types.mssql = [175]; BaseTypes.CHAR.types.mssql = [175];
BaseTypes.TEXT.types.mssql = false; BaseTypes.TEXT.types.mssql = false;
// https://msdn.microsoft.com/en-us/library/ms187745(v=sql.110).aspx
BaseTypes.TINYINT.types.mssql = [30];
BaseTypes.SMALLINT.types.mssql = [34];
BaseTypes.MEDIUMINT.types.mssql = false;
BaseTypes.INTEGER.types.mssql = [38]; BaseTypes.INTEGER.types.mssql = [38];
BaseTypes.BIGINT.types.mssql = false; BaseTypes.BIGINT.types.mssql = false;
BaseTypes.FLOAT.types.mssql = [109]; BaseTypes.FLOAT.types.mssql = [109];
...@@ -154,6 +163,36 @@ module.exports = BaseTypes => { ...@@ -154,6 +163,36 @@ module.exports = BaseTypes => {
} }
inherits(INTEGER, BaseTypes.INTEGER); inherits(INTEGER, BaseTypes.INTEGER);
function TINYINT(length) {
if (!(this instanceof TINYINT)) return new TINYINT(length);
BaseTypes.TINYINT.apply(this, arguments);
// MSSQL does not support any options for tinyint
if (this._length || this.options.length || this._unsigned || this._zerofill) {
warn('MSSQL does not support TINYINT with options. Plain `TINYINT` will be used instead.');
this._length = undefined;
this.options.length = undefined;
this._unsigned = undefined;
this._zerofill = undefined;
}
}
inherits(TINYINT, BaseTypes.TINYINT);
function SMALLINT(length) {
if (!(this instanceof SMALLINT)) return new SMALLINT(length);
BaseTypes.SMALLINT.apply(this, arguments);
// MSSQL does not support any options for smallint
if (this._length || this.options.length || this._unsigned || this._zerofill) {
warn('MSSQL does not support SMALLINT with options. Plain `SMALLINT` will be used instead.');
this._length = undefined;
this.options.length = undefined;
this._unsigned = undefined;
this._zerofill = undefined;
}
}
inherits(SMALLINT, BaseTypes.SMALLINT);
function BIGINT(length) { function BIGINT(length) {
if (!(this instanceof BIGINT)) return new BIGINT(length); if (!(this instanceof BIGINT)) return new BIGINT(length);
BaseTypes.BIGINT.apply(this, arguments); BaseTypes.BIGINT.apply(this, arguments);
...@@ -231,6 +270,8 @@ module.exports = BaseTypes => { ...@@ -231,6 +270,8 @@ module.exports = BaseTypes => {
DATE, DATE,
DATEONLY, DATEONLY,
NOW, NOW,
TINYINT,
SMALLINT,
INTEGER, INTEGER,
BIGINT, BIGINT,
REAL, REAL,
......
...@@ -8,10 +8,19 @@ const inherits = require('../../utils/inherits'); ...@@ -8,10 +8,19 @@ const inherits = require('../../utils/inherits');
module.exports = BaseTypes => { module.exports = BaseTypes => {
BaseTypes.ABSTRACT.prototype.dialectTypes = 'https://dev.mysql.com/doc/refman/5.7/en/data-types.html'; BaseTypes.ABSTRACT.prototype.dialectTypes = 'https://dev.mysql.com/doc/refman/5.7/en/data-types.html';
/**
* types: [buffer_type, ...]
* @see buffer_type here https://dev.mysql.com/doc/refman/5.7/en/c-api-prepared-statement-type-codes.html
* @see hex here https://github.com/sidorares/node-mysql2/blob/master/lib/constants/types.js
*/
BaseTypes.DATE.types.mysql = ['DATETIME']; BaseTypes.DATE.types.mysql = ['DATETIME'];
BaseTypes.STRING.types.mysql = ['VAR_STRING']; BaseTypes.STRING.types.mysql = ['VAR_STRING'];
BaseTypes.CHAR.types.mysql = ['STRING']; BaseTypes.CHAR.types.mysql = ['STRING'];
BaseTypes.TEXT.types.mysql = ['BLOB']; BaseTypes.TEXT.types.mysql = ['BLOB'];
BaseTypes.TINYINT.types.mysql = ['TINY'];
BaseTypes.SMALLINT.types.mysql = ['SHORT'];
BaseTypes.MEDIUMINT.types.mysql = ['INT24'];
BaseTypes.INTEGER.types.mysql = ['LONG']; BaseTypes.INTEGER.types.mysql = ['LONG'];
BaseTypes.BIGINT.types.mysql = ['LONGLONG']; BaseTypes.BIGINT.types.mysql = ['LONGLONG'];
BaseTypes.FLOAT.types.mysql = ['FLOAT']; BaseTypes.FLOAT.types.mysql = ['FLOAT'];
......
...@@ -7,6 +7,15 @@ const inherits = require('../../utils/inherits'); ...@@ -7,6 +7,15 @@ const inherits = require('../../utils/inherits');
module.exports = BaseTypes => { module.exports = BaseTypes => {
const warn = BaseTypes.ABSTRACT.warn.bind(undefined, 'http://www.postgresql.org/docs/9.4/static/datatype.html'); const warn = BaseTypes.ABSTRACT.warn.bind(undefined, 'http://www.postgresql.org/docs/9.4/static/datatype.html');
/**
* types:
* {
* oids: [oid],
* array_oids: [oid]
* }
* @see oid here https://github.com/lib/pq/blob/master/oid/types.go
*/
BaseTypes.UUID.types.postgres = { BaseTypes.UUID.types.postgres = {
oids: [2950], oids: [2950],
array_oids: [2951] array_oids: [2951]
...@@ -230,6 +239,27 @@ module.exports = BaseTypes => { ...@@ -230,6 +239,27 @@ module.exports = BaseTypes => {
array_oids: [1185] array_oids: [1185]
}; };
function SMALLINT(length) {
if (!(this instanceof SMALLINT)) return new SMALLINT(length);
BaseTypes.SMALLINT.apply(this, arguments);
// POSTGRES does not support any parameters for bigint
if (this._length || this.options.length || this._unsigned || this._zerofill) {
warn('PostgreSQL does not support SMALLINT with options. Plain `SMALLINT` will be used instead.');
this._length = undefined;
this.options.length = undefined;
this._unsigned = undefined;
this._zerofill = undefined;
}
}
inherits(SMALLINT, BaseTypes.SMALLINT);
// int2
BaseTypes.SMALLINT.types.postgres = {
oids: [21],
array_oids: [1005]
};
function INTEGER(length) { function INTEGER(length) {
if (!(this instanceof INTEGER)) return new INTEGER(length); if (!(this instanceof INTEGER)) return new INTEGER(length);
BaseTypes.INTEGER.apply(this, arguments); BaseTypes.INTEGER.apply(this, arguments);
...@@ -556,11 +586,12 @@ module.exports = BaseTypes => { ...@@ -556,11 +586,12 @@ module.exports = BaseTypes => {
STRING, STRING,
CHAR, CHAR,
TEXT, TEXT,
SMALLINT,
INTEGER, INTEGER,
BIGINT,
BOOLEAN, BOOLEAN,
DATE, DATE,
DATEONLY, DATEONLY,
BIGINT,
REAL, REAL,
'DOUBLE PRECISION': DOUBLE, 'DOUBLE PRECISION': DOUBLE,
FLOAT, FLOAT,
......
...@@ -6,10 +6,17 @@ const inherits = require('../../utils/inherits'); ...@@ -6,10 +6,17 @@ const inherits = require('../../utils/inherits');
module.exports = BaseTypes => { module.exports = BaseTypes => {
const warn = BaseTypes.ABSTRACT.warn.bind(undefined, 'https://www.sqlite.org/datatype3.html'); const warn = BaseTypes.ABSTRACT.warn.bind(undefined, 'https://www.sqlite.org/datatype3.html');
/**
* @see https://sqlite.org/datatype3.html
*/
BaseTypes.DATE.types.sqlite = ['DATETIME']; BaseTypes.DATE.types.sqlite = ['DATETIME'];
BaseTypes.STRING.types.sqlite = ['VARCHAR', 'VARCHAR BINARY']; BaseTypes.STRING.types.sqlite = ['VARCHAR', 'VARCHAR BINARY'];
BaseTypes.CHAR.types.sqlite = ['CHAR', 'CHAR BINARY']; BaseTypes.CHAR.types.sqlite = ['CHAR', 'CHAR BINARY'];
BaseTypes.TEXT.types.sqlite = ['TEXT']; BaseTypes.TEXT.types.sqlite = ['TEXT'];
BaseTypes.TINYINT.types.sqlite = ['TINYINT'];
BaseTypes.SMALLINT.types.sqlite = ['SMALLINT'];
BaseTypes.MEDIUMINT.types.sqlite = ['MEDIUMINT'];
BaseTypes.INTEGER.types.sqlite = ['INTEGER']; BaseTypes.INTEGER.types.sqlite = ['INTEGER'];
BaseTypes.BIGINT.types.sqlite = ['BIGINT']; BaseTypes.BIGINT.types.sqlite = ['BIGINT'];
BaseTypes.FLOAT.types.sqlite = ['FLOAT']; BaseTypes.FLOAT.types.sqlite = ['FLOAT'];
...@@ -128,6 +135,36 @@ module.exports = BaseTypes => { ...@@ -128,6 +135,36 @@ module.exports = BaseTypes => {
return result; return result;
}; };
function TINYINT(length) {
if (!(this instanceof TINYINT)) return new TINYINT(length);
BaseTypes.TINYINT.apply(this, arguments);
}
inherits(TINYINT, BaseTypes.TINYINT);
TINYINT.prototype.toSql = function toSql() {
return NUMBER.prototype.toSql.call(this);
};
function SMALLINT(length) {
if (!(this instanceof SMALLINT)) return new SMALLINT(length);
BaseTypes.SMALLINT.apply(this, arguments);
}
inherits(SMALLINT, BaseTypes.SMALLINT);
SMALLINT.prototype.toSql = function toSql() {
return NUMBER.prototype.toSql.call(this);
};
function MEDIUMINT(length) {
if (!(this instanceof MEDIUMINT)) return new MEDIUMINT(length);
BaseTypes.MEDIUMINT.apply(this, arguments);
}
inherits(MEDIUMINT, BaseTypes.MEDIUMINT);
MEDIUMINT.prototype.toSql = function toSql() {
return NUMBER.prototype.toSql.call(this);
};
function INTEGER(length) { function INTEGER(length) {
if (!(this instanceof INTEGER)) return new INTEGER(length); if (!(this instanceof INTEGER)) return new INTEGER(length);
BaseTypes.INTEGER.apply(this, arguments); BaseTypes.INTEGER.apply(this, arguments);
...@@ -213,6 +250,9 @@ module.exports = BaseTypes => { ...@@ -213,6 +250,9 @@ module.exports = BaseTypes => {
FLOAT, FLOAT,
REAL, REAL,
'DOUBLE PRECISION': DOUBLE, 'DOUBLE PRECISION': DOUBLE,
TINYINT,
SMALLINT,
MEDIUMINT,
INTEGER, INTEGER,
BIGINT, BIGINT,
TEXT, TEXT,
......
...@@ -406,6 +406,366 @@ suite(Support.getTestDialectTeaser('SQL'), () => { ...@@ -406,6 +406,366 @@ suite(Support.getTestDialectTeaser('SQL'), () => {
}); });
}); });
suite('TINYINT', () => {
const cases = [
{
title: 'TINYINT',
dataType: DataTypes.TINYINT,
expect: {
default: 'TINYINT'
}
},
{
title: 'TINYINT(2)',
dataType: DataTypes.TINYINT(2),
expect: {
default: 'TINYINT(2)',
mssql: 'TINYINT'
}
},
{
title: 'TINYINT({ length: 2 })',
dataType: DataTypes.TINYINT({ length: 2 }),
expect: {
default: 'TINYINT(2)',
mssql: 'TINYINT'
}
},
{
title: 'TINYINT.UNSIGNED',
dataType: DataTypes.TINYINT.UNSIGNED,
expect: {
default: 'TINYINT UNSIGNED',
mssql: 'TINYINT'
}
},
{
title: 'TINYINT(2).UNSIGNED',
dataType: DataTypes.TINYINT(2).UNSIGNED,
expect: {
default: 'TINYINT(2) UNSIGNED',
sqlite: 'TINYINT UNSIGNED(2)',
mssql: 'TINYINT'
}
},
{
title: 'TINYINT.UNSIGNED.ZEROFILL',
dataType: DataTypes.TINYINT.UNSIGNED.ZEROFILL,
expect: {
default: 'TINYINT UNSIGNED ZEROFILL',
mssql: 'TINYINT'
}
},
{
title: 'TINYINT(2).UNSIGNED.ZEROFILL',
dataType: DataTypes.TINYINT(2).UNSIGNED.ZEROFILL,
expect: {
default: 'TINYINT(2) UNSIGNED ZEROFILL',
sqlite: 'TINYINT UNSIGNED ZEROFILL(2)',
mssql: 'TINYINT'
}
},
{
title: 'TINYINT.ZEROFILL',
dataType: DataTypes.TINYINT.ZEROFILL,
expect: {
default: 'TINYINT ZEROFILL',
mssql: 'TINYINT'
}
},
{
title: 'TINYINT(2).ZEROFILL',
dataType: DataTypes.TINYINT(2).ZEROFILL,
expect: {
default: 'TINYINT(2) ZEROFILL',
sqlite: 'TINYINT ZEROFILL(2)',
mssql: 'TINYINT'
}
},
{
title: 'TINYINT.ZEROFILL.UNSIGNED',
dataType: DataTypes.TINYINT.ZEROFILL.UNSIGNED,
expect: {
default: 'TINYINT UNSIGNED ZEROFILL',
mssql: 'TINYINT'
}
},
{
title: 'TINYINT(2).ZEROFILL.UNSIGNED',
dataType: DataTypes.TINYINT(2).ZEROFILL.UNSIGNED,
expect: {
default: 'TINYINT(2) UNSIGNED ZEROFILL',
sqlite: 'TINYINT UNSIGNED ZEROFILL(2)',
mssql: 'TINYINT'
}
}
];
cases.forEach(row => {
testsql(row.title, row.dataType, row.expect);
});
suite('validate', () => {
test('should throw an error if `value` is invalid', () => {
const type = DataTypes.TINYINT();
expect(() => {
type.validate('foobar');
}).to.throw(Sequelize.ValidationError, '"foobar" is not a valid tinyint');
expect(() => {
type.validate(123.45);
}).to.throw(Sequelize.ValidationError, '123.45 is not a valid tinyint');
});
test('should return `true` if `value` is an integer', () => {
const type = DataTypes.TINYINT();
expect(type.validate(-128)).to.equal(true);
expect(type.validate('127')).to.equal(true);
});
});
});
suite('SMALLINT', () => {
const cases = [
{
title: 'SMALLINT',
dataType: DataTypes.SMALLINT,
expect: {
default: 'SMALLINT'
}
},
{
title: 'SMALLINT(4)',
dataType: DataTypes.SMALLINT(4),
expect: {
default: 'SMALLINT(4)',
postgres: 'SMALLINT',
mssql: 'SMALLINT'
}
},
{
title: 'SMALLINT({ length: 4 })',
dataType: DataTypes.SMALLINT({ length: 4 }),
expect: {
default: 'SMALLINT(4)',
postgres: 'SMALLINT',
mssql: 'SMALLINT'
}
},
{
title: 'SMALLINT.UNSIGNED',
dataType: DataTypes.SMALLINT.UNSIGNED,
expect: {
default: 'SMALLINT UNSIGNED',
postgres: 'SMALLINT',
mssql: 'SMALLINT'
}
},
{
title: 'SMALLINT(4).UNSIGNED',
dataType: DataTypes.SMALLINT(4).UNSIGNED,
expect: {
default: 'SMALLINT(4) UNSIGNED',
sqlite: 'SMALLINT UNSIGNED(4)',
postgres: 'SMALLINT',
mssql: 'SMALLINT'
}
},
{
title: 'SMALLINT.UNSIGNED.ZEROFILL',
dataType: DataTypes.SMALLINT.UNSIGNED.ZEROFILL,
expect: {
default: 'SMALLINT UNSIGNED ZEROFILL',
postgres: 'SMALLINT',
mssql: 'SMALLINT'
}
},
{
title: 'SMALLINT(4).UNSIGNED.ZEROFILL',
dataType: DataTypes.SMALLINT(4).UNSIGNED.ZEROFILL,
expect: {
default: 'SMALLINT(4) UNSIGNED ZEROFILL',
sqlite: 'SMALLINT UNSIGNED ZEROFILL(4)',
postgres: 'SMALLINT',
mssql: 'SMALLINT'
}
},
{
title: 'SMALLINT.ZEROFILL',
dataType: DataTypes.SMALLINT.ZEROFILL,
expect: {
default: 'SMALLINT ZEROFILL',
postgres: 'SMALLINT',
mssql: 'SMALLINT'
}
},
{
title: 'SMALLINT(4).ZEROFILL',
dataType: DataTypes.SMALLINT(4).ZEROFILL,
expect: {
default: 'SMALLINT(4) ZEROFILL',
sqlite: 'SMALLINT ZEROFILL(4)',
postgres: 'SMALLINT',
mssql: 'SMALLINT'
}
},
{
title: 'SMALLINT.ZEROFILL.UNSIGNED',
dataType: DataTypes.SMALLINT.ZEROFILL.UNSIGNED,
expect: {
default: 'SMALLINT UNSIGNED ZEROFILL',
postgres: 'SMALLINT',
mssql: 'SMALLINT'
}
},
{
title: 'SMALLINT(4).ZEROFILL.UNSIGNED',
dataType: DataTypes.SMALLINT(4).ZEROFILL.UNSIGNED,
expect: {
default: 'SMALLINT(4) UNSIGNED ZEROFILL',
sqlite: 'SMALLINT UNSIGNED ZEROFILL(4)',
postgres: 'SMALLINT',
mssql: 'SMALLINT'
}
}
];
cases.forEach(row => {
testsql(row.title, row.dataType, row.expect);
});
suite('validate', () => {
test('should throw an error if `value` is invalid', () => {
const type = DataTypes.SMALLINT();
expect(() => {
type.validate('foobar');
}).to.throw(Sequelize.ValidationError, '"foobar" is not a valid smallint');
expect(() => {
type.validate(123.45);
}).to.throw(Sequelize.ValidationError, '123.45 is not a valid smallint');
});
test('should return `true` if `value` is an integer', () => {
const type = DataTypes.SMALLINT();
expect(type.validate(-32768)).to.equal(true);
expect(type.validate('32767')).to.equal(true);
});
});
});
suite('MEDIUMINT', () => {
const cases = [
{
title: 'MEDIUMINT',
dataType: DataTypes.MEDIUMINT,
expect: {
default: 'MEDIUMINT'
}
},
{
title: 'MEDIUMINT(6)',
dataType: DataTypes.MEDIUMINT(6),
expect: {
default: 'MEDIUMINT(6)'
}
},
{
title: 'MEDIUMINT({ length: 6 })',
dataType: DataTypes.MEDIUMINT({ length: 6 }),
expect: {
default: 'MEDIUMINT(6)'
}
},
{
title: 'MEDIUMINT.UNSIGNED',
dataType: DataTypes.MEDIUMINT.UNSIGNED,
expect: {
default: 'MEDIUMINT UNSIGNED'
}
},
{
title: 'MEDIUMINT(6).UNSIGNED',
dataType: DataTypes.MEDIUMINT(6).UNSIGNED,
expect: {
default: 'MEDIUMINT(6) UNSIGNED',
sqlite: 'MEDIUMINT UNSIGNED(6)'
}
},
{
title: 'MEDIUMINT.UNSIGNED.ZEROFILL',
dataType: DataTypes.MEDIUMINT.UNSIGNED.ZEROFILL,
expect: {
default: 'MEDIUMINT UNSIGNED ZEROFILL'
}
},
{
title: 'MEDIUMINT(6).UNSIGNED.ZEROFILL',
dataType: DataTypes.MEDIUMINT(6).UNSIGNED.ZEROFILL,
expect: {
default: 'MEDIUMINT(6) UNSIGNED ZEROFILL',
sqlite: 'MEDIUMINT UNSIGNED ZEROFILL(6)'
}
},
{
title: 'MEDIUMINT.ZEROFILL',
dataType: DataTypes.MEDIUMINT.ZEROFILL,
expect: {
default: 'MEDIUMINT ZEROFILL'
}
},
{
title: 'MEDIUMINT(6).ZEROFILL',
dataType: DataTypes.MEDIUMINT(6).ZEROFILL,
expect: {
default: 'MEDIUMINT(6) ZEROFILL',
sqlite: 'MEDIUMINT ZEROFILL(6)'
}
},
{
title: 'MEDIUMINT.ZEROFILL.UNSIGNED',
dataType: DataTypes.MEDIUMINT.ZEROFILL.UNSIGNED,
expect: {
default: 'MEDIUMINT UNSIGNED ZEROFILL'
}
},
{
title: 'MEDIUMINT(6).ZEROFILL.UNSIGNED',
dataType: DataTypes.MEDIUMINT(6).ZEROFILL.UNSIGNED,
expect: {
default: 'MEDIUMINT(6) UNSIGNED ZEROFILL',
sqlite: 'MEDIUMINT UNSIGNED ZEROFILL(6)'
}
}
];
cases.forEach(row => {
testsql(row.title, row.dataType, row.expect);
});
suite('validate', () => {
test('should throw an error if `value` is invalid', () => {
const type = DataTypes.MEDIUMINT();
expect(() => {
type.validate('foobar');
}).to.throw(Sequelize.ValidationError, '"foobar" is not a valid mediumint');
expect(() => {
type.validate(123.45);
}).to.throw(Sequelize.ValidationError, '123.45 is not a valid mediumint');
});
test('should return `true` if `value` is an integer', () => {
const type = DataTypes.MEDIUMINT();
expect(type.validate(-8388608)).to.equal(true);
expect(type.validate('8388607')).to.equal(true);
});
});
});
suite('BIGINT', () => { suite('BIGINT', () => {
testsql('BIGINT', DataTypes.BIGINT, { testsql('BIGINT', DataTypes.BIGINT, {
default: 'BIGINT' default: 'BIGINT'
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!