FieldPathExpression.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. "use strict";
  2. var assert = require("assert"),
  3. FieldPathExpression = require("../../../../lib/pipeline/expressions/FieldPathExpression");
  4. module.exports = {
  5. "FieldPathExpression": {
  6. "constructor()": {
  7. "should throw Error if empty field path": function testInvalid(){
  8. assert.throws(function() {
  9. new FieldPathExpression('');
  10. });
  11. }
  12. },
  13. "#evaluate()": {
  14. "should return undefined if field path is missing": function testMissing(){
  15. assert.strictEqual(new FieldPathExpression('a').evaluate({}), undefined);
  16. },
  17. "should return value if field path is present": function testPresent(){
  18. assert.strictEqual(new FieldPathExpression('a').evaluate({a:123}), 123);
  19. },
  20. "should return undefined if field path is nested below null": function testNestedBelowNull(){
  21. assert.strictEqual(new FieldPathExpression('a.b').evaluate({a:null}), undefined);
  22. },
  23. "should return undefined if field path is nested below undefined": function NestedBelowUndefined(){
  24. assert.strictEqual(new FieldPathExpression('a.b').evaluate({a:undefined}), undefined);
  25. },
  26. "should return undefined if field path is nested below Number": function testNestedBelowInt(){
  27. assert.strictEqual(new FieldPathExpression('a.b').evaluate({a:2}), undefined);
  28. },
  29. "should return value if field path is nested": function testNestedValue(){
  30. assert.strictEqual(new FieldPathExpression('a.b').evaluate({a:{b:55}}), 55);
  31. },
  32. "should return undefined if field path is nested below empty Object": function testNestedBelowEmptyObject(){
  33. assert.strictEqual(new FieldPathExpression('a.b').evaluate({a:{}}), undefined);
  34. },
  35. "should return empty Array if field path is nested below empty Array": function testNestedBelowEmptyArray(){
  36. assert.deepEqual(new FieldPathExpression('a.b').evaluate({a:[]}), []);
  37. },
  38. "should return Array with null if field path is nested below Array containing null": function testNestedBelowArrayWithNull(){
  39. assert.deepEqual(new FieldPathExpression('a.b').evaluate({a:[null]}), [null]);
  40. },
  41. "should return Array with undefined if field path is nested below Array containing undefined": function testNestedBelowArrayWithUndefined(){
  42. assert.deepEqual(new FieldPathExpression('a.b').evaluate({a:[undefined]}), [undefined]);
  43. },
  44. "should throw Error if field path is nested below Array containing a Number": function testNestedBelowArrayWithInt(){
  45. assert.throws(function(){
  46. new FieldPathExpression('a.b').evaluate({a:[1]});
  47. });
  48. },
  49. "should return Array with value if field path is in Object within Array": function testNestedWithinArray(){
  50. assert.deepEqual(new FieldPathExpression('a.b').evaluate({a:[{b:9}]}), [9]);
  51. },
  52. "should return Array with multiple value types if field path is within Array with multiple value types": function testMultipleArrayValues(){
  53. var path = 'a.b',
  54. doc = {a:[{b:9},null,undefined,{g:4},{b:20},{}]},
  55. expected = [9,null,undefined,undefined,20,undefined];
  56. assert.deepEqual(new FieldPathExpression(path).evaluate(doc), expected);
  57. },
  58. "should return Array with expanded values from nested multiple nested Arrays": function testExpandNestedArrays(){
  59. var path = 'a.b.c',
  60. doc = {a:[{b:[{c:1},{c:2}]},{b:{c:3}},{b:[{c:4}]},{b:[{c:[5]}]},{b:{c:[6,7]}}]},
  61. expected = [[1,2],3,[4],[[5]],[6,7]];
  62. assert.deepEqual(new FieldPathExpression(path).evaluate(doc), expected);
  63. },
  64. "should return null if field path points to a null value": function testPresentNull(){
  65. assert.strictEqual(new FieldPathExpression('a').evaluate({a:null}), null);
  66. },
  67. "should return undefined if field path points to a undefined value": function testPresentUndefined(){
  68. assert.strictEqual(new FieldPathExpression('a').evaluate({a:undefined}), undefined);
  69. },
  70. "should return Number if field path points to a Number value": function testPresentNumber(){
  71. assert.strictEqual(new FieldPathExpression('a').evaluate({a:42}), 42);
  72. }
  73. },
  74. "#optimize()": {
  75. "should not optimize anything": function testOptimize(){
  76. var expr = new FieldPathExpression('a');
  77. assert.strictEqual(expr, expr.optimize());
  78. }
  79. },
  80. "#addDependencies()": {
  81. "should return the field path itself as a dependency": function testDependencies(){
  82. var deps = new FieldPathExpression('a.b').addDependencies([]);
  83. assert.strictEqual(deps.length, 1);
  84. assert.strictEqual(deps[0], 'a.b');
  85. }
  86. },
  87. "#toJson()": {
  88. "should output path String with a '$'-prefix": function testJson(){
  89. assert.equal(new FieldPathExpression('a.b.c').toJson(), "$a.b.c");
  90. }
  91. }
  92. }
  93. };
  94. if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);