NotMatchExpression_test.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. ErrorCodes = require("../../../../lib/Errors").ErrorCodes,
  5. matcher = require("../../../../lib/pipeline/matcher/"),
  6. NotMatchExpression = matcher.NotMatchExpression,
  7. LTMatchExpression = matcher.LTMatchExpression,
  8. MatchDetails = matcher.MatchDetails;
  9. module.exports = {
  10. "NotMatchExpression": {
  11. "Should match a scalar": function (){
  12. var lt = new LTMatchExpression();
  13. assert.strictEqual(lt.init("a", 5).code, ErrorCodes.OK);
  14. var op = new NotMatchExpression();
  15. assert.strictEqual( op.init(lt).code, ErrorCodes.OK);
  16. assert.ok( op.matches({"a":6}), "{$not: {$lt: 5}}, {a:6}" );
  17. assert.ok( !op.matches({"a":4}), "{$not: {$lt: 5}}, {a:4}" );
  18. },
  19. "Should match an Array": function() {
  20. var lt = new LTMatchExpression();
  21. assert.strictEqual(lt.init("a",5).code, ErrorCodes.OK);
  22. var op = new NotMatchExpression();
  23. assert.strictEqual(op.init(lt).code, ErrorCodes.OK);
  24. assert.ok( op.matches({"a": [6]}) , "{$not: {$lt: 5}}, {a: [6]}");
  25. assert.ok( !op.matches({"a": [4]}) , "{$not: {$lt: 5}}, {a: [4]}");
  26. assert.ok( !op.matches({"a": [4,5,6]}) , "{$not: {$lt: 5}}, {a: [4,5,6]}");
  27. },
  28. "Should not have an ElemMatchKey": function() {
  29. var lt = new LTMatchExpression();
  30. assert.strictEqual(lt.init("a",5).code, ErrorCodes.OK);
  31. var op = new NotMatchExpression();
  32. assert.strictEqual( op.init( lt ).code, ErrorCodes.OK);
  33. var details = new MatchDetails();
  34. details.requestElemMatchKey();
  35. assert.ok( ! op.matches({"a":[1]}, details), "{$not: {a: {$lt : 5}}}, {a: [1]}" );
  36. assert.ok( ! details.hasElemMatchKey() , "ElemMatchKey Check");
  37. assert.ok( op.matches({"a": 6 }, details), "{$not: {a: {$lt : 5}}},{a: 6}");
  38. assert.ok( ! details.hasElemMatchKey(), "ElemMatchKey Check");
  39. assert.ok( op.matches({"a":[6]}, details), "{$not: {a: {$lt : 5}}}, {a:[6]}");
  40. assert.ok( ! details.hasElemMatchKey() );
  41. }
  42. }
  43. };