Add more test cases

This commit is contained in:
Raymond Feng 2013-06-24 15:53:14 -07:00
parent 5b3d431488
commit 8e3c3edeeb
3 changed files with 107 additions and 54 deletions

View File

@ -70,7 +70,7 @@ FileSystemProvider.prototype.destroyContainer = function (containerName, cb) {
fs.readdir(dir, function (err, files) { fs.readdir(dir, function (err, files) {
var tasks = []; var tasks = [];
files.forEach(function (f) { files.forEach(function (f) {
tasks.push(fs.unlink.bind(null, f)); tasks.push(fs.unlink.bind(null, path.join(dir, f)));
}); });
async.parallel(tasks, function (err) { async.parallel(tasks, function (err) {
if (err) { if (err) {
@ -126,7 +126,7 @@ FileSystemProvider.prototype.getFiles = function (container, cb) {
var dirs = []; var dirs = [];
var tasks = []; var tasks = [];
files.forEach(function (f) { files.forEach(function (f) {
tasks.push(fs.stat.bind(null, f)); tasks.push(fs.stat.bind(null, path.join(dir, f)));
}); });
async.parallel(tasks, function (err, stats) { async.parallel(tasks, function (err, stats) {
if (err) { if (err) {
@ -146,7 +146,6 @@ FileSystemProvider.prototype.getFiles = function (container, cb) {
FileSystemProvider.prototype.getFile = function (container, file, cb) { FileSystemProvider.prototype.getFile = function (container, file, cb) {
if(!validateName(container, cb)) return; if(!validateName(container, cb)) return;
var container = options.container;
if(!validateName(file, cb)) return; if(!validateName(file, cb)) return;
var filePath = path.join(this.root, container, file); var filePath = path.join(this.root, container, file);
fs.stat(filePath, cb); fs.stat(filePath, cb);

1
test/files/f1.txt Normal file
View File

@ -0,0 +1 @@
Hello....

View File

@ -5,72 +5,125 @@ var path = require('path');
describe('FileSystem based storage provider', function () { describe('FileSystem based storage provider', function () {
var client = null; describe('container apis', function () {
it('should require an existing directory as the root', function (done) { var client = null;
client = new FileSystemProvider({root: path.join(__dirname, 'storage')}); it('should require an existing directory as the root', function (done) {
process.nextTick(done); client = new FileSystemProvider({root: path.join(__dirname, 'storage')});
});
var client = null;
it('should complain if the root directory doesn\'t exist', function (done) {
try {
client = new FileSystemProvider({root: path.join(__dirname, '_storage')});
process.nextTick(done.bind(null, 'Error'));
} catch (err) {
// Should be here
process.nextTick(done); process.nextTick(done);
} });
});
it('should return an empty list of containers', function (done) { it('should complain if the root directory doesn\'t exist', function (done) {
client.getContainers(function (err, containers) { try {
assert(!err); client = new FileSystemProvider({root: path.join(__dirname, '_storage')});
assert.equal(0, containers.length); process.nextTick(done.bind(null, 'Error'));
done(err, containers); } catch (err) {
// Should be here
process.nextTick(done);
}
});
it('should return an empty list of containers', function (done) {
client.getContainers(function (err, containers) {
assert(!err);
assert.equal(0, containers.length);
done(err, containers);
});
});
it('should create a new container', function (done) {
client.createContainer({name: 'c1'}, function (err, container) {
assert(!err);
done(err, container);
});
});
it('should get a container c1', function (done) {
client.getContainer('c1', function (err, container) {
assert(!err);
done(err, container);
});
});
it('should not get a container c2', function (done) {
client.getContainer('c2', function (err, container) {
assert(err);
done(null, container);
});
});
it('should return one container', function (done) {
client.getContainers(function (err, containers) {
assert(!err);
assert.equal(1, containers.length);
done(err, containers);
});
});
it('should destroy a container c1', function (done) {
client.destroyContainer('c1', function (err, container) {
assert(!err);
done(err, container);
});
});
it('should not get a container c1 after destroy', function (done) {
client.getContainer('c1', function (err, container) {
assert(err);
done(null, container);
});
}); });
}); });
it('should create a new container', function (done) { describe('file apis', function () {
client.createContainer({name: 'c1'}, function (err, container) { var fs = require('fs');
assert(!err); var client = new FileSystemProvider({root: path.join(__dirname, 'storage')});
done(err, container);
});
});
it('should get a container c1', function (done) { it('should create a new container', function (done) {
client.getContainer('c1', function (err, container) { client.createContainer({name: 'c1'}, function (err, container) {
assert(!err); assert(!err);
done(err, container); done(err, container);
});
}); });
});
it('should not get a container c2', function (done) { it('should upload a file', function (done) {
client.getContainer('c2', function (err, container) { fs.createReadStream(path.join(__dirname, 'files/f1.txt')).pipe(
assert(err); client.upload({container: 'c1', remote: 'f1.txt'}));
done(null, container); done();
}); });
});
it('should return one container', function (done) { it('should download a file', function (done) {
client.getContainers(function (err, containers) { client.download({
assert(!err); container: 'c1',
assert.equal(1, containers.length); remote: 'f1.txt'
done(err, containers); }).pipe(fs.createWriteStream(path.join(__dirname, 'files/f1_downloaded.txt')));
done();
}); });
});
it('should destroy a container c1', function (done) { it('should get files for a container', function (done) {
client.destroyContainer('c1', function (err, container) { client.getFiles('c1', function (err, files) {
assert(!err); assert(!err);
done(err, container); assert.equal(1, files.length);
done(err, files);
});
}); });
});
it('should not get a container c1 after destroy', function (done) { it('should get a file', function (done) {
client.getContainer('c1', function (err, container) { client.getFile('c1', 'f1.txt', function (err, f) {
assert(err); assert(!err);
done(null, container); assert.ok(f);
done(err, f);
});
}); });
it('should destroy a container c1', function (done) {
client.destroyContainer('c1', function (err, container) {
console.error(err);
assert(!err);
done(err, container);
});
});
}); });
}); });