OSDN Git Service

Modified: Upload, Command execution
[eos/zephyr.git] / test / mocha / Eos.test.js
1 (function() {
2     'use strict';
3     var eos = require('../../server/class/Eos').instance;
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     var db = require('../../server/class/DB').instance;
11     var testCommand = require('./testCommand.json');
12
13     var test1 = `
14     /**
15     * コマンドとオプションのバリデーション
16     * @param {string} Eosコマンドの名前
17     * @param {Array<Object>} コマンドに渡すオプション
18     * @returns {valid: boolean, message: string}
19     * function validate(command, params) {
20     */
21     `;
22
23     
24     describe('Eos クラス', function() {
25         before(function() {
26         });
27
28         describe(test1, function() {
29
30             it('should return Array', function() {
31                 return expect(eos.getFiles()).to.eventually.be.length(2);
32             });
33
34             it('should be rejected(2)', function() {
35                 return eos.validate('hoge').should.be.rejectedWith(Error, 'Command name is invalid');
36             });
37
38             it('should be rejected(3)', function() {
39                 return eos.validate('mrcImageNoiseAdd').should.be.rejectedWith(Error, 'Options need to be Array');
40             });
41
42             it('should be rejected(4)', function() {
43                 return eos.validate('mrcImageNoiseAdd', []).should.be.rejectedWith(Error, 'At least one option is required.');
44             });
45
46             it('should return false when options is not Array', function() {
47                 return eos.validate('mrcImageNoiseAdd', {}).should.be.rejectedWith(Error, 'Options need to be Array');
48             });
49
50             it('should return false when options is invalid Object which have not "name" and "argumetns"', function() {
51                 return eos.validate('mrcImageInfo', [{hoge: 'hoge'}]).should.be.rejectedWith(Error, 'Options need to include Object which have properties "name" and "arguments"');
52             });
53             it('should return false when "argumetns" properties are not Array', function() {
54                 return eos.validate('mrcImageInfo', [{name: 'hoge', arguments: 'hoge'}]).should.be.rejectedWith(Error,'Each "arguments" properties needs to be Array');
55             });
56
57             it('should return false when required options do not exist', function() {
58                 return eos.validate('mrcImageInfo', [{name: 'hoge', arguments: []}]).should.be.rejectedWith('Option -i are required');
59             });
60
61             it('should return false when required options do not exist', function() {
62                 return eos.validate('dcdFilePrint', [{name: '-r', arguments: [1,2]}, { name: '-i', arguments: []}, { name: '-o', arguments: []} ]).should.be.rejectedWith(Error, 'Invalid arguments number');
63             });
64
65             it('should return false when input file does not exist', function() {
66                 return eos.validate('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['hoge.txt']}, { name: '-o', arguments: ['hoge.txt']} ]).should.be.rejectedWith(Error, 'hoge.txt doesn\'t exist.');
67             });
68
69             it('should return false when output file is not string', function() {
70                 return eos.validate('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['file1.txt']}, { name: '-o', arguments: [3]} ]).should.be.rejectedWith(Error, 'argType is invalid');
71             });
72
73             it('should return false when output file has already existed', function() {
74                 return eos.validate('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['file1.txt']}, { name: '-o', arguments: ['file1.txt']} ]).should.be.rejectedWith(Error, 'Invalid outfile name.');
75             });
76
77         });
78
79         var test2 = `
80         /**
81         * 実行時文字列への変換 
82         *
83         * @param command
84         * @param options
85         * @returns {string}
86         * function toExecString(command, params) {
87         */
88         `;
89
90         describe(test2, function() {
91             it('should return true when all options is proper.', function() {
92                 var result = eos.toExecString('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['file1.txt']}, { name: '-o', arguments: ['file3.txt']} ], "1f83f620-c1ed-11e5-9657-7942989daa00");
93                 expect(result).to.equal('dcdFilePrint -r 1 2 3 -s 10 -e 100 -d 10 -m 0 -i /Users/Takafumi/zephyr/server/class/../../user-specific-files/workspace/file1.txt -o /Users/Takafumi/zephyr/server/class/../../user-specific-files/workspace/file3.txt');
94             });
95         });
96
97         var toExecArray = `
98         /**
99         * 実行時配列への変換 
100         *
101         * @param command
102         * @returns {string}
103         * function toExecString(command, params) {
104         */
105         `;
106
107         describe(toExecArray, function() {
108             before(function() {
109                 process.env.NODE_ENV = '';
110                 return db.init()
111                 .then(function() {
112                     return Promise.all([db.test1(), db.test2(), db.testRest()])
113                 });
114             });
115
116             it('should be resolved with length 4', function() {
117                 var command = testCommand.command;
118                 var options = testCommand.options;
119                 var workspace = testCommand.workspace;
120                 return eos.toExecArray(command, options, '1f83f620-c1ed-11e5-9657-7942989daa00').should.be.eventually.length(16);
121             });
122
123         });
124         
125         describe('execute', function() {
126             before(function() {
127                 process.env.NODE_ENV = '';
128                 return db.init()
129                 .then(function() {
130                     return Promise.all([db.test1(), db.test2(), db.testRest()])
131                 });
132             });
133
134             it('should be resolved with length 4', function() {
135                 return expect(eos.getFiles('1f83f620-c1ed-11e5-9657-7942989daa00')).to.eventually.length(4);
136             });
137
138             it('should be resolved about testCommand', function() {
139                 var command = testCommand.command;
140                 var options = testCommand.options;
141                 var workspace = testCommand.workspace;
142                 return eos.validate(command, options, workspace).should.be.fulfilled;
143             });
144
145             after(function() {
146                 process.env.NODE_ENV = 'test';
147             });
148         });
149
150         describe('getFiles', function() {
151             before(function() {
152                 process.env.NODE_ENV = '';
153                 return db.init()
154                 .then(function() {
155                     return Promise.all([db.test1(), db.test2(), db.testRest()])
156                 });
157             });
158
159             it('should be resolved with length 4', function() {
160                 return expect(eos.getFiles('1f83f620-c1ed-11e5-9657-7942989daa00')).to.eventually.length(4);
161             });
162
163             it('should be resolved about testCommand', function() {
164                 var command = testCommand.command;
165                 var options = testCommand.options;
166                 var workspace = testCommand.workspace;
167                 return eos.validate(command, options, workspace).should.be.fulfilled;
168             });
169
170             after(function() {
171                 process.env.NODE_ENV = 'test';
172             });
173         });
174
175         var getUUIDs = `
176         /**
177         * function getUUIDs(fileId)
178         * uuidとファイル名のkey-valueを取得 
179         *
180         * @param fileId
181         * @returns {object} key: filename, value: uuid
182         */
183         `;
184
185         describe(getUUIDs, function() {
186             before(function() {
187                 process.env.NODE_ENV = '';
188                 return db.init()
189                 .then(function() {
190                     return Promise.all([db.test1(), db.test2(), db.testRest()])
191                 });
192             });
193
194             it('should be resolved', function() {
195                 return eos.getUUIDs('1f83f620-c1ed-11e5-9657-7942989daa00').should.eventually.have.property("hoge.txt");
196             });
197         });
198     });
199 })();