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

Commit f47f1899 by sdepold

moved expresso specs to jasmine

1 parent 6ac3f9ea
...@@ -9,6 +9,99 @@ describe('ModelFactory', function() { ...@@ -9,6 +9,99 @@ describe('ModelFactory', function() {
var User = sequelize.define('User', { age: Sequelize.INTEGER, name: Sequelize.STRING, bio: Sequelize.TEXT }) var User = sequelize.define('User', { age: Sequelize.INTEGER, name: Sequelize.STRING, bio: Sequelize.TEXT })
//////////// constructor ////////
describe('constructor', function() {
it("handles extended attributes (unique)", function() {
var User = sequelize.define('User' + config.rand(), {
username: { type: Sequelize.STRING, unique: true }
}, { timestamps: false })
expect(User.attributes).toEqual({username:"VARCHAR(255) UNIQUE",id:"INT NOT NULL auto_increment PRIMARY KEY"})
})
it("handles extended attributes (default)", function() {
var User = sequelize.define('User' + config.rand(), {
username: {type: Sequelize.STRING, defaultValue: 'foo'}
}, { timestamps: false })
expect(User.attributes).toEqual({username:"VARCHAR(255) DEFAULT 'foo'",id:"INT NOT NULL auto_increment PRIMARY KEY"})
})
it("handles extended attributes (null)", function() {
var User = sequelize.define('User' + config.rand(), {
username: {type: Sequelize.STRING, allowNull: false}
}, { timestamps: false })
expect(User.attributes).toEqual({username:"VARCHAR(255) NOT NULL",id:"INT NOT NULL auto_increment PRIMARY KEY"})
})
it("handles extended attributes (primaryKey)", function() {
var User = sequelize.define('User' + config.rand(), {
username: {type: Sequelize.STRING, primaryKey: true}
}, { timestamps: false })
expect(User.attributes).toEqual({username:"VARCHAR(255) PRIMARY KEY"})
})
it("adds timestamps", function() {
var User1 = sequelize.define('User' + config.rand(), {})
var User2 = sequelize.define('User' + config.rand(), {}, { timestamps: true })
expect(User1.attributes).toEqual({id:"INT NOT NULL auto_increment PRIMARY KEY", updatedAt:"DATETIME NOT NULL", createdAt:"DATETIME NOT NULL"})
expect(User2.attributes).toEqual({id:"INT NOT NULL auto_increment PRIMARY KEY", updatedAt:"DATETIME NOT NULL", createdAt:"DATETIME NOT NULL"})
})
it("adds deletedAt if paranoid", function() {
var User = sequelize.define('User' + config.rand(), {}, { paranoid: true })
expect(User.attributes).toEqual({id:"INT NOT NULL auto_increment PRIMARY KEY", deletedAt:"DATETIME", updatedAt:"DATETIME NOT NULL", createdAt:"DATETIME NOT NULL"})
})
it("underscores timestamps if underscored", function() {
var User = sequelize.define('User' + config.rand(), {}, { paranoid: true, underscored: true })
expect(User.attributes).toEqual({id:"INT NOT NULL auto_increment PRIMARY KEY", deleted_at:"DATETIME", updated_at:"DATETIME NOT NULL", created_at:"DATETIME NOT NULL"})
})
it("uses the passed model name as tablename if freezeTableName", function() {
var User = sequelize.define('User', {}, {freezeTableName: true})
expect(User.tableName).toEqual('User')
})
it("uses the pluralized modelname as tablename unless freezeTableName", function() {
var User = sequelize.define('User', {}, {freezeTableName: false})
expect(User.tableName).toEqual('Users')
})
it("attaches class and instance methods", function() {
var User = sequelize.define('User', {}, {
classMethods: { doSmth: function(){ return 1 } },
instanceMethods: { makeItSo: function(){ return 2}}
})
expect(User.doSmth).toBeDefined()
expect(User.doSmth()).toEqual(1)
expect(User.makeItSo).toBeUndefined()
expect(User.build().makeItSo).toBeDefined()
expect(User.build().makeItSo()).toEqual(2)
})
it("throws an error if 2 autoIncrements are passed", function() {
expect(function () {
var User = sequelize.define('User', {
userid: {type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true},
userscore: {type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true},
})
}).toThrow('Invalid model definition. Only one autoincrement field allowed.')
})
})
describe('primaryKeys', function() {
it("determines the correct primaryKeys", function() {
var User = sequelize.define('User' + config.rand(), {
foo: {type: Sequelize.STRING, primaryKey: true},
bar: Sequelize.STRING
})
expect(User.primaryKeys).toEqual({"foo":"VARCHAR(255) PRIMARY KEY"})
})
})
//////////// find ////////////// //////////// find //////////////
describe('.find', function() { describe('.find', function() {
......
...@@ -2,20 +2,38 @@ var config = require("./config/config") ...@@ -2,20 +2,38 @@ var config = require("./config/config")
, Sequelize = require("../index") , Sequelize = require("../index")
describe('Sequelize', function() { describe('Sequelize', function() {
it('should pass the global options correctly', function() { var sequelize = null
var sequelize = new Sequelize(config.database, config.username, config.password, {
logging: false,
define: { underscored:true } var setup = function(options) {
sequelize = new Sequelize(config.database, config.username, config.password, options)
return options
}
beforeEach(function() { setup() })
afterEach(function() { sequelize = null })
describe('constructor', function() {
it('should pass the global options correctly', function() {
setup({ logging: false, define: { underscored:true } })
var Model = sequelize.define('model', {name: Sequelize.STRING})
expect(Model.options.underscored).toBeTruthy()
}) })
var Model = sequelize.define('model', {name: Sequelize.STRING})
expect(Model.options.underscored).toBeTruthy()
})
it('should correctly set the host and the port', function() { it('should correctly set the host and the port', function() {
var options = { host: '127.0.0.1', port: 1234 } var options = setup({ host: '127.0.0.1', port: 1234 })
, sequelize = new Sequelize(config.database, config.username, config.password, options)
expect(sequelize.config.host).toEqual(options.host) expect(sequelize.config.host).toEqual(options.host)
expect(sequelize.config.port).toEqual(options.port) expect(sequelize.config.port).toEqual(options.port)
})
})
describe('define', function() {
it("adds a new model to the model manager", function() {
expect(sequelize.modelManager.all.length).toEqual(0)
sequelize.define('foo', { title: Sequelize.STRING })
expect(sequelize.modelManager.all.length).toEqual(1)
})
}) })
}) })
var assert = require("assert")
, config = require("./../config")
, Sequelize = require("./../../index")
, sequelize = new Sequelize('database', 'username', 'password')
module.exports = {
'it should add a new model to the model-manager': function() {
var s = new Sequelize('database', 'username', 'password')
assert.eql(s.modelManager.all.length, 0)
s.define('foo', { title: Sequelize.STRING })
assert.eql(s.modelManager.all.length, 1)
},
'it should handle extended attributes correctly - unique': function() {
var User = sequelize.define('User' + config.rand(), {
username: {type: Sequelize.STRING, unique: true}
}, { timestamps: false })
assert.eql(User.attributes, {username:"VARCHAR(255) UNIQUE",id:"INT NOT NULL auto_increment PRIMARY KEY"})
},
'it should handle extended attributes correctly - default': function() {
var User = sequelize.define('User' + config.rand(), {
username: {type: Sequelize.STRING, defaultValue: 'foo'}
}, { timestamps: false })
assert.eql(User.attributes, {username:"VARCHAR(255) DEFAULT 'foo'",id:"INT NOT NULL auto_increment PRIMARY KEY"})
},
'it should handle extended attributes correctly - null': function() {
var User = sequelize.define('User' + config.rand(), {
username: {type: Sequelize.STRING, allowNull: false}
}, { timestamps: false })
assert.eql(User.attributes, {username:"VARCHAR(255) NOT NULL",id:"INT NOT NULL auto_increment PRIMARY KEY"})
},
'it should handle extended attributes correctly - primary key': function() {
var User = sequelize.define('User' + config.rand(), {
username: {type: Sequelize.STRING, primaryKey: true}
}, { timestamps: false })
assert.eql(User.attributes, {username:"VARCHAR(255) PRIMARY KEY"})
},
'primaryKeys should be correctly determined': function() {
var User = sequelize.define('User' + config.rand(), {
foo: {type: Sequelize.STRING, primaryKey: true},
bar: Sequelize.STRING
})
assert.eql(User.primaryKeys, {"foo":"VARCHAR(255) PRIMARY KEY"})
},
'it should add updatedAt and createdAt if timestamps is undefined or true': function() {
var User1 = sequelize.define('User' + config.rand(), {})
var User2 = sequelize.define('User' + config.rand(), {}, { timestamps: true })
assert.eql(User1.attributes, {id:"INT NOT NULL auto_increment PRIMARY KEY", updatedAt:"DATETIME NOT NULL", createdAt:"DATETIME NOT NULL"})
assert.eql(User2.attributes, {id:"INT NOT NULL auto_increment PRIMARY KEY", updatedAt:"DATETIME NOT NULL", createdAt:"DATETIME NOT NULL"})
},
'it should add deletedAt if paranoid is true': function() {
var User = sequelize.define('User' + config.rand(), {}, { paranoid: true })
assert.eql(User.attributes, {id:"INT NOT NULL auto_increment PRIMARY KEY", deletedAt:"DATETIME", updatedAt:"DATETIME NOT NULL", createdAt:"DATETIME NOT NULL"})
},
'timestamp columns should be underscored if underscored is passed': function() {
var User = sequelize.define('User' + config.rand(), {}, { paranoid: true, underscored: true })
assert.eql(User.attributes, {id:"INT NOT NULL auto_increment PRIMARY KEY", deleted_at:"DATETIME", updated_at:"DATETIME NOT NULL", created_at:"DATETIME NOT NULL"})
},
'tablenames should be as passed if they are frozen': function() {
var User = sequelize.define('User', {}, {freezeTableName: true})
assert.eql(User.tableName, 'User')
},
'tablenames should be pluralized if they are not frozen': function() {
var User = sequelize.define('User', {}, {freezeTableName: false})
assert.eql(User.tableName, 'Users')
},
'it should add the passed class/instance methods': function() {
var User = sequelize.define('User', {}, {
classMethods: { doSmth: function(){ return 1 } },
instanceMethods: { makeItSo: function(){ return 2}}
})
assert.isDefined(User.doSmth)
assert.eql(User.doSmth(), 1)
assert.isUndefined(User.makeItSo)
assert.isDefined(User.build().makeItSo)
assert.eql(User.build().makeItSo(), 2)
},
'it shouldn\'t allow two auto increment fields': function() {
assert.throws(function () {
var User = sequelize.define('User', {
userid: {type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true},
userscore: {type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true},
})
})
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!