2009-03-23 23:32:05 +01:00
|
|
|
// querytests.cpp : query.{h,cpp} unit tests.
|
2009-01-20 22:54:57 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../db/query.h"
|
|
|
|
|
|
|
|
#include "../db/db.h"
|
2009-02-05 17:51:51 +01:00
|
|
|
#include "../db/instance.h"
|
2009-01-20 22:54:57 +01:00
|
|
|
#include "../db/json.h"
|
2009-02-05 17:51:51 +01:00
|
|
|
#include "../db/lasterror.h"
|
2009-01-20 22:54:57 +01:00
|
|
|
|
|
|
|
#include "dbtests.h"
|
|
|
|
|
|
|
|
namespace QueryTests {
|
|
|
|
|
|
|
|
class Base {
|
|
|
|
public:
|
|
|
|
Base() {
|
|
|
|
dblock lk;
|
|
|
|
setClient( ns() );
|
|
|
|
addIndex( fromjson( "{\"a\":1}" ) );
|
|
|
|
}
|
2009-01-21 16:36:40 +01:00
|
|
|
~Base() {
|
|
|
|
try {
|
|
|
|
auto_ptr< Cursor > c = theDataFileMgr.findAll( ns() );
|
|
|
|
vector< DiskLoc > toDelete;
|
|
|
|
for(; c->ok(); c->advance() )
|
|
|
|
toDelete.push_back( c->currLoc() );
|
|
|
|
for( vector< DiskLoc >::iterator i = toDelete.begin(); i != toDelete.end(); ++i )
|
|
|
|
theDataFileMgr.deleteRecord( ns(), i->rec(), *i, false );
|
|
|
|
} catch ( ... ) {
|
|
|
|
FAIL( "Exception while cleaning up records" );
|
|
|
|
}
|
|
|
|
}
|
2009-01-20 22:54:57 +01:00
|
|
|
protected:
|
|
|
|
static const char *ns() {
|
|
|
|
return "unittest.querytests";
|
|
|
|
}
|
|
|
|
static void addIndex( const BSONObj &key ) {
|
|
|
|
BSONObjBuilder b;
|
|
|
|
b.append( "name", "index" );
|
|
|
|
b.append( "ns", ns() );
|
|
|
|
b.append( "key", key );
|
|
|
|
BSONObj o = b.done();
|
|
|
|
stringstream indexNs;
|
|
|
|
indexNs << ns() << ".system.indexes";
|
|
|
|
theDataFileMgr.insert( indexNs.str().c_str(), o.objdata(), o.objsize() );
|
|
|
|
}
|
2009-01-21 16:36:40 +01:00
|
|
|
static void insert( const char *s ) {
|
|
|
|
insert( fromjson( s ) );
|
|
|
|
}
|
|
|
|
static void insert( const BSONObj &o ) {
|
|
|
|
theDataFileMgr.insert( ns(), o.objdata(), o.objsize() );
|
|
|
|
}
|
2009-01-20 22:54:57 +01:00
|
|
|
};
|
|
|
|
|
2009-01-21 16:36:40 +01:00
|
|
|
class CountBasic : public Base {
|
|
|
|
public:
|
|
|
|
void run() {
|
|
|
|
insert( "{\"a\":\"b\"}" );
|
|
|
|
BSONObj cmd = fromjson( "{\"query\":{}}" );
|
|
|
|
string err;
|
|
|
|
ASSERT_EQUALS( 1, runCount( ns(), cmd, err ) );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class CountQuery : public Base {
|
|
|
|
public:
|
|
|
|
void run() {
|
|
|
|
insert( "{\"a\":\"b\"}" );
|
|
|
|
insert( "{\"a\":\"b\",\"x\":\"y\"}" );
|
|
|
|
insert( "{\"a\":\"c\"}" );
|
|
|
|
BSONObj cmd = fromjson( "{\"query\":{\"a\":\"b\"}}" );
|
|
|
|
string err;
|
|
|
|
ASSERT_EQUALS( 2, runCount( ns(), cmd, err ) );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class CountFields : public Base {
|
|
|
|
public:
|
|
|
|
void run() {
|
|
|
|
insert( "{\"a\":\"b\"}" );
|
|
|
|
insert( "{\"c\":\"d\"}" );
|
|
|
|
BSONObj cmd = fromjson( "{\"query\":{},\"fields\":{\"a\":1}}" );
|
|
|
|
string err;
|
|
|
|
ASSERT_EQUALS( 1, runCount( ns(), cmd, err ) );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class CountQueryFields : public Base {
|
|
|
|
public:
|
|
|
|
void run() {
|
|
|
|
insert( "{\"a\":\"b\"}" );
|
|
|
|
insert( "{\"a\":\"c\"}" );
|
|
|
|
insert( "{\"d\":\"e\"}" );
|
|
|
|
BSONObj cmd = fromjson( "{\"query\":{\"a\":\"b\"},\"fields\":{\"a\":1}}" );
|
|
|
|
string err;
|
|
|
|
ASSERT_EQUALS( 1, runCount( ns(), cmd, err ) );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-03-02 22:03:48 +01:00
|
|
|
class CountIndexedRegex : public Base {
|
|
|
|
public:
|
|
|
|
void run() {
|
|
|
|
insert( "{\"a\":\"b\"}" );
|
|
|
|
insert( "{\"a\":\"c\"}" );
|
|
|
|
BSONObj cmd = fromjson( "{\"query\":{\"a\":/^b/}}" );
|
|
|
|
string err;
|
|
|
|
ASSERT_EQUALS( 1, runCount( ns(), cmd, err ) );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-02-05 17:51:51 +01:00
|
|
|
class ClientBase {
|
|
|
|
public:
|
|
|
|
// NOTE: Not bothering to backup the old error record.
|
|
|
|
ClientBase() {
|
|
|
|
mongo::lastError.reset( new LastError() );
|
|
|
|
}
|
|
|
|
~ClientBase() {
|
|
|
|
mongo::lastError.release();
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
static void insert( const char *ns, BSONObj o ) {
|
|
|
|
client_.insert( ns, o );
|
|
|
|
}
|
|
|
|
static void update( const char *ns, BSONObj q, BSONObj o, bool upsert = 0 ) {
|
|
|
|
client_.update( ns, Query( q ), o, upsert );
|
|
|
|
}
|
|
|
|
static bool error() {
|
|
|
|
return !client_.getPrevError().getField( "err" ).isNull();
|
|
|
|
}
|
2009-02-27 20:04:19 +01:00
|
|
|
DBDirectClient &client() const { return client_; }
|
2009-02-05 17:51:51 +01:00
|
|
|
private:
|
|
|
|
static DBDirectClient client_;
|
|
|
|
};
|
|
|
|
DBDirectClient ClientBase::client_;
|
|
|
|
|
2009-02-27 20:04:19 +01:00
|
|
|
class BoundedKey : public ClientBase {
|
|
|
|
public:
|
|
|
|
void run() {
|
|
|
|
const char *ns = "querytests.BoundedKey";
|
|
|
|
insert( ns, BSON( "a" << 1 ) );
|
|
|
|
BSONObjBuilder a;
|
|
|
|
a.appendMaxKey( "$lt" );
|
|
|
|
BSONObj limit = a.done();
|
|
|
|
ASSERT( !client().findOne( ns, QUERY( "a" << limit ) ).isEmpty() );
|
|
|
|
client().ensureIndex( ns, BSON( "a" << 1 ) );
|
|
|
|
ASSERT( !client().findOne( ns, QUERY( "a" << limit ).hint( BSON( "a" << 1 ) ) ).isEmpty() );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-03-10 21:49:17 +01:00
|
|
|
class GetMore : public ClientBase {
|
|
|
|
public:
|
2009-03-11 16:58:26 +01:00
|
|
|
~GetMore() {
|
|
|
|
client().dropCollection( "querytests.GetMore" );
|
|
|
|
}
|
2009-03-10 21:49:17 +01:00
|
|
|
void run() {
|
|
|
|
const char *ns = "querytests.GetMore";
|
|
|
|
insert( ns, BSON( "a" << 1 ) );
|
|
|
|
insert( ns, BSON( "a" << 2 ) );
|
|
|
|
insert( ns, BSON( "a" << 3 ) );
|
2009-03-19 21:23:04 +01:00
|
|
|
auto_ptr< DBClientCursor > cursor = client().query( ns, BSONObj(), 2 );
|
2009-03-10 21:49:17 +01:00
|
|
|
long long cursorId = cursor->getCursorId();
|
|
|
|
cursor->decouple();
|
|
|
|
cursor.reset();
|
|
|
|
cursor = client().getMore( ns, cursorId );
|
|
|
|
ASSERT( cursor->more() );
|
|
|
|
ASSERT_EQUALS( 3, cursor->next().getIntField( "a" ) );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-03-11 16:58:26 +01:00
|
|
|
class ReturnOneOfManyAndTail : public ClientBase {
|
|
|
|
public:
|
|
|
|
~ReturnOneOfManyAndTail() {
|
|
|
|
client().dropCollection( "querytests.ReturnOneOfManyAndTail" );
|
|
|
|
}
|
|
|
|
void run() {
|
|
|
|
const char *ns = "querytests.ReturnOneOfManyAndTail";
|
|
|
|
insert( ns, BSON( "a" << 0 ) );
|
|
|
|
insert( ns, BSON( "a" << 1 ) );
|
|
|
|
insert( ns, BSON( "a" << 2 ) );
|
|
|
|
auto_ptr< DBClientCursor > c = client().query( ns, QUERY( "a" << GT << 0 ).hint( BSON( "$natural" << 1 ) ), 1, 0, 0, Option_CursorTailable );
|
2009-03-11 22:05:50 +01:00
|
|
|
// If only one result requested, a cursor is not saved.
|
2009-03-11 16:58:26 +01:00
|
|
|
ASSERT_EQUALS( 0, c->getCursorId() );
|
|
|
|
ASSERT( c->more() );
|
|
|
|
ASSERT_EQUALS( 1, c->next().getIntField( "a" ) );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-03-11 22:05:50 +01:00
|
|
|
class TailNotAtEnd : public ClientBase {
|
|
|
|
public:
|
|
|
|
~TailNotAtEnd() {
|
|
|
|
client().dropCollection( "querytests.TailNotAtEnd" );
|
|
|
|
}
|
|
|
|
void run() {
|
|
|
|
const char *ns = "querytests.TailNotAtEnd";
|
|
|
|
insert( ns, BSON( "a" << 0 ) );
|
|
|
|
insert( ns, BSON( "a" << 1 ) );
|
|
|
|
insert( ns, BSON( "a" << 2 ) );
|
|
|
|
auto_ptr< DBClientCursor > c = client().query( ns, Query().hint( BSON( "$natural" << 1 ) ), 2, 0, 0, Option_CursorTailable );
|
|
|
|
ASSERT( 0 != c->getCursorId() );
|
|
|
|
while( c->more() )
|
|
|
|
c->next();
|
|
|
|
ASSERT( 0 != c->getCursorId() );
|
|
|
|
insert( ns, BSON( "a" << 3 ) );
|
|
|
|
insert( ns, BSON( "a" << 4 ) );
|
|
|
|
insert( ns, BSON( "a" << 5 ) );
|
|
|
|
insert( ns, BSON( "a" << 6 ) );
|
|
|
|
ASSERT( c->more() );
|
|
|
|
ASSERT_EQUALS( 3, c->next().getIntField( "a" ) );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class EmptyTail : public ClientBase {
|
|
|
|
public:
|
|
|
|
~EmptyTail() {
|
|
|
|
client().dropCollection( "querytests.EmptyTail" );
|
|
|
|
}
|
|
|
|
void run() {
|
|
|
|
const char *ns = "querytests.EmptyTail";
|
|
|
|
ASSERT_EQUALS( 0, client().query( ns, Query().hint( BSON( "$natural" << 1 ) ), 2, 0, 0, Option_CursorTailable )->getCursorId() );
|
|
|
|
insert( ns, BSON( "a" << 0 ) );
|
|
|
|
ASSERT( 0 != client().query( ns, QUERY( "a" << 1 ).hint( BSON( "$natural" << 1 ) ), 2, 0, 0, Option_CursorTailable )->getCursorId() );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-03-11 22:19:08 +01:00
|
|
|
class TailableDelete : public ClientBase {
|
|
|
|
public:
|
|
|
|
~TailableDelete() {
|
|
|
|
client().dropCollection( "querytests.TailableDelete" );
|
|
|
|
}
|
|
|
|
void run() {
|
|
|
|
const char *ns = "querytests.TailableDelete";
|
|
|
|
insert( ns, BSON( "a" << 0 ) );
|
|
|
|
insert( ns, BSON( "a" << 1 ) );
|
|
|
|
auto_ptr< DBClientCursor > c = client().query( ns, Query().hint( BSON( "$natural" << 1 ) ), 2, 0, 0, Option_CursorTailable );
|
|
|
|
c->next();
|
|
|
|
c->next();
|
|
|
|
ASSERT( !c->more() );
|
|
|
|
client().remove( ns, QUERY( "a" << 1 ) );
|
|
|
|
insert( ns, BSON( "a" << 2 ) );
|
|
|
|
ASSERT( !c->more() );
|
|
|
|
ASSERT_EQUALS( 0, c->getCursorId() );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class TailableInsertDelete : public ClientBase {
|
|
|
|
public:
|
|
|
|
~TailableInsertDelete() {
|
|
|
|
client().dropCollection( "querytests.TailableInsertDelete" );
|
|
|
|
}
|
|
|
|
void run() {
|
|
|
|
const char *ns = "querytests.TailableInsertDelete";
|
|
|
|
insert( ns, BSON( "a" << 0 ) );
|
|
|
|
insert( ns, BSON( "a" << 1 ) );
|
|
|
|
auto_ptr< DBClientCursor > c = client().query( ns, Query().hint( BSON( "$natural" << 1 ) ), 2, 0, 0, Option_CursorTailable );
|
|
|
|
c->next();
|
|
|
|
c->next();
|
|
|
|
ASSERT( !c->more() );
|
|
|
|
insert( ns, BSON( "a" << 2 ) );
|
|
|
|
client().remove( ns, QUERY( "a" << 1 ) );
|
|
|
|
ASSERT( c->more() );
|
|
|
|
ASSERT_EQUALS( 2, c->next().getIntField( "a" ) );
|
|
|
|
ASSERT( !c->more() );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-03-18 22:50:45 +01:00
|
|
|
class OplogReplayMode : public ClientBase {
|
|
|
|
public:
|
|
|
|
~OplogReplayMode() {
|
|
|
|
client().dropCollection( "querytests.OplogReplayMode" );
|
|
|
|
}
|
|
|
|
void run() {
|
|
|
|
const char *ns = "querytests.OplogReplayMode";
|
|
|
|
insert( ns, BSON( "a" << 3 ) );
|
|
|
|
insert( ns, BSON( "a" << 0 ) );
|
|
|
|
insert( ns, BSON( "a" << 1 ) );
|
|
|
|
insert( ns, BSON( "a" << 2 ) );
|
|
|
|
auto_ptr< DBClientCursor > c = client().query( ns, QUERY( "a" << GT << 1 ).hint( BSON( "$natural" << 1 ) ), 0, 0, 0, Option_OplogReplay );
|
|
|
|
ASSERT( c->more() );
|
|
|
|
ASSERT_EQUALS( 2, c->next().getIntField( "a" ) );
|
|
|
|
ASSERT( !c->more() );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-04-06 21:07:20 +02:00
|
|
|
class BasicCount : public ClientBase {
|
|
|
|
public:
|
|
|
|
~BasicCount() {
|
|
|
|
client().dropCollection( "querytests.BasicCount" );
|
|
|
|
}
|
|
|
|
void run() {
|
|
|
|
const char *ns = "querytests.BasicCount";
|
|
|
|
client().ensureIndex( ns, BSON( "a" << 1 ) );
|
|
|
|
count( 0 );
|
|
|
|
insert( ns, BSON( "a" << 3 ) );
|
|
|
|
count( 0 );
|
|
|
|
insert( ns, BSON( "a" << 4 ) );
|
|
|
|
count( 1 );
|
|
|
|
insert( ns, BSON( "a" << 5 ) );
|
|
|
|
count( 1 );
|
|
|
|
insert( ns, BSON( "a" << 4 ) );
|
|
|
|
count( 2 );
|
|
|
|
}
|
|
|
|
private:
|
2009-04-06 21:19:30 +02:00
|
|
|
void count( unsigned long long c ) const {
|
2009-04-06 21:07:20 +02:00
|
|
|
ASSERT_EQUALS( c, client().count( "querytests.BasicCount", BSON( "a" << 4 ) ) );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-04-07 17:02:29 +02:00
|
|
|
class ArrayId : public ClientBase {
|
|
|
|
public:
|
|
|
|
~ArrayId() {
|
|
|
|
client().dropCollection( "querytests.ArrayId" );
|
|
|
|
}
|
|
|
|
void run() {
|
|
|
|
const char *ns = "querytests.ArrayId";
|
|
|
|
client().ensureIndex( ns, BSON( "_id" << 1 ) );
|
|
|
|
ASSERT( !error() );
|
|
|
|
client().insert( ns, fromjson( "{'_id':[1,2]}" ) );
|
|
|
|
ASSERT( error() );
|
|
|
|
}
|
|
|
|
};
|
2009-04-08 16:45:51 +02:00
|
|
|
|
|
|
|
class UnderscoreNs : public ClientBase {
|
|
|
|
public:
|
|
|
|
~UnderscoreNs() {
|
|
|
|
client().dropCollection( "querytests._UnderscoreNs" );
|
|
|
|
}
|
|
|
|
void run() {
|
|
|
|
const char *ns = "querytests._UnderscoreNs";
|
|
|
|
ASSERT( client().findOne( ns, "{}" ).isEmpty() );
|
|
|
|
client().insert( ns, BSON( "a" << 1 ) );
|
|
|
|
ASSERT_EQUALS( 1, client().findOne( ns, "{}" ).getIntField( "a" ) );
|
|
|
|
ASSERT( !error() );
|
|
|
|
}
|
|
|
|
};
|
2009-04-08 20:01:48 +02:00
|
|
|
|
|
|
|
class EmptyFieldSpec : public ClientBase {
|
|
|
|
public:
|
|
|
|
~EmptyFieldSpec() {
|
|
|
|
client().dropCollection( "querytests.EmptyFieldSpec" );
|
|
|
|
}
|
|
|
|
void run() {
|
|
|
|
const char *ns = "querytests.EmptyFieldSpec";
|
|
|
|
client().insert( ns, BSON( "a" << 1 ) );
|
|
|
|
ASSERT( !client().findOne( ns, "" ).isEmpty() );
|
|
|
|
BSONObj empty;
|
|
|
|
ASSERT( !client().findOne( ns, "", &empty ).isEmpty() );
|
|
|
|
}
|
|
|
|
};
|
2009-04-07 17:02:29 +02:00
|
|
|
|
2009-01-20 22:54:57 +01:00
|
|
|
class All : public UnitTest::Suite {
|
|
|
|
public:
|
|
|
|
All() {
|
2009-01-21 16:36:40 +01:00
|
|
|
add< CountBasic >();
|
|
|
|
add< CountQuery >();
|
|
|
|
add< CountFields >();
|
|
|
|
add< CountQueryFields >();
|
2009-03-02 22:03:48 +01:00
|
|
|
add< CountIndexedRegex >();
|
2009-02-27 20:04:19 +01:00
|
|
|
add< BoundedKey >();
|
2009-03-10 21:49:17 +01:00
|
|
|
add< GetMore >();
|
2009-03-11 16:58:26 +01:00
|
|
|
add< ReturnOneOfManyAndTail >();
|
2009-03-11 22:05:50 +01:00
|
|
|
add< TailNotAtEnd >();
|
|
|
|
add< EmptyTail >();
|
2009-03-11 22:19:08 +01:00
|
|
|
add< TailableDelete >();
|
|
|
|
add< TailableInsertDelete >();
|
2009-03-18 22:50:45 +01:00
|
|
|
add< OplogReplayMode >();
|
2009-04-07 17:02:29 +02:00
|
|
|
add< ArrayId >();
|
2009-04-08 16:45:51 +02:00
|
|
|
add< UnderscoreNs >();
|
2009-04-08 20:01:48 +02:00
|
|
|
add< EmptyFieldSpec >();
|
2009-01-20 22:54:57 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace QueryTests
|
|
|
|
|
|
|
|
UnitTest::TestPtr queryTests() {
|
|
|
|
return UnitTest::createSuite< QueryTests::All >();
|
2009-01-30 18:46:49 +01:00
|
|
|
}
|