From dc6b4a25d7dd2fc29931cb2ddc8720b8f32e6ca7 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Thu, 2 May 2013 11:00:57 +0200 Subject: [PATCH] Test inserting documents --- lib/datastore.js | 2 ++ package.json | 60 +++++++++++++++++++++----------------- test/db.test.js | 76 ++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 108 insertions(+), 30 deletions(-) diff --git a/lib/datastore.js b/lib/datastore.js index 93332e7..7ee6407 100644 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -253,3 +253,5 @@ Datastore.prototype.remove = function (query, options, cb) { }; + +module.exports = Datastore; diff --git a/package.json b/package.json index c81dc94..5aa83a5 100644 --- a/package.json +++ b/package.json @@ -1,31 +1,37 @@ { - "name": "nedb" -, "version": "0.0.1" -, "author": { "name": "tldr.io", "email": "hello@tldr.io"} -, "contributors": [ + "name": "nedb", + "version": "0.0.1", + "author": { + "name": "tldr.io", + "email": "hello@tldr.io" + }, + "contributors": [ "Louis Chatriot" - ] -, "description": "File-based embedded data store for node.js" -, "keywords": [ - "database" - , "datastore" - , "embedded" - ] -, "homepage": "https://github.com/louischatriot/node-embedded-db" -, "repository": { "type": "git" - , "url": "git@github.com:louischatriot/node-embedded-db.git" - } -, "dependencies": { - } -, "devDependencies": { - "chai": "1.0.x" - , "mocha": "1.4.x" - , "request": "2.9.x" - , "sinon": "1.3.x" - } -, "scripts": { + ], + "description": "File-based embedded data store for node.js", + "keywords": [ + "database", + "datastore", + "embedded" + ], + "homepage": "https://github.com/louischatriot/node-embedded-db", + "repository": { + "type": "git", + "url": "git@github.com:louischatriot/node-embedded-db.git" + }, + "dependencies": { + "async": "~0.2.8", + "underscore": "~1.4.4" + }, + "devDependencies": { + "chai": "1.0.x", + "mocha": "1.4.x", + "request": "2.9.x", + "sinon": "1.3.x" + }, + "scripts": { "test": "make test" - } -, "main": "server" -, "licence": "MIT" + }, + "main": "server", + "licence": "MIT" } diff --git a/test/db.test.js b/test/db.test.js index cf4631d..ffa6023 100644 --- a/test/db.test.js +++ b/test/db.test.js @@ -1,12 +1,82 @@ -var db = require('../lib/datastore') +var Datastore = require('../lib/datastore') , testDb = 'workspace/test.db' + , fs = require('fs') + , path = require('path') + , customUtils = require('../lib/customUtils') + , should = require('chai').should() + , assert = require('chai').assert + , _ = require('underscore') ; describe('Database', function () { + beforeEach(function (done) { + customUtils.ensureDirectoryExists(path.dirname(testDb), function () { + fs.exists(testDb, function (exists) { + if (exists) { + fs.unlink(testDb, done); + } else { return done(); } + }); + }); + }); + + 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); + + d.insert({ somedata: 'ok' }, function (err) { + // The data was correctly updated + d.find({}, function (err, docs) { + assert.isNull(err); + docs.length.should.equal(1); + Object.keys(docs[0]).length.should.equal(2); + docs[0].somedata.should.equal('ok'); + assert.isDefined(docs[0]._id); + + // After a reload the data has been correctly persisted + d.loadDatabase(function (err) { + d.find({}, function (err, docs) { + assert.isNull(err); + docs.length.should.equal(1); + Object.keys(docs[0]).length.should.equal(2); + docs[0].somedata.should.equal('ok'); + assert.isDefined(docs[0]._id); + + + done(); + }); + }); + }); + }); + }); + }); + }); + + 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); - it('Dummy', function (done) { - done(); + d.insert({ somedata: 'ok' }, function (err) { + d.insert({ somedata: 'another' }, function (err) { + d.insert({ somedata: 'again' }, function (err) { + d.find({}, function (err, docs) { + docs.length.should.equal(3); + _.pluck(docs, 'somedata').should.contain('ok'); + _.pluck(docs, 'somedata').should.contain('another'); + _.pluck(docs, 'somedata').should.contain('again'); + done(); + }); + }); + }); + }); + }); + }); }); });