Test update effect after a reload of the database

pull/2/head
Louis Chatriot 12 years ago
parent a2b9583155
commit 27ab013a84
  1. 139
      test/db.test.js

@ -232,6 +232,33 @@ describe('Database', function () {
var d = new Datastore(testDb)
, id1, id2, id3;
// Test DB state after update and reload
function testPostUpdateState (cb) {
d.find({}, function (err, docs) {
var doc1 = _.find(docs, function (d) { return d._id === id1; })
, doc2 = _.find(docs, function (d) { return d._id === id2; })
, doc3 = _.find(docs, function (d) { return d._id === id3; })
;
docs.length.should.equal(3);
Object.keys(doc1).length.should.equal(2);
doc1.somedata.should.equal('ok');
doc1._id.should.equal(id1);
Object.keys(doc2).length.should.equal(2);
doc2.newDoc.should.equal('yes');
doc2._id.should.equal(id2);
Object.keys(doc3).length.should.equal(2);
doc3.newDoc.should.equal('yes');
doc3._id.should.equal(id3);
return cb();
});
}
// Actually launch the tests
async.waterfall([
function (cb) {
d.loadDatabase(function (err) {
@ -251,35 +278,85 @@ describe('Database', function () {
d.update({ somedata: 'again' }, { newDoc: 'yes' }, { multi: true }, function (err, n) {
assert.isNull(err);
n.should.equal(2);
return cb();
});
}
, async.apply(testPostUpdateState)
, function (cb) {
d.loadDatabase(function (err) { cb(err); });
}
, async.apply(testPostUpdateState)
], done);
});
d.find({}, function (err, docs) {
var doc1 = _.find(docs, function (d) { return d._id === id1; })
, doc2 = _.find(docs, function (d) { return d._id === id2; })
, doc3 = _.find(docs, function (d) { return d._id === id3; })
;
it("Can update only one document matching the query", function (done) {
var d = new Datastore(testDb)
, id1, id2, id3;
docs.length.should.equal(3);
// Test DB state after update and reload
function testPostUpdateState (cb) {
d.find({}, function (err, docs) {
var doc1 = _.find(docs, function (d) { return d._id === id1; })
, doc2 = _.find(docs, function (d) { return d._id === id2; })
, doc3 = _.find(docs, function (d) { return d._id === id3; })
;
Object.keys(doc1).length.should.equal(2);
doc1.somedata.should.equal('ok');
doc1._id.should.equal(id1);
docs.length.should.equal(3);
Object.keys(doc2).length.should.equal(2);
doc2.newDoc.should.equal('yes');
doc2._id.should.equal(id2);
Object.keys(doc1).length.should.equal(2);
doc1.somedata.should.equal('ok');
doc1._id.should.equal(id1);
Object.keys(doc3).length.should.equal(2);
doc3.newDoc.should.equal('yes');
doc3._id.should.equal(id3);
Object.keys(doc2).length.should.equal(2);
doc2.newDoc.should.equal('yes');
doc2._id.should.equal(id2);
return cb();
// Third object was not updated
Object.keys(doc3).length.should.equal(2);
doc3.somedata.should.equal('again');
doc3._id.should.equal(id3);
return cb();
});
}
// Actually launch the test
async.waterfall([
function (cb) {
d.loadDatabase(function (err) {
d.insert({ somedata: 'ok' }, function (err, doc1) {
id1 = doc1._id;
d.insert({ somedata: 'again', plus: 'additional data' }, function (err, doc2) {
id2 = doc2._id;
d.insert({ somedata: 'again' }, function (err, doc3) {
id3 = doc3._id;
return cb(err);
});
});
});
});
}
, function (cb) { // Test with query that doesn't match anything
d.update({ somedata: 'again' }, { newDoc: 'yes' }, { multi: false }, function (err, n) {
assert.isNull(err);
n.should.equal(1);
return cb();
});
}
, async.apply(testPostUpdateState)
, function (cb) {
d.loadDatabase(function (err) { return cb(err); });
}
, async.apply(testPostUpdateState) // The persisted state has been updated
], done);
});
it("Can update only one document matching the query", function (done) {
}); // ==== End of 'Update' ==== //
describe('Remove', function () {
it('Can remove multiple documents', function (done) {
var d = new Datastore(testDb)
, id1, id2, id3;
@ -299,30 +376,16 @@ describe('Database', function () {
});
}
, function (cb) { // Test with query that doesn't match anything
d.update({ somedata: 'again' }, { newDoc: 'yes' }, { multi: false }, function (err, n) {
d.remove({ somedata: 'again' }, { multi: true }, function (err, n) {
assert.isNull(err);
n.should.equal(1);
n.should.equal(2);
d.find({}, function (err, docs) {
var doc1 = _.find(docs, function (d) { return d._id === id1; })
, doc2 = _.find(docs, function (d) { return d._id === id2; })
, doc3 = _.find(docs, function (d) { return d._id === id3; })
;
docs.length.should.equal(1);
docs.length.should.equal(3);
Object.keys(doc1).length.should.equal(2);
doc1.somedata.should.equal('ok');
doc1._id.should.equal(id1);
Object.keys(doc2).length.should.equal(2);
doc2.newDoc.should.equal('yes');
doc2._id.should.equal(id2);
// Third object was not updated
Object.keys(doc3).length.should.equal(2);
doc3.somedata.should.equal('again');
doc3._id.should.equal(id3);
Object.keys(docs[0]).length.should.equal(2);
docs[0]._id.should.equal(id1);
docs[0].somedata.should.equal('ok');
return cb();
});
@ -332,7 +395,7 @@ describe('Database', function () {
});
}); // ==== End of 'Update' ==== //
}); // ==== End of 'Remove' ==== //

Loading…
Cancel
Save