OSDN Git Service

existDirectory(),notExistDirectory()の仕様変更、Filesテーブルのスキーマの変更 v0.3.0p0013
authorhimetani_cafe <fumifumi@yasunaga-lab.bio.kyutech.ac.jp>
Sun, 24 Jan 2016 08:32:45 +0000 (17:32 +0900)
committerhimetani_cafe <fumifumi@yasunaga-lab.bio.kyutech.ac.jp>
Sun, 24 Jan 2016 08:32:45 +0000 (17:32 +0900)
 Please enter the commit message for your changes. Lines starting
 with '' will be ignored, and an empty message aborts the commit.
 On branch master
 Changes to be committed:
modified:   server/class/DB.js
modified:   test/mocha/DB.test.js

server/class/DB.js
test/mocha/DB.test.js

index f809ebe..066347d 100644 (file)
@@ -154,19 +154,26 @@ function DB() {
      */
     function existFile(fileName, parentDirectory) {
         return new Promise(function(resolve, reject) {
-            var q = {
+            existDirectory(parentDirectory)
+            .catch(function(error) {
+                reject(error);
+            })
+            .then(function(fileId) {
+                var q = {
                 where: {
                     name: fileName,
-                    parentDirectory: parentDirectory,
+                    parentId: fileId,
                     fileType: 1 
-                }
-            };
-            Files.findOne(q)
+                    }
+                };
+                return Files.findOne(q)
+            })
             .then(function(r) {
                 if(r === null) {
                     reject(new Error("\"" + fileName + "\" does not exist in " + '"' + parentDirectory + "\" directory."));
                 } else {
-                    resolve();
+                    console.log(r.fileId);
+                    resolve(r.fileId);
                 }
             });
         });
@@ -182,14 +189,20 @@ function DB() {
      */
     function notExistFile(fileName, parentDirectory) {
         return new Promise(function(resolve, reject) {
-            var q = {
+            existDirectory(parentDirectory)
+            .catch(function(error) {
+                reject(error);
+            })
+            .then(function(fileId) {
+                var q = {
                 where: {
                     name: fileName,
-                    parentDirectory: parentDirectory,
+                    parentId: fileId,
                     fileType: 1 
-                }
-            };
-            Files.findOne(q)
+                    }
+                };
+                return Files.findOne(q)
+            })
             .then(function(r) {
                 if(r === null) {
                     resolve();
@@ -204,13 +217,18 @@ function DB() {
      * existDirectory
      * ディレクトリが存在することを確認する
      * @param {string} directory
-     * @returns {promise} ディレクトリが存在すればresolve、存在しなければreject
+     * @returns {promise} ディレクトリが存在すればresolve{fileId)、存在しなければreject
      */
     function existDirectory(directory) {
         return new Promise(function(resolve, reject) {
-            var arrayDirectory = directory.split('/');
-            arrayDirectory.shift(); // root
-            arrayDirectory.unshift('/');
+            var arrayDirectory; 
+            if(directory === '/') {
+                resolve('1f83f620-c1ed-11e5-9657-7942989daa00');
+            } else {
+                arrayDirectory = directory.split('/');
+                arrayDirectory.shift(); // root
+                arrayDirectory.unshift('/');
+            }
 
             var directoriesPromise = arrayDirectory.map(function(name) {
                 return getDirectory(name);
@@ -227,7 +245,7 @@ function DB() {
                         reject(new Error('"' + directory + '" directory doesn\'t exist.'));
                     }
                 }
-                resolve(directory);
+                resolve(parentId);
             })
             .catch(function(error) {
                 reject(new Error('"' + directory + '" directory doesn\'t exist.'));
@@ -243,19 +261,34 @@ function DB() {
      */
     function notExistDirectory(directory) {
         return new Promise(function(resolve, reject) {
-            var q = {
-                where: {
-                    name: directory,
-                    fileType: 0 
-                }
-            };
-            Files.findOne(q)
+            var arrayDirectory; 
+            if(directory === '/') {
+                reject(new Error('"' + directory + '" directory exists.'));
+            } else {
+                arrayDirectory = directory.split('/');
+                arrayDirectory.shift(); // root
+                arrayDirectory.unshift('/');
+            }
+
+            var directoriesPromise = arrayDirectory.map(function(name) {
+                return getDirectory(name);
+            });
+            Promise.all(directoriesPromise)
             .then(function(r) {
-                if(r === null) {
-                    resolve();
-                } else {
-                    reject(new Error('"' + directory + '" directory exists.'));
+                var parentId = r[0][0].fileId;
+                var index;
+                for(var i=1;i<r.length;i++) {
+                    index = r[i].map(function(c) { return c.parentId }).indexOf(parentId);
+                    if(index > -1) {
+                        parentId = r[i][index].fileId;
+                    } else {
+                        resolve();
+                    }
                 }
+                reject(new Error('"' + directory + '" directory exists.'));
+            })
+            .catch(function(error) {
+                resolve();
             });
         });
     }
@@ -269,15 +302,16 @@ function DB() {
      */
     function createFile(fileName,parentDirectory) {
         return new Promise(function(resolve, reject) {
-            Promise.all([notExistFile(fileName, parentDirectory), existDirectory(parentDirectory)])
+            Promise.all([existDirectory(parentDirectory), notExistFile(fileName, parentDirectory) ])
             .catch(function(error) {
                 reject(error);
             })
-            .then(function() {
+            .then(function(r) {
+                var parentId = r[0]
                 var q = {
                     fileId: uuid.v1(),
                     name: fileName,
-                    parentDirectory: parentDirectory,
+                    parentId: parentId,
                     fileType: 1 
                 }
                 return Files.create(q)
@@ -302,11 +336,10 @@ function DB() {
             .catch(function(error) {
                 reject(error);
             })
-            .then(function() {
+            .then(function(fileId) {
                 var q = {
                     where: {
-                        name: fileName,
-                        parentDirectory: parentDirectory
+                        fileId: fileId
                     }
                 };
                 return Files.destroy(q);
@@ -392,6 +425,15 @@ function DB() {
                 fileType: 0 
             };
             return Files.create(q3);
+        })
+        .then(function() {
+            var q4 = {
+                fileId: uuid.v1(),
+                name: 'hogehoge.txt',
+                parentId: q1.fileId,
+                fileType: 1
+            };
+            return Files.create(q4);
         });
     }
 
index 3bfc150..cc33c8e 100644 (file)
                 .then(function() {
                     return db.test1();
                 })
+                .then(function() {
+                    return db.test2();
+                });
             });
 
-            /*
-               it('should be rejected when does not exist same name file in a directory', function() {
+            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.');
-               });
-               */
+            });
+            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.');
+            });
+            it('should be resolved when file exists in a directory', function() {
+               return expect(db.existFile('hogehoge.txt', '/one')).to.eventually.be.a('string');
+            });
+
         });
 
 
                 .then(function() {
                     return db.test1();
                 })
+                .then(function() {
+                    return db.test2();
+                });
             });
 
-
-            /*
-               it('should be rejected when exist the same name file in a directory', function() {
+            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.');
-               });
-               */
-        });
-
+            });
+            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.');
+            });
+            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();
+            });
 
+        });
 
         var existDirectory = `
         /**
                 })
             });
 
-            it('should be resolved when a directory exists.', function() {
-               return expect(db.existDirectory('/one/two')).to.eventually.equal('/one/two');
-            });
             it('should be rejected when does not exist directory', function() {
-               return db.existDirectory('/one/two/four').should.be.rejectedWith(Error, '"/one/two/four" directory doesn\'t exist.');
+                return db.existDirectory('/one/two/four').should.be.rejectedWith(Error, '"/one/two/four" directory doesn\'t exist.');
+            });
+            it('should be resolved when exists directory(1)', function() {
+                return expect(db.existDirectory('/')).to.eventually.equal('1f83f620-c1ed-11e5-9657-7942989daa00');
+            });
+            it('should be resolved when exists directory(2)', function() {
+                return expect(db.existDirectory('/one')).to.eventually.be.a('string');
+            });
+            it('should be resolved when exists directory(3)', function() {
+                return expect(db.existDirectory('/one/two')).to.eventually.be.a('string');
             });
-
-            /*
-               it('should be resolveed when exists directory(1)', function() {
-               return expect(db.existDirectory('/')).to.eventually.equal('/');
-               });
-               it('should be resolveed when exists directory(2)', function() {
-               return db.existDirectory('/one').should.be.rejectedWith(Error, '"hage" directory doesn\'t exist.');
-               });
-               it('should be resolveed when exists directory(3)', function() {
-               return db.existDirectory('/one/two').should.be.rejectedWith(Error, '"hage" directory doesn\'t exist.');
-               });
-               */
-
         });
 
 
                 .then(function() {
                     return db.test1();
                 })
+                .then(function() {
+                    return db.test2();
+                });
             });
 
-            /*
-               it('should be rejected when exists directory', function() {
+            it('should be rejected when exists directory(1)', function() {
                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.');
+            });
+            it('should be rejected when exists directory(3)', function() {
+               return db.notExistDirectory('/one/two').should.be.rejectedWith(Error,'"/one/two" directory exists.');
+            });
+
         });
 
         var createFile = `
                 .then(function() {
                     return db.test1();
                 })
+                .then(function() {
+                    return db.createFile('tarou.txt', '/');
+                });
             });
 
-            /*
-               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.');
-               });
-               it('should be rejected when a file has already existed in a directory.', function() {
+            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.');
+            });
+            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.');
-               });
-               */
+            });
+            it('should be resolved when file creation successed.', function() {
+                return expect(db.existFile('tarou.txt', '/')).to.eventually.be.a('string');
+            });
         });
 
 
                     return db.test1();
                 });
             });
-            /*
-               it('should be resolved when removeFile() successed', function() {
+            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.');
-               });
-               */
+            });
         });
     });
 })();