From: himetani_cafe Date: Mon, 25 Jan 2016 07:51:04 +0000 (+0900) Subject: [Develop and Test] db.createDirectory(directory) X-Git-Tag: v0.3.0p0014 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=refs%2Ftags%2Fv0.3.0p0014;p=eos%2Fzephyr.git [Develop and Test] db.createDirectory(directory) --- diff --git a/server/class/DB.js b/server/class/DB.js index 066347d..1ae1252 100644 --- a/server/class/DB.js +++ b/server/class/DB.js @@ -162,8 +162,7 @@ function DB() { var q = { where: { name: fileName, - parentId: fileId, - fileType: 1 + parentId: fileId } }; return Files.findOne(q) @@ -172,7 +171,7 @@ function DB() { if(r === null) { reject(new Error("\"" + fileName + "\" does not exist in " + '"' + parentDirectory + "\" directory.")); } else { - console.log(r.fileId); + //console.log(r.fileId); resolve(r.fileId); } }); @@ -189,6 +188,7 @@ function DB() { */ function notExistFile(fileName, parentDirectory) { return new Promise(function(resolve, reject) { + console.log(fileName, parentDirectory); existDirectory(parentDirectory) .catch(function(error) { reject(error); @@ -197,8 +197,7 @@ function DB() { var q = { where: { name: fileName, - parentId: fileId, - fileType: 1 + parentId: fileId } }; return Files.findOne(q) @@ -353,22 +352,27 @@ function DB() { /** * createDirectory * - * @param fileName - * @param parentDirectory + * @param directory * @returns {promise} */ - function createDirectory(fileName,parentDirectory) { + function createDirectory(directory) { return new Promise(function(resolve, reject) { - Promise.all([notExistFile(fileName, parentDirectory), existDirectory(parentDirectory)]) + var leaf = directory.split('/').pop(); + var parentDirectory = directory.replace('/'+leaf, ''); + if(!parentDirectory) { + parentDirectory = '/'; + } + Promise.all([existDirectory(parentDirectory), notExistDirectory(directory), notExistFile(leaf, parentDirectory)]) .catch(function(error) { reject(error); }) - .then(function() { + .then(function(r) { + var parentId = r[0]; var q = { fileId: uuid.v1(), - name: fileName, - parentDirectory: parentDirectory, - fileType: 1 + name: leaf, + parentId: parentId, + fileType: 0 } return Files.create(q) }) diff --git a/test/mocha/DB.test.js b/test/mocha/DB.test.js index cc33c8e..e0c15b7 100644 --- a/test/mocha/DB.test.js +++ b/test/mocha/DB.test.js @@ -161,13 +161,13 @@ }); it('should be rejected when does not exist same name file in a directory', function() { - return db.existFile('hogehoge.txt', '/').should.be.rejectedWith(Error, '"hogehoge.txt" does not exist in "/" directory.'); + return db.existFile('hogehoge.txt', '/').should.be.rejectedWith(Error, '"hogehoge.txt" does not exist in "/" directory.'); }); it('should be rejected when a directory does not exist', function() { - return db.existFile('hogehoge.txt', '/hoge').should.be.rejectedWith(Error, '"/hoge" directory doesn\'t exist.'); + return db.existFile('hogehoge.txt', '/hoge').should.be.rejectedWith(Error, '"/hoge" directory doesn\'t exist.'); }); it('should be resolved when file exists in a directory', function() { - return expect(db.existFile('hogehoge.txt', '/one')).to.eventually.be.a('string'); + return expect(db.existFile('hogehoge.txt', '/one')).to.eventually.be.a('string'); }); }); @@ -199,13 +199,13 @@ }); it('should be rejected when the same name file has already existed in directory(1)', function() { - return db.notExistFile('hoge.txt', '/').should.be.rejectedWith(Error, '"hoge.txt" has already existed in "/" directory.'); + return db.notExistFile('hoge.txt', '/').should.be.rejectedWith(Error, '"hoge.txt" has already existed in "/" directory.'); }); it('should be rejected when the same name file has already existed in directory(2)', function() { - return db.notExistFile('hogehoge.txt', '/one').should.be.rejectedWith(Error, '"hogehoge.txt" has already existed in "/one" directory.'); + return db.notExistFile('hogehoge.txt', '/one').should.be.rejectedWith(Error, '"hogehoge.txt" has already existed in "/one" directory.'); }); it('should be resolved when the same name file does not exist in directory', function() { - return expect(db.notExistFile('hoge.txt', '/one')).to.eventually.equal(); + return expect(db.notExistFile('hoge.txt', '/one')).to.eventually.equal(); }); }); @@ -269,17 +269,18 @@ }); it('should be rejected when exists directory(1)', function() { - return db.notExistDirectory('/').should.be.rejectedWith(Error,'"/" directory exists.'); + return db.notExistDirectory('/').should.be.rejectedWith(Error,'"/" directory exists.'); }); it('should be rejected when exists directory(2)', function() { - return db.notExistDirectory('/one').should.be.rejectedWith(Error,'"/one" directory exists.'); + return db.notExistDirectory('/one').should.be.rejectedWith(Error,'"/one" directory exists.'); }); it('should be rejected when exists directory(3)', function() { - return db.notExistDirectory('/one/two').should.be.rejectedWith(Error,'"/one/two" directory exists.'); + return db.notExistDirectory('/one/two').should.be.rejectedWith(Error,'"/one/two" directory exists.'); }); }); + var createFile = ` /** * createFile @@ -305,10 +306,10 @@ }); it('should be rejected when "parentDirectory" doesn\'t exist', function() { - return db.createFile('hoge.txt', '/hoge').should.be.rejectedWith(Error, '"/hoge" directory doesn\'t exist.'); + return db.createFile('hoge.txt', '/hoge').should.be.rejectedWith(Error, '"/hoge" directory doesn\'t exist.'); }); it('should be rejected when a file has already existed in a directory.', function() { - return db.createFile('hoge.txt', '/').should.be.rejectedWith(Error, '"hoge.txt" has already existed in "/" directory.'); + return db.createFile('hoge.txt', '/').should.be.rejectedWith(Error, '"hoge.txt" has already existed in "/" directory.'); }); it('should be resolved when file creation successed.', function() { return expect(db.existFile('tarou.txt', '/')).to.eventually.be.a('string'); @@ -316,8 +317,6 @@ }); - - var removeFile = ` /** * removeFile @@ -340,12 +339,60 @@ }); }); it('should be resolved when removeFile() successed', function() { - return db.removeFile('hoge.txt', '/') - .then(function() { - return db.existFile('hoge.txt', '/') - }) - .should.be.rejectedWith(Error, '"hoge.txt" does not exist in "/" directory.'); + return db.removeFile('hoge.txt', '/') + .then(function() { + return db.existFile('hoge.txt', '/') + }) + .should.be.rejectedWith(Error, '"hoge.txt" does not exist in "/" directory.'); }); }); + + + var createDirectory = ` + /** + * createFile + * + * @param directory + * @returns {promise} + */ + `; + describe(createDirectory, function() { + var db; + before(function() { + return DB() + .then(function(r) { + db = r; + }) + .then(function() { + return db.test1(); + }) + .then(function() { + return db.test2(); + }) + .then(function() { + return db.createDirectory('/one/hoge'); + }); + /* + .then(function() { + return db.createDirectory('/one/two/three'); + }); + */ + }); + + it('should be rejected when directory has already existed', function() { + return db.createDirectory('/one/two').should.be.rejectedWith(Error, '"/one/two" directory exists.'); + }); + it('should be rejected when parent directory does not exist.', function() { + return db.createDirectory('/four/hoge').should.be.rejectedWith(Error, '"/four" directory doesn\'t exist.'); + }); + it('should be rejected when the same name file has already existed.', function() { + return db.createDirectory('/hoge.txt').should.be.rejectedWith(Error, '"hoge.txt" has already existed in "/" directory.'); + }); + it('should be rejected when the /one/hoge directory has already existed', function() { + return db.notExistDirectory('/one/hoge').should.be.rejectedWith(Error, '"/one/hoge" directory exists.'); + }); + + }); + }); })();