OSDN Git Service

cc33c8e517a43d36bf6cc619708989ae173fbc24
[eos/zephyr.git] / test / mocha / DB.test.js
1 (function() {
2     'use strict';
3     var DB = require('../../server/class/DB');
4     var chai = require('chai');
5     var chaiAsPromised = require('chai-as-promised');
6     chai.use(chaiAsPromised);
7     var expect = chai.expect;
8     chai.should();
9
10     //process.env['NODE_ENV'] = 'production';
11
12
13
14     describe('DB クラス', function() {
15
16         var constructor = `
17         /**
18         * createFile
19         *
20         * @param name
21         * @param directory
22         * @returns {promise}<保存したファイルのファイル名(uuid)をresolveする>
23         */
24         `;
25         describe(constructor, function() {
26             var db;
27             before(function() {
28                 return DB()
29                 .then(function(r) {
30                     db = r;
31                 });
32             });
33
34
35             it('should return hello when call sayHello', function() {
36                 expect(db.sayHello()).to.equal('hello');
37             });
38         });
39
40
41         var getDirectoryParentId = `
42         /**
43         * getDirectoryParentId
44         * ディレクトリのparentIdを取得する 
45         * @param name
46         * @returns {promise} ディレクトリが存在すればresolve(uuid), しなければreject
47         */
48         `;
49         describe(getDirectoryParentId, function() {
50             var db;
51             before(function() {
52                 return DB()
53                 .then(function(r) {
54                     db = r;
55                 })
56                 .then(function() {
57                     return db.test2();
58                 });
59             });
60
61             it('should be rejected when a directory does not exist', function() {
62                 return db.getDirectoryParentId('hoge').should.be.rejectedWith(Error, '"hoge" directory doesn\'t exist.');
63             });
64             it('should be resolved when \'one\' exists', function() {
65                 return expect(db.getDirectoryParentId('one')).to.eventually.include.members(['1f83f620-c1ed-11e5-9657-7942989daa00']);
66             });
67             it('should be rejected when \'two\' exists', function() {
68                 return expect(db.getDirectoryParentId('two')).to.eventually.have.length(2);
69             });
70
71         });
72
73         var getDirectoryId = `
74         /**
75         * getDirectoryId
76         * ディレクトリのfileIdを取得する 
77         * @param name
78         * @returns {promise} ディレクトリが存在すればresolve(uuid), しなければreject
79         */
80         `;
81         describe(getDirectoryId, function() {
82             var db;
83             before(function() {
84                 return DB()
85                 .then(function(r) {
86                     db = r;
87                 })
88                 .then(function() {
89                     return db.test2();
90                 });
91             });
92
93             it('should be rejected when a directory does not exist', function() {
94                 return db.getDirectoryId('hoge').should.be.rejectedWith(Error, '"hoge" directory doesn\'t exist.');
95             });
96             it('should be resolved when \'/\' exists', function() {
97                 return expect(db.getDirectoryId('/')).to.eventually.include.members(['1f83f620-c1ed-11e5-9657-7942989daa00']);
98             });
99             it('should be rejected when \'two\' exists', function() {
100                 return expect(db.getDirectoryId('two')).to.eventually.have.length(2);
101             });
102
103         });
104
105
106         var getDirectory = `
107         /**
108         * getDirectory
109         * ディレクトリの情報を取得する 
110         * @param name
111         * @returns {promise} ディレクトリが存在すればresolve(obj), しなければreject
112         */
113         `;
114         describe(getDirectoryId, function() {
115             var db;
116             before(function() {
117                 return DB()
118                 .then(function(r) {
119                     db = r;
120                 })
121                 .then(function() {
122                     return db.test2();
123                 });
124             });
125
126             it('should be rejected when a directory does not exist', function() {
127                 return db.getDirectoryId('hoge').should.be.rejectedWith(Error, '"hoge" directory doesn\'t exist.');
128             });
129             it('should be resolved when \'/\' exists', function() {
130                 return expect(db.getDirectory('/')).to.eventually.have.length(1);
131             });
132             it('should be rejected when \'two\' exists', function() {
133                 return expect(db.getDirectory('two')).to.eventually.have.length(2);
134             });
135
136         });
137
138
139         var existFile = `
140         /**
141         * existFile
142         * 同一ディレクトリに同名のファイルが存在することを確かめる
143         * @param {string}fileName
144         * @param {string}parentDirectory
145         * @returns {promise}
146         */
147         `;
148         describe(existFile, function() {
149             var db;
150             before(function() {
151                 return DB()
152                 .then(function(r) {
153                     db = r;
154                 })
155                 .then(function() {
156                     return db.test1();
157                 })
158                 .then(function() {
159                     return db.test2();
160                 });
161             });
162
163             it('should be rejected when does not exist same name file in a directory', function() {
164                return db.existFile('hogehoge.txt', '/').should.be.rejectedWith(Error, '"hogehoge.txt" does not exist in "/" directory.');
165             });
166             it('should be rejected when a directory does not exist', function() {
167                return db.existFile('hogehoge.txt', '/hoge').should.be.rejectedWith(Error, '"/hoge" directory doesn\'t exist.');
168             });
169             it('should be resolved when file exists in a directory', function() {
170                return expect(db.existFile('hogehoge.txt', '/one')).to.eventually.be.a('string');
171             });
172
173         });
174
175
176
177         var notExistFile = `
178         /**
179         * notExistFile
180         * 同一ディレクトリに同名のファイルが存在しないことを確かめる
181         * @param {string}fileName
182         * @param {string}parentDirectory
183         * @returns {promise}
184         */
185         `;
186         describe(notExistFile, function() {
187             var db;
188             before(function() {
189                 return DB()
190                 .then(function(r) {
191                     db = r;
192                 })
193                 .then(function() {
194                     return db.test1();
195                 })
196                 .then(function() {
197                     return db.test2();
198                 });
199             });
200
201             it('should be rejected when the same name file has already existed in directory(1)', function() {
202                return db.notExistFile('hoge.txt', '/').should.be.rejectedWith(Error, '"hoge.txt" has already existed in "/" directory.');
203             });
204             it('should be rejected when the same name file has already existed in directory(2)', function() {
205                return db.notExistFile('hogehoge.txt', '/one').should.be.rejectedWith(Error, '"hogehoge.txt" has already existed in "/one" directory.');
206             });
207             it('should be resolved when the same name file does not exist in directory', function() {
208                return expect(db.notExistFile('hoge.txt', '/one')).to.eventually.equal();
209             });
210
211         });
212
213         var existDirectory = `
214         /**
215         * existDirectory
216         * ディレクトリが存在することを確認する
217         * @param {string} directory ディレクトリの絶対パス
218         * @returns {promise} ディレクトリが存在しなければresolve、すればreject
219         */
220         `;
221         describe(existDirectory, function() {
222             var db;
223             before(function() {
224                 return DB()
225                 .then(function(r) {
226                     db = r;
227                 })
228                 .then(function() {
229                     return db.test2();
230                 })
231             });
232
233             it('should be rejected when does not exist directory', function() {
234                 return db.existDirectory('/one/two/four').should.be.rejectedWith(Error, '"/one/two/four" directory doesn\'t exist.');
235             });
236             it('should be resolved when exists directory(1)', function() {
237                 return expect(db.existDirectory('/')).to.eventually.equal('1f83f620-c1ed-11e5-9657-7942989daa00');
238             });
239             it('should be resolved when exists directory(2)', function() {
240                 return expect(db.existDirectory('/one')).to.eventually.be.a('string');
241             });
242             it('should be resolved when exists directory(3)', function() {
243                 return expect(db.existDirectory('/one/two')).to.eventually.be.a('string');
244             });
245         });
246
247
248         var notExistDirectory = `
249         /**
250         * notExistDirectory
251         * ディレクトリが存在しないことを確認する
252         * @param {string} directory
253         * @returns {promise} ディレクトリが存在すればresolve、存在しなければreject
254         */
255         `;
256         describe(existDirectory, function() {
257             var db;
258             before(function() {
259                 return DB()
260                 .then(function(r) {
261                     db = r;
262                 })
263                 .then(function() {
264                     return db.test1();
265                 })
266                 .then(function() {
267                     return db.test2();
268                 });
269             });
270
271             it('should be rejected when exists directory(1)', function() {
272                return db.notExistDirectory('/').should.be.rejectedWith(Error,'"/" directory exists.');
273             });
274             it('should be rejected when exists directory(2)', function() {
275                return db.notExistDirectory('/one').should.be.rejectedWith(Error,'"/one" directory exists.');
276             });
277             it('should be rejected when exists directory(3)', function() {
278                return db.notExistDirectory('/one/two').should.be.rejectedWith(Error,'"/one/two" directory exists.');
279             });
280
281         });
282
283         var createFile = `
284         /**
285         * createFile
286         *
287         * @param name
288         * @param directory
289         * @returns {promise}<保存したファイルのファイル名(uuid)をresolveする>
290         */
291         `;
292         describe(createFile, function() {
293             var db;
294             before(function() {
295                 return DB()
296                 .then(function(r) {
297                     db = r;
298                 })
299                 .then(function() {
300                     return db.test1();
301                 })
302                 .then(function() {
303                     return db.createFile('tarou.txt', '/');
304                 });
305             });
306
307             it('should be rejected when "parentDirectory" doesn\'t exist', function() {
308                return db.createFile('hoge.txt', '/hoge').should.be.rejectedWith(Error, '"/hoge" directory doesn\'t exist.');
309             });
310             it('should be rejected when a file has already existed in a directory.', function() {
311                return db.createFile('hoge.txt', '/').should.be.rejectedWith(Error, '"hoge.txt" has already existed in "/" directory.');
312             });
313             it('should be resolved when file creation successed.', function() {
314                 return expect(db.existFile('tarou.txt', '/')).to.eventually.be.a('string');
315             });
316         });
317
318
319
320
321         var removeFile = `
322         /**
323         * removeFile
324         * ファイルを削除する
325         * @param {string} fileName
326         * @param {string} parentDirectory
327         * @returns {promise} ファイル削除に成功すればresolve、失敗すればreject
328         */
329         `;
330
331         describe(removeFile, function() {
332             var db;
333             before(function() {
334                 return DB()
335                 .then(function(r) {
336                     db = r;
337                 })
338                 .then(function() {
339                     return db.test1();
340                 });
341             });
342             it('should be resolved when removeFile() successed', function() {
343                return db.removeFile('hoge.txt', '/')
344                .then(function() {
345                return db.existFile('hoge.txt', '/')
346                })
347                .should.be.rejectedWith(Error, '"hoge.txt" does not exist in "/" directory.');
348             });
349         });
350     });
351 })();