OSDN Git Service

[DEBUG] Eos class and Testing with promise.
[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
12     var test1 = `
13     /**
14     * コマンドとオプションのバリデーション
15     * @param {string} Eosコマンドの名前
16     * @param {Array<Object>} コマンドに渡すオプション
17     * @returns {valid: boolean, message: string}
18     * function validate(command, params) {
19     */
20     `;
21
22     var test2 = `
23     /**
24     * 実行時文字列への変換 
25     *
26     * @param command
27     * @param options
28     * @returns {string}
29     * function toExecString(command, params) {
30     */
31     `;
32
33     describe('Eos クラス', function() {
34         before(function() {
35         });
36
37         describe(test1, function() {
38
39             it('should return Array', function() {
40                 return expect(eos.getFiles()).to.eventually.be.length(2);
41             });
42
43             it('should be rejected(2)', function() {
44                 return eos.validate('hoge').should.be.rejectedWith(Error, 'Command name is invalid');
45             });
46
47             it('should be rejected(3)', function() {
48                 return eos.validate('mrcImageNoiseAdd').should.be.rejectedWith(Error, 'Options need to be Array');
49             });
50
51             it('should be rejected(4)', function() {
52                 return eos.validate('mrcImageNoiseAdd', []).should.be.rejectedWith(Error, 'At least one option is required.');
53             });
54
55             it('should return false when options is not Array', function() {
56                 return eos.validate('mrcImageNoiseAdd', {}).should.be.rejectedWith(Error, 'Options need to be Array');
57             });
58
59             it('should return false when options is invalid Object which have not "name" and "argumetns"', function() {
60                 return eos.validate('mrcImageInfo', [{hoge: 'hoge'}]).should.be.rejectedWith(Error, 'Options need to include Object which have properties "name" and "arguments"');
61             });
62             it('should return false when "argumetns" properties are not Array', function() {
63                 return eos.validate('mrcImageInfo', [{name: 'hoge', arguments: 'hoge'}]).should.be.rejectedWith(Error,'Each "arguments" properties needs to be Array');
64             });
65
66             it('should return false when required options do not exist', function() {
67                 return eos.validate('mrcImageInfo', [{name: 'hoge', arguments: []}]).should.be.rejectedWith('Option -i are required');
68             });
69
70             it('should return false when required options do not exist', function() {
71                 return eos.validate('dcdFilePrint', [{name: '-r', arguments: [1,2]}, { name: '-i', arguments: []}, { name: '-o', arguments: []} ]).should.be.rejectedWith(Error, 'Invalid arguments number');
72             });
73
74             it('should return false when input file does not exist', function() {
75                 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.');
76             });
77
78             it('should return false when output file is not string', function() {
79                 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');
80             });
81
82             it('should return false when output file has already existed', function() {
83                 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.');
84             });
85
86             /*
87         describe(test2, function() {
88
89             it('should return true when all options is proper.', function() {
90                 var result = eos.toExecString('dcdFilePrint', [{name: '-r', arguments: [1,2,3]}, { name: '-i', arguments: ['file1.txt']}, { name: '-o', arguments: ['file3.txt']} ]);
91                 expect(result).to.equal('dcdFilePrint -r 1 2 3 -s 10 -e 100 -d 10 -m 0 -i file1.txt -o file3.txt');
92             });
93         */
94         });
95
96        describe('getFiles', function() {
97            before(function() {
98                process.env.NODE_ENV = '';
99                return db.init()
100                .then(function() {
101                    return Promise.all([db.test1(), db.test2(), db.testRest()])
102                });
103            });
104
105            it('should be resolved with length 4', function() {
106                return expect(eos.getFiles('1f83f620-c1ed-11e5-9657-7942989daa00')).to.eventually.length(4);
107            });
108            
109            after(function() {
110                process.env.NODE_ENV = 'test';
111             });
112         });
113     });
114 })();