MapExpression_test.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. MapExpression = require("../../../../lib/pipeline/expressions/MapExpression"),
  5. Expression = require("../../../../lib/pipeline/expressions/Expression"),
  6. AddExpression = require("../../../../lib/pipeline/expressions/AddExpression"), // jshint ignore:line
  7. IfNullExpression = require("../../../../lib/pipeline/expressions/IfNullExpression"), // jshint ignore:line
  8. Variables = require("../../../../lib/pipeline/expressions/Variables"),
  9. VariablesIdGenerator = require("../../../../lib/pipeline/expressions/VariablesIdGenerator"),
  10. VariablesParseState = require("../../../../lib/pipeline/expressions/VariablesParseState"),
  11. DepsTracker = require("../../../../lib/pipeline/DepsTracker"),
  12. utils = require("./utils"),
  13. constify = utils.constify,
  14. expressionToJson = utils.expressionToJson;
  15. exports.MapExpression = {
  16. "constructor()": {
  17. "should accept 4 arguments": function() {
  18. new MapExpression(1, 2, 3, 4);
  19. },
  20. "should accept only 4 arguments": function() {
  21. assert.throws(function() { new MapExpression(); });
  22. assert.throws(function() { new MapExpression(1); });
  23. assert.throws(function() { new MapExpression(1, 2); });
  24. assert.throws(function() { new MapExpression(1, 2, 3); });
  25. assert.throws(function() { new MapExpression(1, 2, 3, 4, 5); });
  26. },
  27. },
  28. "#optimize()": {
  29. "should optimize both $map.input and $map.in": function() {
  30. var spec = {$map:{
  31. input: {$ifNull:[null, {$const:[1,2,3]}]},
  32. as: "i",
  33. in: {$add:["$$i","$$i",1,2]},
  34. }},
  35. idGenerator = new VariablesIdGenerator(),
  36. vps = new VariablesParseState(idGenerator),
  37. expr = Expression.parseOperand(spec, vps),
  38. optimized = expr.optimize();
  39. assert.strictEqual(optimized, expr, "should be same reference");
  40. assert.deepEqual(expressionToJson(optimized._input), {$const:[1,2,3]});
  41. assert.deepEqual(expressionToJson(optimized._each), constify({$add:["$$i","$$i",3]}));
  42. },
  43. },
  44. "#serialize()": {
  45. "should serialize to consistent order": function() {
  46. var spec = {$map:{
  47. as: "i",
  48. in: {$add:["$$i","$$i"]},
  49. input: {$const:[1,2,3]},
  50. }},
  51. expected = {$map:{
  52. input: {$const:[1,2,3]},
  53. as: "i",
  54. in: {$add:["$$i","$$i"]},
  55. }},
  56. idGenerator = new VariablesIdGenerator(),
  57. vps = new VariablesParseState(idGenerator),
  58. expr = Expression.parseOperand(spec, vps);
  59. assert.deepEqual(expressionToJson(expr), expected);
  60. },
  61. },
  62. "#evaluate()": {
  63. "should be able to map over a simple array": function() {
  64. var spec = {$map:{
  65. input: {$const:[1,2,3]},
  66. as: "i",
  67. in: {$add:["$$i","$$i"]},
  68. }},
  69. idGenerator = new VariablesIdGenerator(),
  70. vps = new VariablesParseState(idGenerator),
  71. expr = Expression.parseOperand(spec, vps),
  72. vars = new Variables(1, {}); // must set numVars (usually handled by doc src)
  73. assert.deepEqual(expr.evaluate(vars), [2, 4, 6]);
  74. },
  75. },
  76. "#addDependencies()": {
  77. "should add dependencies to both $map.input and $map.in": function() {
  78. var spec = {$map:{
  79. input: "$inputArray",
  80. as: "i",
  81. in: {$add:["$$i","$someConst"]},
  82. }},
  83. idGenerator = new VariablesIdGenerator(),
  84. vps = new VariablesParseState(idGenerator),
  85. expr = Expression.parseOperand(spec, vps),
  86. deps = new DepsTracker();
  87. expr.addDependencies(deps);
  88. assert.strictEqual(Object.keys(deps.fields).length, 2);
  89. assert.strictEqual("inputArray" in deps.fields, true);
  90. assert.strictEqual("someConst" in deps.fields, true);
  91. assert.strictEqual(deps.needWholeDocument, false);
  92. assert.strictEqual(deps.needTextScore, false);
  93. },
  94. },
  95. };