Reorganized tests accordingly

pull/2/head
Louis Chatriot 10 years ago
parent d5958cce91
commit 236ee15e64
  1. 10
      lib/customUtils.js
  2. 27
      test/customUtil.test.js
  3. 31
      test/persistence.test.js

@ -18,16 +18,6 @@ function uid (len) {
} }
function ensureFileDoesntExist (file, callback) {
fs.exists(file, function (exists) {
if (!exists) { return callback(null); }
fs.unlink(file, function (err) { return callback(err); });
});
}
// Interface // Interface
module.exports.uid = uid; module.exports.uid = uid;
module.exports.ensureFileDoesntExist = ensureFileDoesntExist;

@ -7,29 +7,20 @@ var should = require('chai').should()
describe('customUtils', function () { describe('customUtils', function () {
describe('ensureFileDoesntExist', function () { describe('uid', function () {
it('Doesnt do anything if file already doesnt exist', function (done) { it('Generates a string of the expected length', function () {
customUtils.ensureFileDoesntExist('workspace/nonexisting', function (err) { customUtils.uid(3).length.should.equal(3);
assert.isNull(err); customUtils.uid(16).length.should.equal(16);
fs.existsSync('workspace/nonexisting').should.equal(false); customUtils.uid(42).length.should.equal(42);
done(); customUtils.uid(1000).length.should.equal(1000);
}); });
});
it('Deletes file if it exists', function (done) {
fs.writeFileSync('workspace/existing', 'hello world', 'utf8');
fs.existsSync('workspace/existing').should.equal(true);
customUtils.ensureFileDoesntExist('workspace/existing', function (err) { // Very small probability of conflict
assert.isNull(err); it('Generated uids should not be the same', function () {
fs.existsSync('workspace/existing').should.equal(false); customUtils.uid(56).should.not.equal(customUtils.uid(56));
done();
});
}); });
}); });
}); });

@ -491,9 +491,9 @@ describe('Persistence', function () {
var dbFile = 'workspace/test2.db', theDb, theDb2, doc1, doc2; var dbFile = 'workspace/test2.db', theDb, theDb2, doc1, doc2;
async.waterfall([ async.waterfall([
async.apply(customUtils.ensureFileDoesntExist, dbFile) async.apply(Persistence.ensureFileDoesntExist, dbFile)
, async.apply(customUtils.ensureFileDoesntExist, dbFile + '~') , async.apply(Persistence.ensureFileDoesntExist, dbFile + '~')
, async.apply(customUtils.ensureFileDoesntExist, dbFile + '~~') , async.apply(Persistence.ensureFileDoesntExist, dbFile + '~~')
, function (cb) { , function (cb) {
theDb = new Datastore({ filename: dbFile }); theDb = new Datastore({ filename: dbFile });
theDb.loadDatabase(cb); theDb.loadDatabase(cb);
@ -617,4 +617,29 @@ describe('Persistence', function () {
}); // ==== End of 'Prevent dataloss when persisting data' ==== }); // ==== End of 'Prevent dataloss when persisting data' ====
describe('ensureFileDoesntExist', function () {
it('Doesnt do anything if file already doesnt exist', function (done) {
Persistence.ensureFileDoesntExist('workspace/nonexisting', function (err) {
assert.isNull(err);
fs.existsSync('workspace/nonexisting').should.equal(false);
done();
});
});
it('Deletes file if it exists', function (done) {
fs.writeFileSync('workspace/existing', 'hello world', 'utf8');
fs.existsSync('workspace/existing').should.equal(true);
Persistence.ensureFileDoesntExist('workspace/existing', function (err) {
assert.isNull(err);
fs.existsSync('workspace/existing').should.equal(false);
done();
});
});
}); // ==== End of 'ensureFileDoesntExist' ====
}); });

Loading…
Cancel
Save