Browse Source

EAGLESIX-2997 Got GTEMatchExpression working

Jared Hall 11 years ago
parent
commit
85b2ca2feb

+ 2 - 1
lib/pipeline/matcher/ComparisonMatchExpression.js

@@ -121,6 +121,8 @@ proto.matchesSingleElement = function matchesSingleElement(e) {
 		if (["MaxKey","MinKey"].indexOf(Value.getType(this._rhs)) != -1) {
 			return this._matchType !== "EQ";
 		}
+
+		return false;
 	}
 
 	var x = Value.compare(e, this._rhs);
@@ -139,5 +141,4 @@ proto.matchesSingleElement = function matchesSingleElement(e) {
 		default:
 			throw new Error("Invalid comparison type evaluated.");
 	}
-	return false;
 };

+ 66 - 31
test/lib/pipeline/matcher/GTEMatchExpression.js

@@ -1,6 +1,7 @@
 "use strict";
 var assert = require("assert"),
-	MatchDetails = require('../../../../lib/pipeline/matcher/MatchDetails'),
+	BSON = require("bson"),
+	MatchDetails = require("../../../../lib/pipeline/matcher/MatchDetails"),
 	GTEMatchExpression = require("../../../../lib/pipeline/matcher/GTEMatchExpression");
 
 
@@ -8,68 +9,102 @@ module.exports = {
 	"GTEMatchExpression": {
 		"should match scalars and strings properly": function (){
 			var e = new GTEMatchExpression();
-			var s = e.init('x',5);
+			var s = e.init("",5);
 
-			assert.strictEqual(s.code, 'OK');
-			assert.ok( e.matches({'x':5}) );
-			assert.ok( ! e.matches({'x':4}) );
-			assert.ok( e.matches({'x':6}) );
-			assert.ok( ! e.matches({'x': 'eliot'}) );
+			assert.strictEqual(s.code, "OK");
+			assert.ok( e.matchesSingleElement(5.5) );
+			assert.ok( e.matchesSingleElement(5) );
+			assert.ok( ! e.matchesSingleElement(4) );
+			assert.ok( ! e.matchesSingleElement( "foo" ) );
 		},
 		"should handle invalid End of Object Operand": function testInvalidEooOperand(){
 			var e = new GTEMatchExpression();
-			var s = e.init('',{});
+			var s = e.init("",{});
 
-			assert.strictEqual(s.code, 'BAD_VALUE');
+			assert.strictEqual(s.code, "BAD_VALUE");
 		},
 		"should match a pathed number":function() {
 			var e = new GTEMatchExpression();
-			var s = e.init('a',5);
+			var s = e.init("a",5);
 
-			assert.strictEqual(s.code, 'OK');
-			assert.ok( e.matches({'a':5.5}) );
-			assert.ok( ! e.matches({'a':4}) );
+			assert.strictEqual(s.code, "OK");
+			assert.ok( e.matchesJSON({"a":5.5}) );
+			assert.ok( ! e.matchesJSON({"a":4}) );
 		},
 		"should match stuff in an array": function() {
 			var e = new GTEMatchExpression();
-			var s = e.init('a',5);
+			var s = e.init("a",5);
 
-			assert.strictEqual(s.code, 'OK');
-			assert.ok( e.matches({'a':[4,5.5]}) );
-			assert.ok( ! e.matches({'a':[1,2]}) );
+			assert.strictEqual(s.code, "OK");
+			assert.ok( e.matchesJSON({"a":[4,5.5]}) );
+			assert.ok( ! e.matchesJSON({"a":[1,2]}) );
 		},
 		"should not match full array" : function() {
 			var e = new GTEMatchExpression();
-			var s = e.init('a',[5]);
+			var s = e.init("a",[5]);
 
-			assert.strictEqual(s.code, 'OK');
-			assert.ok( e.matches({'a':[6]}) );
+			assert.strictEqual(s.code, "OK");
+			assert.ok( ! e.matchesJSON({"a":[4]}) );
+			assert.ok( e.matchesJSON({"a":[5]}) );
+			assert.ok( e.matchesJSON({"a":[6]}) );
 		},
 		"should not match null" : function() {
 			var e = new GTEMatchExpression();
-			var s = e.init('a',null);
+			var s = e.init("a",null);
 
-			assert.strictEqual(s.code, 'OK');
-			assert.ok( e.matches({}) );
-			assert.ok( e.matches({'a':null}) );
-			assert.ok( ! e.matches({'a':4}) );
+			assert.strictEqual(s.code, "OK");
+			assert.ok( e.matchesJSON({}) );
+			assert.ok( e.matchesJSON({"a":null}) );
+			assert.ok( ! e.matchesJSON({"a":4}) );
+			assert.ok( e.matchesJSON({"b":4}) );
+		},
+		"should match dot notation nulls": function() {
+			var e = new GTEMatchExpression();
+			var s = e.init("a.b",null);
+
+			assert.strictEqual(s.code, "OK");
+			assert.ok(e.matchesJSON({}));
+			assert.ok(e.matchesJSON({a:null}));
+			assert.ok(e.matchesJSON({a:{}}));
+			assert.ok(e.matchesJSON({a:[{b: null}]}));
+			assert.ok(e.matchesJSON({a:[{a:4}, {b:4}]}));
+			assert.ok(!e.matchesJSON({a:[4]}));
+			assert.ok(!e.matchesJSON({a:[{b:4}]}));
+		},
+		"should match MinKey": function (){
+			var operand = {a:new BSON.MinKey()},
+				e = new GTEMatchExpression();
+			var s = e.init("a",operand.a);
+			assert.strictEqual(s.code, "OK");
+			assert.ok(e.matchesJSON({"a":new BSON.MinKey()}, null));
+			assert.ok(e.matchesJSON({"a":new BSON.MaxKey()}, null));
+			assert.ok(e.matchesJSON({"a":4}), null);
+		},
+		"should match MaxKey": function (){
+			var operand = {a:new BSON.MaxKey()},
+				e = new GTEMatchExpression();
+			var s = e.init("a",operand.a);
+			assert.strictEqual(s.code, "OK");
+			assert.ok(e.matchesJSON({"a":new BSON.MaxKey()}, null));
+			assert.ok(!e.matchesJSON({"a":new BSON.MinKey()}, null));
+			assert.ok(!e.matchesJSON({"a":4}), null);
 		},
 		"should handle elemMatchKey":function() {
 			var e = new GTEMatchExpression();
-			var s = e.init('a',5);
+			var s = e.init("a",5);
 			var m = new MatchDetails();
 			m.requestElemMatchKey();
-			assert.strictEqual( s.code, 'OK' );
+			assert.strictEqual( s.code, "OK" );
 
-			assert.ok( ! e.matches({'a':4}, m) );
+			assert.ok( ! e.matchesJSON({"a":4}, m) );
 			assert.ok( ! m.hasElemMatchKey() );
 
-			assert.ok( e.matches({'a':6}, m) );
+			assert.ok( e.matchesJSON({"a":6}, m) );
 			assert.ok( ! m.hasElemMatchKey() );
 
-			assert.ok( e.matches({'a':[2,6,5]}, m));
+			assert.ok( e.matchesJSON({"a":[2,6,5]}, m));
 			assert.ok( m.hasElemMatchKey());
-			assert.strictEqual('1', m.elemMatchKey());
+			assert.strictEqual("1", m.elemMatchKey());
 		}
 	}
 };

+ 51 - 34
test/lib/pipeline/matcher/LTEMatchExpression.js

@@ -1,75 +1,92 @@
 "use strict";
 var assert = require("assert"),
-	MatchDetails = require('../../../../lib/pipeline/matcher/MatchDetails'),
+	MatchDetails = require("../../../../lib/pipeline/matcher/MatchDetails"),
 	LTEMatchExpression = require("../../../../lib/pipeline/matcher/LTEMatchExpression");
 
 
 module.exports = {
 	"LTEMatchExpression": {
-		"should match scalars and strings properly": function (){
-			var e = new LTEMatchExpression();
-			var s = e.init('x',5);
-			
-			assert.strictEqual(s.code, 'OK');
-			assert.ok( e.matches({'x':5}) );
-			assert.ok( e.matches({'x':4}) );
-			assert.ok( ! e.matches({'x':6}) );
-			assert.ok( ! e.matches({'x': 'eliot'}) );
+		"should match element": function (){
+			var operand = {$lte:5},
+				match = {a:4.5},
+				equalMatch = {a:5},
+				notMatch = {a:6},
+				notMatchWrongType = {a:"foo"},
+				lte = new LTEMatchExpression();
+			var s = lte.init("",operand.$lte);
+			assert.strictEqual(s.code, "OK");
+			assert.ok(lte.matchesSingleElement(match));
+			assert.ok(lte.matchesSingleElement(equalMatch));
+			assert.ok(!lte.matchesSingleElement(notMatch));
+			assert.ok(!lte.matchesSingleElement(notMatchWrongType));
+		},
+		"should not work for invalid eoo operand": function(){
+			var operand = {},
+				lte = new LTEMatchExpression();
+			assert.ok(lte.init("", operand).code !== "OK");
+		},
+		"should match scalars properly": function (){
+			var operand = {$lte:5},
+				lte = new LTEMatchExpression();
+			var s = lte.init("a",operand.$lte);
+			assert.strictEqual(s.code, "OK");
+			assert.ok(lte.matchesJSON({"a":4.5}, null));
+			assert.ok(!lte.matchesJSON({"a":6}), null);
 		},
 		"should handle invalid End of Object Operand": function testInvalidEooOperand(){
 			var e = new LTEMatchExpression();
-			var s = e.init('',{});
+			var s = e.init("",{});
 
-			assert.strictEqual(s.code, 'BAD_VALUE');
+			assert.strictEqual(s.code, "BAD_VALUE");
 		},
 		"should match a pathed number":function() {
 			var e = new LTEMatchExpression();
-			var s = e.init('a',5);
+			var s = e.init("a",5);
 
-			assert.strictEqual(s.code, 'OK');
-			assert.ok( e.matches({'a':4.5}) );
-			assert.ok( ! e.matches({'a':6}) );
+			assert.strictEqual(s.code, "OK");
+			assert.ok( e.matches({"a":4.5}) );
+			assert.ok( ! e.matches({"a":6}) );
 		},
 		"should match stuff in an array": function() {
 			var e = new LTEMatchExpression();
-			var s = e.init('a',5);
+			var s = e.init("a",5);
 
-			assert.strictEqual(s.code, 'OK');
-			assert.ok( e.matches({'a':[6,4.5]}) );
-			assert.ok( ! e.matches({'a':[6,7]}) );
+			assert.strictEqual(s.code, "OK");
+			assert.ok( e.matches({"a":[6,4.5]}) );
+			assert.ok( ! e.matches({"a":[6,7]}) );
 		},
 		"should not match full array" : function() {
 			var e = new LTEMatchExpression();
-			var s = e.init('a',[5]);
+			var s = e.init("a",[5]);
 
-			assert.strictEqual(s.code, 'OK');
-			assert.ok(e.matches({'a':[4]}) );
+			assert.strictEqual(s.code, "OK");
+			assert.ok(e.matches({"a":[4]}) );
 		},
 		"should not match null" : function() {
 			var e = new LTEMatchExpression();
-			var s = e.init('a',null);
-		
-			assert.strictEqual(s.code, 'OK');
+			var s = e.init("a",null);
+
+			assert.strictEqual(s.code, "OK");
 			assert.ok( e.matches({}) );
-			assert.ok( e.matches({'a':null}) );
-			assert.ok( ! e.matches({'a':4}) );
+			assert.ok( e.matches({"a":null}) );
+			assert.ok( ! e.matches({"a":4}) );
 		},
 		"should handle elemMatchKey":function() {
 			var e = new LTEMatchExpression();
-			var s = e.init('a',5);
+			var s = e.init("a",5);
 			var m = new MatchDetails();
 			m.requestElemMatchKey();
-			assert.strictEqual( s.code, 'OK' );
+			assert.strictEqual( s.code, "OK" );
 
-			assert.ok( ! e.matches({'a':6}, m) );
+			assert.ok( ! e.matches({"a":6}, m) );
 			assert.ok( ! m.hasElemMatchKey() );
 
-			assert.ok( e.matches({'a':4}, m) );
+			assert.ok( e.matches({"a":4}, m) );
 			assert.ok( ! m.hasElemMatchKey() );
 
-			assert.ok( e.matches({'a':[6,2,5]}, m));
+			assert.ok( e.matches({"a":[6,2,5]}, m));
 			assert.ok( m.hasElemMatchKey());
-			assert.strictEqual('1', m.elemMatchKey());
+			assert.strictEqual("1", m.elemMatchKey());
 		}
 
 	}

+ 36 - 36
test/lib/pipeline/matcher/LTMatchExpression.js

@@ -1,6 +1,6 @@
 "use strict";
 var assert = require("assert"),
-	MatchDetails = require('../../../../lib/pipeline/matcher/MatchDetails'),
+	MatchDetails = require("../../../../lib/pipeline/matcher/MatchDetails"),
 	LTMatchExpression = require("../../../../lib/pipeline/matcher/LTMatchExpression");
 
 
@@ -8,76 +8,76 @@ module.exports = {
 	"LTMatchExpression": {
 		"should match scalars and strings properly": function (){
 			var e = new LTMatchExpression();
-			var s = e.init('x',5);
-			
-			assert.strictEqual(s.code, 'OK');
-			assert.ok( ! e.matches({'x':5}) );
-			assert.ok( e.matches({'x':4}) );
-			assert.ok( ! e.matches({'x':6}) );
-			assert.ok( ! e.matches({'x': 'eliot'}) );
+			var s = e.init("x",5);
+
+			assert.strictEqual(s.code, "OK");
+			assert.ok( ! e.matches({"x":5}) );
+			assert.ok( e.matches({"x":4}) );
+			assert.ok( ! e.matches({"x":6}) );
+			assert.ok( ! e.matches({"x": "eliot"}) );
 		},
 		"should handle invalid End of Object Operand": function testInvalidEooOperand(){
 			var e = new LTMatchExpression();
-			var s = e.init('',{});
+			var s = e.init("",{});
 
-			assert.strictEqual(s.code, 'BAD_VALUE');
+			assert.strictEqual(s.code, "BAD_VALUE");
 		},
 		"should match a pathed number":function() {
 			var e = new LTMatchExpression();
-			var s = e.init('a',5);
+			var s = e.init("a",5);
 
-			assert.strictEqual(s.code, 'OK');
-			assert.ok( e.matches({'a':4.5}) );
-			assert.ok( ! e.matches({'a':6}) );
+			assert.strictEqual(s.code, "OK");
+			assert.ok( e.matches({"a":4.5}) );
+			assert.ok( ! e.matches({"a":6}) );
 		},
 		"should match an empty pathed number":function() {
 			var e = new LTMatchExpression();
-			var s = e.init('',5);
+			var s = e.init("",5);
 
-			assert.strictEqual(s.code, 'OK');
-			assert.ok( e.matches({'':4.5}) );
-			assert.ok( ! e.matches({'':6}) );
+			assert.strictEqual(s.code, "OK");
+			assert.ok( e.matches({"":4.5}) );
+			assert.ok( ! e.matches({"":6}) );
 		},
 		"should match stuff in an array": function() {
 			var e = new LTMatchExpression();
-			var s = e.init('a',5);
+			var s = e.init("a",5);
 
-			assert.strictEqual(s.code, 'OK');
-			assert.ok( e.matches({'a':[6,4.5]}) );
-			assert.ok( ! e.matches({'a':[6,7]}) );
+			assert.strictEqual(s.code, "OK");
+			assert.ok( e.matches({"a":[6,4.5]}) );
+			assert.ok( ! e.matches({"a":[6,7]}) );
 		},
 		"should not match full array" : function() {
 			var e = new LTMatchExpression();
-			var s = e.init('a',[5]);
+			var s = e.init("a",[5]);
 
-			assert.strictEqual(s.code, 'OK');
-			assert.ok( e.matches({'a':[4]}) );
+			assert.strictEqual(s.code, "OK");
+			assert.ok( e.matches({"a":[4]}) );
 		},
 		"should not match null" : function() {
 			var e = new LTMatchExpression();
-			var s = e.init('a',null);
-		
-			assert.strictEqual(s.code, 'OK');
+			var s = e.init("a",null);
+
+			assert.strictEqual(s.code, "OK");
 			assert.ok( ! e.matches({}) );
-			assert.ok( ! e.matches({'a':null}) );
-			assert.ok( ! e.matches({'a':4}) );
+			assert.ok( ! e.matches({"a":null}) );
+			assert.ok( ! e.matches({"a":4}) );
 		},
 		"should handle elemMatchKey":function() {
 			var e = new LTMatchExpression();
-			var s = e.init('a',5);
+			var s = e.init("a",5);
 			var m = new MatchDetails();
 			m.requestElemMatchKey();
-			assert.strictEqual( s.code, 'OK' );
+			assert.strictEqual( s.code, "OK" );
 
-			assert.ok( ! e.matches({'a':6}, m) );
+			assert.ok( ! e.matches({"a":6}, m) );
 			assert.ok( ! m.hasElemMatchKey() );
 
-			assert.ok( e.matches({'a':4}, m) );
+			assert.ok( e.matches({"a":4}, m) );
 			assert.ok( ! m.hasElemMatchKey() );
 
-			assert.ok( e.matches({'a':[6,2,5]}, m));
+			assert.ok( e.matches({"a":[6,2,5]}, m));
 			assert.ok( m.hasElemMatchKey());
-			assert.strictEqual('1', m.elemMatchKey());
+			assert.strictEqual("1", m.elemMatchKey());
 		}