Skip to content

Commit

Permalink
Migrated instance_db tests to promises
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Sep 19, 2014
1 parent 5961b97 commit 8e5b5d0
Showing 1 changed file with 55 additions and 95 deletions.
150 changes: 55 additions & 95 deletions test/instance_db.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
var Database = require('../index.js');
var Model = Database.Model;
var Instance = Database.Instance;
var should = require('should');
var Concoction = require('concoction');

describe('orm', function () {
"use strict";

describe('Instance', function () {
var db = new Database({ database: 'iridium_test' });
var db = new Database(config);

var model = new Model(db, 'instances', {
username: String,
Expand All @@ -23,10 +17,9 @@ describe('orm', function () {
]
});

before(function(done) {
db.connect(function(err) {
if(err) return done(err);
model.remove(done);
before(function () {
return db.connect().then(function() {
return model.remove();
});
});

Expand All @@ -36,146 +29,113 @@ describe('orm', function () {

describe('database', function () {
describe('save', function() {
it('should correctly store changes made to the instance', function(done) {
model.create({
it('should correctly store changes made to the instance', function() {
return model.create({
username: 'billy',
age: 10,
sessions: [{
id: 'aaaa',
expires: new Date()
}]
}, function(err, original) {
if(err) return done(err);

}).then(function(original) {
original.age = 12;

original.sessions.push({
id: 'bbbb',
expires: new Date()
});

original.save(function(err, updated) {
if(err) return done(err);
return original.save().then(function(updated) {
updated.should.equal(original);
updated.age.should.eql(12);
updated.sessions.length.toString().should.eql('2');

done();
});
});
});

it('should allow passing of specific change-sets', function(done) {
model.create({
it('should allow passing of specific change-sets', function() {
return model.create({
username: 'bob',
age: 12,
sessions: [{
id: 'aaaa',
expires: new Date()
}]
}, function(err, instance) {
if(err) return done(err);

instance.save({ $set: { sessions: [] }, $inc: { age: 1 }}, function(err) {
if(err) return done(err);
instance.age.should.eql(13);
instance.sessions.should.eql([]);
done();
});
}).then(function(instance) {
return instance.save({ $set: { sessions: [] }, $inc: { age: 1 }});
}).then(function(instance) {
instance.age.should.eql(13);
instance.sessions.should.eql([]);
});
});

it('should allow conditions and changesets to be used', function(done) {
model.create({
it('should allow conditions and changesets to be used', function() {
return model.create({
username: 'sally',
age: 15,
sessions: [{
id: 'aaaa',
expires: new Date()
}]
}, function(err, instance) {
if(err) return done(err);
instance.save({ age: 14 }, { $inc: { age: 1 }}, function(err) {
if(err) return done(err);
instance.age.should.eql(15);

instance.save({ age: 15 }, { $inc: { age: 1 }}, function(err) {
if(err) return done(err);
instance.age.should.eql(16);

done();
});
});
}).then(function(instance) {
return instance.save({ age: 14 }, { $inc: { age: 1 }});
}).then(function(instance) {
instance.age.should.eql(15);
return instance.save({ age: 15 }, { $inc: { age: 1 }});
}).then(function(instance) {
instance.age.should.eql(16);
});
});
});

describe('update', function() {
it('should retrieve the latest version of the model from the database', function(done) {
model.findOne('billy', function(err, billy1) {
if(err) return done(err);

model.findOne('billy', function(err, billy2) {
if(err) return done(err);

billy1.age++;
billy1.save(function(err) {
if(err) return done(err);
billy2.age.should.eql(billy1.age - 1);

billy2.update(function(err) {
if(err) return done(err);

billy2.age.should.eql(billy1.age);
done();
});
});
});
})
it('should retrieve the latest version of the model from the database', function() {
var billy1, billy2;
return model.findOne('billy').then(function(instance) {
billy1 = instance;
return model.findOne('billy');
}).then(function(instance) {
billy2 = instance;
billy1.age++;
return billy1.save();
}).then(function() {
billy2.age.should.equal(billy1.age - 1);
return billy2.update();
}).then(function() {
billy2.age.should.equal(billy1.age);
});
});
});

describe('remove', function() {
it('should remove instances from the database', function(done) {
model.get('billy', function(err, billy) {
if(err) return done(err);
should.exist(billy);

billy.remove(function(err) {
if(err) return done(err);
billy.__state.isNew.should.be.true;

model.get('billy', function(err, nothing) {
if(err) return done(err);
should.not.exist(nothing);

done();
});
});
it('should remove instances from the database', function() {
return model.get('billy').then(function(instance) {
should.exist(instance);
return instance.remove();
}).then(function(instance) {
instance.__state.isNew.should.be.true;
return model.get('billy');
}).then(function(instance) {
should.not.exist(instance);
});
});

it('should set instance isNew to true after removal', function(done) {
model.create({
it('should set instance isNew to true after removal', function() {
return model.create({
username: 'billy',
age: 10,
sessions: [{
id: 'aaaa',
expires: new Date()
}]
}, function(err, original) {
/// <param name="original" value="new model.Instance({})"/>
if(err) return done(err);

}).then(function(original) {
should.exist(original);
original.__state.isNew.should.be.false;

original.remove(function(err) {
should.not.exist(err);
original.__state.isNew.should.be.true;
original.__state.isNew.should.be.false;

done();
});
return original.remove();
}).then(function(removed) {
removed.__state.isNew.should.be.true;
});
});
});
Expand Down

0 comments on commit 8e5b5d0

Please sign in to comment.