• R/O
  • HTTP
  • SSH
  • HTTPS

zephyr: Commit

This repository is for zephyr, which is implemented for a GUI of Eos.


Commit MetaInfo

Revision7b926b296129303c8f0c4154082a58da0f098795 (tree)
Time2016-01-23 18:38:53
Authorhimetani_cafe <fumifumi@yasu...>
Commiterhimetani_cafe

Log Message

develop and test db.createFile()

Change Summary

Incremental Difference

--- a/server/class/DB.js
+++ b/server/class/DB.js
@@ -40,16 +40,18 @@ function DB() {
4040 sayHello: function() {
4141 return test;
4242 },
43+ checkFile: checkFile,
44+ checkDirectory: checkDirectory,
4345 createFile: createFile,
44- checkFile: checkFile
46+ test1: test1
4547 /*
46- removeFile: removeFile,
47- createDirectory: createDirectory,
48- removeDirectory: removeDirectory,
49- getFilename: getFilename,
50- moveFile: moveFile,
51- moveDirectory: moveDirectory
52- */
48+ removeFile: removeFile,
49+ createDirectory: createDirectory,
50+ removeDirectory: removeDirectory,
51+ getFilename: getFilename,
52+ moveFile: moveFile,
53+ moveDirectory: moveDirectory
54+ */
5355 };
5456 resolve(c);
5557 });
@@ -65,17 +67,9 @@ function DB() {
6567 */
6668 function createFile(fileName,parentDirectory) {
6769 return new Promise(function(resolve, reject) {
68- var q = {
69- where: {
70- name: parentDirectory,
71- fileType: 0
72- }
73- };
74- Files.findOne(q)
75- .then(function(r) {
76- if(r === null) {
77- return Promise.reject(new Error('parentDirectory doesn\'t exist.'));
78- }
70+ Promise.all([checkFile(fileName, parentDirectory), checkDirectory(parentDirectory)])
71+ .catch(function(error) {
72+ reject(error);
7973 })
8074 .then(function() {
8175 var q = {
@@ -86,11 +80,8 @@ function DB() {
8680 }
8781 return Files.create(q)
8882 })
89- .catch(function(error) {
90- reject(error);
91- })
92- .then(function(r) {
93- resolve(r);
83+ .then(function() {
84+ resolve();
9485 });
9586 });
9687 }
@@ -104,25 +95,62 @@ function DB() {
10495 */
10596 function checkFile(fileName, parentDirectory) {
10697 return new Promise(function(resolve, reject) {
107- var q = {
108- where: {
109- name: fileName,
110- parentDirectory: parentDirectory,
111- fileType: 1
98+ var q = {
99+ where: {
100+ name: fileName,
101+ parentDirectory: parentDirectory,
102+ fileType: 1
112103 }
113- };
114- Files.findOne(q)
115- .then(function(r) {
116- if(r.dataValues) {
117- reject(new Error('parentDirectory doesn\'t exist.'));
118- } else {
104+ };
105+ Files.findOne(q)
106+ .then(function(r) {
107+ if(r === null) {
119108 resolve();
109+ } else {
110+ reject(new Error("\"" + fileName + "\" has already existed in " + '"' + parentDirectory + "\" directory."));
120111 }
121- });
112+ });
122113 });
123114 }
124115
116+ /**
117+ * checkDirectory
118+ *
119+ * @param {string} directory
120+ * @returns {promise} ディレクトリが存在すればresolve、存在しなければreject
121+ */
125122 function checkDirectory(directory) {
123+ return new Promise(function(resolve, reject) {
124+ var q = {
125+ where: {
126+ name: directory,
127+ fileType: 0
128+ }
129+ };
130+ Files.findOne(q)
131+ .then(function(r) {
132+ if(r === null) {
133+ reject(new Error('"' + directory + '" directory doesn\'t exist.'));
134+ } else {
135+ resolve();
136+ }
137+ });
138+ });
139+ }
140+
141+ /**
142+ * test1
143+ * test用にデータベースのレコードを追加する関数
144+ * @returns {promise}
145+ */
146+ function test1() {
147+ var q = {
148+ fileId: uuid.v1(),
149+ name: 'hoge.txt',
150+ parentDirectory: '/',
151+ fileType: 1
152+ };
153+ return Files.create(q);
126154 }
127155
128156 /**
--- a/test/mocha/DB.test.js
+++ b/test/mocha/DB.test.js
@@ -55,7 +55,7 @@
5555 db = r;
5656 })
5757 .then(function() {
58- return db.createFile('hoge.txt', '/');
58+ return db.test1();
5959 })
6060 });
6161
@@ -67,6 +67,33 @@
6767
6868
6969
70+ var checkDirectory = `
71+ /**
72+ * checkDirectory
73+ *
74+ * @param {string} directory
75+ * @returns {promise} ディレクトリが存在すればresolve、存在しなければreject
76+ */
77+ `;
78+ describe(checkDirectory, function() {
79+ var db;
80+ before(function() {
81+ return DB()
82+ .then(function(r) {
83+ db = r;
84+ })
85+ .then(function() {
86+ return db.test1();
87+ })
88+ });
89+
90+ it('should be rejected when have the same name file in a directory', function() {
91+ return db.checkDirectory('hage').should.be.rejected;
92+ });
93+ });
94+
95+
96+
7097 var createFile = `
7198 /**
7299 * createFile
@@ -77,14 +104,29 @@
77104 */
78105 `;
79106 describe(createFile, function() {
107+ var db;
108+ before(function() {
109+ return DB()
110+ .then(function(r) {
111+ db = r;
112+ })
113+ .then(function() {
114+ return db.test1();
115+ })
116+ });
117+
80118 /*
81119 it('should return object which have proverty "dataValues"', function() {
82- return expect(db.createFile('hage.txt','/')).to.eventually.have.property('dataValues');
120+ return expect(db.createFile('hage.txt','/')).to.eventually.have.property('dataValues');
83121 });
122+ */
84123 it('should be rejected when "parentDirectory" doesn\'t exist', function() {
85- return db.createFile('hoge.txt', 'hoge').should.be.rejected;
124+ return db.createFile('hoge.txt', 'hoge').should.be.rejectedWith(Error, '"hoge" directory doesn\'t exist.');
86125 });
87- */
126+ it('should be rejected when a file has already existed in a directory.', function() {
127+ return db.createFile('hoge.txt', '/').should.be.rejectedWith(Error, '"hoge.txt" has already existed in "/" directory.');
128+ });
129+
88130 });
89131 });
90132 })();
Show on old repository browser