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

Commit 7e823676 by Sascha Depold

Merge branch 'mocha' of github.com:sequelize/sequelize into mocha

2 parents fa959096 cc5ffa4f
test: REPORTER ?= dot
@./node_modules/mocha/bin/mocha -c $(find ./test -name "*.test.js") TESTS = $(shell find ./test/* -name "*.test.js")
.PHONY: test sqlite:
@DIALECT=sqlite ./node_modules/mocha/bin/mocha \
--colors \
--reporter $(REPORTER) \
$(TESTS)
mysql:
@DIALECT=mysql ./node_modules/mocha/bin/mocha \
--colors \
--reporter $(REPORTER) \
$(TESTS)
.PHONY: sqlite mysql
\ No newline at end of file
...@@ -10,10 +10,11 @@ var chai = require('chai') ...@@ -10,10 +10,11 @@ var chai = require('chai')
chai.Assertion.includeStack = true chai.Assertion.includeStack = true
describe(Support.getTestDialectTeaser("Configuration"), function() { describe(Support.getTestDialectTeaser("Configuration"), function() {
/*describe('Connections problems should fail with a nice message', function() { describe.skip('Connections problems should fail with a nice message', function() {
it("when we don't have the correct server details", function(done) { it("when we don't have the correct server details", function(done) {
if (noDomains === true) { if (noDomains === true) {
console.log('WARNING: Configuration specs requires NodeJS version >= 0.8 for full compatibility') console.log('WARNING: Configuration specs requires NodeJS version >= 0.8 for full compatibility')
expect('').to.equal('') // Silence Buster!
done() done()
} else { } else {
var seq = new Sequelize(config[dialect].database, config[dialect].username, config[dialect].password, {storage: '/path/to/no/where/land', logging: false, host: '0.0.0.1', port: config[dialect].port, dialect: dialect}) var seq = new Sequelize(config[dialect].database, config[dialect].username, config[dialect].password, {storage: '/path/to/no/where/land', logging: false, host: '0.0.0.1', port: config[dialect].port, dialect: dialect})
...@@ -37,11 +38,11 @@ describe(Support.getTestDialectTeaser("Configuration"), function() { ...@@ -37,11 +38,11 @@ describe(Support.getTestDialectTeaser("Configuration"), function() {
it('when we don\'t have the correct login information', function(done) { it('when we don\'t have the correct login information', function(done) {
if (dialect !== "postgres" && dialect !== "postgres-native" && dialect !== "mysql") { if (dialect !== "postgres" && dialect !== "postgres-native" && dialect !== "mysql") {
console.log('This dialect doesn\'t support me :(') console.log('This dialect doesn\'t support me :(')
expect('').toEqual('') // Silence Buster expect('').to.equal('') // Silence Buster
return done() return done()
} else if (noDomains === true) { } else if (noDomains === true) {
console.log('WARNING: Configuration specs requires NodeJS version >= 0.8 for full compatibility') console.log('WARNING: Configuration specs requires NodeJS version >= 0.8 for full compatibility')
expect('').toEqual('') // Silence Buster! expect('').to.equal('') // Silence Buster!
return done() return done()
} else { } else {
var seq = new Sequelize(config[dialect].database, config[dialect].username, 'fakepass123', {logging: false, host: config[dialect].host, port: 1, dialect: dialect}) var seq = new Sequelize(config[dialect].database, config[dialect].username, 'fakepass123', {logging: false, host: config[dialect].host, port: 1, dialect: dialect})
...@@ -67,7 +68,7 @@ describe(Support.getTestDialectTeaser("Configuration"), function() { ...@@ -67,7 +68,7 @@ describe(Support.getTestDialectTeaser("Configuration"), function() {
}).to.throw('The dialect undefined is not supported.') }).to.throw('The dialect undefined is not supported.')
done() done()
}) })
})*/ })
describe('Instantiation with a URL string', function() { describe('Instantiation with a URL string', function() {
it('should accept username, password, host, port, and database', function() { it('should accept username, password, host, port, and database', function() {
...@@ -84,42 +85,46 @@ describe(Support.getTestDialectTeaser("Configuration"), function() { ...@@ -84,42 +85,46 @@ describe(Support.getTestDialectTeaser("Configuration"), function() {
expect(config.port).to.equal('9821') expect(config.port).to.equal('9821')
}) })
it('should work with no authentication options', function() { it('should work with no authentication options', function(done) {
var sequelize = new Sequelize('mysql://example.com:9821/dbname') var sequelize = new Sequelize('mysql://example.com:9821/dbname')
var config = sequelize.config var config = sequelize.config
expect(config.username).to.equal(undefined) expect(config.username).to.not.be.ok
expect(config.password).to.equal(null) expect(config.password).to.be.null
done()
}) })
it('should use the default port when no other is specified', function() { it('should use the default port when no other is specified', function(done) {
var sequelize = new Sequelize('mysql://example.com/dbname') var sequelize = new Sequelize('mysql://example.com/dbname')
var config = sequelize.config var config = sequelize.config
// The default port should be set // The default port should be set
expect(config.port).to.equal(3306) expect(config.port).to.equal(3306)
done()
}) })
}) })
describe('Intantiation with arguments', function() { describe('Intantiation with arguments', function() {
it('should accept two parameters (database, username)', function() { it('should accept two parameters (database, username)', function(done) {
var sequelize = new Sequelize('dbname', 'root') var sequelize = new Sequelize('dbname', 'root')
var config = sequelize.config var config = sequelize.config
expect(config.database).to.equal('dbname') expect(config.database).to.equal('dbname')
expect(config.username).to.equal('root') expect(config.username).to.equal('root')
done()
}) })
it('should accept three parameters (database, username, password)', function() { it('should accept three parameters (database, username, password)', function(done) {
var sequelize = new Sequelize('dbname', 'root', 'pass') var sequelize = new Sequelize('dbname', 'root', 'pass')
var config = sequelize.config var config = sequelize.config
expect(config.database).to.equal('dbname') expect(config.database).to.equal('dbname')
expect(config.username).to.equal('root') expect(config.username).to.equal('root')
expect(config.password).to.equal('pass') expect(config.password).to.equal('pass')
done()
}) })
it('should accept four parameters (database, username, password, options)', function() { it('should accept four parameters (database, username, password, options)', function(done) {
var sequelize = new Sequelize('dbname', 'root', 'pass', { port: 999 }) var sequelize = new Sequelize('dbname', 'root', 'pass', { port: 999 })
var config = sequelize.config var config = sequelize.config
...@@ -127,6 +132,7 @@ describe(Support.getTestDialectTeaser("Configuration"), function() { ...@@ -127,6 +132,7 @@ describe(Support.getTestDialectTeaser("Configuration"), function() {
expect(config.username).to.equal('root') expect(config.username).to.equal('root')
expect(config.password).to.equal('pass') expect(config.password).to.equal('pass')
expect(config.port).to.equal(999) expect(config.port).to.equal(999)
done()
}) })
}) })
}) })
...@@ -2,6 +2,7 @@ var chai = require('chai') ...@@ -2,6 +2,7 @@ var chai = require('chai')
, expect = chai.expect , expect = chai.expect
, Sequelize = require(__dirname + '/../index') , Sequelize = require(__dirname + '/../index')
, Support = require(__dirname + '/support') , Support = require(__dirname + '/support')
, config = require(__dirname + '/config/config')
chai.Assertion.includeStack = true chai.Assertion.includeStack = true
...@@ -203,7 +204,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() { ...@@ -203,7 +204,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
validations[validator].msg = message validations[validator].msg = message
var UserFail = this.sequelize.define('User' + Math.random(), { var UserFail = this.sequelize.define('User' + config.rand(), {
name: { name: {
type: Sequelize.STRING, type: Sequelize.STRING,
validate: validations validate: validations
...@@ -222,6 +223,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() { ...@@ -222,6 +223,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
//////////////////////////// ////////////////////////////
// test the success cases // // test the success cases //
//////////////////////////// ////////////////////////////
for (var j = 0; j < validatorDetails.pass.length; j++) { for (var j = 0; j < validatorDetails.pass.length; j++) {
var succeedingValue = validatorDetails.pass[j] var succeedingValue = validatorDetails.pass[j]
...@@ -236,7 +238,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() { ...@@ -236,7 +238,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
validations[validator].msg = validator + "(" + succeedingValue + ")" validations[validator].msg = validator + "(" + succeedingValue + ")"
var UserSuccess = this.sequelize.define('User' + Math.random(), { var UserSuccess = this.sequelize.define('User' + config.rand(), {
name: { name: {
type: Sequelize.STRING, type: Sequelize.STRING,
validate: validations validate: validations
...@@ -252,7 +254,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() { ...@@ -252,7 +254,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
} }
it('correctly validates using custom validation methods', function(done) { it('correctly validates using custom validation methods', function(done) {
var User = this.sequelize.define('User' + Math.random(), { var User = this.sequelize.define('User' + config.rand(), {
name: { name: {
type: Sequelize.STRING, type: Sequelize.STRING,
validate: { validate: {
...@@ -278,7 +280,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() { ...@@ -278,7 +280,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
}) })
it('skips other validations if allowNull is true and the value is null', function(done) { it('skips other validations if allowNull is true and the value is null', function(done) {
var User = this.sequelize.define('User' + Math.random(), { var User = this.sequelize.define('User' + config.rand(), {
age: { age: {
type: Sequelize.INTEGER, type: Sequelize.INTEGER,
allowNull: true, allowNull: true,
...@@ -304,7 +306,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() { ...@@ -304,7 +306,7 @@ describe(Support.getTestDialectTeaser("DaoValidator"), function() {
}) })
it('validates a model with custom model-wide validation methods', function(done) { it('validates a model with custom model-wide validation methods', function(done) {
var Foo = this.sequelize.define('Foo' + Math.random(), { var Foo = this.sequelize.define('Foo' + config.rand(), {
field1: { field1: {
type: Sequelize.INTEGER, type: Sequelize.INTEGER,
allowNull: true allowNull: true
......
/* jshint camelcase: false */
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, sinon = require('sinon')
, CustomEventEmitter = require("../../lib/emitters/custom-event-emitter")
chai.Assertion.includeStack = true
describe(Support.getTestDialectTeaser("CustomEventEmitter"), function () {
describe("proxy", function () {
it("should correctly work with success listeners", function(done) {
var emitter = new CustomEventEmitter()
, proxy = new CustomEventEmitter()
, success = sinon.spy()
emitter.success(success)
proxy.success(function () {
process.nextTick(function () {
expect(success.called).to.be.true
done()
})
})
proxy.proxy(emitter)
proxy.emit('success')
})
it("should correctly work with error listeners", function(done) {
var emitter = new CustomEventEmitter()
, proxy = new CustomEventEmitter()
, error = sinon.spy()
emitter.error(error)
proxy.error(function() {
process.nextTick(function() {
expect(error.called).to.be.true
done()
})
})
proxy.proxy(emitter)
proxy.emit('error')
})
it("should correctly work with complete/done listeners", function(done) {
var emitter = new CustomEventEmitter()
, proxy = new CustomEventEmitter()
, complete = sinon.spy()
emitter.complete(complete)
proxy.complete(function() {
process.nextTick(function() {
expect(complete.called).to.be.true
done()
})
})
proxy.proxy(emitter)
proxy.emit('success')
})
})
})
...@@ -6,17 +6,17 @@ var chai = require('chai') ...@@ -6,17 +6,17 @@ var chai = require('chai')
chai.Assertion.includeStack = true chai.Assertion.includeStack = true
describe(Support.getTestDialectTeaser("Language Util"), function() { describe(Support.getTestDialectTeaser("Language Util"), function() {
before(function(done) {
this.sequelize.options.language = 'es'
done()
})
after(function(done) { after(function(done) {
this.sequelize.options.language = 'en' this.sequelize.options.language = 'en'
done() done()
}) })
describe("Plural", function(){ describe("Plural", function(){
before(function(done) {
this.sequelize.options.language = 'es'
done()
})
it("should rename tables to their plural form...", function(done){ it("should rename tables to their plural form...", function(done){
var self = this var self = this
, table = self.sequelize.define('arbol', {name: Sequelize.STRING}) , table = self.sequelize.define('arbol', {name: Sequelize.STRING})
......
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, dialect = Support.getTestDialect()
, sinon = require('sinon')
, DataTypes = require(__dirname + "/../../lib/data-types")
chai.Assertion.includeStack = true
if (dialect.match(/^mysql/)) {
describe('[MYSQL Specific] Associations', function() {
describe('many-to-many', function() {
describe('where tables have the same prefix', function() {
it("should create a table wp_table1wp_table2s", function(done) {
var Table2 = this.sequelize.define('wp_table2', {foo: DataTypes.STRING})
, Table1 = this.sequelize.define('wp_table1', {foo: DataTypes.STRING})
, self = this
Table1.hasMany(Table2)
Table2.hasMany(Table1)
Table1.sync({ force: true }).success(function() {
Table2.sync({ force: true }).success(function() {
expect(self.sequelize.daoFactoryManager.getDAO('wp_table1swp_table2s')).to.exist
done()
})
})
})
})
describe('when join table name is specified', function() {
beforeEach(function(done){
var Table2 = this.sequelize.define('ms_table1', {foo: DataTypes.STRING})
, Table1 = this.sequelize.define('ms_table2', {foo: DataTypes.STRING})
Table1.hasMany(Table2, {joinTableName: 'table1_to_table2'})
Table2.hasMany(Table1, {joinTableName: 'table1_to_table2'})
Table1.sync({ force: true }).success(function() {
Table2.sync({ force: true }).success(function() {
done()
})
})
})
it("should not use only a specified name", function() {
expect(this.sequelize.daoFactoryManager.getDAO('ms_table1sms_table2s')).not.to.exist
expect(this.sequelize.daoFactoryManager.getDAO('table1_to_table2')).to.exist
})
})
})
describe('HasMany', function() {
beforeEach(function(done) {
//prevent periods from occurring in the table name since they are used to delimit (table.column)
this.User = this.sequelize.define('User' + Math.ceil(Math.random()*10000000), { name: DataTypes.STRING })
this.Task = this.sequelize.define('Task' + Math.ceil(Math.random()*10000000), { name: DataTypes.STRING })
this.users = null
this.tasks = null
this.User.hasMany(this.Task, {as:'Tasks'})
this.Task.hasMany(this.User, {as:'Users'})
var self = this
, users = []
, tasks = []
for (var i = 0; i < 5; ++i) {
users[users.length] = {name: 'User' + Math.random()}
}
for (var x = 0; x < 5; ++x) {
tasks[tasks.length] = {name: 'Task' + Math.random()}
}
this.User.sync({ force: true }).success(function() {
self.Task.sync({ force: true }).success(function() {
self.User.bulkCreate(users).success(function() {
self.Task.bulkCreate(tasks).success(function() {
done()
})
})
})
})
})
describe('addDAO / getDAO', function() {
beforeEach(function(done) {
var self = this
self.user = null
self.task = null
self.User.all().success(function(_users) {
self.Task.all().success(function(_tasks) {
self.user = _users[0]
self.task = _tasks[0]
done()
})
})
})
it('should correctly add an association to the dao', function(done) {
var self = this
self.user.getTasks().on('success', function(_tasks) {
expect(_tasks.length).to.equal(0)
self.user.addTask(self.task).on('success', function() {
self.user.getTasks().on('success', function(_tasks) {
expect(_tasks.length).to.equal(1)
done()
})
})
})
})
})
describe('removeDAO', function() {
beforeEach(function(done) {
var self = this
self.user = null
self.tasks = null
self.User.all().success(function(_users) {
self.Task.all().success(function(_tasks) {
self.user = _users[0]
self.tasks = _tasks
done()
})
})
})
it("should correctly remove associated objects", function(done) {
var self = this
self.user.getTasks().on('success', function(__tasks) {
expect(__tasks.length).to.equal(0)
self.user.setTasks(self.tasks).on('success', function() {
self.user.getTasks().on('success', function(_tasks) {
expect(_tasks.length).to.equal(self.tasks.length)
self.user.removeTask(self.tasks[0]).on('success', function() {
self.user.getTasks().on('success', function(_tasks) {
expect(_tasks.length).to.equal(self.tasks.length - 1)
done()
})
})
})
})
})
})
})
})
})
}
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, dialect = Support.getTestDialect()
, sinon = require('sinon')
, DataTypes = require(__dirname + "/../../lib/data-types")
chai.Assertion.includeStack = true
if (dialect.match(/^mysql/)) {
describe('[MYSQL Specific] Connector Manager', function() {
this.timeout(10000)
it('works correctly after being idle', function(done) {
var User = this.sequelize.define('User', { username: DataTypes.STRING })
, spy = sinon.spy()
User.sync({force: true}).on('success', function() {
User.create({username: 'user1'}).on('success', function() {
User.count().on('success', function(count) {
expect(count).to.equal(1)
spy()
setTimeout(function() {
User.count().on('success', function(count) {
expect(count).to.equal(1)
spy()
if (spy.calledTwice) {
done()
}
})
}, 1000)
})
})
})
})
})
}
/* jshint camelcase: false */
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + "/../../lib/data-types")
, dialect = Support.getTestDialect()
, config = require(__dirname + "/../config/config")
chai.Assertion.includeStack = true
if (dialect.match(/^mysql/)) {
describe("[MYSQL Specific] DAOFactory", function () {
describe('constructor', function() {
it("handles extended attributes (unique)", function(done) {
var User = this.sequelize.define('User' + config.rand(), {
username: { type: DataTypes.STRING, unique: true }
}, { timestamps: false })
expect(User.attributes).to.deep.equal({username:"VARCHAR(255) UNIQUE",id:"INTEGER NOT NULL auto_increment PRIMARY KEY"})
done()
})
it("handles extended attributes (default)", function(done) {
var User = this.sequelize.define('User' + config.rand(), {
username: {type: DataTypes.STRING, defaultValue: 'foo'}
}, { timestamps: false })
expect(User.attributes).to.deep.equal({username:"VARCHAR(255) DEFAULT 'foo'",id:"INTEGER NOT NULL auto_increment PRIMARY KEY"})
done()
})
it("handles extended attributes (null)", function(done) {
var User = this.sequelize.define('User' + config.rand(), {
username: {type: DataTypes.STRING, allowNull: false}
}, { timestamps: false })
expect(User.attributes).to.deep.equal({username:"VARCHAR(255) NOT NULL",id:"INTEGER NOT NULL auto_increment PRIMARY KEY"})
done()
})
it("handles extended attributes (comment)", function(done) {
var User = this.sequelize.define('User' + config.rand(), {
username: {type: DataTypes.STRING, comment: 'This be\'s a comment'}
}, { timestamps: false })
expect(User.attributes).to.deep.equal({username:"VARCHAR(255) COMMENT 'This be\\'s a comment'",id:"INTEGER NOT NULL auto_increment PRIMARY KEY"})
done()
})
it("handles extended attributes (primaryKey)", function(done) {
var User = this.sequelize.define('User' + config.rand(), {
username: {type: DataTypes.STRING, primaryKey: true}
}, { timestamps: false })
expect(User.attributes).to.deep.equal({username:"VARCHAR(255) PRIMARY KEY"})
done()
})
it("adds timestamps", function(done) {
var User1 = this.sequelize.define('User' + config.rand(), {})
var User2 = this.sequelize.define('User' + config.rand(), {}, { timestamps: true })
expect(User1.attributes).to.deep.equal({id:"INTEGER NOT NULL auto_increment PRIMARY KEY", updatedAt:"DATETIME NOT NULL", createdAt:"DATETIME NOT NULL"})
expect(User2.attributes).to.deep.equal({id:"INTEGER NOT NULL auto_increment PRIMARY KEY", updatedAt:"DATETIME NOT NULL", createdAt:"DATETIME NOT NULL"})
done()
})
it("adds deletedAt if paranoid", function(done) {
var User = this.sequelize.define('User' + config.rand(), {}, { paranoid: true })
expect(User.attributes).to.deep.equal({id:"INTEGER NOT NULL auto_increment PRIMARY KEY", deletedAt:"DATETIME", updatedAt:"DATETIME NOT NULL", createdAt:"DATETIME NOT NULL"})
done()
})
it("underscores timestamps if underscored", function(done) {
var User = this.sequelize.define('User' + config.rand(), {}, { paranoid: true, underscored: true })
expect(User.attributes).to.deep.equal({id:"INTEGER NOT NULL auto_increment PRIMARY KEY", deleted_at:"DATETIME", updated_at:"DATETIME NOT NULL", created_at:"DATETIME NOT NULL"})
done()
})
})
describe('primaryKeys', function() {
it("determines the correct primaryKeys", function(done) {
var User = this.sequelize.define('User' + config.rand(), {
foo: {type: DataTypes.STRING, primaryKey: true},
bar: DataTypes.STRING
})
expect(User.primaryKeys).to.deep.equal({"foo":"VARCHAR(255) PRIMARY KEY"})
done()
})
})
})
}
var chai = require('chai') var chai = require('chai')
, expect = chai.expect , expect = chai.expect
, Support = require(__dirname + '/support') , Support = require(__dirname + '/support')
, DataTypes = require(__dirname + "/../lib/data-types")
, dialect = Support.getTestDialect()
, QueryChainer = require("../lib/query-chainer") , QueryChainer = require("../lib/query-chainer")
, CustomEventEmitter = require("../lib/emitters/custom-event-emitter") , CustomEventEmitter = require("../lib/emitters/custom-event-emitter")
, _ = require('lodash')
chai.Assertion.includeStack = true chai.Assertion.includeStack = true
...@@ -16,7 +13,7 @@ describe(Support.getTestDialectTeaser("QueryChainer"), function () { ...@@ -16,7 +13,7 @@ describe(Support.getTestDialectTeaser("QueryChainer"), function () {
}) })
describe('add', function() { describe('add', function() {
it('adds a new serial item if method is passed', function(done) { it('adds a new serial item if method is passed', function(done) {
expect(this.queryChainer.serials.length).to.equal(0) expect(this.queryChainer.serials.length).to.equal(0)
this.queryChainer.add({}, 'foo') this.queryChainer.add({}, 'foo')
expect(this.queryChainer.serials.length).to.equal(1) expect(this.queryChainer.serials.length).to.equal(1)
...@@ -57,7 +54,7 @@ describe(Support.getTestDialectTeaser("QueryChainer"), function () { ...@@ -57,7 +54,7 @@ describe(Support.getTestDialectTeaser("QueryChainer"), function () {
this.queryChainer.run().success(function(results) { this.queryChainer.run().success(function(results) {
expect(results).to.exist expect(results).to.exist
expect(results.length).to.equal(1) expect(results).to.have.length(1)
expect(results[0]).to.equal(1) expect(results[0]).to.equal(1)
done() done()
}) })
...@@ -75,7 +72,7 @@ describe(Support.getTestDialectTeaser("QueryChainer"), function () { ...@@ -75,7 +72,7 @@ describe(Support.getTestDialectTeaser("QueryChainer"), function () {
this.queryChainer.add(emitter3) this.queryChainer.add(emitter3)
this.queryChainer.run().success(function(results) { this.queryChainer.run().success(function(results) {
expect(results.length).to.equal(3) expect(results).to.have.length(3)
expect(results).to.include.members([1,2,3]) expect(results).to.include.members([1,2,3])
done() done()
}) })
...@@ -126,7 +123,7 @@ describe(Support.getTestDialectTeaser("QueryChainer"), function () { ...@@ -126,7 +123,7 @@ describe(Support.getTestDialectTeaser("QueryChainer"), function () {
this.queryChainer.runSerially().success(function(results) { this.queryChainer.runSerially().success(function(results) {
expect(results).to.exist expect(results).to.exist
expect(results.length).to.equal(1) expect(results).to.have.length(1)
expect(results[0]).to.equal(1) expect(results[0]).to.equal(1)
done() done()
}) })
...@@ -142,7 +139,7 @@ describe(Support.getTestDialectTeaser("QueryChainer"), function () { ...@@ -142,7 +139,7 @@ describe(Support.getTestDialectTeaser("QueryChainer"), function () {
this.queryChainer.add(emitter3, 'run') this.queryChainer.add(emitter3, 'run')
this.queryChainer.runSerially().success(function(results) { this.queryChainer.runSerially().success(function(results) {
expect(results.length).to.equal(3) expect(results).to.have.length(3)
expect(results).to.contain.members([1,2,3]) expect(results).to.contain.members([1,2,3])
done() done()
}) })
......
/* jshint camelcase: false */
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + "/../../lib/data-types")
, dialect = Support.getTestDialect()
, dbFile = __dirname + '/test.sqlite'
, storages = [dbFile]
chai.Assertion.includeStack = true
if (dialect === 'sqlite') {
describe('[SQLITE Specific] DAOFactory', function() {
after(function(done) {
this.sequelize.options.storage = ':memory:'
done()
})
beforeEach(function(done) {
this.sequelize.options.storage = dbFile
this.User = this.sequelize.define('User', {
age: DataTypes.INTEGER,
name: DataTypes.STRING,
bio: DataTypes.TEXT
})
this.User.sync({ force: true }).success(function() {
done()
})
})
storages.forEach(function(storage) {
describe('with storage "' + storage + '"', function() {
after(function(done) {
if (storage === dbFile) {
require("fs").writeFile(dbFile, '', function() {
done()
})
}
})
describe('create', function() {
it('creates a table entry', function(done) {
var self = this
this.User.create({ age: 21, name: 'John Wayne', bio: 'noot noot' }).success(function(user) {
expect(user.age).to.equal(21)
expect(user.name).to.equal('John Wayne')
expect(user.bio).to.equal('noot noot')
self.User.all().success(function(users) {
var usernames = users.map(function(user) {
return user.name
})
expect(usernames).to.contain('John Wayne')
done()
})
})
})
it('should allow the creation of an object with options as attribute', function(done) {
var Person = this.sequelize.define('Person', {
name: DataTypes.STRING,
options: DataTypes.TEXT
})
Person.sync({ force: true }).success(function() {
var options = JSON.stringify({ foo: 'bar', bar: 'foo' })
Person.create({
name: 'John Doe',
options: options
}).success(function(people) {
expect(people.options).to.deep.equal(options)
done()
})
})
})
it('should allow the creation of an object with a boolean (true) as attribute', function(done) {
var Person = this.sequelize.define('Person', {
name: DataTypes.STRING,
has_swag: DataTypes.BOOLEAN
})
Person.sync({ force: true }).success(function() {
Person.create({
name: 'John Doe',
has_swag: true
}).success(function(people) {
expect(people.has_swag).to.be.ok
done()
})
})
})
it('should allow the creation of an object with a boolean (false) as attribute', function(done) {
var Person = this.sequelize.define('Person', {
name: DataTypes.STRING,
has_swag: DataTypes.BOOLEAN
})
Person.sync({ force: true }).success(function() {
Person.create({
name: 'John Doe',
has_swag: false
}).success(function(people) {
expect(people.has_swag).to.not.be.ok
done()
})
})
})
})
describe('.find', function() {
beforeEach(function(done) {
this.User.create({name: 'user', bio: 'footbar'}).success(function() {
done()
})
})
it("finds normal lookups", function(done) {
this.User.find({ where: { name:'user' } }).success(function(user) {
expect(user.name).to.equal('user')
done()
})
})
it("should make aliased attributes available", function(done) {
this.User.find({ where: { name:'user' }, attributes: ['id', ['name', 'username']] }).success(function(user) {
expect(user.username).to.equal('user')
done()
})
})
})
describe('.all', function() {
beforeEach(function(done) {
this.User.bulkCreate([
{name: 'user', bio: 'foobar'},
{name: 'user', bio: 'foobar'}
]).success(function() {
done()
})
})
it("should return all users", function(done) {
this.User.all().on('success', function(users) {
expect(users).to.have.length(2)
done()
})
})
})
describe('.min', function() {
it("should return the min value", function(done) {
var self = this
, users = []
for (var i = 2; i < 5; i++) {
users[users.length] = {age: i}
}
this.User.bulkCreate(users).success(function() {
self.User.min('age').on('success', function(min) {
expect(min).to.equal(2)
done()
})
})
})
})
describe('.max', function() {
it("should return the max value", function(done) {
var self = this
, users = []
for (var i = 2; i <= 5; i++) {
users[users.length] = {age: i}
}
this.User.bulkCreate(users).success(function() {
self.User.max('age').on('success', function(min) {
expect(min).to.equal(5)
done()
})
})
})
})
})
})
})
}
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + "/../../lib/data-types")
, dialect = Support.getTestDialect()
chai.Assertion.includeStack = true
if (dialect === 'sqlite') {
describe('[SQLITE Specific] DAO', function() {
beforeEach(function(done) {
this.User = this.sequelize.define('User', {
username: DataTypes.STRING
})
this.User.sync({ force: true }).success(function() {
done()
})
})
describe('findAll', function() {
it("handles dates correctly", function(done) {
var self = this
this.User
.create({ username: 'user', createdAt: new Date(2011, 04, 04) })
.success(function() {
self.User.create({ username: 'new user' }).success(function() {
self.User.findAll({
where: ['createdAt > ?', new Date(2012, 01, 01)]
}).success(function(users) {
expect(users).to.have.length(1)
done()
})
})
})
})
})
})
}
File mode changed
...@@ -2,10 +2,8 @@ var fs = require('fs') ...@@ -2,10 +2,8 @@ var fs = require('fs')
, Sequelize = require(__dirname + "/../index") , Sequelize = require(__dirname + "/../index")
, DataTypes = require(__dirname + "/../lib/data-types") , DataTypes = require(__dirname + "/../lib/data-types")
, config = require(__dirname + "/config/config") , config = require(__dirname + "/config/config")
, chai = require('chai')
, expect = chai.expect
module.exports = { var Support = {
Sequelize: Sequelize, Sequelize: Sequelize,
initTests: function(options) { initTests: function(options) {
...@@ -113,3 +111,18 @@ module.exports = { ...@@ -113,3 +111,18 @@ module.exports = {
return "[" + dialect.toUpperCase() + "] " + moduleName return "[" + dialect.toUpperCase() + "] " + moduleName
} }
} }
var sequelize = Support.createSequelizeInstance({ dialect: Support.getTestDialect() })
before(function(done) {
this.sequelize = sequelize
done()
})
afterEach(function(done) {
Support.clearDatabase(this.sequelize, function() {
done()
})
})
module.exports = Support
var Support = require(__dirname + '/support')
, dialect = Support.getTestDialect()
var sequelize = Support.createSequelizeInstance({ dialect: dialect })
before(function(done) {
this.sequelize = sequelize
done()
})
afterEach(function(done) {
Support.clearDatabase(this.sequelize, done)
})
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!