0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-29 16:47:28 +01:00
mongodb/dbtests/mockdbclient.h

91 lines
2.6 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"
class MockDBClientConnection : public DBClientConnection {
public:
2008-12-29 02:28:49 +01:00
MockDBClientConnection() : connect_() {}
virtual
2009-01-30 04:51:50 +01:00
BSONObj findOne(const char *ns, Query query, BSONObj *fieldsToReturn = 0, int queryOptions = 0) {
2008-12-29 02:28:49 +01:00
return one_;
}
virtual
2008-12-05 22:03:35 +01:00
bool connect(const char *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 {
virtual void beforeCommand() {}
virtual void afterCommand() {}
};
DirectDBClientConnection( ReplPair *rp, ConnectionCallback *cc = 0 ) :
rp_( rp ),
cc_( cc ) {
}
2009-01-30 04:51:50 +01:00
virtual BSONObj findOne(const char *ns, Query query, BSONObj *fieldsToReturn = 0, int queryOptions = 0) {
2008-12-29 02:28:49 +01:00
if ( cc_ ) cc_->beforeCommand();
SetGlobalReplPair s( rp_ );
BSONObjBuilder result;
2009-01-30 04:51:50 +01:00
result.append( "ok", runCommandAgainstRegistered( "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 char *serverHostname, string& errmsg ) {
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
};