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

93 lines
2.7 KiB
C
Raw Normal View History

2008-12-05 22:03:35 +01:00
// mockdbclient.h - mocked out client for testing.
/**
* Copyright (C) 2008 10gen Inc.
2008-12-29 02:28:49 +01:00
*
* 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.
2008-12-29 02:28:49 +01:00
*
* 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.
2008-12-29 02:28:49 +01:00
*
* 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/>.
*/
2008-12-05 22:03:35 +01:00
#pragma once
#include "../client/dbclient.h"
#include "../db/commands.h"
2010-04-22 01:32:36 +02:00
#include "../db/replpair.h"
2008-12-05 22:03:35 +01:00
class MockDBClientConnection : public DBClientConnection {
public:
2008-12-29 02:28:49 +01:00
MockDBClientConnection() : connect_() {}
virtual
BSONObj findOne(const string &ns, Query query, const BSONObj *fieldsToReturn = 0, int queryOptions = 0) {
2008-12-29 02:28:49 +01:00
return one_;
}
virtual
bool connect(const string &serverHostname, string& errmsg) {
2008-12-29 02:28:49 +01:00
return connect_;
}
virtual
bool isMaster(bool& isMaster, BSONObj *info=0) {
return isMaster_;
2008-12-29 02:28:49 +01:00
}
void one( const BSONObj &one ) {
one_ = one;
}
void connect( bool val ) {
connect_ = val;
}
void setIsMaster( bool val ) {
isMaster_ = val;
2008-12-29 02:28:49 +01:00
}
private:
2008-12-29 02:28:49 +01:00
BSONObj one_;
bool connect_;
bool isMaster_;
2008-12-05 22:03:35 +01:00
};
class DirectDBClientConnection : public DBClientConnection {
public:
2008-12-29 02:28:49 +01:00
struct ConnectionCallback {
2009-02-18 19:42:32 +01:00
virtual ~ConnectionCallback() {}
2008-12-29 02:28:49 +01:00
virtual void beforeCommand() {}
virtual void afterCommand() {}
};
DirectDBClientConnection( ReplPair *rp, ConnectionCallback *cc = 0 ) :
rp_( rp ),
cc_( cc ) {
}
virtual BSONObj findOne(const string &ns, Query query, const BSONObj *fieldsToReturn = 0, int queryOptions = 0) {
2008-12-29 02:28:49 +01:00
if ( cc_ ) cc_->beforeCommand();
SetGlobalReplPair s( rp_ );
BSONObjBuilder result;
2010-01-20 20:02:14 +01:00
result.append( "ok", Command::runAgainstRegistered( "admin.$cmd", query.obj, result ) ? 1.0 : 0.0 );
2008-12-29 02:28:49 +01:00
if ( cc_ ) cc_->afterCommand();
2009-02-09 19:04:32 +01:00
return result.obj();
2008-12-29 02:28:49 +01:00
}
virtual bool connect( const string &serverHostname, string& errmsg ) {
2008-12-29 02:28:49 +01:00
return true;
}
private:
2008-12-29 02:28:49 +01:00
ReplPair *rp_;
ConnectionCallback *cc_;
class SetGlobalReplPair {
public:
SetGlobalReplPair( ReplPair *rp ) {
backup_ = replPair;
replPair = rp;
}
~SetGlobalReplPair() {
replPair = backup_;
}
private:
ReplPair *backup_;
};
2008-12-05 22:03:35 +01:00
};