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

Commit d136bf6d by Mick Hansen

refactor(instance): rename updateAttributes to update and move tests to seperate file

1 parent b704d169
......@@ -362,6 +362,10 @@ module.exports = (function() {
return this;
};
Instance.prototype.setAttributes = function(updates) {
return this.set(updates);
};
/**
* If changed is called with a string it will return a boolean indicating whether the value of that key in `dataValues` is different from the value in `_previousDataValues`.
*
......@@ -692,27 +696,20 @@ module.exports = (function() {
};
/**
* This is the same as calling `setAttributes`, then calling `save`.
* This is the same as calling `set` and then calling `save`.
*
* @see {Instance#setAttributes}
* @see {Instance#set}
* @see {Instance#save}
* @param {Object} updates See `setAttributes`
* @param {Object} updates See `set`
* @param {Object} options See `save`
*
* @return {Promise<this>}
* @alias updateAttributes
*/
Instance.prototype.updateAttributes = function(updates, options) {
if (options instanceof Array) {
options = { fields: options };
}
this.set(updates);
return this.save(options);
};
Instance.prototype.setAttributes = function(updates) {
return this.set(updates);
Instance.prototype.update = function(values, options) {
return this.set(values, {attributes: options && options.attributes}).save(options);
};
Instance.prototype.updateAttributes = Instance.prototype.update;
/**
* Destroy the row corresponding to this instance. Depending on your setting for paranoid, the row will either be completely deleted, or have its deletedAt timestamp set to the current time.
......
......@@ -1620,142 +1620,6 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
});
});
describe('updateAttributes', function() {
if (current.dialect.supports.transactions) {
it('supports transactions', function(done) {
Support.prepareTransactionTest(this.sequelize, function(sequelize) {
var User = sequelize.define('User', { username: Support.Sequelize.STRING });
User.sync({ force: true }).success(function() {
User.create({ username: 'foo' }).success(function(user) {
sequelize.transaction().then(function(t) {
user.updateAttributes({ username: 'bar' }, { transaction: t }).success(function() {
User.all().success(function(users1) {
User.all({ transaction: t }).success(function(users2) {
expect(users1[0].username).to.equal('foo');
expect(users2[0].username).to.equal('bar');
t.rollback().success(function() { done(); });
});
});
});
});
});
});
});
});
}
it('updates attributes in the database', function(done) {
this.User.create({ username: 'user' }).success(function(user) {
expect(user.username).to.equal('user');
user.updateAttributes({ username: 'person' }).success(function(user) {
expect(user.username).to.equal('person');
done();
});
});
});
it('ignores unknown attributes', function(done) {
this.User.create({ username: 'user' }).success(function(user) {
user.updateAttributes({ username: 'person', foo: 'bar'}).success(function(user) {
expect(user.username).to.equal('person');
expect(user.foo).not.to.exist;
done();
});
});
});
it("doesn't update primary keys or timestamps", function(done) {
var User = this.sequelize.define('User' + config.rand(), {
name: DataTypes.STRING,
bio: DataTypes.TEXT,
identifier: {type: DataTypes.STRING, primaryKey: true}
});
User.sync({ force: true }).success(function() {
User.create({
name: 'snafu',
identifier: 'identifier'
}).success(function(user) {
var oldCreatedAt = user.createdAt
, oldUpdatedAt = user.updatedAt
, oldIdentifier = user.identifier;
setTimeout(function() {
user.updateAttributes({
name: 'foobar',
createdAt: new Date(2000, 1, 1),
identifier: 'another identifier'
}).success(function(user) {
expect(new Date(user.createdAt)).to.equalDate(new Date(oldCreatedAt));
expect(new Date(user.updatedAt)).to.not.equalTime(new Date(oldUpdatedAt));
expect(user.identifier).to.equal(oldIdentifier);
done();
});
}, 1000);
});
});
});
it('uses primary keys in where clause', function(done) {
var User = this.sequelize.define('User' + config.rand(), {
name: DataTypes.STRING,
bio: DataTypes.TEXT,
identifier: {type: DataTypes.STRING, primaryKey: true}
});
User.sync({ force: true }).success(function() {
User.create({
name: 'snafu',
identifier: 'identifier'
}).success(function(user) {
var emitter = user.updateAttributes({name: 'foobar'});
emitter.on('sql', function(sql) {
expect(sql).to.match(/WHERE [`"]identifier[`"]..identifier./);
done();
});
});
});
});
it('stores and restores null values', function(done) {
var Download = this.sequelize.define('download', {
startedAt: DataTypes.DATE,
canceledAt: DataTypes.DATE,
finishedAt: DataTypes.DATE
});
Download.sync().success(function() {
Download.create({
startedAt: new Date()
}).success(function(download) {
expect(download.startedAt instanceof Date).to.be.true;
expect(download.canceledAt).to.not.be.ok;
expect(download.finishedAt).to.not.be.ok;
download.updateAttributes({
canceledAt: new Date()
}).success(function(download) {
expect(download.startedAt instanceof Date).to.be.true;
expect(download.canceledAt instanceof Date).to.be.true;
expect(download.finishedAt).to.not.be.ok;
Download.all({
where: (dialect === 'postgres' || dialect === 'mssql' ? '"finishedAt" IS NULL' : '`finishedAt` IS NULL')
}).success(function(downloads) {
downloads.forEach(function(download) {
expect(download.startedAt instanceof Date).to.be.true;
expect(download.canceledAt instanceof Date).to.be.true;
expect(download.finishedAt).to.not.be.ok;
done();
});
});
});
});
});
});
});
describe('destroy', function() {
if (current.dialect.supports.transactions) {
it('supports transactions', function(done) {
......
'use strict';
var chai = require('chai')
, Sequelize = require('../../index')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, dialect = Support.getTestDialect()
, DataTypes = require(__dirname + '/../../lib/data-types')
, datetime = require('chai-datetime')
, config = require(__dirname + '/../config/config')
, current = Support.sequelize;
chai.use(datetime);
chai.config.includeStack = true;
describe(Support.getTestDialectTeaser('Instance'), function() {
describe('update', function() {
beforeEach(function () {
this.User = this.sequelize.define('User', {
username: { type: DataTypes.STRING },
uuidv1: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV1 },
uuidv4: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4 },
touchedAt: { type: DataTypes.DATE, defaultValue: DataTypes.NOW },
aNumber: { type: DataTypes.INTEGER },
bNumber: { type: DataTypes.INTEGER },
aDate: { type: DataTypes.DATE },
validateTest: {
type: DataTypes.INTEGER,
allowNull: true,
validate: {isInt: true}
},
validateCustom: {
type: DataTypes.STRING,
allowNull: true,
validate: {len: {msg: 'Length failed.', args: [1, 20]}}
},
dateAllowNullTrue: {
type: DataTypes.DATE,
allowNull: true
}
});
return this.User.sync({ force: true });
});
if (current.dialect.supports.transactions) {
it('supports transactions', function(done) {
Support.prepareTransactionTest(this.sequelize, function(sequelize) {
var User = sequelize.define('User', { username: Support.Sequelize.STRING });
User.sync({ force: true }).success(function() {
User.create({ username: 'foo' }).success(function(user) {
sequelize.transaction().then(function(t) {
user.update({ username: 'bar' }, { transaction: t }).success(function() {
User.all().success(function(users1) {
User.all({ transaction: t }).success(function(users2) {
expect(users1[0].username).to.equal('foo');
expect(users2[0].username).to.equal('bar');
t.rollback().success(function() { done(); });
});
});
});
});
});
});
});
});
}
it('updates attributes in the database', function(done) {
this.User.create({ username: 'user' }).success(function(user) {
expect(user.username).to.equal('user');
user.update({ username: 'person' }).success(function(user) {
expect(user.username).to.equal('person');
done();
});
});
});
it('ignores unknown attributes', function(done) {
this.User.create({ username: 'user' }).success(function(user) {
user.update({ username: 'person', foo: 'bar'}).success(function(user) {
expect(user.username).to.equal('person');
expect(user.foo).not.to.exist;
done();
});
});
});
it("doesn't update primary keys or timestamps", function(done) {
var User = this.sequelize.define('User' + config.rand(), {
name: DataTypes.STRING,
bio: DataTypes.TEXT,
identifier: {type: DataTypes.STRING, primaryKey: true}
});
User.sync({ force: true }).success(function() {
User.create({
name: 'snafu',
identifier: 'identifier'
}).success(function(user) {
var oldCreatedAt = user.createdAt
, oldUpdatedAt = user.updatedAt
, oldIdentifier = user.identifier;
setTimeout(function() {
user.update({
name: 'foobar',
createdAt: new Date(2000, 1, 1),
identifier: 'another identifier'
}).success(function(user) {
expect(new Date(user.createdAt)).to.equalDate(new Date(oldCreatedAt));
expect(new Date(user.updatedAt)).to.not.equalTime(new Date(oldUpdatedAt));
expect(user.identifier).to.equal(oldIdentifier);
done();
});
}, 1000);
});
});
});
it('uses primary keys in where clause', function(done) {
var User = this.sequelize.define('User' + config.rand(), {
name: DataTypes.STRING,
bio: DataTypes.TEXT,
identifier: {type: DataTypes.STRING, primaryKey: true}
});
User.sync({ force: true }).success(function() {
User.create({
name: 'snafu',
identifier: 'identifier'
}).success(function(user) {
var emitter = user.update({name: 'foobar'});
emitter.on('sql', function(sql) {
expect(sql).to.match(/WHERE [`"]identifier[`"]..identifier./);
done();
});
});
});
});
it('stores and restores null values', function(done) {
var Download = this.sequelize.define('download', {
startedAt: DataTypes.DATE,
canceledAt: DataTypes.DATE,
finishedAt: DataTypes.DATE
});
Download.sync().success(function() {
Download.create({
startedAt: new Date()
}).success(function(download) {
expect(download.startedAt instanceof Date).to.be.true;
expect(download.canceledAt).to.not.be.ok;
expect(download.finishedAt).to.not.be.ok;
download.update({
canceledAt: new Date()
}).success(function(download) {
expect(download.startedAt instanceof Date).to.be.true;
expect(download.canceledAt instanceof Date).to.be.true;
expect(download.finishedAt).to.not.be.ok;
Download.all({
where: (dialect === 'postgres' || dialect === 'mssql' ? '"finishedAt" IS NULL' : '`finishedAt` IS NULL')
}).success(function(downloads) {
downloads.forEach(function(download) {
expect(download.startedAt instanceof Date).to.be.true;
expect(download.canceledAt instanceof Date).to.be.true;
expect(download.finishedAt).to.not.be.ok;
done();
});
});
});
});
});
});
});
});
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!