~ubuntu-branches/ubuntu/trusty/mongodb/trusty-proposed

« back to all changes in this revision

Viewing changes to dbtests/mockdbclient.h

  • Committer: Bazaar Package Importer
  • Author(s): Antonin Kral
  • Date: 2010-01-29 19:48:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100129194845-8wbmkf626fwcavc9
Tags: upstream-1.3.1
ImportĀ upstreamĀ versionĀ 1.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// mockdbclient.h - mocked out client for testing.
 
2
 
 
3
/**
 
4
 *    Copyright (C) 2008 10gen Inc.
 
5
 *
 
6
 *    This program is free software: you can redistribute it and/or  modify
 
7
 *    it under the terms of the GNU Affero General Public License, version 3,
 
8
 *    as published by the Free Software Foundation.
 
9
 *
 
10
 *    This program is distributed in the hope that it will be useful,
 
11
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *    GNU Affero General Public License for more details.
 
14
 *
 
15
 *    You should have received a copy of the GNU Affero General Public License
 
16
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
 */
 
18
 
 
19
#pragma once
 
20
 
 
21
#include "../client/dbclient.h"
 
22
#include "../db/commands.h"
 
23
 
 
24
class MockDBClientConnection : public DBClientConnection {
 
25
public:
 
26
    MockDBClientConnection() : connect_() {}
 
27
    virtual
 
28
    BSONObj findOne(const string &ns, Query query, const BSONObj *fieldsToReturn = 0, int queryOptions = 0) {
 
29
        return one_;
 
30
    }
 
31
    virtual
 
32
    bool connect(const string &serverHostname, string& errmsg) {
 
33
        return connect_;
 
34
    }
 
35
    virtual
 
36
    bool isMaster(bool& isMaster, BSONObj *info=0) {
 
37
        return isMaster_;
 
38
    }
 
39
    void one( const BSONObj &one ) {
 
40
        one_ = one;
 
41
    }
 
42
    void connect( bool val ) {
 
43
        connect_ = val;
 
44
    }
 
45
    void setIsMaster( bool val ) {
 
46
        isMaster_ = val;
 
47
    }
 
48
private:
 
49
    BSONObj one_;
 
50
    bool connect_;
 
51
    bool isMaster_;
 
52
};
 
53
 
 
54
class DirectDBClientConnection : public DBClientConnection {
 
55
public:
 
56
    struct ConnectionCallback {
 
57
        virtual ~ConnectionCallback() {}
 
58
        virtual void beforeCommand() {}
 
59
        virtual void afterCommand() {}
 
60
    };
 
61
    DirectDBClientConnection( ReplPair *rp, ConnectionCallback *cc = 0 ) :
 
62
            rp_( rp ),
 
63
            cc_( cc ) {
 
64
    }
 
65
    virtual BSONObj findOne(const string &ns, Query query, const BSONObj *fieldsToReturn = 0, int queryOptions = 0) {
 
66
        if ( cc_ ) cc_->beforeCommand();
 
67
        SetGlobalReplPair s( rp_ );
 
68
        BSONObjBuilder result;
 
69
        result.append( "ok", Command::runAgainstRegistered( "admin.$cmd", query.obj, result ) ? 1.0 : 0.0 );
 
70
        if ( cc_ ) cc_->afterCommand();
 
71
        return result.obj();
 
72
    }
 
73
    virtual bool connect( const string &serverHostname, string& errmsg ) {
 
74
        return true;
 
75
    }
 
76
private:
 
77
    ReplPair *rp_;
 
78
    ConnectionCallback *cc_;
 
79
    class SetGlobalReplPair {
 
80
    public:
 
81
        SetGlobalReplPair( ReplPair *rp ) {
 
82
            backup_ = replPair;
 
83
            replPair = rp;
 
84
        }
 
85
        ~SetGlobalReplPair() {
 
86
            replPair = backup_;
 
87
        }
 
88
    private:
 
89
        ReplPair *backup_;
 
90
    };
 
91
};