From 39dd722913897f26ba90cc6d2d3af0367f4a1169 Mon Sep 17 00:00:00 2001 From: himetani_cafe Date: Sat, 23 Jan 2016 20:48:13 +0900 Subject: [PATCH] develop and test (not)existFile(), (not)existDirectory(), --- server/class/DB.js | 79 ++++++++++++++++++++++++++++++++++++++++++++------- test/mocha/DB.test.js | 77 ++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 132 insertions(+), 24 deletions(-) diff --git a/server/class/DB.js b/server/class/DB.js index 06e3558..47d2743 100644 --- a/server/class/DB.js +++ b/server/class/DB.js @@ -40,8 +40,10 @@ function DB() { sayHello: function() { return test; }, - checkFile: checkFile, - checkDirectory: checkDirectory, + existFile: existFile, + notExistFile: notExistFile, + existDirectory: existDirectory, + notExistDirectory: notExistDirectory, createFile: createFile, test1: test1, removeFile: removeFile @@ -67,7 +69,7 @@ function DB() { */ function createFile(fileName,parentDirectory) { return new Promise(function(resolve, reject) { - Promise.all([checkFile(fileName, parentDirectory), checkDirectory(parentDirectory)]) + Promise.all([notExistFile(fileName, parentDirectory), existDirectory(parentDirectory)]) .catch(function(error) { reject(error); }) @@ -86,14 +88,44 @@ function DB() { }); } + /** - * checkFile - * 同一ディレクトリに同名のファイルが存在するかどうか調べる + * existFile + * 同一ディレクトリに同名のファイルが存在することを確かめる * @param {string}fileName * @param {string}parentDirectory - * @returns {promise} + * @returns {promise} ファイルが存在すればresolve、存在しなければreject */ - function checkFile(fileName, parentDirectory) { + function existFile(fileName, parentDirectory) { + return new Promise(function(resolve, reject) { + var q = { + where: { + name: fileName, + parentDirectory: parentDirectory, + fileType: 1 + } + }; + Files.findOne(q) + .then(function(r) { + console.log(r); + if(r === null) { + reject(new Error("\"" + fileName + "\" does not exist in " + '"' + parentDirectory + "\" directory.")); + } else { + resolve(); + } + }); + }); + } + + + /** + * notExistFile + * 同一ディレクトリに同名のファイルが存在していないことを確かめる + * @param {string}fileName + * @param {string}parentDirectory + * @returns {promise} ファイルが存在しなければresolve、存在すればreject + */ + function notExistFile(fileName, parentDirectory) { return new Promise(function(resolve, reject) { var q = { where: { @@ -114,12 +146,12 @@ function DB() { } /** - * checkDirectory - * ディレクトリが存在するかどうか調べる + * existDirectory + * ディレクトリが存在することを確認する * @param {string} directory * @returns {promise} ディレクトリが存在すればresolve、存在しなければreject */ - function checkDirectory(directory) { + function existDirectory(directory) { return new Promise(function(resolve, reject) { var q = { where: { @@ -138,6 +170,33 @@ function DB() { }); } + + /** + * notExistDirectory + * ディレクトリが存在しないことを確認する + * @param {string} directory + * @returns {promise} ディレクトリが存在しなければresolve、存在すればreject + */ + function notExistDirectory(directory) { + return new Promise(function(resolve, reject) { + var q = { + where: { + name: directory, + fileType: 0 + } + }; + Files.findOne(q) + .then(function(r) { + if(r === null) { + resolve(); + } else { + reject(new Error('"' + directory + '" directory exists.')); + } + }); + }); + } + + /** * removeFile * ファイルを削除する diff --git a/test/mocha/DB.test.js b/test/mocha/DB.test.js index d174f49..4657c97 100644 --- a/test/mocha/DB.test.js +++ b/test/mocha/DB.test.js @@ -37,17 +37,45 @@ }); + var existFile = ` + /** + * existFile + * 同一ディレクトリに同名のファイルが存在することを確かめる + * @param {string}fileName + * @param {string}parentDirectory + * @returns {promise} + */ + `; + describe(existFile, function() { + var db; + before(function() { + return DB() + .then(function(r) { + db = r; + }) + .then(function() { + return db.test1(); + }) + }); + + + 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.'); + }); + }); + - var checkFile = ` + + var notExistFile = ` /** - * checkFile - * 同一ディレクトリに同名のファイルが存在するかどうか調べる + * notExistFile + * 同一ディレクトリに同名のファイルが存在しないことを確かめる * @param {string}fileName * @param {string}parentDirectory * @returns {promise} */ `; - describe(checkFile, function() { + describe(notExistFile, function() { var db; before(function() { return DB() @@ -60,22 +88,45 @@ }); - it('should be rejected when have the same name file in a directory', function() { - return db.checkFile('hoge.txt', '/').should.be.rejected; + it('should be rejected when exist the same name file in a directory', function() { + return db.notExistFile('hoge.txt', '/').should.be.rejectedWith(Error, '"hoge.txt" has already existed in "/" directory.'); }); }); - var checkDirectory = ` + var existDirectory = ` /** - * checkDirectory - * + * existDirectory + * ディレクトリが存在することを確認する + * @param {string} directory + * @returns {promise} ディレクトリが存在しなければresolve、すればreject + */ + `; + describe(existDirectory, function() { + var db; + before(function() { + return DB() + .then(function(r) { + db = r; + }) + }); + + it('should be rejected when does not exist directory', function() { + return db.existDirectory('hage').should.be.rejectedWith(Error, '"hage" directory doesn\'t exist.'); + }); + }); + + + var notExistDirectory = ` + /** + * notExistDirectory + * ディレクトリが存在しないことを確認する * @param {string} directory * @returns {promise} ディレクトリが存在すればresolve、存在しなければreject */ `; - describe(checkDirectory, function() { + describe(existDirectory, function() { var db; before(function() { return DB() @@ -87,13 +138,11 @@ }) }); - it('should be rejected when have the same name file in a directory', function() { - return db.checkDirectory('hage').should.be.rejected; + it('should be rejected when exists directory', function() { + return db.notExistDirectory('/').should.be.rejectedWith(Error,'"/" directory exists.'); }); }); - - var createFile = ` /** * createFile -- 2.11.0