FieldPathExpression_test.js 6.0 KB

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