2009-01-20 21:44:40 +01:00
|
|
|
// matchertests.cpp : matcher unit tests
|
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copyright (C) 2008 10gen Inc.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-09-30 20:32:17 +02:00
|
|
|
#include "stdafx.h"
|
2009-01-20 21:44:40 +01:00
|
|
|
#include "../db/matcher.h"
|
|
|
|
|
|
|
|
#include "../db/json.h"
|
|
|
|
|
|
|
|
#include "dbtests.h"
|
|
|
|
|
|
|
|
namespace MatcherTests {
|
|
|
|
|
|
|
|
class Basic {
|
|
|
|
public:
|
|
|
|
void run() {
|
|
|
|
BSONObj query = fromjson( "{\"a\":\"b\"}" );
|
2010-01-19 17:04:52 +01:00
|
|
|
Matcher m( query );
|
2009-01-20 21:44:40 +01:00
|
|
|
ASSERT( m.matches( fromjson( "{\"a\":\"b\"}" ) ) );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class DoubleEqual {
|
|
|
|
public:
|
|
|
|
void run() {
|
|
|
|
BSONObj query = fromjson( "{\"a\":5}" );
|
2010-01-19 17:04:52 +01:00
|
|
|
Matcher m( query );
|
2009-01-20 21:44:40 +01:00
|
|
|
ASSERT( m.matches( fromjson( "{\"a\":5}" ) ) );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class MixedNumericEqual {
|
|
|
|
public:
|
|
|
|
void run() {
|
|
|
|
BSONObjBuilder query;
|
2009-02-09 21:38:26 +01:00
|
|
|
query.append( "a", 5 );
|
2010-01-19 17:04:52 +01:00
|
|
|
Matcher m( query.done() );
|
2009-01-20 21:44:40 +01:00
|
|
|
ASSERT( m.matches( fromjson( "{\"a\":5}" ) ) );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class MixedNumericGt {
|
|
|
|
public:
|
|
|
|
void run() {
|
|
|
|
BSONObj query = fromjson( "{\"a\":{\"$gt\":4}}" );
|
2010-01-19 17:04:52 +01:00
|
|
|
Matcher m( query );
|
2009-01-20 21:44:40 +01:00
|
|
|
BSONObjBuilder b;
|
2009-02-09 21:38:26 +01:00
|
|
|
b.append( "a", 5 );
|
2009-01-20 21:44:40 +01:00
|
|
|
ASSERT( m.matches( b.done() ) );
|
|
|
|
}
|
|
|
|
};
|
2009-06-24 20:03:35 +02:00
|
|
|
|
|
|
|
class MixedNumericIN {
|
|
|
|
public:
|
|
|
|
void run(){
|
|
|
|
BSONObj query = fromjson( "{ a : { $in : [4,6] } }" );
|
|
|
|
ASSERT_EQUALS( 4 , query["a"].embeddedObject()["$in"].embeddedObject()["0"].number() );
|
2009-11-30 21:10:14 +01:00
|
|
|
ASSERT_EQUALS( NumberInt , query["a"].embeddedObject()["$in"].embeddedObject()["0"].type() );
|
2009-06-24 20:03:35 +02:00
|
|
|
|
2010-01-19 17:04:52 +01:00
|
|
|
Matcher m( query );
|
2009-06-24 20:03:35 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
BSONObjBuilder b;
|
|
|
|
b.append( "a" , 4.0 );
|
|
|
|
ASSERT( m.matches( b.done() ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
BSONObjBuilder b;
|
|
|
|
b.append( "a" , 5 );
|
|
|
|
ASSERT( ! m.matches( b.done() ) );
|
|
|
|
}
|
|
|
|
|
2009-01-20 21:44:40 +01:00
|
|
|
|
2009-06-24 20:03:35 +02:00
|
|
|
{
|
|
|
|
BSONObjBuilder b;
|
|
|
|
b.append( "a" , 4 );
|
|
|
|
ASSERT( m.matches( b.done() ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-03-31 18:27:10 +02:00
|
|
|
class MixedNumericEmbedded {
|
|
|
|
public:
|
|
|
|
void run(){
|
|
|
|
Matcher m( BSON( "a" << BSON( "x" << 1 ) ) );
|
|
|
|
ASSERT( m.matches( BSON( "a" << BSON( "x" << 1 ) ) ) );
|
|
|
|
ASSERT( m.matches( BSON( "a" << BSON( "x" << 1.0 ) ) ) );
|
|
|
|
}
|
|
|
|
};
|
2009-06-24 20:03:35 +02:00
|
|
|
|
2009-03-25 19:47:04 +01:00
|
|
|
class Size {
|
|
|
|
public:
|
|
|
|
void run() {
|
2010-01-19 17:04:52 +01:00
|
|
|
Matcher m( fromjson( "{a:{$size:4}}" ) );
|
2009-03-25 19:47:04 +01:00
|
|
|
ASSERT( m.matches( fromjson( "{a:[1,2,3,4]}" ) ) );
|
|
|
|
ASSERT( !m.matches( fromjson( "{a:[1,2,3]}" ) ) );
|
|
|
|
ASSERT( !m.matches( fromjson( "{a:[1,2,3,'a','b']}" ) ) );
|
2009-04-06 17:27:43 +02:00
|
|
|
ASSERT( !m.matches( fromjson( "{a:[[1,2,3,4]]}" ) ) );
|
2009-03-25 19:47:04 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-06-24 20:03:35 +02:00
|
|
|
|
2009-05-12 22:37:23 +02:00
|
|
|
class All : public Suite {
|
2009-01-20 21:44:40 +01:00
|
|
|
public:
|
2009-09-17 23:23:38 +02:00
|
|
|
All() : Suite( "matcher" ){
|
|
|
|
}
|
|
|
|
|
|
|
|
void setupTests(){
|
2009-01-20 21:44:40 +01:00
|
|
|
add< Basic >();
|
|
|
|
add< DoubleEqual >();
|
|
|
|
add< MixedNumericEqual >();
|
|
|
|
add< MixedNumericGt >();
|
2009-06-24 20:03:35 +02:00
|
|
|
add< MixedNumericIN >();
|
2009-03-25 19:47:04 +01:00
|
|
|
add< Size >();
|
2010-03-31 18:27:10 +02:00
|
|
|
add< MixedNumericEmbedded >();
|
2009-01-20 21:44:40 +01:00
|
|
|
}
|
2009-09-17 23:23:38 +02:00
|
|
|
} dball;
|
2009-01-20 21:44:40 +01:00
|
|
|
|
|
|
|
} // namespace MatcherTests
|
|
|
|
|