Commit ca0ca0ad authored by Leonardo Menezes's avatar Leonardo Menezes
Browse files

added tests to create index

parent 2f3aa932
Loading
Loading
Loading
Loading
+3 −6
Original line number Diff line number Diff line
@@ -690,7 +690,7 @@ angular.module('cerebro').controller('CreateIndexController', ['$scope',
    $scope.shards = '';
    $scope.replicas = '';
    $scope.name = '';
    $scope.indices = [];
    $scope.indices = undefined;

    $scope.setup = function() {
      if (!$scope.editor) {
@@ -714,11 +714,8 @@ angular.module('cerebro').controller('CreateIndexController', ['$scope',
    );

    $scope.loadIndexMetadata = function(index) {
      console.log('loading');
      DataService.getIndexMetadata(index,
        function(meta) {
          console.log('loadded');
          console.log(meta);
          var body = {settings: meta.settings, mappings: meta.mappings};
          $scope.editor.setValue(JSON.stringify(body, null, 2));
        },
@@ -731,7 +728,7 @@ angular.module('cerebro').controller('CreateIndexController', ['$scope',
    $scope.createIndex = function() {
      if ($scope.name.trim()) {
        try {
          var data = $scope.editor.getValue();
          var data = $scope.editor.getValue() || {};
          if (Object.keys(data).length === 0) {
            data = {settings: {index: {}}};
            if ($scope.shards.trim()) {
@@ -751,7 +748,7 @@ angular.module('cerebro').controller('CreateIndexController', ['$scope',
            }
          );
        } catch (error) {
          AlertService.error('Malformed filter', error);
          AlertService.error('Malformed settings', error);
        }
      } else {
        AlertService.error('You must specify a valid index name');
+3 −6
Original line number Diff line number Diff line
@@ -6,7 +6,7 @@ angular.module('cerebro').controller('CreateIndexController', ['$scope',
    $scope.shards = '';
    $scope.replicas = '';
    $scope.name = '';
    $scope.indices = [];
    $scope.indices = undefined;

    $scope.setup = function() {
      if (!$scope.editor) {
@@ -30,11 +30,8 @@ angular.module('cerebro').controller('CreateIndexController', ['$scope',
    );

    $scope.loadIndexMetadata = function(index) {
      console.log('loading');
      DataService.getIndexMetadata(index,
        function(meta) {
          console.log('loadded');
          console.log(meta);
          var body = {settings: meta.settings, mappings: meta.mappings};
          $scope.editor.setValue(JSON.stringify(body, null, 2));
        },
@@ -47,7 +44,7 @@ angular.module('cerebro').controller('CreateIndexController', ['$scope',
    $scope.createIndex = function() {
      if ($scope.name.trim()) {
        try {
          var data = $scope.editor.getValue();
          var data = $scope.editor.getValue() || {};
          if (Object.keys(data).length === 0) {
            data = {settings: {index: {}}};
            if ($scope.shards.trim()) {
@@ -67,7 +64,7 @@ angular.module('cerebro').controller('CreateIndexController', ['$scope',
            }
          );
        } catch (error) {
          AlertService.error('Malformed filter', error);
          AlertService.error('Malformed settings', error);
        }
      } else {
        AlertService.error('You must specify a valid index name');
+160 −0
Original line number Diff line number Diff line
describe('CreateIndexController', function() {

    beforeEach(angular.mock.module('cerebro'));

    beforeEach(angular.mock.inject(function($rootScope, $controller, $injector) {
        this.scope = $rootScope.$new();
        this.DataService = $injector.get('DataService');
        this.AlertService = $injector.get('AlertService');
        this.AceEditorService = $injector.get('AceEditorService');
        this.createController = function() {
            return $controller('CreateIndexController',
                {$scope: this.scope}, this.AlertService, this.DataService, this.AceEditorService);
        };
        this._controller = this.createController();
    }));

    describe('initial controller state', function() {
        it('should have intial state correctly set', function() {
            expect(this.scope.editor).toEqual(undefined);
            expect(this.scope.shards).toEqual('');
            expect(this.scope.replicas).toEqual('');
            expect(this.scope.name).toEqual('');
            expect(this.scope.indices).toEqual(undefined);
        });
    });

    describe('setup', function() {
        it('initializes editor and loads available indices', function () {
            var indices = ['index1', 'index2'];
            var editor = 'someFakeEditor';
            spyOn(this.DataService, 'getData').andReturn({indices: indices});
            spyOn(this.AceEditorService, 'init').andReturn(editor);
            this.scope.setup();
            expect(this.DataService.getData).toHaveBeenCalled();
            expect(this.AceEditorService.init).toHaveBeenCalled();
            expect(this.scope.indices).toEqual(indices);
            expect(this.scope.editor).toEqual(editor);
        });
    });

    describe('watch', function() {
        it('loads indices if not loaded', function () {
            var indices = ['index1', 'index2'];
            spyOn(this.DataService, 'getData').andReturn({indices: indices});
            this.scope.$digest();
            expect(this.DataService.getData).toHaveBeenCalled();
            expect(this.scope.indices).toEqual(indices);
        });

        it('do not reload indices', function () {
            var indices = ['index1', 'index2'];
            var old_indices = ['old', 'index'];
            this.scope.indices = old_indices;
            spyOn(this.DataService, 'getData').andReturn({indices: indices});
            this.scope.$digest();
            expect(this.DataService.getData).toHaveBeenCalled();
            expect(this.scope.indices).toEqual(old_indices);
        });
    });

    describe('loadIndexMetadata', function() {
        it('correctly loads metadata from index', function () {
                    // AlertService.error('Error while loading index settings', error);
            var metadata = {settings: {some: 'settings'}, mappings: {some: 'mappings'}};
            this.DataService.getIndexMetadata = function(index, success, error) {
                success(metadata);
            };
            this.scope.editor = {setValue: function(){}};
            spyOn(this.DataService, 'getIndexMetadata').andCallThrough();
            spyOn(this.scope.editor, 'setValue').andReturn(true);
            this.scope.loadIndexMetadata('indexName');
            expect(this.DataService.getIndexMetadata).toHaveBeenCalledWith(
                'indexName',
                jasmine.any(Function),
                jasmine.any(Function)
            );
            expect(this.scope.editor.setValue).toHaveBeenCalledWith(JSON.stringify({settings: {some: "settings"}, mappings: {some: "mappings"}}, null, 2));
        });

        it('handles error while loading index metadata', function () {
            // AlertService.error('Error while loading index settings', error);
            this.DataService.getIndexMetadata = function(index, success, error) {
                error('some error');
            };
            spyOn(this.DataService, 'getIndexMetadata').andCallThrough();
            spyOn(this.AlertService, 'error').andReturn(true);
            this.scope.loadIndexMetadata('indexName');
            expect(this.DataService.getIndexMetadata).toHaveBeenCalledWith(
                'indexName',
                jasmine.any(Function),
                jasmine.any(Function)
            );
            expect(this.AlertService.error).toHaveBeenCalledWith('Error while loading index settings', 'some error');
        });
    });

    describe('createIndex', function() {
        it('correctly creates an index', function () {
            this.scope.name = 'someIndex';
            this.scope.editor = {getValue: function(){ return {};}};
            this.DataService.createIndex = function(name, metadata, success, error) {
                success('success');
            };
            spyOn(this.DataService, 'createIndex').andCallThrough();
            spyOn(this.DataService, 'forceRefresh').andReturn(true);
            spyOn(this.AlertService, 'success').andReturn(true);
            this.scope.createIndex();
            expect(this.DataService.createIndex).toHaveBeenCalledWith('someIndex', {settings: {index: {}}}, jasmine.any(Function), jasmine.any(Function));
            expect(this.DataService.forceRefresh).toHaveBeenCalled();
            expect(this.AlertService.success).toHaveBeenCalledWith('Index successfully created');
        });

        it('handles error while creating index', function () {
            this.scope.name = 'someIndex';
            this.scope.editor = {getValue: function(){ return {};}};
            this.DataService.createIndex = function(name, metadata, success, error) {
                error('boom!');
            };
            spyOn(this.DataService, 'createIndex').andCallThrough();
            spyOn(this.AlertService, 'error').andReturn(true);
            this.scope.createIndex();
            expect(this.DataService.createIndex).toHaveBeenCalledWith('someIndex', {settings: {index: {}}}, jasmine.any(Function), jasmine.any(Function));
            expect(this.AlertService.error).toHaveBeenCalledWith('Error while creating index', 'boom!');
        });

        it('merges shards and replicas settings into metadata', function () {
            this.scope.name = 'someIndex';
            this.scope.shards = '18';
            this.scope.replicas = '10';
            this.scope.editor = {getValue: function(){ return {};}};
            this.DataService.createIndex = function(name, metadata, success, error) {
                success('success');
            };
            spyOn(this.DataService, 'createIndex').andCallThrough();
            spyOn(this.DataService, 'forceRefresh').andReturn(true);
            spyOn(this.AlertService, 'success').andReturn(true);
            this.scope.createIndex();
            expect(this.DataService.createIndex).toHaveBeenCalledWith('someIndex', {settings: {index: {number_of_shards: '18', number_of_replicas: '10'}}}, jasmine.any(Function), jasmine.any(Function));
            expect(this.DataService.forceRefresh).toHaveBeenCalled();
            expect(this.AlertService.success).toHaveBeenCalledWith('Index successfully created');
        });

        it('prevents creation when name is empty', function () {
            this.scope.name = '';
            spyOn(this.AlertService, 'error').andReturn(true);
            this.scope.createIndex();
            expect(this.AlertService.error).toHaveBeenCalledWith('You must specify a valid index name');
        });

        it('prevents creation when settings is not valid json', function () {
            this.scope.name = 'someName';
            this.scope.editor = {getValue: function(){ throw 'boooom!';}};
            spyOn(this.AlertService, 'error').andReturn(true);
            spyOn(this.scope.editor, 'getValue').andCallThrough();
            this.scope.createIndex();
            expect(this.AlertService.error).toHaveBeenCalledWith('Malformed settings', 'boooom!');
        });
    });

});