2008-12-05 22:03:35 +01:00
|
|
|
// mockdbclient.h - mocked out client for testing.
|
|
|
|
|
|
|
|
/**
|
2008-12-08 02:58:20 +01:00
|
|
|
* Copyright (C) 2008 10gen Inc.
|
2008-12-29 02:28:49 +01:00
|
|
|
*
|
2008-12-08 02:58:20 +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
|
|
|
*
|
2008-12-08 02:58:20 +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
|
|
|
*
|
2008-12-08 02:58:20 +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 {
|
2008-12-08 02:58:20 +01:00
|
|
|
public:
|
2008-12-29 02:28:49 +01:00
|
|
|
MockDBClientConnection() : connect_() {}
|
|
|
|
virtual
|
2008-12-05 22:03:35 +01:00
|
|
|
BSONObj findOne(const char *ns, BSONObj 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
|
2008-12-05 22:03:35 +01:00
|
|
|
BSONObj cmdIsMaster(bool& isMaster) {
|
2008-12-29 02:28:49 +01:00
|
|
|
return res_;
|
|
|
|
}
|
|
|
|
void one( const BSONObj &one ) {
|
|
|
|
one_ = one;
|
|
|
|
}
|
|
|
|
void connect( bool val ) {
|
|
|
|
connect_ = val;
|
|
|
|
}
|
|
|
|
void res( const BSONObj &val ) {
|
|
|
|
res_ = val;
|
|
|
|
}
|
2008-12-08 02:58:20 +01:00
|
|
|
private:
|
2008-12-29 02:28:49 +01:00
|
|
|
BSONObj one_;
|
|
|
|
bool connect_;
|
|
|
|
BSONObj res_;
|
2008-12-05 22:03:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class DirectDBClientConnection : public DBClientConnection {
|
2008-12-08 02:58:20 +01:00
|
|
|
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 ) {
|
|
|
|
}
|
|
|
|
virtual BSONObj findOne(const char *ns, BSONObj query, BSONObj *fieldsToReturn = 0, int queryOptions = 0) {
|
|
|
|
if ( cc_ ) cc_->beforeCommand();
|
|
|
|
SetGlobalReplPair s( rp_ );
|
|
|
|
BSONObjBuilder result;
|
|
|
|
result.append( "ok", runCommandAgainstRegistered( "admin.$cmd", query, result ) ? 1.0 : 0.0 );
|
|
|
|
if ( cc_ ) cc_->afterCommand();
|
|
|
|
return result.doneAndDecouple();
|
|
|
|
}
|
|
|
|
virtual bool connect( const char *serverHostname, string& errmsg ) {
|
|
|
|
return true;
|
|
|
|
}
|
2008-12-08 02:58:20 +01:00
|
|
|
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
|
|
|
};
|