Commit 6aad1a7e authored by Leonardo Menezes's avatar Leonardo Menezes
Browse files

added js tests for index filter

parent 6807f2b8
Loading
Loading
Loading
Loading
+100 −100
Original line number Diff line number Diff line
@@ -14,6 +14,106 @@ angular.module('cerebro', ['ngRoute']).config(['$routeProvider',
        otherwise({redirectTo: '/connect'});
  }]);

function IndexFilter(name, closed, special, healthy, asc, timestamp) {
  this.name = name;
  this.closed = closed;
  this.special = special;
  this.healthy = healthy;
  this.sort = 'name';
  this.asc = asc;
  this.timestamp = timestamp;

  this.getSorting = function() {
    var asc = this.asc;
    switch (this.sort) {
      case 'name':
        return function(a, b) {
          if (asc) {
            return a.name.localeCompare(b.name);
          } else {
            return b.name.localeCompare(a.name);
          }
        };
      default:
        return undefined;
    }
  };

  this.clone = function() {
    return new IndexFilter(
        this.name,
        this.closed,
        this.special,
        this.healthy,
        this.asc,
        this.timestamp
    );
  };

  this.equals = function(other) {
    return (
        other !== null &&
        this.name === other.name &&
        this.closed === other.closed &&
        this.special === other.special &&
        this.healthy === other.healthy &&
        this.asc === other.asc &&
        this.timestamp === other.timestamp
    );
  };

  this.isBlank = function() {
    return (
        !this.name &&
        this.closed &&
        this.special &&
        this.healthy &&
        this.asc
    );
  };

  this.matches = function(index) {
    var matches = true;
    if (!this.special && index.special) {
      matches = false;
    }
    if (!this.closed && index.closed) {
      matches = false;
    }
    // Hide healthy == show unhealthy only
    if (!this.healthy && !index.unhealthy) {
      matches = false;
    }
    if (matches && this.name) {
      try {
        var regExp = new RegExp(this.name.trim(), 'i');
        matches = regExp.test(index.name);
        if (!matches && index.aliases) {
          for (var idx = 0; idx < index.aliases.length; idx++) {
            if ((matches = regExp.test(index.aliases[idx]))) {
              break;
            }
          }
        }
      }
      catch (err) { // if not valid regexp, still try normal matching
        matches = index.name.indexOf(this.name.toLowerCase()) != -1;
        if (!matches) {
          for (var idx = 0; idx < index.aliases.length; idx++) {
            var alias = index.aliases[idx].toLowerCase();
            matches = true;
            if ((matches = (alias.indexOf(this.name.toLowerCase()) != -1))) {
              break;
            }
          }
        }
      }
    }
    return matches;
  };

}

function Page(elements, total, first, last, next, previous) {
    this.elements = elements;
    this.total = total;
@@ -536,106 +636,6 @@ angular.module('cerebro').filter('bytes', function() {

});

function IndexFilter(name, closed, special, healthy, asc, timestamp) {
  this.name = name;
  this.closed = closed;
  this.special = special;
  this.healthy = healthy;
  this.sort = 'name';
  this.asc = asc;
  this.timestamp = timestamp;

  this.getSorting = function() {
    var asc = this.asc;
    switch (this.sort) {
      case 'name':
        return function(a, b) {
          if (asc) {
            return a.name.localeCompare(b.name);
          } else {
            return b.name.localeCompare(a.name);
          }
        };
      default:
        return undefined;
    }
  };

  this.clone = function() {
    return new IndexFilter(
        this.name,
        this.closed,
        this.special,
        this.healthy,
        this.asc,
        this.timestamp
    );
  };

  this.equals = function(other) {
    return (
        other !== null &&
        this.name === other.name &&
        this.closed === other.closed &&
        this.special === other.special &&
        this.healthy === other.healthy &&
        this.asc === other.asc &&
        this.timestamp === other.timestamp
    );
  };

  this.isBlank = function() {
    return (
        !this.name &&
        this.closed &&
        this.special &&
        this.healthy &&
        this.asc
    );
  };

  this.matches = function(index) {
    var matches = true;
    if (!this.special && index.special) {
      matches = false;
    }
    if (!this.closed && index.closed) {
      matches = false;
    }
    // Hide healthy == show unhealthy only
    if (!this.healthy && !index.unhealthy) {
      matches = false;
    }
    if (matches && this.name) {
      try {
        var regExp = new RegExp(this.name.trim(), 'i');
        matches = regExp.test(index.name);
        if (!matches && index.aliases) {
          for (var idx = 0; idx < index.aliases.length; idx++) {
            if ((matches = regExp.test(index.aliases[idx]))) {
              break;
            }
          }
        }
      }
      catch (err) { // if not valid regexp, still try normal matching
        matches = index.name.indexOf(this.name.toLowerCase()) != -1;
        if (!matches) {
          for (var idx = 0; idx < index.aliases.length; idx++) {
            var alias = index.aliases[idx].toLowerCase();
            matches = true;
            if ((matches = (alias.indexOf(this.name.toLowerCase()) != -1))) {
              break;
            }
          }
        }
      }
    }
    return matches;
  };

}

function NodeFilter(name, data, master, client, timestamp) {
  this.name = name;
  this.data = data;
+1 −0
Original line number Diff line number Diff line
@@ -17,5 +17,6 @@
<script type='text/javascript' src="../public/app.js"></script>
<!-- TESTS -->
<script src="./common/paginator.js"></script>
<script src="./common/index_filter.js"></script>
</body>
</html>
+95 −0
Original line number Diff line number Diff line
test("Filters out special indices", function() {
    var filter = new IndexFilter("", true, false, true, true, 0);
    var index = {name: "index_name", closed: false, special: true, unhealthy: false, aliases: []};
    ok(!filter.matches(index), "Filters out special indices");
})

test("Maintains special indices", function() {
    var filter = new IndexFilter("", true, true, true, true, 0);
    var index = {name: "index_name", closed: false, special: true, unhealthy: false, aliases: []};
    ok(filter.matches(index), "Filters out special indices");
})

test("Filters out closed indices", function() {
    var filter = new IndexFilter("", false, true, true, true, 0);
    var index = {name: "index_name", closed: true, special: false, unhealthy: false, aliases: []};
    ok(!filter.matches(index), "Filters out closed indices");
})

test("Maintains closed indices", function() {
    var filter = new IndexFilter("", true, true, true, true, 0);
    var index = {name: "index_name", closed: true, special: false, unhealthy: false, aliases: []};
    ok(filter.matches(index), "Filters out closed indices");
})

test("Maintains healthy indices", function() {
    var filter = new IndexFilter("", true, true, true, true, 0);
    var index = {name: "index_name", closed: true, special: false, unhealthy: false, aliases: []};
    ok(filter.matches(index), "Maintains healthy indices");
})

test("Maintains unhealthy indices", function() {
    var filter = new IndexFilter("", true, true, true, true, 0);
    var index = {name: "index_name", closed: true, special: false, unhealthy: true, aliases: []};
    ok(filter.matches(index), "Maintains unhealthy indices");
})

test("Filters out healthy indices", function() {
    var filter = new IndexFilter("", true, true, false, true, 0);
    var index = {name: "index_name", closed: true, special: false, unhealthy: false, aliases: []};
    ok(!filter.matches(index), "Filters out healthy indices");
})

test("Filter by name on different name index", function() {
    var filter = new IndexFilter("abc", true,  true, true, true, 0);
    var index = {name: "cba", closed: true, special: false, unhealthy: false, aliases: []};
    ok(!filter.matches(index), "Doesnt match if filter name is not a substring of name");
})

test("Filter by name on index with matching name", function() {
    var filter = new IndexFilter("abc", true, true, true, true, 0);
    var index = {name: "abcdef", closed: true, special: false, unhealthy: false, aliases: []};
    ok(filter.matches(index), "Matches if filter name is a substring of name");
})

test("Filter by name regexp on index with matching name", function() {
    var filter = new IndexFilter("a\.+f", true, true, true, true, 0);
    var index = {name: "abcdef", closed: true, special: false, unhealthy: false, aliases: []};
    ok(filter.matches(index), "Matches if filter reg exp matches index name");
})

test("Use regexp as plain string if regexp doesnt compile", function() {
    var filter = new IndexFilter("a\.f-", true, true, true, true, 0);
    var index = {name: "a.f-", closed: true, special: false, unhealthy: false, aliases: []};
    ok(filter.matches(index), "Matches if filter reg exp matches index name");
})

test("Use regexp as plain string if regexp doesnt compile", function() {
    var filter = new IndexFilter("a\.f-", true, true, true, true, 0);
    var index = {name: "a.f-", closed: true, special: false, unhealthy: false, aliases: []};
    ok(filter.matches(index), "Matches if filter non compiling reg exp matches index name");
})

test("Checks also index aliases for matches", function() {
    var filter = new IndexFilter("also", true, true, true, true, 0);
    var index = {name: "a.f-", closed: true, special: false, unhealthy: false, aliases: ["whatever", "also_aliases"]};
    ok(filter.matches(index), "Matches also on index aliases");
})

test("Checks also index aliases for matches if RegExp doesnt compile", function() {
    var filter = new IndexFilter("[a\.f-", true, true, true, true, 0);
    var index = {name: "a.f-", closed: true, special: false, unhealthy: false, aliases: ["somethingelse", "[a\.f-lalala"]};
    ok(filter.matches(index), "Matches also on index aliases if regexp doesnt compile");
})

test("Doesnt match if neither name or aliases match the RegExp", function() {
    var filter = new IndexFilter("[a\.f-", true, true, true, true, 0);
    var index = {name: "a.f-", closed: true, special: false, unhealthy: false, aliases: ["ddd"]};
    ok(!filter.matches(index), "Matches also on index aliases if regexp doesnt compile");
})

test("Doesnt match if neither name or aliases match the text", function() {
    var filter = new IndexFilter("bbbb", true, true, true, true, 0);
    var index = {name: "a.f-", closed: true, special: false, unhealthy: false, aliases: ["ddd"]};
    ok(!filter.matches(index), "Matches also on index aliases if regexp doesnt compile");
})