FieldPathExpression.js 4.6 KB

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