Test inserting documents

pull/2/head
Louis Chatriot 12 years ago
parent 3628922a75
commit dc6b4a25d7
  1. 2
      lib/datastore.js
  2. 60
      package.json
  3. 76
      test/db.test.js

@ -253,3 +253,5 @@ Datastore.prototype.remove = function (query, options, cb) {
}; };
module.exports = Datastore;

@ -1,31 +1,37 @@
{ {
"name": "nedb" "name": "nedb",
, "version": "0.0.1" "version": "0.0.1",
, "author": { "name": "tldr.io", "email": "hello@tldr.io"} "author": {
, "contributors": [ "name": "tldr.io",
"email": "hello@tldr.io"
},
"contributors": [
"Louis Chatriot" "Louis Chatriot"
] ],
, "description": "File-based embedded data store for node.js" "description": "File-based embedded data store for node.js",
, "keywords": [ "keywords": [
"database" "database",
, "datastore" "datastore",
, "embedded" "embedded"
] ],
, "homepage": "https://github.com/louischatriot/node-embedded-db" "homepage": "https://github.com/louischatriot/node-embedded-db",
, "repository": { "type": "git" "repository": {
, "url": "git@github.com:louischatriot/node-embedded-db.git" "type": "git",
} "url": "git@github.com:louischatriot/node-embedded-db.git"
, "dependencies": { },
} "dependencies": {
, "devDependencies": { "async": "~0.2.8",
"chai": "1.0.x" "underscore": "~1.4.4"
, "mocha": "1.4.x" },
, "request": "2.9.x" "devDependencies": {
, "sinon": "1.3.x" "chai": "1.0.x",
} "mocha": "1.4.x",
, "scripts": { "request": "2.9.x",
"sinon": "1.3.x"
},
"scripts": {
"test": "make test" "test": "make test"
} },
, "main": "server" "main": "server",
, "licence": "MIT" "licence": "MIT"
} }

@ -1,12 +1,82 @@
var db = require('../lib/datastore') var Datastore = require('../lib/datastore')
, testDb = 'workspace/test.db' , 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 () { 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) { d.insert({ somedata: 'ok' }, function (err) {
done(); 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();
});
});
});
});
});
});
}); });
}); });

Loading…
Cancel
Save