0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-30 17:10:48 +01:00
mongodb/dbtests/matchertests.cpp

138 lines
3.8 KiB
C++
Raw Normal View History

// 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"
#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 );
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 );
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() );
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 );
BSONObjBuilder b;
2009-02-09 21:38:26 +01:00
b.append( "a", 5 );
ASSERT( m.matches( b.done() ) );
}
};
class MixedNumericIN {
public:
void run(){
BSONObj query = fromjson( "{ a : { $in : [4,6] } }" );
ASSERT_EQUALS( 4 , query["a"].embeddedObject()["$in"].embeddedObject()["0"].number() );
ASSERT_EQUALS( NumberInt , query["a"].embeddedObject()["$in"].embeddedObject()["0"].type() );
2010-01-19 17:04:52 +01:00
Matcher m( query );
{
BSONObjBuilder b;
b.append( "a" , 4.0 );
ASSERT( m.matches( b.done() ) );
}
{
BSONObjBuilder b;
b.append( "a" , 5 );
ASSERT( ! m.matches( b.done() ) );
}
{
BSONObjBuilder b;
b.append( "a" , 4 );
ASSERT( m.matches( b.done() ) );
}
}
};
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-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
}
};
class All : public Suite {
public:
2009-09-17 23:23:38 +02:00
All() : Suite( "matcher" ){
}
void setupTests(){
add< Basic >();
add< DoubleEqual >();
add< MixedNumericEqual >();
add< MixedNumericGt >();
add< MixedNumericIN >();
2009-03-25 19:47:04 +01:00
add< Size >();
add< MixedNumericEmbedded >();
}
2009-09-17 23:23:38 +02:00
} dball;
} // namespace MatcherTests