ConstantExpression_test.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. var assert = require("assert"),
  3. ConstantExpression = require("../../../../lib/pipeline/expressions/ConstantExpression"),
  4. VariablesIdGenerator = require("../../../../lib/pipeline/expressions/VariablesIdGenerator"),
  5. VariablesParseState = require("../../../../lib/pipeline/expressions/VariablesParseState"),
  6. DepsTracker = require("../../../../lib/pipeline/DepsTracker");
  7. // Mocha one-liner to make these tests self-hosted
  8. 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));
  9. exports.ConstantExpression = {
  10. ".constructor()": {
  11. "should accept one argument": function () {
  12. new ConstantExpression(5);
  13. },
  14. "should not accept 0 arguments": function () {
  15. assert.throws(function () {
  16. new ConstantExpression();
  17. });
  18. },
  19. "should not accept 2 arguments": function () {
  20. assert.throws(function () {
  21. new ConstantExpression(1, 2);
  22. });
  23. }
  24. },
  25. ".parse()": {
  26. "should create an expression from a json element": function testCreateFromBsonElement() {
  27. var idGenerator = new VariablesIdGenerator(),
  28. vps = new VariablesParseState(idGenerator),
  29. expression = ConstantExpression.parse("foo", vps);
  30. assert.deepEqual("foo", expression.evaluate({}));
  31. }
  32. },
  33. ".create()": {
  34. "should create an expression": function testCreate() {
  35. assert(ConstantExpression.create() instanceof ConstantExpression);
  36. }
  37. //SKIPPED: testCreateFronBsonElement
  38. },
  39. "#optimize()": {
  40. "should not optimize anything": function testOptimize() {
  41. var expr = new ConstantExpression(5);
  42. assert.strictEqual(expr, expr.optimize());
  43. }
  44. },
  45. "#addDependencies()": {
  46. "should return nothing": function testDependencies() {
  47. var expr = ConstantExpression.create(5),
  48. deps = new DepsTracker();
  49. expr.addDependencies(deps);
  50. assert.deepEqual(deps.fields, {});
  51. assert.strictEqual(deps.needWholeDocument, false);
  52. assert.strictEqual(deps.needTextScore, false);
  53. }
  54. },
  55. //TODO: AddToBsonObj
  56. //TODO: AddToBsonArray
  57. "#evaluate()": {
  58. "should do what comes natural with an int": function () {
  59. var c = 567;
  60. var expr = new ConstantExpression(c);
  61. assert.deepEqual(expr.evaluate(), c);
  62. },
  63. "should do what comes natural with a float": function () {
  64. var c = 567.123;
  65. var expr = new ConstantExpression(c);
  66. assert.deepEqual(expr.evaluate(), c);
  67. },
  68. "should do what comes natural with a String": function () {
  69. var c = "Quoth the raven";
  70. var expr = new ConstantExpression(c);
  71. assert.deepEqual(expr.evaluate(), c);
  72. },
  73. "should do what comes natural with a date": function () {
  74. var c = new Date();
  75. var expr = new ConstantExpression(c);
  76. assert.deepEqual(expr.evaluate(), c);
  77. }
  78. }
  79. };