|
|
@ -6,6 +6,7 @@ var Datastore = require('../lib/datastore') |
|
|
|
, should = require('chai').should() |
|
|
|
, should = require('chai').should() |
|
|
|
, assert = require('chai').assert |
|
|
|
, assert = require('chai').assert |
|
|
|
, _ = require('underscore') |
|
|
|
, _ = require('underscore') |
|
|
|
|
|
|
|
, async = require('async') |
|
|
|
; |
|
|
|
; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -20,63 +21,127 @@ describe('Database', function () { |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
it('Able to insert a document in the database and retrieve it even after a reload', function (done) { |
|
|
|
describe('Insert', function () { |
|
|
|
var d = new Datastore(testDb); |
|
|
|
|
|
|
|
d.loadDatabase(function (err) { |
|
|
|
it('Able to insert a document in the database and retrieve it even after a reload', function (done) { |
|
|
|
assert.isNull(err); |
|
|
|
var d = new Datastore(testDb); |
|
|
|
d.find({}, function (err, docs) { |
|
|
|
d.loadDatabase(function (err) { |
|
|
|
docs.length.should.equal(0); |
|
|
|
assert.isNull(err); |
|
|
|
|
|
|
|
d.find({}, function (err, docs) { |
|
|
|
d.insert({ somedata: 'ok' }, function (err) { |
|
|
|
docs.length.should.equal(0); |
|
|
|
// The data was correctly updated
|
|
|
|
|
|
|
|
d.find({}, function (err, docs) { |
|
|
|
d.insert({ somedata: 'ok' }, function (err) { |
|
|
|
assert.isNull(err); |
|
|
|
// The data was correctly updated
|
|
|
|
docs.length.should.equal(1); |
|
|
|
d.find({}, function (err, docs) { |
|
|
|
Object.keys(docs[0]).length.should.equal(2); |
|
|
|
assert.isNull(err); |
|
|
|
docs[0].somedata.should.equal('ok'); |
|
|
|
docs.length.should.equal(1); |
|
|
|
assert.isDefined(docs[0]._id); |
|
|
|
Object.keys(docs[0]).length.should.equal(2); |
|
|
|
|
|
|
|
docs[0].somedata.should.equal('ok'); |
|
|
|
// After a reload the data has been correctly persisted
|
|
|
|
assert.isDefined(docs[0]._id); |
|
|
|
d.loadDatabase(function (err) { |
|
|
|
|
|
|
|
d.find({}, function (err, docs) { |
|
|
|
// After a reload the data has been correctly persisted
|
|
|
|
assert.isNull(err); |
|
|
|
d.loadDatabase(function (err) { |
|
|
|
docs.length.should.equal(1); |
|
|
|
d.find({}, function (err, docs) { |
|
|
|
Object.keys(docs[0]).length.should.equal(2); |
|
|
|
assert.isNull(err); |
|
|
|
docs[0].somedata.should.equal('ok'); |
|
|
|
docs.length.should.equal(1); |
|
|
|
assert.isDefined(docs[0]._id); |
|
|
|
Object.keys(docs[0]).length.should.equal(2); |
|
|
|
|
|
|
|
docs[0].somedata.should.equal('ok'); |
|
|
|
|
|
|
|
assert.isDefined(docs[0]._id); |
|
|
|
done(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
it('Can insert multiple documents in the database', function (done) { |
|
|
|
it('Can insert multiple documents in the database', function (done) { |
|
|
|
var d = new Datastore(testDb); |
|
|
|
var d = new Datastore(testDb); |
|
|
|
d.loadDatabase(function (err) { |
|
|
|
d.loadDatabase(function (err) { |
|
|
|
assert.isNull(err); |
|
|
|
assert.isNull(err); |
|
|
|
d.find({}, function (err, docs) { |
|
|
|
d.find({}, function (err, docs) { |
|
|
|
docs.length.should.equal(0); |
|
|
|
docs.length.should.equal(0); |
|
|
|
|
|
|
|
|
|
|
|
d.insert({ somedata: 'ok' }, function (err) { |
|
|
|
d.insert({ somedata: 'ok' }, function (err) { |
|
|
|
d.insert({ somedata: 'another' }, function (err) { |
|
|
|
d.insert({ somedata: 'another' }, function (err) { |
|
|
|
d.insert({ somedata: 'again' }, function (err) { |
|
|
|
d.insert({ somedata: 'again' }, function (err) { |
|
|
|
d.find({}, function (err, docs) { |
|
|
|
d.find({}, function (err, docs) { |
|
|
|
docs.length.should.equal(3); |
|
|
|
docs.length.should.equal(3); |
|
|
|
_.pluck(docs, 'somedata').should.contain('ok'); |
|
|
|
_.pluck(docs, 'somedata').should.contain('ok'); |
|
|
|
_.pluck(docs, 'somedata').should.contain('another'); |
|
|
|
_.pluck(docs, 'somedata').should.contain('another'); |
|
|
|
_.pluck(docs, 'somedata').should.contain('again'); |
|
|
|
_.pluck(docs, 'somedata').should.contain('again'); |
|
|
|
done(); |
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
}); // ==== End of 'Insert' ==== //
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
describe('Find', function () { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
it('Can find all documents an empty query is used', function (done) { |
|
|
|
|
|
|
|
var d = new Datastore(testDb); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async.waterfall([ |
|
|
|
|
|
|
|
function (cb) { |
|
|
|
|
|
|
|
d.loadDatabase(function (err) { |
|
|
|
|
|
|
|
d.insert({ somedata: 'ok' }, function (err) { |
|
|
|
|
|
|
|
d.insert({ somedata: 'another', plus: 'additional data' }, function (err) { |
|
|
|
|
|
|
|
d.insert({ somedata: 'again' }, function (err) { return cb(err); }); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
, function (cb) { // Test with empty object
|
|
|
|
|
|
|
|
d.find({}, function (err, docs) { |
|
|
|
|
|
|
|
assert.isNull(err); |
|
|
|
|
|
|
|
docs.length.should.equal(3); |
|
|
|
|
|
|
|
_.pluck(docs, 'somedata').should.contain('ok'); |
|
|
|
|
|
|
|
_.pluck(docs, 'somedata').should.contain('another'); |
|
|
|
|
|
|
|
_.find(docs, function (d) { return d.somedata === 'another' }).plus.should.equal('additional data'); |
|
|
|
|
|
|
|
_.pluck(docs, 'somedata').should.contain('again'); |
|
|
|
|
|
|
|
return cb(); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
], done); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
it('Can find all documents matching a basic query', function (done) { |
|
|
|
|
|
|
|
var d = new Datastore(testDb); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async.waterfall([ |
|
|
|
|
|
|
|
function (cb) { |
|
|
|
|
|
|
|
d.loadDatabase(function (err) { |
|
|
|
|
|
|
|
d.insert({ somedata: 'ok' }, function (err) { |
|
|
|
|
|
|
|
d.insert({ somedata: 'again', plus: 'additional data' }, function (err) { |
|
|
|
|
|
|
|
d.insert({ somedata: 'again' }, function (err) { return cb(err); }); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
, function (cb) { // Test with empty object
|
|
|
|
|
|
|
|
d.find({ somedata: 'again' }, function (err, docs) { |
|
|
|
|
|
|
|
assert.isNull(err); |
|
|
|
|
|
|
|
docs.length.should.equal(2); |
|
|
|
|
|
|
|
_.pluck(docs, 'somedata').should.not.contain('ok'); |
|
|
|
|
|
|
|
return cb(); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
], done); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}); // ==== End of 'Find' ==== //
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|