ConstantExpression_test.js 2.7 KB

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