FieldPathExpression_test.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. "use strict";
  2. var assert = require("assert"),
  3. FieldPathExpression = require("../../../../lib/pipeline/expressions/FieldPathExpression"),
  4. Variables = require("../../../../lib/pipeline/expressions/Variables"),
  5. DepsTracker = require("../../../../lib/pipeline/DepsTracker");
  6. // Mocha one-liner to make these tests self-hosted
  7. if(!module.parent)return(require.cache[__filename]=null,(new(require("mocha"))({ui:"exports",reporter:"spec",grep:process.env.TEST_GREP})).addFile(__filename).run(process.exit));
  8. exports.FieldPathExpression = {
  9. ".constructor()": {
  10. "should throw Error if empty field path": function testInvalid(){
  11. assert.throws(function() {
  12. new FieldPathExpression("");
  13. });
  14. },
  15. },
  16. "#evaluate()": {
  17. "should return undefined if field path is missing": function testMissing(){
  18. var expr = FieldPathExpression.create("a");
  19. assert.strictEqual(expr.evaluate({}), undefined);
  20. },
  21. "should return value if field path is present": function testPresent(){
  22. var expr = FieldPathExpression.create("a");
  23. assert.strictEqual(expr.evaluateInternal(new Variables(1, {a:123})), 123);
  24. },
  25. "should return undefined if field path is nested below null": function testNestedBelowNull(){
  26. var expr = FieldPathExpression.create("a.b");
  27. assert.strictEqual(expr.evaluateInternal(new Variables(1,{a:null})), undefined);
  28. },
  29. "should return undefined if field path is nested below undefined": function NestedBelowUndefined(){
  30. var expr = FieldPathExpression.create("a.b");
  31. assert.strictEqual(expr.evaluateInternal(new Variables(1,{a:undefined})), undefined);
  32. },
  33. "should return undefined if field path is nested below missing": function testNestedBelowMissing(){
  34. var expr = FieldPathExpression.create("a.b");
  35. assert.strictEqual(expr.evaluateInternal(new Variables(1,{z:1})), undefined);
  36. },
  37. "should return undefined if field path is nested below Number": function testNestedBelowInt(){
  38. var vars = new Variables(1,{a:2}),
  39. expr = FieldPathExpression.create("a.b"),
  40. results = expr.evaluateInternal(vars);
  41. assert.strictEqual(results, undefined);
  42. },
  43. "should return value if field path is nested": function testNestedValue(){
  44. var vars = new Variables(1,{a:{b:55}}),
  45. expr = FieldPathExpression.create("a.b"),
  46. results = expr.evaluateInternal(vars);
  47. assert.strictEqual(results, 55);
  48. },
  49. "should return undefined if field path is nested below empty Object": function testNestedBelowEmptyObject(){
  50. var vars = new Variables(1,{a:{}}),
  51. expr = FieldPathExpression.create("a.b"),
  52. results = expr.evaluateInternal(vars);
  53. assert.strictEqual(results, undefined);
  54. },
  55. "should return empty Array if field path is nested below empty Array": function testNestedBelowEmptyArray(){
  56. var vars = new Variables(1,{a:[]}),
  57. expr = FieldPathExpression.create("a.b"),
  58. results = expr.evaluateInternal(vars);
  59. assert.deepEqual(results, []);
  60. },
  61. "should return empty Array if field path is nested below Array containing null": function testNestedBelowArrayWithNull(){
  62. var vars = new Variables(1,{a:[null]}),
  63. expr = FieldPathExpression.create("a.b"),
  64. results = expr.evaluateInternal(vars);
  65. assert.deepEqual(results, []);
  66. },
  67. "should return empty Array if field path is nested below Array containing undefined": function testNestedBelowArrayWithUndefined(){
  68. var vars = new Variables(1,{a:[undefined]}),
  69. expr = FieldPathExpression.create("a.b"),
  70. results = expr.evaluateInternal(vars);
  71. assert.deepEqual(results, []);
  72. },
  73. "should return empty Array if field path is nested below Array containing a Number": function testNestedBelowArrayWithInt(){
  74. var vars = new Variables(1,{a:[9]}),
  75. expr = FieldPathExpression.create("a.b"),
  76. results = expr.evaluateInternal(vars);
  77. assert.deepEqual(results, []);
  78. },
  79. "should return Array with value if field path is in Object within Array": function testNestedWithinArray(){
  80. assert.deepEqual(FieldPathExpression.create("a.b").evaluateInternal(new Variables(1,{a:[{b:9}]})), [9]);
  81. },
  82. "should return Array with multiple value types if field path is within Array with multiple value types": function testMultipleArrayValues(){
  83. var path = "a.b",
  84. doc = {a:[{b:9},null,undefined,{g:4},{b:20},{}]},
  85. expected = [9,20];
  86. assert.deepEqual(FieldPathExpression.create(path).evaluateInternal(new Variables(1,doc)), expected);
  87. },
  88. "should return Array with expanded values from nested multiple nested Arrays": function testExpandNestedArrays(){
  89. var path = "a.b.c",
  90. doc = {a:[{b:[{c:1},{c:2}]},{b:{c:3}},{b:[{c:4}]},{b:[{c:[5]}]},{b:{c:[6,7]}}]},
  91. expected = [[1,2],3,[4],[[5]],[6,7]];
  92. assert.deepEqual(FieldPathExpression.create(path).evaluateInternal(new Variables(1,doc)), expected);
  93. },
  94. "should return null if field path points to a null value": function testPresentNull(){
  95. assert.strictEqual(FieldPathExpression.create("a").evaluateInternal(new Variables(1,{a:null})), null);
  96. },
  97. "should return undefined if field path points to a undefined value": function testPresentUndefined(){
  98. assert.strictEqual(FieldPathExpression.create("a").evaluateInternal(new Variables(1,{a:undefined})), undefined);
  99. },
  100. "should return Number if field path points to a Number value": function testPresentNumber(){
  101. assert.strictEqual(FieldPathExpression.create("a").evaluateInternal(new Variables(1,{a:42})), 42);
  102. }
  103. },
  104. "#optimize()": {
  105. "should not optimize anything": function testOptimize(){
  106. var expr = FieldPathExpression.create("a");
  107. // An attempt to optimize returns the Expression itself.
  108. assert.strictEqual(expr, expr.optimize());
  109. },
  110. },
  111. "#addDependencies()": {
  112. "should return the field path itself as a dependency": function testDependencies(){
  113. var fpe = FieldPathExpression.create("a.b"),
  114. deps = new DepsTracker();
  115. fpe.addDependencies(deps);
  116. assert.strictEqual(Object.keys(deps.fields).length, 1);
  117. assert("a.b" in deps.fields);
  118. assert.strictEqual(deps.needWholeDocument, false);
  119. assert.strictEqual(deps.needTextScore, false);
  120. },
  121. },
  122. "#serialize()": {
  123. "should output path String with a '$'-prefix": function testJson(){
  124. assert.equal(FieldPathExpression.create("a.b.c").serialize(), "$a.b.c");
  125. },
  126. },
  127. };