From 73a96e7f17a2c203982c56a1042a07f1103f0842 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Sat, 25 May 2013 11:02:54 +0200 Subject: [PATCH] Docs without _id are not considered --- lib/datastore.js | 2 +- test/db.test.js | 31 ++++++++++++++++++++----------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/lib/datastore.js b/lib/datastore.js index 087ea6f..5068397 100644 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -69,7 +69,7 @@ Datastore.treatRawData = function (rawData) { try { doc = model.deserialize(data[i]); - res.push(doc); + if (doc._id) {res.push(doc); } } catch (e) { } } diff --git a/test/db.test.js b/test/db.test.js index 3341d26..7945b3d 100644 --- a/test/db.test.js +++ b/test/db.test.js @@ -40,33 +40,42 @@ describe('Database', function () { it('Every line represents a document', function () { var now = new Date() - , rawData = model.serialize({a: 2, ages: [1, 5, 12]}) + '\n' + - model.serialize({hello: 'world'}) + '\n' + - model.serialize({nested: { today: now }}) + , rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + + model.serialize({ _id: "2", hello: 'world' }) + '\n' + + model.serialize({ _id: "3", nested: { today: now } }) , treatedData = Datastore.treatRawData(rawData) ; treatedData.length.should.equal(3); - _.isEqual(treatedData[0], { a: 2, ages: [1, 5, 12] }).should.equal(true); - _.isEqual(treatedData[1], { hello: 'world' }).should.equal(true); - _.isEqual(treatedData[2], { nested: { today: now } }).should.equal(true); + _.isEqual(treatedData[0], { _id: "1", a: 2, ages: [1, 5, 12] }).should.equal(true); + _.isEqual(treatedData[1], { _id: "2", hello: 'world' }).should.equal(true); + _.isEqual(treatedData[2], { _id: "3", nested: { today: now } }).should.equal(true); }); it('Badly formatted lines have no impact on the treated data', function () { var now = new Date() - , rawData = model.serialize({a: 2, ages: [1, 5, 12]}) + '\n' + + , rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + 'garbage\n' + - model.serialize({nested: { today: now }}) + model.serialize({ _id: "3", nested: { today: now } }) , treatedData = Datastore.treatRawData(rawData) ; treatedData.length.should.equal(2); - _.isEqual(treatedData[0], { a: 2, ages: [1, 5, 12] }).should.equal(true); - _.isEqual(treatedData[1], { nested: { today: now } }).should.equal(true); + _.isEqual(treatedData[0], { _id: "1", a: 2, ages: [1, 5, 12] }).should.equal(true); + _.isEqual(treatedData[1], { _id: "3", nested: { today: now } }).should.equal(true); }); - it('Documents with', function () { + it('Well formatted lines that have no _id are not included in the data', function () { + var now = new Date() + , rawData = model.serialize({ _id: "1", a: 2, ages: [1, 5, 12] }) + '\n' + + model.serialize({ _id: "2", hello: 'world' }) + '\n' + + model.serialize({ nested: { today: now } }) + , treatedData = Datastore.treatRawData(rawData) + ; + treatedData.length.should.equal(2); + _.isEqual(treatedData[0], { _id: "1", a: 2, ages: [1, 5, 12] }).should.equal(true); + _.isEqual(treatedData[1], { _id: "2", hello: 'world' }).should.equal(true); }); }); // ==== End of 'Loading the database data from file' ==== //