UNPKG

prepared-query-options

Version:

JS Library for managing OData query parameter building and parsing

455 lines (349 loc) 19.3 kB
/*globals require, describe, it*/ var Predicate = require("../src/predicate"), should = require("chai").should(); describe("Predicate", function () { var predicate; describe("Constructing a new Predicate", function () { it("Should initialize the predicate properties", function () { predicate = new Predicate("property").equals('value'); should.equal(undefined, predicate.joinedPredicates); predicate.property.should.equal("property"); predicate.getValue().should.equal("property eq 'value'"); }); }); describe("class.join()", function () { it("Should create and return new Predicate", function () { var first = new Predicate("property1").equals('value'); var second = new Predicate("property2").equals('value'); var returned = Predicate.join([first, second]); should.equal(true, returned instanceof Predicate); }); it("Should set the new Predicate's group operator", function () { var first = new Predicate("property1").equals('value'); var second = new Predicate("property2").equals('value'); var returned = Predicate.join([first, second], Predicate.GROUP_OPERATOR.OR); returned.groupOperator.should.equal(Predicate.GROUP_OPERATOR.OR); }); it("Should only accept an array of predicates", function () { var first = new Predicate("property1").equals('value'); var second = new Predicate("property2").equals('value'); var returned; returned = Predicate.join("string"); should.equal(null, returned); returned = Predicate.join(first); should.equal(null, returned); returned = Predicate.join(1); should.equal(null, returned); returned = Predicate.join([first, second]); returned.joinedPredicates.length.should.equal(2); }); it("Should add the predicates to the predicates array", function () { var first = new Predicate("property1").equals('value'); var second = new Predicate("property2").equals('value'); var returned = Predicate.join([first, second]); returned.joinedPredicates.length.should.equal(2); returned.joinedPredicates[0].property.should.equal("property1"); returned.joinedPredicates[1].property.should.equal("property2"); }); }); describe(".join()", function () { it("Should return the original predicate", function () { var predicate = new Predicate("property1").equals('value'); var additional = new Predicate("property2").equals('value'); var returned = predicate.join(additional); should.equal(returned, predicate); }); it("Should set the original Predicate's group operator", function () { var predicate = new Predicate("property1").equals('value'); var additional = new Predicate("property2").equals('value'); var returned = predicate.join(additional, Predicate.GROUP_OPERATOR.OR); returned.groupOperator.should.equal(Predicate.GROUP_OPERATOR.OR); }); it("Should clear the values of the original predicate", function () { var predicate = new Predicate("property1").equals('value'); var additional = new Predicate("property2").equals('value'); predicate.join(additional); should.equal(undefined, predicate.property); should.equal(undefined, predicate.getValue); }); it("Should accept a predicate", function () { var predicate = new Predicate("property1").equals('value'); var additional = new Predicate("property2").equals('value'); predicate.join("string"); should.equal(undefined, predicate.joinedPredicates); predicate.join(1); should.equal(undefined, predicate.joinedPredicates); predicate.join(additional); predicate.joinedPredicates.length.should.equal(2); }); it("Should accept an array of predicates", function () { var predicate = new Predicate("property1").equals('value'); var additional1 = new Predicate("property2").equals('value'); var additional2 = new Predicate("property3").equals('value'); predicate.join("string"); should.equal(undefined, predicate.joinedPredicates); predicate.join(1); should.equal(undefined, predicate.joinedPredicates); predicate.join([additional1, additional2]); predicate.joinedPredicates.length.should.equal(3); }); it("Should add the predicates to the predicates array", function () { var predicate = new Predicate("property1").equals('value'); var additional = new Predicate("property2").equals('value'); predicate.join(additional); predicate.joinedPredicates.length.should.equal(2); predicate.joinedPredicates[0].property.should.equal("property1"); predicate.joinedPredicates[1].property.should.equal("property2"); }); it("Should allow chaining", function () { var predicate = new Predicate("property1").equals('value'); predicate.join(new Predicate("property2").equals('value')).join(new Predicate("property3").equals('value')); predicate.joinedPredicates.length.should.equal(3); predicate.joinedPredicates[0].property.should.equal("property1"); predicate.joinedPredicates[1].property.should.equal("property2"); predicate.joinedPredicates[2].property.should.equal("property3"); }); it("Should accept any case operators", function () { var predicate = new Predicate("property1").equals('value'); var additional = new Predicate("property2").equals('value'); predicate.join(additional, 'OR'); predicate.joinedPredicates.length.should.equal(2); predicate.joinedPredicates[0].property.should.equal("property1"); predicate.joinedPredicates[1].property.should.equal("property2"); predicate.groupOperator.should.equal(Predicate.GROUP_OPERATOR.OR); }); }); describe(".and()", function () { it("Should join two predicates with the and operator", function () { var predicate = new Predicate("property1").equals('value'); var additional = new Predicate("property2").equals('value'); predicate.and(additional); predicate.joinedPredicates.length.should.equal(2); predicate.groupOperator.should.equal(Predicate.GROUP_OPERATOR.AND); predicate.joinedPredicates[0].property.should.equal("property1"); predicate.joinedPredicates[1].property.should.equal("property2"); }); it("Should allow chaining", function () { var predicate = new Predicate("property1").equals('value'); predicate.and(new Predicate("property2").equals('value')).and(new Predicate("property3").equals('value')); predicate.joinedPredicates.length.should.equal(3); predicate.groupOperator.should.equal(Predicate.GROUP_OPERATOR.AND); predicate.joinedPredicates[0].property.should.equal("property1"); predicate.joinedPredicates[1].property.should.equal("property2"); predicate.joinedPredicates[2].property.should.equal("property3"); }); }); describe(".or()", function () { it("Should join two predicates with the or operator", function () { var predicate = new Predicate("property1").equals('value'); var additional = new Predicate("property2").equals('value'); predicate.or(additional); predicate.joinedPredicates.length.should.equal(2); predicate.groupOperator.should.equal(Predicate.GROUP_OPERATOR.OR); predicate.joinedPredicates[0].property.should.equal("property1"); predicate.joinedPredicates[1].property.should.equal("property2"); }); it("Should allow chaining", function () { var predicate = new Predicate("property1").equals('value'); predicate.or(new Predicate("property2").equals('value')).or(new Predicate("property3").equals('value')); predicate.joinedPredicates.length.should.equal(3); predicate.groupOperator.should.equal(Predicate.GROUP_OPERATOR.OR); predicate.joinedPredicates[0].property.should.equal("property1"); predicate.joinedPredicates[1].property.should.equal("property2"); predicate.joinedPredicates[2].property.should.equal("property3"); }); }); describe(".equals()", function () { it("Should take a value and set the getValue function", function () { var predicate = new Predicate("property").equals("value"); predicate.getValue().should.equal("property eq 'value'"); }); }); describe(".notEqualTo()", function () { it("Should take a value and set the operator", function () { var predicate = new Predicate("property").notEqualTo("value"); predicate.getValue().should.equal("property ne 'value'"); }); }); describe(".greaterThan()", function () { it("Should take a value and set the operator", function () { var predicate = new Predicate("property").greaterThan("value"); predicate.getValue().should.equal("property gt 'value'"); }); }); describe(".greaterThanOrEqualTo()", function () { it("Should take a value and set the operator", function () { var predicate = new Predicate("property").greaterThanOrEqualTo("value"); predicate.getValue().should.equal("property ge 'value'"); }); }); describe(".lessThan()", function () { it("Should take a value and set the operator", function () { var predicate = new Predicate("property").lessThan("value"); predicate.getValue().should.equal("property lt 'value'"); }); }); describe(".lessThanOrEqualTo()", function () { it("Should take a value and set the operator", function () { var predicate = new Predicate("property").lessThanOrEqualTo("value"); predicate.getValue().should.equal("property le 'value'"); }); }); describe(".startsWith()", function () { it("Should get the correct url string", function () { var predicate = new Predicate("property").startsWith("value"); predicate.getValue().should.equal("startswith(property, 'value')"); }); }); describe(".endsWith()", function () { it("Should get the correct url string", function () { var predicate = new Predicate("property").endsWith("value"); predicate.getValue().should.equal("endswith(property, 'value')"); }); }); describe(".contains()", function () { it("Should get the correct url string", function () { var predicate = new Predicate("property").contains("value"); predicate.getValue().should.equal("contains(property, 'value')"); }); }); describe(".test()", function () { it("Should return true if the object matches the predicate", function () { var today = Date.now(); var object = { prop1: 'test', prop2: 12, prop3: false, prop4: null, prop5: { sub1: 'test', sub2: true }, prop6: today, prop7: today.toString(), prop8: "TEST" }; var predicate = new Predicate("prop1").contains("es"); predicate.test(object).should.equal(true); predicate = new Predicate("prop1").contains("es").or(new Predicate('prop2').greaterThan(13)); predicate.test(object).should.equal(true); predicate = new Predicate("prop1").contains("es").and(new Predicate('prop2').greaterThan(13)); predicate.test(object).should.equal(false); predicate = new Predicate("prop1").contains("es").and(new Predicate('prop2').greaterThan(13).or(new Predicate('prop3').equals(false))); predicate.test(object).should.equal(true); predicate = new Predicate("prop4").notEqualTo("test"); predicate.test(object).should.equal(true); predicate = new Predicate("prop5.sub1").contains("es"); predicate.test(object).should.equal(true); var yesterday = new Date(); yesterday = yesterday.setDate(yesterday.getDate() - 1).toString(); var tomorrow = new Date(); tomorrow = tomorrow.setDate(tomorrow.getDate() + 1).toString(); predicate = new Predicate("prop6").greaterThan(yesterday); predicate.test(object).should.equal(true); predicate = new Predicate("prop7").lessThan(tomorrow); predicate.test(object).should.equal(true); predicate = new Predicate("prop6").greaterThanOrEqualTo(today.toString()); predicate.test(object).should.equal(true); predicate = new Predicate("prop7").lessThanOrEqualTo(today.toString()); predicate.test(object).should.equal(true); predicate = new Predicate("prop6").equals(today.toString()); predicate.test(object).should.equal(true); predicate = new Predicate("prop7").equals(today.toString()); predicate.test(object).should.equal(true); predicate = new Predicate("prop8").startsWith("te"); predicate.test(object).should.equal(true); predicate = new Predicate("undef").equals('test'); predicate.test(object).should.equal(false); }); }); describe(".toString()", function () { it("Should return a string for a simple predicate", function () { var predicate = new Predicate("property1").equals("value"); var urlString = predicate.toString(); urlString.should.equal("property1 eq 'value'"); }); it("Should quote string values", function () { var predicate = new Predicate("property1").equals("value"); var urlString = predicate.toString(); urlString.should.equal("property1 eq 'value'"); }); it("Should not quote int values", function () { var predicate = new Predicate("property1").equals(1); var urlString = predicate.toString(); urlString.should.equal("property1 eq 1"); }); it("Should not quote boolean values", function () { var predicate = new Predicate("property1").equals(true); var urlString = predicate.toString(); urlString.should.equal("property1 eq true"); predicate = new Predicate("property1").equals(false); urlString = predicate.toString(); urlString.should.equal("property1 eq false"); }); it("Should return a string when an existing predicate is joined", function () { var predicate = new Predicate("property1").equals('value'); var additional = new Predicate("property2").equals(1); predicate.join(additional); var urlString = predicate.toString(); urlString.should.equal("property1 eq 'value' and property2 eq 1"); }); it("Should return a string when predicates are joined", function () { var predicate = new Predicate("property1").equals('value'); var additional = new Predicate("property2").equals(false); var joinedPredicate = Predicate.join([predicate, additional]); var urlString = joinedPredicate.toString(); urlString.should.equal("property1 eq 'value' and property2 eq false"); }); it("Should allow setting the group operator when predicates are joined", function () { var predicate = new Predicate("property1").equals('value'); var additional = new Predicate("property2").equals(false); var joinedPredicate = Predicate.join([predicate, additional], Predicate.GROUP_OPERATOR.OR); var urlString = joinedPredicate.toString(); urlString.should.equal("property1 eq 'value' or property2 eq false"); }); }); describe("class.fromString()", function () { it("Should take a string and return a predicate", function () { var urlString = "property1 gt 5 and (property2 eq false or startswith(property3, 'test'))"; var predicate = Predicate.fromString(urlString); var value = predicate.toString(); value.should.equal(urlString); urlString = "contains(prop1, 'test') or contains(prop2, 'test') or contains(prop3, 'test')"; predicate = Predicate.fromString(urlString); value = predicate.toString(); value.should.equal(urlString); urlString = "contains(prop1, 'test')"; predicate = Predicate.fromString(urlString); value = predicate.toString(); value.should.equal(urlString); urlString = "contains(prop1, 'test') or (prop2 eq 3 and (prop3 eq 4 and prop4.a eq 6)) or (startswith(prop5, 'test') and prop6 lt 100)"; predicate = Predicate.fromString(urlString); value = predicate.toString(); value.should.equal(urlString); }); it("Should return null when given an invalid string", function () { var urlString = ""; should.equal(Predicate.fromString(urlString), null); urlString = "foo"; should.equal(Predicate.fromString(urlString), null); urlString = 21; should.equal(Predicate.fromString(urlString), null); urlString = false; should.equal(Predicate.fromString(urlString), null); urlString = "prop1 gt 5 and foo"; should.equal(Predicate.fromString(urlString), null); urlString = "prop1 gt 5 and prop2 eq 4 or prop3 eq 5"; should.equal(Predicate.fromString(urlString), null); urlString = "prop1 gt 5 and prop2 eq 4 and (prop3 eq 5"; should.equal(Predicate.fromString(urlString), null); }); it("Should create a predicate from a string and allow modification", function () { var urlString = "property1 gt 5 and (property2 eq false or startswith(property3, 'test'))"; var predicate = Predicate.fromString(urlString); var primaryPredicate = new Predicate('property').contains('test').join(predicate, Predicate.GROUP_OPERATOR.OR); var value = primaryPredicate.toString(); value.should.equal("contains(property, 'test') or (property1 gt 5 and (property2 eq false or startswith(property3, 'test')))"); }); }); });