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

Commit b33ff7c7 by Mick Hansen

convert tests to promises

1 parent 793ee3f4
...@@ -4,6 +4,7 @@ var chai = require('chai') ...@@ -4,6 +4,7 @@ var chai = require('chai')
, expect = chai.expect , expect = chai.expect
, Support = require(__dirname + '/../support') , Support = require(__dirname + '/../support')
, Sequelize = require(__dirname + '/../../../index') , Sequelize = require(__dirname + '/../../../index')
, Promise = Sequelize.Promise
, DataTypes = require(__dirname + '/../../../lib/data-types') , DataTypes = require(__dirname + '/../../../lib/data-types')
, datetime = require('chai-datetime') , datetime = require('chai-datetime')
, async = require('async'); , async = require('async');
...@@ -13,7 +14,7 @@ chai.config.includeStack = true; ...@@ -13,7 +14,7 @@ chai.config.includeStack = true;
describe(Support.getTestDialectTeaser('Include'), function() { describe(Support.getTestDialectTeaser('Include'), function() {
describe('find', function() { describe('find', function() {
it('should include a non required model, with conditions and two includes N:M 1:M', function(done ) { it('should include a non required model, with conditions and two includes N:M 1:M', function( ) {
var A = this.sequelize.define('A', { name: DataTypes.STRING(40) }, { paranoid: true }) var A = this.sequelize.define('A', { name: DataTypes.STRING(40) }, { paranoid: true })
, B = this.sequelize.define('B', { name: DataTypes.STRING(40) }, { paranoid: true }) , B = this.sequelize.define('B', { name: DataTypes.STRING(40) }, { paranoid: true })
, C = this.sequelize.define('C', { name: DataTypes.STRING(40) }, { paranoid: true }) , C = this.sequelize.define('C', { name: DataTypes.STRING(40) }, { paranoid: true })
...@@ -33,19 +34,14 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -33,19 +34,14 @@ describe(Support.getTestDialectTeaser('Include'), function() {
D.hasMany(B); D.hasMany(B);
this.sequelize.sync({ force: true }).done(function(err ) { return this.sequelize.sync({ force: true }).then(function() {
expect(err).not.to.be.ok; return A.find({
A.find({
include: [ include: [
{ model: B, required: false, include: [ { model: B, required: false, include: [
{ model: C, required: false }, { model: C, required: false },
{ model: D } { model: D }
]} ]}
] ]
}).done(function(err ) {
expect(err).not.to.be.ok;
done();
}); });
}); });
}); });
...@@ -178,7 +174,7 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -178,7 +174,7 @@ describe(Support.getTestDialectTeaser('Include'), function() {
}); });
}); });
it('should support many levels of belongsTo (with a lower level having a where)', function(done) { it('should support many levels of belongsTo (with a lower level having a where)', function() {
var A = this.sequelize.define('a', {}) var A = this.sequelize.define('a', {})
, B = this.sequelize.define('b', {}) , B = this.sequelize.define('b', {})
, C = this.sequelize.define('c', {}) , C = this.sequelize.define('c', {})
...@@ -200,49 +196,44 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -200,49 +196,44 @@ describe(Support.getTestDialectTeaser('Include'), function() {
F.belongsTo(G); F.belongsTo(G);
G.belongsTo(H); G.belongsTo(H);
var b, singles = [ return this.sequelize.sync({force: true}).then(function() {
B, return Promise.join(
C, A.create({}),
D, (function (singles) {
E, var promise = Promise.resolve()
F, , previousInstance
G, , b;
H
]; singles.forEach(function (model) {
this.sequelize.sync().done(function() {
async.auto({
a: function(callback) {
A.create({}).done(callback);
},
singleChain: function(callback) {
var previousInstance;
async.eachSeries(singles, function(model, callback) {
var values = {}; var values = {};
if (model.name === 'g') { if (model.name === 'g') {
values.name = 'yolo'; values.name = 'yolo';
} }
model.create(values).done(function(err, instance) {
promise = promise.then(function () {
return model.create(values).then(function (instance) {
if (previousInstance) { if (previousInstance) {
previousInstance['set'+ Sequelize.Utils.uppercaseFirst(model.name)](instance).done(function() { return previousInstance['set'+ Sequelize.Utils.uppercaseFirst(model.name)](instance).then(function() {
previousInstance = instance; previousInstance = instance;
callback();
}); });
} else { } else {
previousInstance = b = instance; previousInstance = b = instance;
callback();
} }
}); });
}, callback); });
}, });
ab: ['a', 'singleChain', function(callback, results) {
results.a.setB(b).done(callback); promise = promise.then(function () {
}] return b;
}, function() { });
A.find({ return promise;
})([B, C, D, E, F, G, H])
).spread(function (a, b) {
return a.setB(b);
}).then(function () {
return A.find({
include: [ include: [
{model: B, include: [ {model: B, include: [
{model: C, include: [ {model: C, include: [
...@@ -260,10 +251,8 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -260,10 +251,8 @@ describe(Support.getTestDialectTeaser('Include'), function() {
]} ]}
]} ]}
] ]
}).done(function(err, a) { }).then(function(a) {
expect(err).not.to.be.ok;
expect(a.b.c.d.e.f.g.h).to.be.ok; expect(a.b.c.d.e.f.g.h).to.be.ok;
done();
}); });
}); });
}); });
......
...@@ -12,19 +12,16 @@ chai.config.includeStack = true; ...@@ -12,19 +12,16 @@ chai.config.includeStack = true;
describe(Support.getTestDialectTeaser('Include'), function() { describe(Support.getTestDialectTeaser('Include'), function() {
describe('findAndCountAll', function() { describe('findAndCountAll', function() {
it('should be able to include a required model. Result rows should match count', function() {
it('Try to include a required model. Result rows should match count', function(done ) { var User = this.sequelize.define('User', { name: DataTypes.STRING(40) }, { paranoid: true }),
var DT = DataTypes, SomeConnection = this.sequelize.define('SomeConnection', {
S = this.sequelize, m: DataTypes.STRING(40),
User = S.define('User', { name: DT.STRING(40) }, { paranoid: true }), fk: DataTypes.INTEGER,
SomeConnection = S.define('SomeConnection', { u: DataTypes.INTEGER
m: DT.STRING(40),
fk: DT.INTEGER,
u: DT.INTEGER
}, { paranoid: true }), }, { paranoid: true }),
A = S.define('A', { name: DT.STRING(40) }, { paranoid: true }), A = this.sequelize.define('A', { name: DataTypes.STRING(40) }, { paranoid: true }),
B = S.define('B', { name: DT.STRING(40) }, { paranoid: true }), B = this.sequelize.define('B', { name: DataTypes.STRING(40) }, { paranoid: true }),
C = S.define('C', { name: DT.STRING(40) }, { paranoid: true }); C = this.sequelize.define('C', { name: DataTypes.STRING(40) }, { paranoid: true });
// Associate them // Associate them
User.hasMany(SomeConnection, { foreignKey: 'u' }); User.hasMany(SomeConnection, { foreignKey: 'u' });
...@@ -39,17 +36,17 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -39,17 +36,17 @@ describe(Support.getTestDialectTeaser('Include'), function() {
C.hasMany(SomeConnection, { foreignKey: 'fk', constraints: false }); C.hasMany(SomeConnection, { foreignKey: 'fk', constraints: false });
// Sync them // Sync them
S.sync({ force: true }).done(function(err ) { expect(err).not.to.be.ok; return this.sequelize.sync({ force: true }).then(function () {
// Create an enviroment // Create an enviroment
return Promise.join(
User.bulkCreate([ User.bulkCreate([
{ name: 'Youtube' }, { name: 'Youtube' },
{ name: 'Facebook' }, { name: 'Facebook' },
{ name: 'Google' }, { name: 'Google' },
{ name: 'Yahoo' }, { name: 'Yahoo' },
{ name: '404' } { name: '404' }
]).done(function(err, users ) { expect(err).not.to.be.ok; expect(users).to.be.length(5); ]),
SomeConnection.bulkCreate([ // Lets count, m: A and u: 1 SomeConnection.bulkCreate([ // Lets count, m: A and u: 1
{ u: 1, m: 'A', fk: 1 }, // 1 // Will be deleted { u: 1, m: 'A', fk: 1 }, // 1 // Will be deleted
{ u: 2, m: 'A', fk: 1 }, { u: 2, m: 'A', fk: 1 },
...@@ -75,35 +72,31 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -75,35 +72,31 @@ describe(Support.getTestDialectTeaser('Include'), function() {
{ u: 2, m: 'B', fk: 2 }, { u: 2, m: 'B', fk: 2 },
{ u: 1, m: 'A', fk: 4 }, // 4 { u: 1, m: 'A', fk: 4 }, // 4
{ u: 4, m: 'A', fk: 2 } { u: 4, m: 'A', fk: 2 }
]).done(function(err, conns ) { expect(err).not.to.be.ok; expect(conns).to.be.length(24); ]),
A.bulkCreate([ A.bulkCreate([
{ name: 'Just' }, { name: 'Just' },
{ name: 'for' }, { name: 'for' },
{ name: 'testing' }, { name: 'testing' },
{ name: 'proposes' }, { name: 'proposes' },
{ name: 'only' } { name: 'only' }
]).done(function(err, as ) { expect(err).not.to.be.ok; expect(as).to.be.length(5); ]),
B.bulkCreate([ B.bulkCreate([
{ name: 'this should not' }, { name: 'this should not' },
{ name: 'be loaded' } { name: 'be loaded' }
]).done(function(err, bs ) { expect(err).not.to.be.ok; expect(bs).to.be.length(2); ]),
C.bulkCreate([ C.bulkCreate([
{ name: 'because we only want A' } { name: 'because we only want A' }
]).done(function(err, cs ) { expect(err).not.to.be.ok; expect(cs).to.be.length(1); ])
).then(function () {
// Delete some of conns to prove the concept // Delete some of conns to prove the concept
SomeConnection.destroy({where: { return SomeConnection.destroy({where: {
m: 'A', m: 'A',
u: 1, u: 1,
fk: [1, 2] fk: [1, 2]
}}).done(function(err ) { expect(err).not.to.be.ok; }}).then(function() {
// Last and most important queries ( we connected 4, but deleted 2, witch means we must get 2 only ) // Last and most important queries ( we connected 4, but deleted 2, witch means we must get 2 only )
A.findAndCountAll({ return A.findAndCountAll({
include: [{ include: [{
model: SomeConnection, required: true, model: SomeConnection, required: true,
where: { where: {
...@@ -111,36 +104,15 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -111,36 +104,15 @@ describe(Support.getTestDialectTeaser('Include'), function() {
u: 1 u: 1
} }
}], }],
limit: 5 limit: 5
}).then(function(result ) {
})
.done(function(err, result ) {
// Test variables
expect(err).not.to.be.ok;
expect(result.count).to.be.equal(2); expect(result.count).to.be.equal(2);
expect(result.rows.length).to.be.equal(2); expect(result.rows.length).to.be.equal(2);
done();
// Last and most important queries - END
}); });
// Delete some of conns to prove the concept - END
});
// Create an enviroment - END
}); });
}); });
}); });
}); });
});
// Sync them - END
});
});
it('should count on a where and not use an uneeded include', function() { it('should count on a where and not use an uneeded include', function() {
var Project = this.sequelize.define('Project', { var Project = this.sequelize.define('Project', {
......
...@@ -11,7 +11,7 @@ chai.config.includeStack = true; ...@@ -11,7 +11,7 @@ chai.config.includeStack = true;
describe(Support.getTestDialectTeaser('Paranoid'), function() { describe(Support.getTestDialectTeaser('Paranoid'), function() {
beforeEach(function(done ) { beforeEach(function( ) {
var S = this.sequelize, var S = this.sequelize,
DT = DataTypes, DT = DataTypes,
...@@ -32,34 +32,24 @@ describe(Support.getTestDialectTeaser('Paranoid'), function() { ...@@ -32,34 +32,24 @@ describe(Support.getTestDialectTeaser('Paranoid'), function() {
D.hasMany(A); D.hasMany(A);
S.sync({ force: true }).done(function(err ) { return S.sync({ force: true });
expect(err).not.to.be.ok;
done();
}); });
}); it('paranoid with timestamps: false should be ignored / not crash', function() {
var S = this.sequelize
it('paranoid with timestamps: false should be ignored / not crash', function(done) { , Test = S.define('Test', {
var S = this.sequelize,
Test = S.define('Test', {
name: DataTypes.STRING name: DataTypes.STRING
},{ },{
timestamps: false, timestamps: false,
paranoid: true paranoid: true
}); });
S.sync({ force: true }).done(function() { return S.sync({ force: true }).then(function() {
Test.find(1).done(function(err ) { return Test.find(1);
expect(err).to.be.not.ok;
done();
});
}); });
}); });
it('test if non required is marked as false', function(done ) { it('test if non required is marked as false', function( ) {
var A = this.A, var A = this.A,
B = this.B, B = this.B,
options = { options = {
...@@ -71,17 +61,12 @@ describe(Support.getTestDialectTeaser('Paranoid'), function() { ...@@ -71,17 +61,12 @@ describe(Support.getTestDialectTeaser('Paranoid'), function() {
] ]
}; };
A.find(options).done(function(err ) { return A.find(options).then(function() {
expect(err).not.to.be.ok;
expect(options.include[0].required).to.be.equal(false); expect(options.include[0].required).to.be.equal(false);
done();
}); });
}); });
it('test if required is marked as true', function(done ) { it('test if required is marked as true', function( ) {
var A = this.A, var A = this.A,
B = this.B, B = this.B,
options = { options = {
...@@ -93,13 +78,9 @@ describe(Support.getTestDialectTeaser('Paranoid'), function() { ...@@ -93,13 +78,9 @@ describe(Support.getTestDialectTeaser('Paranoid'), function() {
] ]
}; };
A.find(options).done(function(err ) { return A.find(options).then(function() {
expect(err).not.to.be.ok;
expect(options.include[0].required).to.be.equal(true); expect(options.include[0].required).to.be.equal(true);
done();
}); });
}); });
it('should not load paranoid, destroyed instances, with a non-paranoid parent', function () { it('should not load paranoid, destroyed instances, with a non-paranoid parent', function () {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!