Skip to content

Instantly share code, notes, and snippets.

@TGOlson
Created April 18, 2014 22:37
Show Gist options
  • Select an option

  • Save TGOlson/11067300 to your computer and use it in GitHub Desktop.

Select an option

Save TGOlson/11067300 to your computer and use it in GitHub Desktop.
Testing using Q with the built in Mongo promises.
var mongoose = require('mongoose');
var Entity = require('./server/models/entity')
var Q = require('q');
var db = mongoose.connect('mongodb://localhost/lp-test');
test1();
function test1() {
var newEntity = {
name: 'something',
type: 'something else'
}
var parent;
var p = Q.when(Entity.create(newEntity));
p.then(function (entity) {
parent = entity;
return entity
})
.then(function(ent){
return createEntityToEntity(ent);
})
.then(function (rval) {
console.log('rval', rval);
console.log('parent', parent)
})
.fin(function () {
// close files, database connections, stop servers, conclude tests
db.connection.close();
})
.done();
// var promise = entity.exec();
}
function createEntityToEntity(entity) {
console.log('in create')
// var d = Q.defer();
var newEntity = {
name: 'another new',
type: 'way new'
}
var p = Q.when(Entity.create(newEntity));
// var p = Entity.create(newEntity);
return p.then(function(newguy) {
console.log('newguyhere', newguy);
return [
updater(entity, newguy),
updater(entity, {_id: 1}),
];
// d.resolve();
})
.spread(function (ent1, ent2) {
console.log('spread', ent1, ent2)
});
// .then(d.resolve)
// .done();
// return d.promise;
}
function updater(entity, newnewEnt) {
console.log('in updater', entity)
var p = entity.update({$push: {childs: newnewEnt._id}}, {upset: true}).exec();
return p;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment