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

Commit ed4aa9fe by Sascha Depold

moved tests into the right scope

1 parent 5a8a6800
Showing with 871 additions and 869 deletions
...@@ -1514,648 +1514,587 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -1514,648 +1514,587 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
}) })
describe('special where conditions/smartWhere object', function() { describe('find', function() {
beforeEach(function(done) { it('supports transactions', function(done) {
var self = this Support.prepareTransactionTest(dialect, this.sequelize, function(sequelize) {
var User = sequelize.define('User', { username: Sequelize.STRING })
this.User.bulkCreate([ User.sync({ force: true }).success(function() {
{username: 'boo', intVal: 5, theDate: '2013-01-01 12:00'}, sequelize.transaction(function(t) {
{username: 'boo2', intVal: 10, theDate: '2013-01-10 12:00'} User.create({ username: 'foo' }, { transaction: t }).success(function() {
]).success(function(user2) { User.find({ username: 'foo' }).success(function(user1) {
User.find({ username: 'foo' }, { transaction: t }).success(function(user2) {
expect(user1).to.be.null
expect(user2).to.not.be.null
t.rollback().success(function() {
done() done()
}) })
}) })
})
it('should be able to find rows where attribute is in a list of values', function (done) { })
this.User.findAll({ })
where: { })
username: ['boo', 'boo2'] })
}
}).success(function(users){
expect(users).to.have.length(2);
done()
});
}) })
it('should not break when trying to find rows using an array of primary keys', function (done) { describe('general / basic function', function() {
this.User.findAll({ beforeEach(function(done) {
where: { var self = this
id: [1, 2, 3] this.User.create({username: 'barfooz'}).success(function(user) {
self.UserPrimary = self.sequelize.define('UserPrimary', {
specialkey: {
type: DataTypes.STRING,
primaryKey: true
} }
}).success(function(users){
done();
});
}) })
it('should be able to find a row using like', function(done) { self.UserPrimary.sync({force: true}).success(function() {
this.User.findAll({ self.UserPrimary.create({specialkey: 'a string'}).success(function() {
where: { self.user = user
username: {
like: '%2'
}
}
}).success(function(users) {
expect(users).to.be.an.instanceof(Array)
expect(users).to.have.length(1)
expect(users[0].username).to.equal('boo2')
expect(users[0].intVal).to.equal(10)
done() done()
}) })
}) })
it('should be able to find a row using not like', function(done) {
this.User.findAll({
where: {
username: {
nlike: '%2'
}
}
}).success(function(users) {
expect(users).to.be.an.instanceof(Array)
expect(users).to.have.length(1)
expect(users[0].username).to.equal('boo')
expect(users[0].intVal).to.equal(5)
done()
}) })
}) })
it('should be able to find a row between a certain date using the between shortcut', function(done) { it('does not modify the passed arguments', function (done) {
this.User.findAll({ var options = { where: ['specialkey = ?', 'awesome']}
where: {
theDate: { this.UserPrimary.find(options).success(function(user) {
'..': ['2013-01-02', '2013-01-11'] expect(options).to.deep.equal({ where: ['specialkey = ?', 'awesome']})
}
}
}).success(function(users) {
expect(users[0].username).to.equal('boo2')
expect(users[0].intVal).to.equal(10)
done() done()
}) })
}) })
it('should be able to find a row not between a certain integer using the not between shortcut', function(done) { it('doesn\'t throw an error when entering in a non integer value for a specified primary field', function(done) {
this.User.findAll({ this.UserPrimary.find('a string').success(function(user) {
where: { expect(user.specialkey).to.equal('a string')
intVal: {
'!..': [8, 10]
}
}
}).success(function(users) {
expect(users[0].username).to.equal('boo')
expect(users[0].intVal).to.equal(5)
done() done()
}) })
}) })
it('should be able to handle false/true values just fine...', function(done) { it('doesn\'t throw an error when entering in a non integer value', function(done) {
var User = this.User this.User.find('a string value').success(function(user) {
, escapeChar = (dialect === "postgres") ? '"' : '`' expect(user).to.be.null
User.bulkCreate([
{username: 'boo5', aBool: false},
{username: 'boo6', aBool: true}
]).success(function() {
User.all({where: [escapeChar + 'aBool' + escapeChar + ' = ?', false]}).success(function(users) {
expect(users).to.have.length(1)
expect(users[0].username).to.equal('boo5')
User.all({where: [escapeChar + 'aBool' + escapeChar + ' = ?', true]}).success(function(_users) {
expect(_users).to.have.length(1)
expect(_users[0].username).to.equal('boo6')
done() done()
}) })
}) })
it('returns a single dao', function(done) {
var self = this
this.User.find(this.user.id).success(function(user) {
expect(Array.isArray(user)).to.not.be.ok
expect(user.id).to.equal(self.user.id)
expect(user.id).to.equal(1)
done()
}) })
}) })
it('should be able to handle false/true values through associations as well...', function(done) { it('returns a single dao given a string id', function(done) {
var User = this.User var self = this
, escapeChar = (dialect === "postgres") ? '"' : '`' this.User.find(this.user.id + '').success(function(user) {
var Passports = this.sequelize.define('Passports', { expect(Array.isArray(user)).to.not.be.ok
isActive: Sequelize.BOOLEAN expect(user.id).to.equal(self.user.id)
expect(user.id).to.equal(1)
done()
})
}) })
User.hasMany(Passports) it("should make aliased attributes available", function(done) {
Passports.belongsTo(User) this.User.find({
where: { id: 1 },
User.sync({ force: true }).success(function() { attributes: ['id', ['username', 'name']]
Passports.sync({ force: true }).success(function() { }).success(function(user) {
User.bulkCreate([ expect(user.name).to.equal('barfooz')
{username: 'boo5', aBool: false},
{username: 'boo6', aBool: true}
]).success(function() {
Passports.bulkCreate([
{isActive: true},
{isActive: false}
]).success(function() {
User.find(1).success(function(user) {
Passports.find(1).success(function(passport) {
user.setPassports([passport]).success(function() {
User.find(2).success(function(_user) {
Passports.find(2).success(function(_passport) {
_user.setPassports([_passport]).success(function() {
_user.getPassports({where: [escapeChar + 'isActive' + escapeChar + ' = ?', false]}).success(function(theFalsePassport) {
user.getPassports({where: [escapeChar + 'isActive' + escapeChar + ' = ?', true]}).success(function(theTruePassport) {
expect(theFalsePassport).to.have.length(1)
expect(theFalsePassport[0].isActive).to.be.false
expect(theTruePassport).to.have.length(1)
expect(theTruePassport[0].isActive).to.be.true
done() done()
}) })
}) })
it("should not try to convert boolean values if they are not selected", function(done) {
var UserWithBoolean = this.sequelize.define('UserBoolean', {
active: Sequelize.BOOLEAN
}) })
UserWithBoolean.sync({force: true}).success(function () {
UserWithBoolean.create({ active: true }).success(function(user) {
UserWithBoolean.find({ where: { id: user.id }, attributes: [ 'id' ] }).success(function(user) {
expect(user.active).not.to.exist
done()
}) })
}) })
}) })
}) })
it('finds a specific user via where option', function(done) {
this.User.find({ where: { username: 'barfooz' } }).success(function(user) {
expect(user.username).to.equal('barfooz')
done()
}) })
}) })
it("doesn't find a user if conditions are not matching", function(done) {
this.User.find({ where: { username: 'foo' } }).success(function(user) {
expect(user).to.be.null
done()
}) })
}) })
it('allows sql logging', function(done) {
this.User.find({ where: { username: 'foo' } }).on('sql', function(sql) {
expect(sql).to.exist
expect(sql.toUpperCase().indexOf("SELECT")).to.be.above(-1)
done()
}) })
}) })
it('should be able to return a record with primaryKey being null for new inserts', function(done) { it('ignores passed limit option', function(done) {
var Session = this.sequelize.define('Session', { this.User.find({ limit: 10 }).success(function(user) {
token: { type: DataTypes.TEXT, allowNull: false }, // it returns an object instead of an array
lastUpdate: { type: DataTypes.DATE, allowNull: false } expect(Array.isArray(user)).to.not.be.ok
}, { expect(user.hasOwnProperty('username')).to.be.ok
charset: 'utf8', done()
collate: 'utf8_general_ci',
omitNull: true
}) })
, User = this.sequelize.define('User', {
name: { type: DataTypes.STRING, allowNull: false, unique: true },
password: { type: DataTypes.STRING, allowNull: false },
isAdmin: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: false }
}, {
charset: 'utf8',
collate: 'utf8_general_ci'
}) })
User.hasMany(Session, { as: 'Sessions' }) it('finds entries via primary keys', function(done) {
Session.belongsTo(User) var self = this
, UserPrimary = self.sequelize.define('UserWithPrimaryKey', {
Session.sync({ force: true }).success(function() { identifier: {type: Sequelize.STRING, primaryKey: true},
User.sync({ force: true }).success(function() { name: Sequelize.STRING
User.create({name: 'Name1', password: '123', isAdmin: false}).success(function(user) {
var sess = Session.build({
lastUpdate: new Date(),
token: '123'
}) })
user.addSession(sess).success(function(u) { UserPrimary.sync({ force: true }).success(function() {
expect(u.token).to.equal('123') UserPrimary.create({
identifier: 'an identifier',
name: 'John'
}).success(function(u) {
expect(u.id).not.to.exist
UserPrimary.find('an identifier').success(function(u2) {
expect(u2.identifier).to.equal('an identifier')
expect(u2.name).to.equal('John')
done() done()
}) })
}) })
}) })
}) })
})
it('should be able to find a row between a certain date', function(done) { it('finds entries via a string primary key called id', function(done) {
this.User.findAll({ var self = this
where: { , UserPrimary = self.sequelize.define('UserWithPrimaryKey', {
theDate: { id: {type: Sequelize.STRING, primaryKey: true},
between: ['2013-01-02', '2013-01-11'] name: Sequelize.STRING
}
}
}).success(function(users) {
expect(users[0].username).to.equal('boo2')
expect(users[0].intVal).to.equal(10)
done()
})
}) })
it('should be able to find a row between a certain date and an additional where clause', function(done) { UserPrimary.sync({ force: true }).success(function() {
this.User.findAll({ UserPrimary.create({
where: { id: 'a string based id',
theDate: { name: 'Johnno'
between: ['2013-01-02', '2013-01-11'] }).success(function(u) {
}, UserPrimary.find('a string based id').success(function(u2) {
intVal: 10 expect(u2.id).to.equal('a string based id')
} expect(u2.name).to.equal('Johnno')
}).success(function(users) {
expect(users[0].username).to.equal('boo2')
expect(users[0].intVal).to.equal(10)
done() done()
}) })
}) })
it('should be able to find a row not between a certain integer', function(done) {
this.User.findAll({
where: {
intVal: {
nbetween: [8, 10]
}
}
}).success(function(users) {
expect(users[0].username).to.equal('boo')
expect(users[0].intVal).to.equal(5)
done()
}) })
}) })
it('should be able to find a row using not between and between logic', function(done) {
this.User.findAll({
where: {
theDate: {
between: ['2012-12-10', '2013-01-02'],
nbetween: ['2013-01-04', '2013-01-20']
}
}
}).success(function(users) {
expect(users[0].username).to.equal('boo')
expect(users[0].intVal).to.equal(5)
done()
})
})
it('should be able to find a row using not between and between logic with dates', function(done) { it('returns the selected fields as instance.selectedValues', function(done) {
this.User.findAll({ var self = this
where: { this.User.create({
theDate: { username: 'JohnXOXOXO'
between: [new Date('2012-12-10'), new Date('2013-01-02')], }).success(function() {
nbetween: [new Date('2013-01-04'), new Date('2013-01-20')] self.User.find({
} where: { username: 'JohnXOXOXO' },
} attributes: ['username']
}).success(function(users) { }).success(function(user) {
expect(users[0].username).to.equal('boo') expect(user.selectedValues).to.have.property('username', 'JohnXOXOXO')
expect(users[0].intVal).to.equal(5)
done() done()
}) })
}) })
it('should be able to find a row using greater than or equal to logic with dates', function(done) {
this.User.findAll({
where: {
theDate: {
gte: new Date('2013-01-09')
}
}
}).success(function(users) {
expect(users[0].username).to.equal('boo2')
expect(users[0].intVal).to.equal(10)
done()
}) })
it('returns the selected fields and all fields of the included table as instance.selectedValues', function(done) {
var self = this
self.Mission = self.sequelize.define('Mission', {
title: {type: Sequelize.STRING, defaultValue: 'a mission!!'},
foo: {type: Sequelize.INTEGER, defaultValue: 2},
}) })
it('should be able to find a row using greater than or equal to', function(done) { self.Mission.belongsTo(self.User)
this.User.find({ self.User.hasMany(self.Mission)
where: {
intVal: { self.Mission.sync({ force: true }).success(function() {
gte: 6 self.Mission.create().success(function(mission) {
} self.User.create({username: 'John DOE'}).success(function(user) {
} mission.setUser(user).success(function() {
self.User.find({
where: { username: 'John DOE' },
attributes: ['username'],
include: [self.Mission]
}).success(function(user) { }).success(function(user) {
expect(user.username).to.equal('boo2') expect(user.selectedValues).to.deep.equal({ username: 'John DOE' })
expect(user.intVal).to.equal(10)
done() done()
}) })
}) })
it('should be able to find a row using greater than', function(done) {
this.User.find({
where: {
intVal: {
gt: 5
}
}
}).success(function(user) {
expect(user.username).to.equal('boo2')
expect(user.intVal).to.equal(10)
done()
}) })
}) })
it('should be able to find a row using lesser than or equal to', function(done) {
this.User.find({
where: {
intVal: {
lte: 5
}
}
}).success(function(user) {
expect(user.username).to.equal('boo')
expect(user.intVal).to.equal(5)
done()
}) })
}) })
it('should be able to find a row using lesser than', function(done) { it('returns the selected fields for both the base table and the included table as instance.selectedValues', function(done) {
this.User.find({ var self = this
where: { self.Mission = self.sequelize.define('Mission', {
intVal: { title: {type: Sequelize.STRING, defaultValue: 'another mission!!'},
lt: 6 foo: {type: Sequelize.INTEGER, defaultValue: 4},
} })
}
self.Mission.belongsTo(self.User)
self.User.hasMany(self.Mission)
self.Mission.sync({ force: true }).success(function() {
self.Mission.create().success(function(mission) {
self.User.create({username: 'Brain Picker'}).success(function(user) {
mission.setUser(user).success(function() {
self.User.find({
where: { username: 'Brain Picker' },
attributes: ['username'],
include: [{model: self.Mission, as: self.Mission.tableName, attributes: ['title']}]
}).success(function(user) { }).success(function(user) {
expect(user.username).to.equal('boo') expect(user.selectedValues).to.deep.equal({ username: 'Brain Picker' })
expect(user.intVal).to.equal(5) expect(user.missions[0].selectedValues).to.deep.equal({ id: 1, title: 'another mission!!'})
expect(user.missions[0].foo).not.to.exist
done() done()
}) })
}) })
it('should have no problem finding a row using lesser and greater than', function(done) {
this.User.findAll({
where: {
intVal: {
lt: 6,
gt: 4
}
}
}).success(function(users) {
expect(users[0].username).to.equal('boo')
expect(users[0].intVal).to.equal(5)
done()
}) })
}) })
it('should be able to find a row using not equal to logic', function(done) {
this.User.find({
where: {
intVal: {
ne: 10
}
}
}).success(function(user) {
expect(user.username).to.equal('boo')
expect(user.intVal).to.equal(5)
done()
}) })
}) })
it('should be able to find multiple users with any of the special where logic properties', function(done) { it('always honors ZERO as primary key', function(_done) {
this.User.findAll({ var self = this
where: { , permutations = [
intVal: { 0,
lte: 10 '0',
} {where: {id: 0}},
} {where: {id: '0'}}
}).success(function(users) { ]
expect(users[0].username).to.equal('boo') , done = _.after(2 * permutations.length, _done)
expect(users[0].intVal).to.equal(5)
expect(users[1].username).to.equal('boo2') this.User.bulkCreate([{username: 'jack'}, {username: 'jack'}]).success(function() {
expect(users[1].intVal).to.equal(10) permutations.forEach(function(perm) {
self.User.find(perm).done(function(err, user) {
expect(err).to.be.null
expect(user).to.be.null
done() done()
}).on('sql', function(s) {
expect(s.indexOf(0)).not.to.equal(-1)
done()
})
}) })
}) })
}) })
describe('find', function() { it('should allow us to find IDs using capital letters', function(done) {
it('supports transactions', function(done) { var User = this.sequelize.define('User' + config.rand(), {
Support.prepareTransactionTest(dialect, this.sequelize, function(sequelize) { ID: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
var User = sequelize.define('User', { username: Sequelize.STRING }) Login: { type: Sequelize.STRING }
})
User.sync({ force: true }).success(function() { User.sync({ force: true }).success(function() {
sequelize.transaction(function(t) { User.create({Login: 'foo'}).success(function() {
User.create({ username: 'foo' }, { transaction: t }).success(function() { User.find(1).success(function(user) {
User.find({ username: 'foo' }).success(function(user1) { expect(user).to.exist
User.find({ username: 'foo' }, { transaction: t }).success(function(user2) { expect(user.ID).to.equal(1)
expect(user1).to.be.null
expect(user2).to.not.be.null
t.rollback().success(function() {
done() done()
}) })
}) })
}) })
}) })
}) })
})
})
})
describe('general / basic function', function() { describe('eager loading', function() {
beforeEach(function(done) { beforeEach(function(done) {
var self = this var self = this
this.User.create({username: 'barfooz'}).success(function(user) { self.Task = self.sequelize.define('Task', { title: Sequelize.STRING })
self.UserPrimary = self.sequelize.define('UserPrimary', { self.Worker = self.sequelize.define('Worker', { name: Sequelize.STRING })
specialkey: {
type: DataTypes.STRING,
primaryKey: true
}
})
self.UserPrimary.sync({force: true}).success(function() { this.init = function(callback) {
self.UserPrimary.create({specialkey: 'a string'}).success(function() { self.Task.sync({ force: true }).success(function() {
self.user = user self.Worker.sync({ force: true }).success(function() {
done() self.Worker.create({ name: 'worker' }).success(function(worker) {
self.Task.create({ title: 'homework' }).success(function(task) {
self.worker = worker
self.task = task
callback()
}) })
}) })
}) })
}) })
}
it('does not modify the passed arguments', function (done) {
var options = { where: ['specialkey = ?', 'awesome']}
this.UserPrimary.find(options).success(function(user) {
expect(options).to.deep.equal({ where: ['specialkey = ?', 'awesome']})
done() done()
}) })
})
it('doesn\'t throw an error when entering in a non integer value for a specified primary field', function(done) { describe('belongsTo', function() {
this.UserPrimary.find('a string').success(function(user) { describe('generic', function() {
expect(user.specialkey).to.equal('a string') it('throws an error about unexpected input if include contains a non-object', function(done) {
var self = this
expect(function() {
self.Worker.find({ include: [ 1 ] })
}).to.throw(Error, 'Include unexpected. Element has to be either an instance of DAOFactory or an object.')
done() done()
}) })
})
it('doesn\'t throw an error when entering in a non integer value', function(done) { it('throws an error about missing attributes if include contains an object with daoFactory', function(done) {
this.User.find('a string value').success(function(user) { var self = this
expect(user).to.be.null expect(function() {
self.Worker.find({ include: [ { daoFactory: self.Worker } ] })
}).to.throw(Error, 'Include malformed. Expected attributes: daoFactory, as!')
done() done()
}) })
})
it('returns a single dao', function(done) { it('throws an error if included DaoFactory is not associated', function(done) {
var self = this var self = this
this.User.find(this.user.id).success(function(user) { expect(function() {
expect(Array.isArray(user)).to.not.be.ok self.Worker.find({ include: [ self.Task ] })
expect(user.id).to.equal(self.user.id) }).to.throw(Error, 'Task is not associated to Worker!')
expect(user.id).to.equal(1)
done() done()
}) })
})
it('returns a single dao given a string id', function(done) { it('returns the associated worker via task.worker', function(done) {
var self = this var self = this
this.User.find(this.user.id + '').success(function(user) { this.Task.belongsTo(this.Worker)
expect(Array.isArray(user)).to.not.be.ok this.init(function() {
expect(user.id).to.equal(self.user.id) self.task.setWorker(self.worker).success(function() {
expect(user.id).to.equal(1) self.Task.find({
where: { title: 'homework' },
include: [ self.Worker ]
}).complete(function(err, task) {
expect(err).to.be.null
expect(task).to.exist
expect(task.worker).to.exist
expect(task.worker.name).to.equal('worker')
done() done()
}) })
}) })
it("should make aliased attributes available", function(done) {
this.User.find({
where: { id: 1 },
attributes: ['id', ['username', 'name']]
}).success(function(user) {
expect(user.name).to.equal('barfooz')
done()
}) })
}) })
it("should not try to convert boolean values if they are not selected", function(done) {
var UserWithBoolean = this.sequelize.define('UserBoolean', {
active: Sequelize.BOOLEAN
}) })
UserWithBoolean.sync({force: true}).success(function () { it('returns the private and public ip', function(done) {
UserWithBoolean.create({ active: true }).success(function(user) { var self = Object.create(this)
UserWithBoolean.find({ where: { id: user.id }, attributes: [ 'id' ] }).success(function(user) { self.Domain = self.sequelize.define('Domain', { ip: Sequelize.STRING })
expect(user.active).not.to.exist self.Environment = self.sequelize.define('Environment', { name: Sequelize.STRING })
self.Environment
.belongsTo(self.Domain, { as: 'PrivateDomain', foreignKey: 'privateDomainId' })
.belongsTo(self.Domain, { as: 'PublicDomain', foreignKey: 'publicDomainId' })
self.Domain.sync({ force: true }).success(function() {
self.Environment.sync({ force: true }).success(function() {
self.Domain.create({ ip: '192.168.0.1' }).success(function(privateIp) {
self.Domain.create({ ip: '91.65.189.19' }).success(function(publicIp) {
self.Environment.create({ name: 'environment' }).success(function(env) {
env.setPrivateDomain(privateIp).success(function() {
env.setPublicDomain(publicIp).success(function() {
self.Environment.find({
where: { name: 'environment' },
include: [
{ daoFactory: self.Domain, as: 'PrivateDomain' },
{ daoFactory: self.Domain, as: 'PublicDomain' }
]
}).complete(function(err, environment) {
expect(err).to.be.null
expect(environment).to.exist
expect(environment.privateDomain).to.exist
expect(environment.privateDomain.ip).to.equal('192.168.0.1')
expect(environment.publicDomain).to.exist
expect(environment.publicDomain.ip).to.equal('91.65.189.19')
done() done()
}) })
}) })
}) })
}) })
it('finds a specific user via where option', function(done) {
this.User.find({ where: { username: 'barfooz' } }).success(function(user) {
expect(user.username).to.equal('barfooz')
done()
}) })
}) })
})
it("doesn't find a user if conditions are not matching", function(done) {
this.User.find({ where: { username: 'foo' } }).success(function(user) {
expect(user).to.be.null
done()
}) })
}) })
it('allows sql logging', function(done) { it('eager loads with non-id primary keys', function(done) {
this.User.find({ where: { username: 'foo' } }).on('sql', function(sql) { var self = this
expect(sql).to.exist self.User = self.sequelize.define('UserPKeagerbelong', {
expect(sql.toUpperCase().indexOf("SELECT")).to.be.above(-1) username: {
done() type: Sequelize.STRING,
primaryKey: true
}
}) })
self.Group = self.sequelize.define('GroupPKeagerbelong', {
name: {
type: Sequelize.STRING,
primaryKey: true
}
}) })
self.User.belongsTo(self.Group)
it('ignores passed limit option', function(done) { self.sequelize.sync({ force: true }).success(function() {
this.User.find({ limit: 10 }).success(function(user) { self.User.create({ username: 'someone', GroupPKeagerbelongId: 'people' }).success(function() {
// it returns an object instead of an array self.Group.create({ name: 'people' }).success(function() {
expect(Array.isArray(user)).to.not.be.ok self.User.find({
expect(user.hasOwnProperty('username')).to.be.ok where: {
username: 'someone'
},
include: [self.Group]
}).complete(function (err, someUser) {
expect(err).to.be.null
expect(someUser).to.exist
expect(someUser.username).to.equal('someone')
expect(someUser.groupPKeagerbelong.name).to.equal('people')
done() done()
}) })
}) })
})
})
})
it('finds entries via primary keys', function(done) { it('getting parent data in many to one relationship', function(done) {
var self = this
, UserPrimary = self.sequelize.define('UserWithPrimaryKey', { var self = this;
identifier: {type: Sequelize.STRING, primaryKey: true},
name: Sequelize.STRING var User = self.sequelize.define('User', {
id: {type: Sequelize.INTEGER, autoIncrement: true, primaryKey: true},
username: {type: Sequelize.STRING}
}) })
UserPrimary.sync({ force: true }).success(function() { var Message = self.sequelize.define('Message', {
UserPrimary.create({ id: {type: Sequelize.INTEGER, autoIncrement: true, primaryKey: true},
identifier: 'an identifier', user_id: {type: Sequelize.INTEGER},
name: 'John' message: {type: Sequelize.STRING}
}).success(function(u) { })
expect(u.id).not.to.exist
User.hasMany(Message)
Message.belongsTo(User, { foreignKey: 'user_id' })
Message.sync({ force: true }).success(function() {
User.sync({ force: true }).success(function() {
User.create({username: 'test_testerson'}).success(function(user) {
Message.create({user_id: user.id, message: 'hi there!'}).success(function(message) {
Message.create({user_id: user.id, message: 'a second message'}).success(function(message) {
Message.findAll({
where: {user_id: user.id},
attributes: [
'user_id',
'message'
],
include: [{ model: User, as: User.tableName, attributes: ['username'] }]
}).success(function(messages) {
expect(messages.length).to.equal(2);
expect(messages[0].message).to.equal('hi there!');
expect(messages[0].user.username).to.equal('test_testerson');
expect(messages[1].message).to.equal('a second message');
expect(messages[1].user.username).to.equal('test_testerson');
UserPrimary.find('an identifier').success(function(u2) {
expect(u2.identifier).to.equal('an identifier')
expect(u2.name).to.equal('John')
done() done()
}) })
}) })
}) })
}) })
it('finds entries via a string primary key called id', function(done) { })
var self = this
, UserPrimary = self.sequelize.define('UserWithPrimaryKey', {
id: {type: Sequelize.STRING, primaryKey: true},
name: Sequelize.STRING
}) })
UserPrimary.sync({ force: true }).success(function() { })
UserPrimary.create({
id: 'a string based id', it('allows mulitple assocations of the same model with different alias', function (done) {
name: 'Johnno' var self = this
}).success(function(u) {
UserPrimary.find('a string based id').success(function(u2) { this.Worker.belongsTo(this.Task, { as: 'ToDo' })
expect(u2.id).to.equal('a string based id') this.Worker.belongsTo(this.Task, { as: 'DoTo' })
expect(u2.name).to.equal('Johnno') this.init(function () {
self.Worker.find({
include: [
{ model: self.Task, as: 'ToDo' },
{ model: self.Task, as: 'DoTo' }
]
}).success(function () {
// Just being able to include both shows that this test works, so no assertions needed
done() done()
}) })
}) })
}) })
}) })
describe('hasOne', function() {
it('returns the selected fields as instance.selectedValues', function(done) { beforeEach(function(done) {
var self = this var self = this
this.User.create({ this.Worker.hasOne(this.Task)
username: 'JohnXOXOXO' this.init(function() {
}).success(function() { self.worker.setTask(self.task).success(function() {
self.User.find({
where: { username: 'JohnXOXOXO' },
attributes: ['username']
}).success(function(user) {
expect(user.selectedValues).to.have.property('username', 'JohnXOXOXO')
done() done()
}) })
}) })
}) })
it('returns the selected fields and all fields of the included table as instance.selectedValues', function(done) { it('throws an error if included DaoFactory is not associated', function(done) {
var self = this var self = this
self.Mission = self.sequelize.define('Mission', { expect(function() {
title: {type: Sequelize.STRING, defaultValue: 'a mission!!'}, self.Task.find({ include: [ self.Worker ] })
foo: {type: Sequelize.INTEGER, defaultValue: 2}, }).to.throw(Error, 'Worker is not associated to Task!')
done()
}) })
self.Mission.belongsTo(self.User) it('returns the associated task via worker.task', function(done) {
self.User.hasMany(self.Mission) this.Worker.find({
where: { name: 'worker' },
self.Mission.sync({ force: true }).success(function() { include: [ this.Task ]
self.Mission.create().success(function(mission) { }).complete(function(err, worker) {
self.User.create({username: 'John DOE'}).success(function(user) { expect(err).to.be.null
mission.setUser(user).success(function() { expect(worker).to.exist
self.User.find({ expect(worker.task).to.exist
where: { username: 'John DOE' }, expect(worker.task.title).to.equal('homework')
attributes: ['username'],
include: [self.Mission]
}).success(function(user) {
expect(user.selectedValues).to.deep.equal({ username: 'John DOE' })
done() done()
}) })
}) })
})
})
})
})
it('returns the selected fields for both the base table and the included table as instance.selectedValues', function(done) { it('eager loads with non-id primary keys', function(done) {
var self = this var self = this
self.Mission = self.sequelize.define('Mission', { self.User = self.sequelize.define('UserPKeagerone', {
title: {type: Sequelize.STRING, defaultValue: 'another mission!!'}, username: {
foo: {type: Sequelize.INTEGER, defaultValue: 4}, type: Sequelize.STRING,
primaryKey: true
}
}) })
self.Group = self.sequelize.define('GroupPKeagerone', {
name: {
type: Sequelize.STRING,
primaryKey: true
}
})
self.Group.hasOne(self.User)
self.Mission.belongsTo(self.User) self.sequelize.sync({ force: true }).success(function() {
self.User.hasMany(self.Mission) self.User.create({ username: 'someone', GroupPKeageroneId: 'people' }).success(function() {
self.Group.create({ name: 'people' }).success(function() {
self.Mission.sync({ force: true }).success(function() { self.Group.find({
self.Mission.create().success(function(mission) { where: {
self.User.create({username: 'Brain Picker'}).success(function(user) { name: 'people'
mission.setUser(user).success(function() { },
self.User.find({ include: [self.User]
where: { username: 'Brain Picker' }, }).complete(function (err, someGroup) {
attributes: ['username'], expect(err).to.be.null
include: [{model: self.Mission, as: self.Mission.tableName, attributes: ['title']}] expect(someGroup).to.exist
}).success(function(user) { expect(someGroup.name).to.equal('people')
expect(user.selectedValues).to.deep.equal({ username: 'Brain Picker' }) expect(someGroup.userPKeagerone.username).to.equal('someone')
expect(user.missions[0].selectedValues).to.deep.equal({ id: 1, title: 'another mission!!'})
expect(user.missions[0].foo).not.to.exist
done() done()
}) })
}) })
...@@ -2164,256 +2103,209 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -2164,256 +2103,209 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
}) })
it('always honors ZERO as primary key', function(_done) { describe('hasOne with alias', function() {
it('throws an error if included DaoFactory is not referenced by alias', function(done) {
var self = this var self = this
, permutations = [ expect(function() {
0, self.Worker.find({ include: [ self.Task ] })
'0', }).to.throw(Error, 'Task is not associated to Worker!')
{where: {id: 0}},
{where: {id: '0'}}
]
, done = _.after(2 * permutations.length, _done)
this.User.bulkCreate([{username: 'jack'}, {username: 'jack'}]).success(function() {
permutations.forEach(function(perm) {
self.User.find(perm).done(function(err, user) {
expect(err).to.be.null
expect(user).to.be.null
done()
}).on('sql', function(s) {
expect(s.indexOf(0)).not.to.equal(-1)
done() done()
}) })
describe('alias', function(done) {
beforeEach(function(done) {
var self = this
this.Worker.hasOne(this.Task, { as: 'ToDo' })
this.init(function() {
self.worker.setToDo(self.task).success(function() {
done()
}) })
}) })
}) })
it('should allow us to find IDs using capital letters', function(done) { it('throws an error if alias is not associated', function(done) {
var User = this.sequelize.define('User' + config.rand(), { var self = this
ID: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true }, expect(function() {
Login: { type: Sequelize.STRING } self.Worker.find({ include: [ { daoFactory: self.Task, as: 'Work' } ] })
}).to.throw(Error, 'Task (Work) is not associated to Worker!')
done()
}) })
User.sync({ force: true }).success(function() { it('returns the associated task via worker.task', function(done) {
User.create({Login: 'foo'}).success(function() { this.Worker.find({
User.find(1).success(function(user) { where: { name: 'worker' },
expect(user).to.exist include: [ { daoFactory: this.Task, as: 'ToDo' } ]
expect(user.ID).to.equal(1) }).complete(function(err, worker) {
expect(err).to.be.null
expect(worker).to.exist
expect(worker.toDo).to.exist
expect(worker.toDo.title).to.equal('homework')
done() done()
}) })
}) })
})
it('returns the associated task via worker.task when daoFactory is aliased with model', function(done) {
this.Worker.find({
where: { name: 'worker' },
include: [ { model: this.Task, as: 'ToDo' } ]
}).complete(function(err, worker) {
expect(worker.toDo.title).to.equal('homework')
done()
}) })
}) })
describe('eager loading', function() { it('allows mulitple assocations of the same model with different alias', function (done) {
beforeEach(function(done) {
var self = this var self = this
self.Task = self.sequelize.define('Task', { title: Sequelize.STRING })
self.Worker = self.sequelize.define('Worker', { name: Sequelize.STRING })
this.init = function(callback) { this.Worker.hasOne(this.Task, { as: 'DoTo' })
self.Task.sync({ force: true }).success(function() { this.init(function () {
self.Worker.sync({ force: true }).success(function() { self.Worker.find({
self.Worker.create({ name: 'worker' }).success(function(worker) { include: [
self.Task.create({ title: 'homework' }).success(function(task) { { model: self.Task, as: 'ToDo' },
self.worker = worker { model: self.Task, as: 'DoTo' }
self.task = task ]
callback() }).success(function () {
// Just being able to include both shows that this test works, so no assertions needed
done()
}) })
}) })
}) })
}) })
}
done()
}) })
describe('belongsTo', function() { describe('hasMany', function() {
describe('generic', function() { beforeEach(function(done) {
it('throws an error about unexpected input if include contains a non-object', function(done) {
var self = this var self = this
expect(function() { this.Worker.hasMany(this.Task)
self.Worker.find({ include: [ 1 ] }) this.init(function() {
}).to.throw(Error, 'Include unexpected. Element has to be either an instance of DAOFactory or an object.') self.worker.setTasks([ self.task ]).success(function() {
done() done()
}) })
})
})
it('throws an error about missing attributes if include contains an object with daoFactory', function(done) { it('throws an error if included DaoFactory is not associated', function(done) {
var self = this var self = this
expect(function() { expect(function() {
self.Worker.find({ include: [ { daoFactory: self.Worker } ] }) self.Task.find({ include: [ self.Worker ] })
}).to.throw(Error, 'Include malformed. Expected attributes: daoFactory, as!') }).to.throw(Error, 'Worker is not associated to Task!')
done() done()
}) })
it('throws an error if included DaoFactory is not associated', function(done) { it('returns the associated tasks via worker.tasks', function(done) {
var self = this this.Worker.find({
expect(function() { where: { name: 'worker' },
self.Worker.find({ include: [ self.Task ] }) include: [ this.Task ]
}).to.throw(Error, 'Task is not associated to Worker!') }).complete(function(err, worker) {
expect(err).to.be.null
expect(worker).to.exist
expect(worker.tasks).to.exist
expect(worker.tasks[0].title).to.equal('homework')
done() done()
}) })
})
it('returns the associated worker via task.worker', function(done) { it('eager loads with non-id primary keys', function(done) {
var self = this var self = this
this.Task.belongsTo(this.Worker) self.User = self.sequelize.define('UserPKeagerone', {
this.init(function() { username: {
self.task.setWorker(self.worker).success(function() { type: Sequelize.STRING,
self.Task.find({ primaryKey: true
where: { title: 'homework' }, }
include: [ self.Worker ] })
}).complete(function(err, task) { self.Group = self.sequelize.define('GroupPKeagerone', {
name: {
type: Sequelize.STRING,
primaryKey: true
}
})
self.Group.hasMany(self.User)
self.User.hasMany(self.Group)
self.sequelize.sync({ force: true }).success(function() {
self.User.create({ username: 'someone' }).success(function(someUser) {
self.Group.create({ name: 'people' }).success(function(someGroup) {
someUser.setGroupPKeagerones([someGroup]).complete(function (err, data) {
expect(err).to.be.null expect(err).to.be.null
expect(task).to.exist self.User.find({
expect(task.worker).to.exist where: {
expect(task.worker.name).to.equal('worker') username: 'someone'
},
include: [self.Group]
}).complete(function (err, someUser) {
expect(err).to.be.null
expect(someUser).to.exist
expect(someUser.username).to.equal('someone')
expect(someUser.groupPKeagerones[0].name).to.equal('people')
done() done()
}) })
}) })
}) })
}) })
}) })
})
})
it('returns the private and public ip', function(done) { describe('hasMany with alias', function() {
var self = Object.create(this) it('throws an error if included DaoFactory is not referenced by alias', function(done) {
self.Domain = self.sequelize.define('Domain', { ip: Sequelize.STRING }) var self = this
self.Environment = self.sequelize.define('Environment', { name: Sequelize.STRING }) expect(function() {
self.Environment self.Worker.find({ include: [ self.Task ] })
.belongsTo(self.Domain, { as: 'PrivateDomain', foreignKey: 'privateDomainId' }) }).to.throw(Error, 'Task is not associated to Worker!')
.belongsTo(self.Domain, { as: 'PublicDomain', foreignKey: 'publicDomainId' })
self.Domain.sync({ force: true }).success(function() {
self.Environment.sync({ force: true }).success(function() {
self.Domain.create({ ip: '192.168.0.1' }).success(function(privateIp) {
self.Domain.create({ ip: '91.65.189.19' }).success(function(publicIp) {
self.Environment.create({ name: 'environment' }).success(function(env) {
env.setPrivateDomain(privateIp).success(function() {
env.setPublicDomain(publicIp).success(function() {
self.Environment.find({
where: { name: 'environment' },
include: [
{ daoFactory: self.Domain, as: 'PrivateDomain' },
{ daoFactory: self.Domain, as: 'PublicDomain' }
]
}).complete(function(err, environment) {
expect(err).to.be.null
expect(environment).to.exist
expect(environment.privateDomain).to.exist
expect(environment.privateDomain.ip).to.equal('192.168.0.1')
expect(environment.publicDomain).to.exist
expect(environment.publicDomain.ip).to.equal('91.65.189.19')
done() done()
}) })
})
})
})
})
})
})
})
})
it('eager loads with non-id primary keys', function(done) { describe('alias', function() {
beforeEach(function(done) {
var self = this var self = this
self.User = self.sequelize.define('UserPKeagerbelong', { this.Worker.hasMany(this.Task, { as: 'ToDos' })
username: { this.init(function() {
type: Sequelize.STRING, self.worker.setToDos([ self.task ]).success(function() {
primaryKey: true
}
})
self.Group = self.sequelize.define('GroupPKeagerbelong', {
name: {
type: Sequelize.STRING,
primaryKey: true
}
})
self.User.belongsTo(self.Group)
self.sequelize.sync({ force: true }).success(function() {
self.User.create({ username: 'someone', GroupPKeagerbelongId: 'people' }).success(function() {
self.Group.create({ name: 'people' }).success(function() {
self.User.find({
where: {
username: 'someone'
},
include: [self.Group]
}).complete(function (err, someUser) {
expect(err).to.be.null
expect(someUser).to.exist
expect(someUser.username).to.equal('someone')
expect(someUser.groupPKeagerbelong.name).to.equal('people')
done() done()
}) })
}) })
}) })
})
})
it('getting parent data in many to one relationship', function(done) {
var self = this;
var User = self.sequelize.define('User', {
id: {type: Sequelize.INTEGER, autoIncrement: true, primaryKey: true},
username: {type: Sequelize.STRING}
})
var Message = self.sequelize.define('Message', {
id: {type: Sequelize.INTEGER, autoIncrement: true, primaryKey: true},
user_id: {type: Sequelize.INTEGER},
message: {type: Sequelize.STRING}
})
User.hasMany(Message)
Message.belongsTo(User, { foreignKey: 'user_id' })
Message.sync({ force: true }).success(function() {
User.sync({ force: true }).success(function() {
User.create({username: 'test_testerson'}).success(function(user) {
Message.create({user_id: user.id, message: 'hi there!'}).success(function(message) {
Message.create({user_id: user.id, message: 'a second message'}).success(function(message) {
Message.findAll({
where: {user_id: user.id},
attributes: [
'user_id',
'message'
],
include: [{ model: User, as: User.tableName, attributes: ['username'] }]
}).success(function(messages) {
expect(messages.length).to.equal(2);
expect(messages[0].message).to.equal('hi there!');
expect(messages[0].user.username).to.equal('test_testerson');
expect(messages[1].message).to.equal('a second message');
expect(messages[1].user.username).to.equal('test_testerson');
it('throws an error if alias is not associated', function(done) {
var self = this
expect(function() {
self.Worker.find({ include: [ { daoFactory: self.Task, as: 'Work' } ] })
}).to.throw(Error, 'Task (Work) is not associated to Worker!')
done() done()
}) })
}) it('returns the associated task via worker.task', function(done) {
this.Worker.find({
where: { name: 'worker' },
include: [ { daoFactory: this.Task, as: 'ToDos' } ]
}).complete(function(err, worker) {
expect(err).to.be.null
expect(worker).to.exist
expect(worker.toDos).to.exist
expect(worker.toDos[0].title).to.equal('homework')
done()
}) })
}) })
it('returns the associated task via worker.task when daoFactory is aliased with model', function(done) {
this.Worker.find({
where: { name: 'worker' },
include: [ { model: this.Task, as: 'ToDos' } ]
}).complete(function(err, worker) {
expect(worker.toDos[0].title).to.equal('homework')
done()
}) })
}) })
})
it('allows mulitple assocations of the same model with different alias', function (done) { it('allows mulitple assocations of the same model with different alias', function (done) {
var self = this var self = this
this.Worker.belongsTo(this.Task, { as: 'ToDo' }) this.Worker.hasMany(this.Task, { as: 'DoTos' })
this.Worker.belongsTo(this.Task, { as: 'DoTo' })
this.init(function () { this.init(function () {
self.Worker.find({ self.Worker.find({
include: [ include: [
{ model: self.Task, as: 'ToDo' }, { model: self.Task, as: 'ToDos' },
{ model: self.Task, as: 'DoTo' } { model: self.Task, as: 'DoTos' }
] ]
}).success(function () { }).success(function () {
// Just being able to include both shows that this test works, so no assertions needed // Just being able to include both shows that this test works, so no assertions needed
...@@ -2422,68 +2314,61 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -2422,68 +2314,61 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
}) })
}) })
})
})
describe('hasOne', function() { describe('queryOptions', function() {
beforeEach(function(done) { beforeEach(function(done) {
var self = this var self = this
this.Worker.hasOne(this.Task) this.User.create({username: 'barfooz'}).success(function(user) {
this.init(function() { self.user = user
self.worker.setTask(self.task).success(function() {
done() done()
}) })
}) })
})
it('throws an error if included DaoFactory is not associated', function(done) { it("should return a DAO when queryOptions are not set", function(done) {
var self = this var self = this
expect(function() { this.User.find({ where: { username: 'barfooz'}}).done(function(err, user) {
self.Task.find({ include: [ self.Worker ] }) expect(user).to.be.instanceOf(self.User.DAO)
}).to.throw(Error, 'Worker is not associated to Task!')
done() done()
}) })
})
it('returns the associated task via worker.task', function(done) { it("should return a DAO when raw is false", function(done) {
this.Worker.find({ var self = this
where: { name: 'worker' }, this.User.find({ where: { username: 'barfooz'}}, { raw: false }).done(function(err, user) {
include: [ this.Task ] expect(user).to.be.instanceOf(self.User.DAO)
}).complete(function(err, worker) {
expect(err).to.be.null
expect(worker).to.exist
expect(worker.task).to.exist
expect(worker.task.title).to.equal('homework')
done() done()
}) })
}) })
it('eager loads with non-id primary keys', function(done) { it("should return raw data when raw is true", function(done) {
var self = this var self = this
self.User = self.sequelize.define('UserPKeagerone', { this.User.find({ where: { username: 'barfooz'}}, { raw: true }).done(function(err, user) {
username: { expect(user).to.not.be.instanceOf(self.User.DAO)
type: Sequelize.STRING, expect(user).to.be.instanceOf(Object)
primaryKey: true done()
} })
})
}) })
self.Group = self.sequelize.define('GroupPKeagerone', {
name: {
type: Sequelize.STRING,
primaryKey: true
}
}) })
self.Group.hasOne(self.User)
self.sequelize.sync({ force: true }).success(function() { describe('findAll', function() {
self.User.create({ username: 'someone', GroupPKeageroneId: 'people' }).success(function() { it('supports transactions', function(done) {
self.Group.create({ name: 'people' }).success(function() { Support.prepareTransactionTest(dialect, this.sequelize, function(sequelize) {
self.Group.find({ var User = sequelize.define('User', { username: Sequelize.STRING })
where: {
name: 'people' User.sync({ force: true }).success(function() {
}, sequelize.transaction(function(t) {
include: [self.User] User.create({ username: 'foo' }, { transaction: t }).success(function() {
}).complete(function (err, someGroup) { User.findAll({ username: 'foo' }).success(function(users1) {
expect(err).to.be.null User.findAll({ transaction: t }).success(function(users2) {
expect(someGroup).to.exist User.findAll({ username: 'foo' }, { transaction: t }).success(function(users3) {
expect(someGroup.name).to.equal('people') expect(users1.length).to.equal(0)
expect(someGroup.userPKeagerone.username).to.equal('someone') expect(users2.length).to.equal(1)
expect(users3.length).to.equal(1)
t.rollback().success(function() {
done() done()
}) })
}) })
...@@ -2491,283 +2376,400 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -2491,283 +2376,400 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
}) })
}) })
})
})
})
describe('hasOne with alias', function() { describe('special where conditions/smartWhere object', function() {
it('throws an error if included DaoFactory is not referenced by alias', function(done) { beforeEach(function(done) {
var self = this var self = this
expect(function() {
self.Worker.find({ include: [ self.Task ] }) this.User.bulkCreate([
}).to.throw(Error, 'Task is not associated to Worker!') {username: 'boo', intVal: 5, theDate: '2013-01-01 12:00'},
{username: 'boo2', intVal: 10, theDate: '2013-01-10 12:00'}
]).success(function(user2) {
done() done()
}) })
})
describe('alias', function(done) { it('should be able to find rows where attribute is in a list of values', function (done) {
beforeEach(function(done) { this.User.findAll({
var self = this where: {
this.Worker.hasOne(this.Task, { as: 'ToDo' }) username: ['boo', 'boo2']
this.init(function() { }
self.worker.setToDo(self.task).success(function() { }).success(function(users){
expect(users).to.have.length(2);
done() done()
});
}) })
it('should not break when trying to find rows using an array of primary keys', function (done) {
this.User.findAll({
where: {
id: [1, 2, 3]
}
}).success(function(users){
done();
});
})
it('should be able to find a row using like', function(done) {
this.User.findAll({
where: {
username: {
like: '%2'
}
}
}).success(function(users) {
expect(users).to.be.an.instanceof(Array)
expect(users).to.have.length(1)
expect(users[0].username).to.equal('boo2')
expect(users[0].intVal).to.equal(10)
done()
}) })
}) })
it('throws an error if alias is not associated', function(done) { it('should be able to find a row using not like', function(done) {
var self = this this.User.findAll({
expect(function() { where: {
self.Worker.find({ include: [ { daoFactory: self.Task, as: 'Work' } ] }) username: {
}).to.throw(Error, 'Task (Work) is not associated to Worker!') nlike: '%2'
}
}
}).success(function(users) {
expect(users).to.be.an.instanceof(Array)
expect(users).to.have.length(1)
expect(users[0].username).to.equal('boo')
expect(users[0].intVal).to.equal(5)
done() done()
}) })
})
it('returns the associated task via worker.task', function(done) { it('should be able to find a row between a certain date using the between shortcut', function(done) {
this.Worker.find({ this.User.findAll({
where: { name: 'worker' }, where: {
include: [ { daoFactory: this.Task, as: 'ToDo' } ] theDate: {
}).complete(function(err, worker) { '..': ['2013-01-02', '2013-01-11']
expect(err).to.be.null }
expect(worker).to.exist }
expect(worker.toDo).to.exist }).success(function(users) {
expect(worker.toDo.title).to.equal('homework') expect(users[0].username).to.equal('boo2')
expect(users[0].intVal).to.equal(10)
done() done()
}) })
}) })
it('returns the associated task via worker.task when daoFactory is aliased with model', function(done) { it('should be able to find a row not between a certain integer using the not between shortcut', function(done) {
this.Worker.find({ this.User.findAll({
where: { name: 'worker' }, where: {
include: [ { model: this.Task, as: 'ToDo' } ] intVal: {
}).complete(function(err, worker) { '!..': [8, 10]
expect(worker.toDo.title).to.equal('homework') }
}
}).success(function(users) {
expect(users[0].username).to.equal('boo')
expect(users[0].intVal).to.equal(5)
done() done()
}) })
}) })
it('allows mulitple assocations of the same model with different alias', function (done) { it('should be able to handle false/true values just fine...', function(done) {
var self = this var User = this.User
, escapeChar = (dialect === "postgres") ? '"' : '`'
this.Worker.hasOne(this.Task, { as: 'DoTo' }) User.bulkCreate([
this.init(function () { {username: 'boo5', aBool: false},
self.Worker.find({ {username: 'boo6', aBool: true}
include: [ ]).success(function() {
{ model: self.Task, as: 'ToDo' }, User.all({where: [escapeChar + 'aBool' + escapeChar + ' = ?', false]}).success(function(users) {
{ model: self.Task, as: 'DoTo' } expect(users).to.have.length(1)
] expect(users[0].username).to.equal('boo5')
}).success(function () {
// Just being able to include both shows that this test works, so no assertions needed User.all({where: [escapeChar + 'aBool' + escapeChar + ' = ?', true]}).success(function(_users) {
expect(_users).to.have.length(1)
expect(_users[0].username).to.equal('boo6')
done() done()
}) })
}) })
}) })
}) })
it('should be able to handle false/true values through associations as well...', function(done) {
var User = this.User
, escapeChar = (dialect === "postgres") ? '"' : '`'
var Passports = this.sequelize.define('Passports', {
isActive: Sequelize.BOOLEAN
}) })
describe('hasMany', function() { User.hasMany(Passports)
beforeEach(function(done) { Passports.belongsTo(User)
var self = this
this.Worker.hasMany(this.Task) User.sync({ force: true }).success(function() {
this.init(function() { Passports.sync({ force: true }).success(function() {
self.worker.setTasks([ self.task ]).success(function() { User.bulkCreate([
{username: 'boo5', aBool: false},
{username: 'boo6', aBool: true}
]).success(function() {
Passports.bulkCreate([
{isActive: true},
{isActive: false}
]).success(function() {
User.find(1).success(function(user) {
Passports.find(1).success(function(passport) {
user.setPassports([passport]).success(function() {
User.find(2).success(function(_user) {
Passports.find(2).success(function(_passport) {
_user.setPassports([_passport]).success(function() {
_user.getPassports({where: [escapeChar + 'isActive' + escapeChar + ' = ?', false]}).success(function(theFalsePassport) {
user.getPassports({where: [escapeChar + 'isActive' + escapeChar + ' = ?', true]}).success(function(theTruePassport) {
expect(theFalsePassport).to.have.length(1)
expect(theFalsePassport[0].isActive).to.be.false
expect(theTruePassport).to.have.length(1)
expect(theTruePassport[0].isActive).to.be.true
done() done()
}) })
}) })
}) })
it('throws an error if included DaoFactory is not associated', function(done) {
var self = this
expect(function() {
self.Task.find({ include: [ self.Worker ] })
}).to.throw(Error, 'Worker is not associated to Task!')
done()
}) })
})
it('returns the associated tasks via worker.tasks', function(done) { })
this.Worker.find({ })
where: { name: 'worker' }, })
include: [ this.Task ] })
}).complete(function(err, worker) { })
expect(err).to.be.null })
expect(worker).to.exist
expect(worker.tasks).to.exist
expect(worker.tasks[0].title).to.equal('homework')
done()
}) })
}) })
it('eager loads with non-id primary keys', function(done) { it('should be able to return a record with primaryKey being null for new inserts', function(done) {
var self = this var Session = this.sequelize.define('Session', {
self.User = self.sequelize.define('UserPKeagerone', { token: { type: DataTypes.TEXT, allowNull: false },
username: { lastUpdate: { type: DataTypes.DATE, allowNull: false }
type: Sequelize.STRING, }, {
primaryKey: true charset: 'utf8',
} collate: 'utf8_general_ci',
omitNull: true
}) })
self.Group = self.sequelize.define('GroupPKeagerone', {
name: { , User = this.sequelize.define('User', {
type: Sequelize.STRING, name: { type: DataTypes.STRING, allowNull: false, unique: true },
primaryKey: true password: { type: DataTypes.STRING, allowNull: false },
} isAdmin: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: false }
}, {
charset: 'utf8',
collate: 'utf8_general_ci'
})
User.hasMany(Session, { as: 'Sessions' })
Session.belongsTo(User)
Session.sync({ force: true }).success(function() {
User.sync({ force: true }).success(function() {
User.create({name: 'Name1', password: '123', isAdmin: false}).success(function(user) {
var sess = Session.build({
lastUpdate: new Date(),
token: '123'
}) })
self.Group.hasMany(self.User)
self.User.hasMany(self.Group)
self.sequelize.sync({ force: true }).success(function() { user.addSession(sess).success(function(u) {
self.User.create({ username: 'someone' }).success(function(someUser) { expect(u.token).to.equal('123')
self.Group.create({ name: 'people' }).success(function(someGroup) {
someUser.setGroupPKeagerones([someGroup]).complete(function (err, data) {
expect(err).to.be.null
self.User.find({
where: {
username: 'someone'
},
include: [self.Group]
}).complete(function (err, someUser) {
expect(err).to.be.null
expect(someUser).to.exist
expect(someUser.username).to.equal('someone')
expect(someUser.groupPKeagerones[0].name).to.equal('people')
done() done()
}) })
}) })
}) })
}) })
}) })
})
})
describe('hasMany with alias', function() { it('should be able to find a row between a certain date', function(done) {
it('throws an error if included DaoFactory is not referenced by alias', function(done) { this.User.findAll({
var self = this where: {
expect(function() { theDate: {
self.Worker.find({ include: [ self.Task ] }) between: ['2013-01-02', '2013-01-11']
}).to.throw(Error, 'Task is not associated to Worker!') }
}
}).success(function(users) {
expect(users[0].username).to.equal('boo2')
expect(users[0].intVal).to.equal(10)
done() done()
}) })
})
describe('alias', function() { it('should be able to find a row between a certain date and an additional where clause', function(done) {
beforeEach(function(done) { this.User.findAll({
var self = this where: {
this.Worker.hasMany(this.Task, { as: 'ToDos' }) theDate: {
this.init(function() { between: ['2013-01-02', '2013-01-11']
self.worker.setToDos([ self.task ]).success(function() { },
intVal: 10
}
}).success(function(users) {
expect(users[0].username).to.equal('boo2')
expect(users[0].intVal).to.equal(10)
done() done()
}) })
}) })
})
it('throws an error if alias is not associated', function(done) { it('should be able to find a row not between a certain integer', function(done) {
var self = this this.User.findAll({
expect(function() { where: {
self.Worker.find({ include: [ { daoFactory: self.Task, as: 'Work' } ] }) intVal: {
}).to.throw(Error, 'Task (Work) is not associated to Worker!') nbetween: [8, 10]
}
}
}).success(function(users) {
expect(users[0].username).to.equal('boo')
expect(users[0].intVal).to.equal(5)
done() done()
}) })
})
it('returns the associated task via worker.task', function(done) { it('should be able to find a row using not between and between logic', function(done) {
this.Worker.find({ this.User.findAll({
where: { name: 'worker' }, where: {
include: [ { daoFactory: this.Task, as: 'ToDos' } ] theDate: {
}).complete(function(err, worker) { between: ['2012-12-10', '2013-01-02'],
expect(err).to.be.null nbetween: ['2013-01-04', '2013-01-20']
expect(worker).to.exist }
expect(worker.toDos).to.exist }
expect(worker.toDos[0].title).to.equal('homework') }).success(function(users) {
expect(users[0].username).to.equal('boo')
expect(users[0].intVal).to.equal(5)
done() done()
}) })
}) })
it('returns the associated task via worker.task when daoFactory is aliased with model', function(done) { it('should be able to find a row using not between and between logic with dates', function(done) {
this.Worker.find({ this.User.findAll({
where: { name: 'worker' }, where: {
include: [ { model: this.Task, as: 'ToDos' } ] theDate: {
}).complete(function(err, worker) { between: [new Date('2012-12-10'), new Date('2013-01-02')],
expect(worker.toDos[0].title).to.equal('homework') nbetween: [new Date('2013-01-04'), new Date('2013-01-20')]
}
}
}).success(function(users) {
expect(users[0].username).to.equal('boo')
expect(users[0].intVal).to.equal(5)
done() done()
}) })
}) })
it('allows mulitple assocations of the same model with different alias', function (done) { it('should be able to find a row using greater than or equal to logic with dates', function(done) {
var self = this this.User.findAll({
where: {
this.Worker.hasMany(this.Task, { as: 'DoTos' }) theDate: {
this.init(function () { gte: new Date('2013-01-09')
self.Worker.find({ }
include: [ }
{ model: self.Task, as: 'ToDos' }, }).success(function(users) {
{ model: self.Task, as: 'DoTos' } expect(users[0].username).to.equal('boo2')
] expect(users[0].intVal).to.equal(10)
}).success(function () {
// Just being able to include both shows that this test works, so no assertions needed
done() done()
}) })
}) })
})
})
})
})
describe('queryOptions', function() { it('should be able to find a row using greater than or equal to', function(done) {
beforeEach(function(done) { this.User.find({
var self = this where: {
this.User.create({username: 'barfooz'}).success(function(user) { intVal: {
self.user = user gte: 6
}
}
}).success(function(user) {
expect(user.username).to.equal('boo2')
expect(user.intVal).to.equal(10)
done() done()
}) })
}) })
it("should return a DAO when queryOptions are not set", function(done) { it('should be able to find a row using greater than', function(done) {
var self = this this.User.find({
this.User.find({ where: { username: 'barfooz'}}).done(function(err, user) { where: {
expect(user).to.be.instanceOf(self.User.DAO) intVal: {
gt: 5
}
}
}).success(function(user) {
expect(user.username).to.equal('boo2')
expect(user.intVal).to.equal(10)
done() done()
}) })
}) })
it("should return a DAO when raw is false", function(done) { it('should be able to find a row using lesser than or equal to', function(done) {
var self = this this.User.find({
this.User.find({ where: { username: 'barfooz'}}, { raw: false }).done(function(err, user) { where: {
expect(user).to.be.instanceOf(self.User.DAO) intVal: {
lte: 5
}
}
}).success(function(user) {
expect(user.username).to.equal('boo')
expect(user.intVal).to.equal(5)
done() done()
}) })
}) })
it("should return raw data when raw is true", function(done) { it('should be able to find a row using lesser than', function(done) {
var self = this this.User.find({
this.User.find({ where: { username: 'barfooz'}}, { raw: true }).done(function(err, user) { where: {
expect(user).to.not.be.instanceOf(self.User.DAO) intVal: {
expect(user).to.be.instanceOf(Object) lt: 6
}
}
}).success(function(user) {
expect(user.username).to.equal('boo')
expect(user.intVal).to.equal(5)
done() done()
}) })
}) })
})
})
describe('findAll', function() {
it('supports transactions', function(done) {
Support.prepareTransactionTest(dialect, this.sequelize, function(sequelize) {
var User = sequelize.define('User', { username: Sequelize.STRING })
User.sync({ force: true }).success(function() {
sequelize.transaction(function(t) {
User.create({ username: 'foo' }, { transaction: t }).success(function() {
User.findAll({ username: 'foo' }).success(function(users1) {
User.findAll({ transaction: t }).success(function(users2) {
User.findAll({ username: 'foo' }, { transaction: t }).success(function(users3) {
expect(users1.length).to.equal(0)
expect(users2.length).to.equal(1)
expect(users3.length).to.equal(1)
t.rollback().success(function() { it('should have no problem finding a row using lesser and greater than', function(done) {
this.User.findAll({
where: {
intVal: {
lt: 6,
gt: 4
}
}
}).success(function(users) {
expect(users[0].username).to.equal('boo')
expect(users[0].intVal).to.equal(5)
done() done()
}) })
}) })
it('should be able to find a row using not equal to logic', function(done) {
this.User.find({
where: {
intVal: {
ne: 10
}
}
}).success(function(user) {
expect(user.username).to.equal('boo')
expect(user.intVal).to.equal(5)
done()
}) })
}) })
it('should be able to find multiple users with any of the special where logic properties', function(done) {
this.User.findAll({
where: {
intVal: {
lte: 10
}
}
}).success(function(users) {
expect(users[0].username).to.equal('boo')
expect(users[0].intVal).to.equal(5)
expect(users[1].username).to.equal('boo2')
expect(users[1].intVal).to.equal(10)
done()
}) })
}) })
}) })
})
})
describe('eager loading', function() { describe('eager loading', function() {
describe('belongsTo', function() { describe('belongsTo', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!