|
@@ -1,6 +1,7 @@
|
|
|
"use strict";
|
|
"use strict";
|
|
|
var assert = require("assert"),
|
|
var assert = require("assert"),
|
|
|
- FieldPathExpression = require("../../../../lib/pipeline/expressions/FieldPathExpression");
|
|
|
|
|
|
|
+ FieldPathExpression = require("../../../../lib/pipeline/expressions/FieldPathExpression"),
|
|
|
|
|
+ Variables = require("../../../../lib/pipeline/expressions/Variables");
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
module.exports = {
|
|
@@ -20,79 +21,108 @@ module.exports = {
|
|
|
"#evaluate()": {
|
|
"#evaluate()": {
|
|
|
|
|
|
|
|
"should return undefined if field path is missing": function testMissing(){
|
|
"should return undefined if field path is missing": function testMissing(){
|
|
|
- assert.strictEqual(new FieldPathExpression('a').evaluate({}), undefined);
|
|
|
|
|
|
|
+ assert.strictEqual(FieldPathExpression.create('a').evaluateInternal(new Variables(1, {})), undefined);
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
"should return value if field path is present": function testPresent(){
|
|
"should return value if field path is present": function testPresent(){
|
|
|
- assert.strictEqual(new FieldPathExpression('a').evaluate({a:123}), 123);
|
|
|
|
|
|
|
+ var vars = new Variables(1, {a:123}),
|
|
|
|
|
+ fieldPath = FieldPathExpression.create('a'),
|
|
|
|
|
+ results = fieldPath.evaluateInternal(vars);
|
|
|
|
|
+ assert.strictEqual(results, 123);
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
"should return undefined if field path is nested below null": function testNestedBelowNull(){
|
|
"should return undefined if field path is nested below null": function testNestedBelowNull(){
|
|
|
- assert.strictEqual(new FieldPathExpression('a.b').evaluate({a:null}), undefined);
|
|
|
|
|
|
|
+ var vars = new Variables(1,{a:null}),
|
|
|
|
|
+ fieldPath = FieldPathExpression.create('a.b'),
|
|
|
|
|
+ results = fieldPath.evaluateInternal(vars);
|
|
|
|
|
+ assert.strictEqual(results, undefined);
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
"should return undefined if field path is nested below undefined": function NestedBelowUndefined(){
|
|
"should return undefined if field path is nested below undefined": function NestedBelowUndefined(){
|
|
|
- assert.strictEqual(new FieldPathExpression('a.b').evaluate({a:undefined}), undefined);
|
|
|
|
|
|
|
+ var vars = new Variables(1,{a:undefined}),
|
|
|
|
|
+ fieldPath = FieldPathExpression.create('a.b'),
|
|
|
|
|
+ results = fieldPath.evaluateInternal(vars);
|
|
|
|
|
+ assert.strictEqual(results, undefined);
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
"should return undefined if field path is nested below Number": function testNestedBelowInt(){
|
|
"should return undefined if field path is nested below Number": function testNestedBelowInt(){
|
|
|
- assert.strictEqual(new FieldPathExpression('a.b').evaluate({a:2}), undefined);
|
|
|
|
|
|
|
+ var vars = new Variables(1,{a:2}),
|
|
|
|
|
+ fieldPath = FieldPathExpression.create('a.b'),
|
|
|
|
|
+ results = fieldPath.evaluateInternal(vars);
|
|
|
|
|
+ assert.strictEqual(results, undefined);
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
"should return value if field path is nested": function testNestedValue(){
|
|
"should return value if field path is nested": function testNestedValue(){
|
|
|
- assert.strictEqual(new FieldPathExpression('a.b').evaluate({a:{b:55}}), 55);
|
|
|
|
|
|
|
+ var vars = new Variables(1,{a:{b:55}}),
|
|
|
|
|
+ fieldPath = FieldPathExpression.create('a.b'),
|
|
|
|
|
+ results = fieldPath.evaluateInternal(vars);
|
|
|
|
|
+ assert.strictEqual(results, 55);
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
"should return undefined if field path is nested below empty Object": function testNestedBelowEmptyObject(){
|
|
"should return undefined if field path is nested below empty Object": function testNestedBelowEmptyObject(){
|
|
|
- assert.strictEqual(new FieldPathExpression('a.b').evaluate({a:{}}), undefined);
|
|
|
|
|
|
|
+ var vars = new Variables(1,{a:{}}),
|
|
|
|
|
+ fieldPath = FieldPathExpression.create('a.b'),
|
|
|
|
|
+ results = fieldPath.evaluateInternal(vars);
|
|
|
|
|
+ assert.strictEqual(results, undefined);
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
"should return empty Array if field path is nested below empty Array": function testNestedBelowEmptyArray(){
|
|
"should return empty Array if field path is nested below empty Array": function testNestedBelowEmptyArray(){
|
|
|
- assert.deepEqual(new FieldPathExpression('a.b').evaluate({a:[]}), []);
|
|
|
|
|
|
|
+ var vars = new Variables(1,{a:[]}),
|
|
|
|
|
+ fieldPath = FieldPathExpression.create('a.b'),
|
|
|
|
|
+ results = fieldPath.evaluateInternal(vars);
|
|
|
|
|
+ assert.deepEqual(results, []);
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
"should return Array with null if field path is nested below Array containing null": function testNestedBelowArrayWithNull(){
|
|
"should return Array with null if field path is nested below Array containing null": function testNestedBelowArrayWithNull(){
|
|
|
- assert.deepEqual(new FieldPathExpression('a.b').evaluate({a:[null]}), [null]);
|
|
|
|
|
|
|
+ debugger;
|
|
|
|
|
+ var vars = new Variables(1,{a:[null]}),
|
|
|
|
|
+ fieldPath = FieldPathExpression.create('a.b'),
|
|
|
|
|
+ results = fieldPath.evaluateInternal(vars);
|
|
|
|
|
+ assert.deepEqual(results, []);
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
"should return Array with undefined if field path is nested below Array containing undefined": function testNestedBelowArrayWithUndefined(){
|
|
"should return Array with undefined if field path is nested below Array containing undefined": function testNestedBelowArrayWithUndefined(){
|
|
|
- assert.deepEqual(new FieldPathExpression('a.b').evaluate({a:[undefined]}), [undefined]);
|
|
|
|
|
|
|
+ var vars = new Variables(1,{a:[undefined]}),
|
|
|
|
|
+ fieldPath = FieldPathExpression.create('a.b'),
|
|
|
|
|
+ results = fieldPath.evaluateInternal(vars);
|
|
|
|
|
+ assert.deepEqual(results, []);
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
"should throw Error if field path is nested below Array containing a Number": function testNestedBelowArrayWithInt(){
|
|
"should throw Error if field path is nested below Array containing a Number": function testNestedBelowArrayWithInt(){
|
|
|
- assert.throws(function(){
|
|
|
|
|
- new FieldPathExpression('a.b').evaluate({a:[1]});
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ var vars = new Variables(1,{a:[9]}),
|
|
|
|
|
+ fieldPath = FieldPathExpression.create('a.b'),
|
|
|
|
|
+ results = fieldPath.evaluateInternal(vars);
|
|
|
|
|
+ assert.deepEqual(results, []);
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
"should return Array with value if field path is in Object within Array": function testNestedWithinArray(){
|
|
"should return Array with value if field path is in Object within Array": function testNestedWithinArray(){
|
|
|
- assert.deepEqual(new FieldPathExpression('a.b').evaluate({a:[{b:9}]}), [9]);
|
|
|
|
|
|
|
+ assert.deepEqual(FieldPathExpression.create('a.b').evaluateInternal(new Variables(1,{a:[{b:9}]})), [9]);
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
"should return Array with multiple value types if field path is within Array with multiple value types": function testMultipleArrayValues(){
|
|
"should return Array with multiple value types if field path is within Array with multiple value types": function testMultipleArrayValues(){
|
|
|
var path = 'a.b',
|
|
var path = 'a.b',
|
|
|
doc = {a:[{b:9},null,undefined,{g:4},{b:20},{}]},
|
|
doc = {a:[{b:9},null,undefined,{g:4},{b:20},{}]},
|
|
|
- expected = [9,null,undefined,undefined,20,undefined];
|
|
|
|
|
- assert.deepEqual(new FieldPathExpression(path).evaluate(doc), expected);
|
|
|
|
|
|
|
+ expected = [9,20];
|
|
|
|
|
+ assert.deepEqual(FieldPathExpression.create(path).evaluateInternal(new Variables(1,doc)), expected);
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
"should return Array with expanded values from nested multiple nested Arrays": function testExpandNestedArrays(){
|
|
"should return Array with expanded values from nested multiple nested Arrays": function testExpandNestedArrays(){
|
|
|
var path = 'a.b.c',
|
|
var path = 'a.b.c',
|
|
|
doc = {a:[{b:[{c:1},{c:2}]},{b:{c:3}},{b:[{c:4}]},{b:[{c:[5]}]},{b:{c:[6,7]}}]},
|
|
doc = {a:[{b:[{c:1},{c:2}]},{b:{c:3}},{b:[{c:4}]},{b:[{c:[5]}]},{b:{c:[6,7]}}]},
|
|
|
expected = [[1,2],3,[4],[[5]],[6,7]];
|
|
expected = [[1,2],3,[4],[[5]],[6,7]];
|
|
|
- assert.deepEqual(new FieldPathExpression(path).evaluate(doc), expected);
|
|
|
|
|
|
|
+ assert.deepEqual(FieldPathExpression.create(path).evaluateInternal(new Variables(1,doc)), expected);
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
"should return null if field path points to a null value": function testPresentNull(){
|
|
"should return null if field path points to a null value": function testPresentNull(){
|
|
|
- assert.strictEqual(new FieldPathExpression('a').evaluate({a:null}), null);
|
|
|
|
|
|
|
+ assert.strictEqual(FieldPathExpression.create('a').evaluateInternal(new Variables(1,{a:null})), null);
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
"should return undefined if field path points to a undefined value": function testPresentUndefined(){
|
|
"should return undefined if field path points to a undefined value": function testPresentUndefined(){
|
|
|
- assert.strictEqual(new FieldPathExpression('a').evaluate({a:undefined}), undefined);
|
|
|
|
|
|
|
+ assert.strictEqual(FieldPathExpression.create('a').evaluateInternal(new Variables(1,{a:undefined})), undefined);
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
"should return Number if field path points to a Number value": function testPresentNumber(){
|
|
"should return Number if field path points to a Number value": function testPresentNumber(){
|
|
|
- assert.strictEqual(new FieldPathExpression('a').evaluate({a:42}), 42);
|
|
|
|
|
|
|
+ assert.strictEqual(FieldPathExpression.create('a').evaluateInternal(new Variables(1,{a:42})), 42);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
},
|
|
},
|
|
@@ -100,7 +130,7 @@ module.exports = {
|
|
|
"#optimize()": {
|
|
"#optimize()": {
|
|
|
|
|
|
|
|
"should not optimize anything": function testOptimize(){
|
|
"should not optimize anything": function testOptimize(){
|
|
|
- var expr = new FieldPathExpression('a');
|
|
|
|
|
|
|
+ var expr = FieldPathExpression.create('a');
|
|
|
assert.strictEqual(expr, expr.optimize());
|
|
assert.strictEqual(expr, expr.optimize());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -110,7 +140,7 @@ module.exports = {
|
|
|
|
|
|
|
|
"should return the field path itself as a dependency": function testDependencies(){
|
|
"should return the field path itself as a dependency": function testDependencies(){
|
|
|
var deps = {};
|
|
var deps = {};
|
|
|
- var fpe = new FieldPathExpression('a.b');
|
|
|
|
|
|
|
+ var fpe = FieldPathExpression.create('a.b');
|
|
|
fpe.addDependencies(deps);
|
|
fpe.addDependencies(deps);
|
|
|
assert.strictEqual(Object.keys(deps).length, 1);
|
|
assert.strictEqual(Object.keys(deps).length, 1);
|
|
|
assert.ok(deps['a.b']);
|
|
assert.ok(deps['a.b']);
|
|
@@ -121,7 +151,7 @@ module.exports = {
|
|
|
"#toJSON()": {
|
|
"#toJSON()": {
|
|
|
|
|
|
|
|
"should output path String with a '$'-prefix": function testJson(){
|
|
"should output path String with a '$'-prefix": function testJson(){
|
|
|
- assert.equal(new FieldPathExpression('a.b.c').toJSON(), "$a.b.c");
|
|
|
|
|
|
|
+ assert.equal(FieldPathExpression.create('a.b.c').serialize(), "$a.b.c");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|