From 403fb6a50e49e29e6682165a812e78436beebb46 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Thu, 28 Nov 2013 20:38:56 +0100 Subject: [PATCH] Persistence well tested --- test/persistence.test.js | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/test/persistence.test.js b/test/persistence.test.js index 9ed5d57..709f6f4 100644 --- a/test/persistence.test.js +++ b/test/persistence.test.js @@ -358,7 +358,7 @@ describe('Persistence', function () { }); }); }); - }); + }); it('persistCachedDatabase should update the contents of the datafile and leave a clean state', function (done) { d.insert({ hello: 'world' }, function () { @@ -369,8 +369,11 @@ describe('Persistence', function () { if (fs.existsSync(testDb + '~')) { fs.unlinkSync(testDb + '~'); } if (fs.existsSync(testDb + '~~')) { fs.unlinkSync(testDb + '~~'); } fs.existsSync(testDb).should.equal(false); - fs.existsSync(testDb + '~').should.equal(false); - fs.existsSync(testDb + '~~').should.equal(false); + + fs.writeFileSync(testDb + '~', 'something', 'utf8'); + fs.writeFileSync(testDb + '~~', 'something else', 'utf8'); + fs.existsSync(testDb + '~').should.equal(true); + fs.existsSync(testDb + '~~').should.equal(true); d.persistence.persistCachedDatabase(function (err) { var contents = fs.readFileSync(testDb, 'utf8'); @@ -442,7 +445,30 @@ describe('Persistence', function () { }); }); }); - }); + }); + + it('persistCachedDatabase should update the contents of the datafile and leave a clean state even if there is a temp or old datafile', function (done) { + var testDb = 'workspace/test2.db', theDb; + + if (fs.existsSync(testDb)) { fs.unlinkSync(testDb); } + if (fs.existsSync(testDb + '~')) { fs.unlinkSync(testDb + '~'); } + if (fs.existsSync(testDb + '~~')) { fs.unlinkSync(testDb + '~~'); } + + theDb = new Datastore({ filename: testDb }); + + theDb.loadDatabase(function (err) { + var contents = fs.readFileSync(testDb, 'utf8'); + assert.isNull(err); + fs.existsSync(testDb).should.equal(true); + fs.existsSync(testDb + '~').should.equal(false); + fs.existsSync(testDb + '~~').should.equal(false); + if (contents != "") { + throw "Datafile contents not as expected"; + } + done(); + }); + }); + // This test is a bit complicated since it depends on the time I/O actions take to execute // That depends on the machine and the load on the machine when the tests are run