Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should work with nested clauses', function (done) {
uiFilter.addClause(new UI.Clause(uiSegmentSrv, UI.Operators.AND, new UI.RestrictionDTO('location', UI.Comparators.EQ, 'Stuttgart')));
uiFilter.addClause(new API.Clause(new API.NestedRestriction()
.withClause(new API.Clause(new API.Restriction('severity', API.Comparators.GE, 'WARNING'), API.Operators.AND))
.withClause(new API.Clause(new API.Restriction('severity', API.Comparators.LE, 'MAJOR'), API.Operators.AND)), API.Operators.OR));
expect(uiFilter.getQueryString()).to.eql("select all alarms where location = 'Stuttgart' or (severity >= 'WARNING' and severity <= 'MAJOR')");
// let's try the other way around
uiFilter.clear();
uiFilter.addClause(new API.Clause(new API.NestedRestriction()
.withClause(new API.Clause(new API.Restriction('severity', API.Comparators.GE, 'WARNING'), API.Operators.AND))
.withClause(new API.Clause(new API.Restriction('severity', API.Comparators.LE, 'MAJOR'), API.Operators.AND)), API.Operators.OR));
uiFilter.addClause(new UI.Clause(uiSegmentSrv, UI.Operators.AND, new UI.RestrictionDTO('location', UI.Comparators.EQ, 'Stuttgart')));
expect(uiFilter.getQueryString()).to.eql("select all alarms where (severity >= 'WARNING' and severity <= 'MAJOR') and location = 'Stuttgart'");
// let's try 2 nested restrictions
uiFilter.clear();
uiFilter.addClause(new API.Clause(new API.NestedRestriction()
it('should map from api to ui filter with nested restrictions when serialized and deserialized again', () => {
// Create the filter
const apiFilter = new API.Filter()
.withClause(new API.Clause(new API.Restriction("alarmAckUser", API.Comparators.EQ, "Administrator"), API.Operators.AND))
.withClause(new API.Clause(
new API.NestedRestriction()
.withClause(new API.Clause(new API.Restriction("severity", API.Comparators.GE, "WARNING"), API.Operators.AND)),
API.Operators.AND));
// Simulate persisting and reloading
const serialized = JSON.stringify(apiFilter);
const deserialized = JSON.parse(serialized);
const cloned = API.Filter.fromJson(deserialized);
// Now try to map it to an ui filter
const uiFilter = mapping.getUiFilter(cloned);
expect(uiFilter.getQueryString()).to.eql("select all alarms where alarmAckUser = 'Administrator' and (severity >= 'WARNING')");
});
});
.withClause(new API.Clause(new API.Restriction('severity', API.Comparators.GE, 'WARNING'), API.Operators.AND))
.withClause(new API.Clause(new API.Restriction('severity', API.Comparators.LE, 'MAJOR'), API.Operators.AND)), API.Operators.OR));
expect(uiFilter.getQueryString()).to.eql("select all alarms where location = 'Stuttgart' or (severity >= 'WARNING' and severity <= 'MAJOR')");
// let's try the other way around
uiFilter.clear();
uiFilter.addClause(new API.Clause(new API.NestedRestriction()
.withClause(new API.Clause(new API.Restriction('severity', API.Comparators.GE, 'WARNING'), API.Operators.AND))
.withClause(new API.Clause(new API.Restriction('severity', API.Comparators.LE, 'MAJOR'), API.Operators.AND)), API.Operators.OR));
uiFilter.addClause(new UI.Clause(uiSegmentSrv, UI.Operators.AND, new UI.RestrictionDTO('location', UI.Comparators.EQ, 'Stuttgart')));
expect(uiFilter.getQueryString()).to.eql("select all alarms where (severity >= 'WARNING' and severity <= 'MAJOR') and location = 'Stuttgart'");
// let's try 2 nested restrictions
uiFilter.clear();
uiFilter.addClause(new API.Clause(new API.NestedRestriction()
.withClause(new API.Clause(new API.Restriction('location', API.Comparators.EQ, 'Stuttgart'), API.Operators.OR))
.withClause(new API.Clause(new API.Restriction('location', API.Comparators.EQ, 'Fulda'), API.Operators.OR)), API.Operators.AND)
);
uiFilter.addClause(new API.Clause(new API.NestedRestriction()
.withClause(new API.Clause(new API.Restriction('severity', API.Comparators.GE, 'WARNING'), API.Operators.AND))
.withClause(new API.Clause(new API.Restriction('severity', API.Comparators.LE, 'MAJOR'), API.Operators.AND)), API.Operators.AND)
);
expect(uiFilter.getQueryString()).to.eql("select all alarms where (location = 'Stuttgart' or location = 'Fulda') and (severity >= 'WARNING' and severity <= 'MAJOR')");
done();
});
cloneClause(clause) {
const operator = _.find(API.Operators, function(operator) {
return operator.label === clause.operator.label;
});
// Nested restriction
if (clause.restriction.clauses) {
const nestedRestriction = this.cloneNestedRestriction(clause.restriction);
return new API.Clause(nestedRestriction, operator);
} else { // Normal restriction
const restriction = this.cloneRestriction(clause.restriction);
return new API.Clause(restriction, operator);
}
}
cloneClause(clause) {
const operator = _.find(API.Operators, function(operator) {
return operator.label === clause.operator.label;
});
// Nested restriction
if (clause.restriction.clauses) {
const nestedRestriction = this.cloneNestedRestriction(clause.restriction);
return new API.Clause(nestedRestriction, operator);
} else { // Normal restriction
const restriction = this.cloneRestriction(clause.restriction);
return new API.Clause(restriction, operator);
}
}
it('should handle deep nested clauses', function (done) {
const nestedRestriction = new API.NestedRestriction()
.withClause(new API.Clause(new API.Restriction("severity", API.Comparators.GE, 'WARNING'), API.Operators.AND))
.withClause(new API.Clause(new API.Restriction("severity", API.Comparators.LE, 'MAJOR'), API.Operators.AND))
.withClause(new API.Clause(new API.NestedRestriction()
.withClause(new API.Clause(new API.Restriction("location", API.Comparators.EQ, "Fulda"), API.Operators.OR)), API.Operators.OR), API.Operators.OR);
uiFilter.addClause(new API.Clause(nestedRestriction, API.Operators.OR));
expect(uiFilter.getQueryString()).to.eql("select all alarms where (severity >= 'WARNING' and severity <= 'MAJOR' or (location = 'Fulda'))");
done();
});
it('should handle deep nested clauses', function (done) {
const nestedRestriction = new API.NestedRestriction()
.withClause(new API.Clause(new API.Restriction("severity", API.Comparators.GE, 'WARNING'), API.Operators.AND))
.withClause(new API.Clause(new API.Restriction("severity", API.Comparators.LE, 'MAJOR'), API.Operators.AND))
.withClause(new API.Clause(new API.NestedRestriction()
.withClause(new API.Clause(new API.Restriction("location", API.Comparators.EQ, "Fulda"), API.Operators.OR)), API.Operators.OR), API.Operators.OR);
uiFilter.addClause(new API.Clause(nestedRestriction, API.Operators.OR));
expect(uiFilter.getQueryString()).to.eql("select all alarms where (severity >= 'WARNING' and severity <= 'MAJOR' or (location = 'Fulda'))");
done();
});
it ('should handle multi-select with 1 value selected', () => {
const filter = new API.Filter()
.withClause(new API.Clause(new API.Restriction('severity', API.Comparators.EQ, '$severity'), API.Operators.AND));
ctx.templateSrv.init([{
name: 'severity',
multi: true,
current: {
value: ['NORMAL']
}
}]);
const actualFilter = ctx.datasource.buildQuery(filter, {});
expect(filter).not.to.equal(actualFilter);
expect(actualFilter.clauses.length).to.equal(1);
expect(actualFilter.clauses[0].restriction.attribute).to.equal('severity');
expect(actualFilter.clauses[0].restriction.value).to.equal('NORMAL');
});
getApiClause(uiClause) {
if (!(uiClause instanceof UI.Clause)) {
throw new TypeError("uiClause is not of type UI.Clause");
}
const apiOperator = new OperatorMapping().getApiOperator(uiClause.operator.value);
const apiRestriction = new RestrictionMapping(this.uiSegmentSrv).getApiRestriction(uiClause.restriction);
if (apiRestriction !== null) {
return new API.Clause(apiRestriction, apiOperator);
}
return null;
}
}