OSDN Git Service

develop and test (not)existFile(), (not)existDirectory(), v0.3.0p0010
authorhimetani_cafe <fumifumi@yasunaga-lab.bio.kyutech.ac.jp>
Sat, 23 Jan 2016 11:48:13 +0000 (20:48 +0900)
committerhimetani_cafe <fumifumi@yasunaga-lab.bio.kyutech.ac.jp>
Sat, 23 Jan 2016 11:48:13 +0000 (20:48 +0900)
server/class/DB.js
test/mocha/DB.test.js

index 06e3558..47d2743 100644 (file)
@@ -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
-     * å\90\8cä¸\80ã\83\87ã\82£ã\83¬ã\82¯ã\83\88ã\83ªã\81«å\90\8cå\90\8dã\81®ã\83\95ã\82¡ã\82¤ã\83«ã\81\8cå­\98å\9c¨ã\81\99ã\82\8bã\81\8bã\81©ã\81\86ã\81\8b調ã\81¹
+     * existFile
+     * å\90\8cä¸\80ã\83\87ã\82£ã\83¬ã\82¯ã\83\88ã\83ªã\81«å\90\8cå\90\8dã\81®ã\83\95ã\82¡ã\82¤ã\83«ã\81\8cå­\98å\9c¨ã\81\99ã\82\8bã\81\93ã\81¨ã\82\92確ã\81\8bã\82\81
      * @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
-     * ã\83\87ã\82£ã\83¬ã\82¯ã\83\88ã\83ªã\81\8cå­\98å\9c¨ã\81\99ã\82\8bã\81\8bã\81©ã\81\86ã\81\8b調ã\81¹
+     * existDirectory
+     * ã\83\87ã\82£ã\83¬ã\82¯ã\83\88ã\83ªã\81\8cå­\98å\9c¨ã\81\99ã\82\8bã\81\93ã\81¨ã\82\92確èª\8dã\81\99
      * @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
      * ファイルを削除する
index d174f49..4657c97 100644 (file)
         });
 
 
+        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
-        * å\90\8cä¸\80ã\83\87ã\82£ã\83¬ã\82¯ã\83\88ã\83ªã\81«å\90\8cå\90\8dã\81®ã\83\95ã\82¡ã\82¤ã\83«ã\81\8cå­\98å\9c¨ã\81\99ã\82\8bã\81\8bã\81©ã\81\86ã\81\8b調ã\81¹
+        * notExistFile
+        * å\90\8cä¸\80ã\83\87ã\82£ã\83¬ã\82¯ã\83\88ã\83ªã\81«å\90\8cå\90\8dã\81®ã\83\95ã\82¡ã\82¤ã\83«ã\81\8cå­\98å\9c¨ã\81\97ã\81ªã\81\84ã\81\93ã\81¨ã\82\92確ã\81\8bã\82\81
         * @param {string}fileName
         * @param {string}parentDirectory
         * @returns {promise}
         */
         `;
-        describe(checkFile, function() {
+        describe(notExistFile, function() {
             var db;
             before(function() {
                 return DB()
             });
 
 
-            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()
                 })
             });
 
-            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