Просмотр исходного кода

refactoring and more unit tests for graphite functions

Torkel Ödegaard 12 лет назад
Родитель
Сommit
be330b6a0c

+ 5 - 5
src/app/controllers/graphiteTarget.js

@@ -2,10 +2,10 @@ define([
   'angular',
   'angular',
   'underscore',
   'underscore',
   'config',
   'config',
-  '../services/graphite/graphiteFuncs',
+  '../services/graphite/gfunc',
   '../services/graphite/parser'
   '../services/graphite/parser'
 ],
 ],
-function (angular, _, config, graphiteFuncs, Parser) {
+function (angular, _, config, gfunc, Parser) {
   'use strict';
   'use strict';
 
 
   var module = angular.module('kibana.controllers');
   var module = angular.module('kibana.controllers');
@@ -13,7 +13,7 @@ function (angular, _, config, graphiteFuncs, Parser) {
   module.controller('GraphiteTargetCtrl', function($scope, $http, filterSrv, graphiteSrv) {
   module.controller('GraphiteTargetCtrl', function($scope, $http, filterSrv, graphiteSrv) {
 
 
     $scope.init = function() {
     $scope.init = function() {
-      $scope.funcCategories = graphiteFuncs.getCategories();
+      $scope.funcCategories = gfunc.getCategories();
       parseTarget();
       parseTarget();
     };
     };
 
 
@@ -56,7 +56,7 @@ function (angular, _, config, graphiteFuncs, Parser) {
 
 
       switch(astNode.type) {
       switch(astNode.type) {
       case 'function':
       case 'function':
-        var innerFunc = graphiteFuncs.createFuncInstance(astNode.name);
+        var innerFunc = gfunc.createFuncInstance(astNode.name);
 
 
         _.each(astNode.params, function(param, index) {
         _.each(astNode.params, function(param, index) {
           parseTargeRecursive(param, innerFunc, index);
           parseTargeRecursive(param, innerFunc, index);
@@ -226,7 +226,7 @@ function (angular, _, config, graphiteFuncs, Parser) {
     };
     };
 
 
     $scope.addFunction = function(funcDef) {
     $scope.addFunction = function(funcDef) {
-      $scope.functions.push(graphiteFuncs.createFuncInstance(funcDef));
+      $scope.functions.push(gfunc.createFuncInstance(funcDef));
       $scope.targetChanged();
       $scope.targetChanged();
     };
     };
 
 

+ 8 - 16
src/app/services/graphite/graphiteFuncs.js → src/app/services/graphite/gfunc.js

@@ -14,6 +14,9 @@ function (_) {
   };
   };
 
 
   function addFuncDef(funcDef) {
   function addFuncDef(funcDef) {
+    funcDef.params = funcDef.params || [];
+    funcDef.defaultParams = funcDef.defaultParams || [];
+
     if (funcDef.category) {
     if (funcDef.category) {
       funcDef.category.push(funcDef);
       funcDef.category.push(funcDef);
     }
     }
@@ -38,8 +41,6 @@ function (_) {
   addFuncDef({
   addFuncDef({
     name: "holtWintersForecast",
     name: "holtWintersForecast",
     category: categories.Calculate,
     category: categories.Calculate,
-    params: [],
-    defaultParams: []
   });
   });
 
 
   addFuncDef({
   addFuncDef({
@@ -60,16 +61,12 @@ function (_) {
     name: 'sumSeries',
     name: 'sumSeries',
     shortName: 'sum',
     shortName: 'sum',
     category: categories.Combine,
     category: categories.Combine,
-    params: [],
-    defaultParams: []
   });
   });
 
 
   addFuncDef({
   addFuncDef({
     name: 'averageSeries',
     name: 'averageSeries',
     shortName: 'avg',
     shortName: 'avg',
     category: categories.Combine,
     category: categories.Combine,
-    params: [],
-    defaultParams: []
   });
   });
 
 
   addFuncDef({
   addFuncDef({
@@ -106,15 +103,11 @@ function (_) {
   addFuncDef({
   addFuncDef({
     name: 'integral',
     name: 'integral',
     category: categories.Transform,
     category: categories.Transform,
-    params: [],
-    defaultParams: []
   });
   });
 
 
   addFuncDef({
   addFuncDef({
     name: 'derivate',
     name: 'derivate',
     category: categories.Transform,
     category: categories.Transform,
-    params: [],
-    defaultParams: []
   });
   });
 
 
   addFuncDef({
   addFuncDef({
@@ -150,15 +143,14 @@ function (_) {
   };
   };
 
 
   return {
   return {
-    createFuncInstance: function(name) {
-      if (_.isString(name)) {
-        var funcDef = index[name];
-        if (!funcDef) {
+    createFuncInstance: function(funcDef) {
+      if (_.isString(funcDef)) {
+        if (!index[funcDef]) {
           throw { message: 'Method not found ' + name };
           throw { message: 'Method not found ' + name };
         }
         }
-        name = funcDef;
+        funcDef = index[funcDef];
       }
       }
-      return new FuncInstance(name);
+      return new FuncInstance(funcDef);
     },
     },
 
 
     getCategories: function() {
     getCategories: function() {

+ 1 - 0
src/test/karma.conf.js

@@ -8,6 +8,7 @@ module.exports = function(config) {
     files: [
     files: [
       'test/test-main.js',
       'test/test-main.js',
       {pattern: 'app/**/*.js', included: false},
       {pattern: 'app/**/*.js', included: false},
+      {pattern: 'vendor/**/*.js', included: false},
       {pattern: 'test/**/*.js', included: false}
       {pattern: 'test/**/*.js', included: false}
     ],
     ],
 
 

+ 46 - 0
src/test/specs/gfunc-specs.js

@@ -0,0 +1,46 @@
+define([
+  'app/services/graphite/gfunc'
+], function(gfunc) {
+
+  describe('when creating func instance from func namae', function() {
+
+    it('should return func instance', function() {
+      var func = gfunc.createFuncInstance('sumSeries');
+      expect(func).to.be.ok();
+      expect(func.def.name).to.equal('sumSeries');
+      expect(func.def.params.length).to.equal(0);
+      expect(func.def.defaultParams.length).to.equal(0);
+      expect(func.def.defaultParams.length).to.equal(0);
+    });
+
+    it('should return func instance with shortName', function() {
+      var func = gfunc.createFuncInstance('sum');
+      expect(func).to.be.ok();
+    });
+
+    it('should return func instance from funcDef', function() {
+      var func = gfunc.createFuncInstance('sum');
+      var func = gfunc.createFuncInstance(func.def);
+      expect(func).to.be.ok();
+    });
+
+    it('func instance should have text representation', function() {
+      var func = gfunc.createFuncInstance('groupByNode');
+      func.params[0] = 5;
+      func.params[1] = 'avg';
+      func.updateText();
+      expect(func.text).to.equal("groupByNode(5, avg)");
+    });
+
+  });
+
+  describe('when requesting function categories', function() {
+
+    it('should return function categories', function() {
+      var catIndex = gfunc.getCategories();
+      expect(catIndex.Special.length).to.equal(3);
+    });
+
+  });
+
+});

+ 13 - 1
src/test/test-main.js

@@ -1,10 +1,22 @@
 require.config({
 require.config({
-    baseUrl:'base'
+  baseUrl: 'base',
+
+  paths: {
+    underscore:            'app/components/underscore.extended',
+    'underscore-src':      'vendor/underscore',
+  },
+
+  shim: {
+    underscore: {
+      exports: '_'
+    },
+  }
 });
 });
 
 
 require([
 require([
   'test/specs/lexer-specs',
   'test/specs/lexer-specs',
   'test/specs/parser-specs',
   'test/specs/parser-specs',
+  'test/specs/gfunc-specs',
 ], function () {
 ], function () {
   window.__karma__.start();
   window.__karma__.start();
 });
 });