Promisify 'autoupdate'

This commit is contained in:
Pradnya Baviskar 2015-06-23 15:44:34 +05:30
parent 019705d32f
commit e7430184a9
2 changed files with 100 additions and 7 deletions

View File

@ -830,9 +830,12 @@ DataSource.prototype.autoupdate = function (models, cb) {
models = undefined;
}
cb = cb || utils.createPromiseCallback();
if (!this.connector.autoupdate) {
// NOOP
return cb && process.nextTick(cb);
process.nextTick(cb);
return cb.promise;
}
// First argument is a model name
@ -846,7 +849,8 @@ DataSource.prototype.autoupdate = function (models, cb) {
models = models || Object.keys(attachedModels);
if (models.length === 0) {
return process.nextTick(cb);
process.nextTick(cb);
return cb.promise;
}
var invalidModels = models.filter(function (m) {
@ -854,16 +858,16 @@ DataSource.prototype.autoupdate = function (models, cb) {
});
if (invalidModels.length) {
return process.nextTick(function () {
if (cb) {
cb(new Error('Cannot migrate models not attached to this datasource: ' +
invalidModels.join(' ')));
}
process.nextTick(function () {
cb(new Error('Cannot migrate models not attached to this datasource: ' +
invalidModels.join(' ')));
});
return cb.promise;
}
}
this.connector.autoupdate(models, cb);
return cb.promise;
};
/**

View File

@ -548,6 +548,95 @@ describe('Memory connector', function() {
});
});
describe('With mocked autoupdate', function() {
var ds, model;
beforeEach(function() {
ds = new DataSource({
connector: 'memory'
});
ds.connector.autoupdate = function(models, cb) {
process.nextTick(cb);
};
model = ds.createModel('m1', {
name: String
});
ds.automigrate();
ds.createModel('m1', {
name: String,
address: String
});
});
it('autoupdates all models', function(done) {
ds.autoupdate(function(err, result){
done(err);
});
});
it('autoupdates all models - promise variant', function(done) {
ds.autoupdate()
.then(function(result) {
done();
})
.catch(function(err){
done(err);
});
});
it('autoupdates one model', function(done) {
ds.autoupdate('m1', function(err) {
done(err);
});
});
it('autoupdates one model - promise variant', function(done) {
ds.autoupdate('m1')
.then(function(result) {
done();
})
.catch(function(err){
done(err);
});
});
it('autoupdates one or more models in an array', function(done) {
ds.autoupdate(['m1'], function(err) {
done(err);
});
});
it('autoupdates one or more models in an array - promise variant', function(done) {
ds.autoupdate(['m1'])
.then(function(result) {
done();
})
.catch(function(err){
done(err);
});
});
it('autoupdate reports errors for models not attached', function(done) {
ds.autoupdate(['m1', 'm2'], function(err) {
err.should.be.an.instanceOf(Error);
done();
});
});
it('autoupdate reports errors for models not attached - promise variant', function(done) {
ds.autoupdate(['m1', 'm2'])
.then(function(){
done(new Error('automigrate() should have failed'));
})
.catch(function(err){
err.should.be.an.instanceOf(Error);
done();
});
});
});
});
describe('Optimized connector', function() {