Factorized tests and bumped version

pull/2/head
Louis Chatriot 12 years ago
parent b8215bd40f
commit 2c176b3941
  1. 2
      package.json
  2. 59
      test/db.test.js

@ -1,6 +1,6 @@
{
"name": "nedb",
"version": "0.0.1",
"version": "0.0.2",
"author": {
"name": "tldr.io",
"email": "hello@tldr.io"

@ -11,22 +11,32 @@ var Datastore = require('../lib/datastore')
describe('Database', function () {
var d = new Datastore(testDb);
beforeEach(function (done) {
async.waterfall([
function (cb) {
customUtils.ensureDirectoryExists(path.dirname(testDb), function () {
fs.exists(testDb, function (exists) {
if (exists) {
fs.unlink(testDb, done);
} else { return done(); }
fs.unlink(testDb, cb);
} else { return cb(); }
});
});
}
, function (cb) {
d.loadDatabase(function (err) {
assert.isNull(err);
return cb();
});
}
], done);
});
describe('Insert', function () {
it('Able to insert a document in the database and retrieve it even after a reload', function (done) {
var d = new Datastore(testDb);
d.loadDatabase(function (err) {
assert.isNull(err);
d.find({}, function (err, docs) {
docs.length.should.equal(0);
@ -56,12 +66,8 @@ describe('Database', function () {
});
});
});
});
it('Can insert multiple documents in the database', function (done) {
var d = new Datastore(testDb);
d.loadDatabase(function (err) {
assert.isNull(err);
d.find({}, function (err, docs) {
docs.length.should.equal(0);
@ -80,15 +86,12 @@ describe('Database', function () {
});
});
});
});
it('Can insert and get back from DB complex objects with all primitive and secondary types', function (done) {
var da = new Date()
, obj = { a: ['ee', 'ff', 42], date: da, subobj: { a: 'b', b: 'c' } }
, d = new Datastore(testDb)
;
d.loadDatabase(function () {
d.insert(obj, function (err) {
d.findOne({}, function (err, res) {
assert.isNull(err);
@ -104,7 +107,6 @@ describe('Database', function () {
});
});
});
});
}); // ==== End of 'Insert' ==== //
@ -112,17 +114,13 @@ describe('Database', function () {
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) {
@ -139,17 +137,13 @@ describe('Database', function () {
});
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 query that will return docs
d.find({ somedata: 'again' }, function (err, docs) {
@ -170,17 +164,13 @@ describe('Database', function () {
});
it('Can find one document matching a basic query and return null if none is found', 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 query that will return docs
d.findOne({ somedata: 'ok' }, function (err, doc) {
@ -207,17 +197,13 @@ describe('Database', function () {
describe('Update', function () {
it("If the query doesn't match anything, database is not modified", 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: 'another' }, function (err) { return cb(err); });
});
});
});
}
, function (cb) { // Test with query that doesn't match anything
d.update({ somedata: 'nope' }, { newDoc: 'yes' }, { multi: true }, function (err, n) {
@ -253,8 +239,7 @@ describe('Database', function () {
});
it("Can update multiple documents matching the query", function (done) {
var d = new Datastore(testDb)
, id1, id2, id3;
var id1, id2, id3;
// Test DB state after update and reload
function testPostUpdateState (cb) {
@ -285,7 +270,6 @@ describe('Database', function () {
// Actually launch the tests
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) {
@ -296,7 +280,6 @@ describe('Database', function () {
});
});
});
});
}
, function (cb) { // Test with query that doesn't match anything
d.update({ somedata: 'again' }, { newDoc: 'yes' }, { multi: true }, function (err, n) {
@ -314,8 +297,7 @@ describe('Database', function () {
});
it("Can update only one document matching the query", function (done) {
var d = new Datastore(testDb)
, id1, id2, id3;
var id1, id2, id3;
// Test DB state after update and reload
function testPostUpdateState (cb) {
@ -347,7 +329,6 @@ describe('Database', function () {
// 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) {
@ -358,7 +339,6 @@ describe('Database', function () {
});
});
});
});
}
, function (cb) { // Test with query that doesn't match anything
d.update({ somedata: 'again' }, { newDoc: 'yes' }, { multi: false }, function (err, n) {
@ -381,8 +361,7 @@ describe('Database', function () {
describe('Remove', function () {
it('Can remove multiple documents', function (done) {
var d = new Datastore(testDb)
, id1, id2, id3;
var id1, id2, id3;
// Test DB status
function testPostUpdateState (cb) {
@ -400,7 +379,6 @@ describe('Database', function () {
// 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) {
@ -411,7 +389,6 @@ describe('Database', function () {
});
});
});
});
}
, function (cb) { // Test with query that doesn't match anything
d.remove({ somedata: 'again' }, { multi: true }, function (err, n) {

Loading…
Cancel
Save