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

Commit 46b0e5b9 by sdepold

moved specs

1 parent f37fce43
......@@ -367,4 +367,83 @@ describe('Model', function() {
})
})
})
describe('updateAttributes', function() {
it("updates attributes in the database", function() {
Helpers.async(function(done) {
User.create({ username: 'user' }).success(function(user) {
expect(user.username).toEqual('user')
user.updateAttributes({ username: 'person' }).success(function(user) {
expect(user.username).toEqual('person')
done()
})
})
})
})
it("ignores unknown attributes", function() {
Helpers.async(function(done) {
User.create({ username: 'user' }).success(function(user) {
user.updateAttributes({ username: 'person', foo: 'bar'}).success(function(user) {
expect(user.username).toEqual('person')
expect(user.foo).toBeUndefined()
done()
})
})
})
})
it("doesn't update primary keys or timestamps", function() {
var User = sequelize.define('User' + config.rand(), {
name: Sequelize.STRING, bio: Sequelize.TEXT, identifier: {type: Sequelize.STRING, primaryKey: true}
})
Helpers.async(function(done) {
User.sync({ force: true }).success(done)
})
Helpers.async(function(done) {
User.create({
name: 'snafu',
identifier: 'identifier'
}).success(function(user) {
var oldCreatedAt = user.createdAt
, oldIdentifier = user.identifier
user.updateAttributes({
name: 'foobar',
createdAt: new Date(2000, 1, 1),
identifier: 'another identifier'
}).success(function(user) {
expect(user.createdAt).toEqual(oldCreatedAt)
expect(user.identifier).toEqual(oldIdentifier)
done()
})
})
})
})
it("uses primary keys in where clause", function() {
var User = sequelize.define('User' + config.rand(), {
name: Sequelize.STRING, bio: Sequelize.TEXT, identifier: {type: Sequelize.STRING, primaryKey: true}
})
Helpers.async(function(done) {
User.sync({ force:true }).success(done)
})
Helpers.async(function(done) {
User.create({
name: 'snafu',
identifier: 'identifier'
}).success(function(user) {
var emitter = user.updateAttributes({name: 'foobar'})
emitter.success(function() {
expect(emitter.query.sql).toMatch(/WHERE `identifier`..identifier./)
done()
})
})
})
})
})
})
var assert = require("assert")
, config = require("./../config")
, Sequelize = require("./../../index")
, sequelize = new Sequelize(config.database, config.username, config.password, {logging: false, define: { charset: 'latin1' }})
, User = sequelize.define('User' + config.rand(), { name: Sequelize.STRING, bio: Sequelize.TEXT })
module.exports = {
'it should update the attributes': function(exit) {
User.sync({force:true}).on('success', function() {
User.create({name: 'snafu'}).on('success', function(user) {
assert.eql(user.name, 'snafu')
user.updateAttributes({name: 'foobar'}).on('success', function(user) {
assert.eql(user.name, 'foobar')
exit(function(){})
})
})
})
},
'it should not set attributes which were not defined': function(exit) {
User.sync({force:true}).on('success', function() {
User.create({name: 'snafu'}).on('success', function(user) {
user.updateAttributes({name: 'foobar', foo: 'bar'}).on('success', function(user) {
assert.eql(user.name, 'foobar')
assert.isUndefined(user.foo)
exit(function(){})
})
})
})
},
'it should not set primary keys or timestamps': function(exit) {
var User = sequelize.define('User' + config.rand(), {
name: Sequelize.STRING, bio: Sequelize.TEXT, identifier: {type: Sequelize.STRING, primaryKey: true}
})
User.sync({force:true}).on('success', function() {
User.create({name: 'snafu', identifier: 'identifier'}).on('success', function(user) {
var oldCreatedAt = user.createdAt
, oldIdentifier = user.identifier
user.updateAttributes({name: 'foobar', createdAt: new Date(2000, 1, 1), identifier: 'another identifier'}).on('success', function(user) {
assert.eql(user.createdAt, oldCreatedAt)
assert.eql(user.identifier, oldIdentifier)
exit(function(){})
})
})
})
},
"it should use the primary keys in the where clause": function(exit) {
var User = sequelize.define('User' + config.rand(), {
name: Sequelize.STRING, bio: Sequelize.TEXT, identifier: {type: Sequelize.STRING, primaryKey: true}
})
User.sync({force:true}).on('success', function() {
User.create({name: 'snafu', identifier: 'identifier'}).on('success', function(user) {
var emitter = user.updateAttributes({name: 'foobar'})
emitter.success(function() {
assert.match(emitter.query.sql, /WHERE `identifier`..identifier./)
})
exit(function(){})
})
})
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!