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"
, "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"
}

@ -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();
});
});
});
});
});
});
});
});

Loading…
Cancel
Save