Sotaro KARASAWA 11 лет назад
Родитель
Сommit
438e274e43
6 измененных файлов с 158 добавлено и 20 удалено
  1. 21 3
      Gruntfile.js
  2. 5 1
      circle.yml
  3. 11 3
      package.json
  4. 43 0
      test/bootstrap.js
  5. 78 0
      test/models/config.test.js
  6. 0 13
      test/test.js

+ 21 - 3
Gruntfile.js

@@ -1,11 +1,12 @@
 /*
- * @package RMW
+ * @package Crowi
  */
 
 module.exports = function(grunt) {
 
   var paths = {
-        scripts: ['Gruntfile.js', 'app.js', 'lib/**/*.js', 'models/**/*.js', 'routes/**/*.js', 'form/**/*.js', 'resource/js/**/*.js'],
+        scripts: ['Gruntfile.js', 'app.js', 'lib/**/*.js', 'resource/js/**/*.js'],
+        tests: ['test/**/*.test.js'],
         styles: ['resource/css/*.scss'],
         all: []
       };
@@ -99,6 +100,18 @@ module.exports = function(grunt) {
       },
       all: paths.scripts
     },
+    mochaTest: {
+      all: {
+        src: ['test/**/*.test.js'],
+        options: {
+          globals: ['chai'],
+          require: ['test/bootstrap.js'],
+          timeout: 3000,
+          quiet: false,
+          clearRequireCache: true,
+        },
+      }
+    },
     watch: {
       css: {
         files: paths.styles,
@@ -108,6 +121,10 @@ module.exports = function(grunt) {
         files: paths.all,
         tasks: ['dev'],
       },
+      test: {
+        files: paths.all,
+        tasks: ['test'],
+      },
       default: {
         files: paths.all,
         tasks: ['default'],
@@ -121,10 +138,11 @@ module.exports = function(grunt) {
   grunt.loadNpmTasks('grunt-contrib-concat');
   grunt.loadNpmTasks('grunt-contrib-jshint');
   grunt.loadNpmTasks('grunt-sass');
-
+  grunt.loadNpmTasks('grunt-mocha-test');
 
   // grunt watch dev
   grunt.registerTask('default', ['sass', 'concat', 'uglify']);
   grunt.registerTask('dev', ['sass:dev', 'concat', 'jshint']);
+  grunt.registerTask('test', ['mochaTest']);
 
 };

+ 5 - 1
circle.yml

@@ -1,10 +1,14 @@
+machine:
+  timezone: JST
+  services:
+    - mongodb
 deployment:
   demo:
     branch: master
     heroku:
       appname: crowi-demo
   dev:
-    branch: master
+    branch: /(master|wip-v.*)/
     heroku:
       appname: crowi-dev
   strk:

+ 11 - 3
package.json

@@ -12,6 +12,11 @@
   "contributors": [
     "Keisuke SATO <riaf@me.com> (http://riaf.jp)"
   ],
+  "config": {
+    "blanket": {
+      "pattern": "./lib/**/*.js"
+    }
+  },
   "repository": {
     "type": "git",
     "url": "https://github.com/crowi/crowi.git"
@@ -40,6 +45,7 @@
     "grunt-contrib-jshint": "^0.10.0",
     "grunt-contrib-uglify": "~0.2.2",
     "grunt-contrib-watch": "~0.5.3",
+    "grunt-mocha-test": "^0.12.7",
     "grunt-sass": "~0.17.0",
     "mongoose": "=3.8.14",
     "mongoose-paginate": "~3.1.0",
@@ -52,10 +58,12 @@
     "time": "=0.10.0"
   },
   "devDependencies": {
+    "blanket": "^1.1.6",
     "chai": "^1.10.0",
     "mocha": "~2.1.0",
-    "mongoose-mock": "~0.4.0",
-    "proxyquire": "~1.3.0"
+    "proxyquire": "~1.3.0",
+    "sinon": "^1.12.2",
+    "sinon-chai": "^2.7.0"
   },
   "license": [
     {
@@ -65,7 +73,7 @@
   ],
   "scripts": {
     "start": "node app.js",
-    "test": "./node_modules/.bin/mocha",
+    "test": "./node_modules/.bin/grunt test",
     "postinstall": "./node_modules/.bin/bower cache clean && ./node_modules/.bin/bower install && ./node_modules/.bin/grunt"
   },
   "env": {

+ 43 - 0
test/bootstrap.js

@@ -0,0 +1,43 @@
+'use strict';
+
+var express = require('express')
+  , async = require('async')
+  , mongoose= require('mongoose')
+  , MODEL_DIR = __dirname + '/../lib/models'
+  , mongoUri
+  , testDBUtil
+  ;
+
+mongoUri = process.env.MONGOLAB_URI || process.env.MONGOHQ_URL || process.env.MONGO_URI || null;
+
+
+testDBUtil = {
+  generateFixture: function (conn, model, fixture, cb) {
+    var m = conn.model(model);
+    async.each(fixture, function(data, next) {
+      var newDoc = new m;
+
+      Object.keys(data).forEach(function(k) {
+        newDoc[k] = data[k];
+      });
+      newDoc.save(next);
+
+    }, function(err) {
+      cb();
+    });
+  },
+  cleanUpDb: function (conn, model, cb) {
+    if (!model) {
+      return cb(null, null);
+    }
+
+    var m = conn.model(model);
+    m.remove({}, cb);
+  },
+};
+
+global.express = express;
+global.mongoose = mongoose;
+global.mongoUri = mongoUri;
+global.MODEL_DIR = MODEL_DIR;
+global.testDBUtil = testDBUtil;

+ 78 - 0
test/models/config.test.js

@@ -0,0 +1,78 @@
+var chai = require('chai')
+  , expect = chai.expect
+  , sinon = require('sinon')
+  , sinonChai = require('sinon-chai')
+  , proxyquire = require('proxyquire')
+  ;
+chai.use(sinonChai);
+
+describe('Config model test', function () {
+  var conn
+    , app = new express()
+    , Config = proxyquire(MODEL_DIR + '/config.js', {mongoose: mongoose})(app)
+    ;
+
+  before(function (done) {
+    if (mongoUri) {
+      // 基本的に mongoUri がセットされてたら、そのURIにはつながる前提
+      conn = mongoose.createConnection(mongoUri, function(err) {
+        if (err) {
+          done(); // ここで skip したいなあ
+        }
+
+        Config = conn.model('Config');
+        var fixture = [
+          {ns: 'crowi', key: 'test:test', value: JSON.stringify('crowi test value')},
+          {ns: 'crowi', key: 'test:test2', value: JSON.stringify(11111)},
+          {ns: 'crowi', key: 'test:test3', value: JSON.stringify([1, 2, 3, 4, 5])},
+          {ns: 'plugin', key: 'other:config', value: JSON.stringify('this is data')},
+        ];
+
+        testDBUtil.generateFixture(conn, 'Config', fixture, done);
+      });
+    }
+  });
+
+  beforeEach(function () {
+  });
+
+  after(function (done) {
+    if (mongoUri) {
+      testDBUtil.cleanUpDb(conn, 'Config', function(err, doc) {
+        conn.close();
+        done();
+      });
+    }
+  });
+
+  describe('.CONSTANTS', function () {
+    it('Config has constants', function() {
+      expect(Config.SECURITY_REGISTRATION_MODE_OPEN).to.have.string('Open');
+      expect(Config.SECURITY_REGISTRATION_MODE_RESTRICTED).to.have.string('Resricted');
+      expect(Config.SECURITY_REGISTRATION_MODE_CLOSED).to.have.string('Closed');
+    });
+  });
+
+  describe('.loadAllConfig', function () {
+    it('Get config array', function(done) {
+      Config.loadAllConfig(function(err, config) {
+
+        expect(config.crowi).to.be.an('Object');
+        expect(config.crowi).to.have.property('test:test')
+          .and.equal('crowi test value');
+        expect(config.crowi).to.have.property('test:test2')
+          .and.equal(11111);
+        expect(config.crowi).to.have.property('test:test3')
+          .and.to.be.instanceof(Array)
+          .and.deep.equal([1, 2, 3, 4, 5]);
+
+        expect(config.plugin).to.be.an('Object')
+          .and.have.property('other:config')
+          .and.equal('this is data');
+
+        done();
+      });
+    });
+  });
+});
+

+ 0 - 13
test/test.js

@@ -1,13 +0,0 @@
-// sample
-
-var assert = require("assert");
-
-describe('Array', function(){
-  describe('#indexOf()', function(){
-    it('should return -1 when the value is not present', function(){
-      assert.equal(-1, [1,2,3].indexOf(5));
-      assert.equal(-1, [1,2,3].indexOf(0));
-    })
-  })
-})
-