summaryrefslogtreecommitdiff
path: root/src/mongo/dbtests/removetests.cpp
blob: b46681c1fb8ac1735c40cff22d6b83dd3cc29877 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// removetests.cpp : unit tests relating to removing documents
//

/**
 *    Copyright (C) 2012 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/dbhelpers.h"

#include "dbtests.h"

namespace RemoveTests {

    static const char * const ns = "unittests.removetests";
    static DBDirectClient client;
    
    /** Simple test for Helpers::RemoveRange. */
    class RemoveRange {
    public:
        RemoveRange() :
        _min( 4 ),
        _max( 8 ) {
        }
        void run() {
            for( int i = 0; i < 10; ++i ) {
                client.insert( ns, BSON( "_id" << i ) );
            }
            
            {
                // Remove _id range [_min, _max).
                Lock::DBWrite lk(ns);
                Client::Context ctx( ns );
                Helpers::removeRange( ns, BSON( "_id" << _min ), BSON( "_id" << _max ) );
            }

            // Check that the expected documents remain.
            ASSERT_EQUALS( expected(), docs() );
        }
    private:
        BSONArray expected() const {
            BSONArrayBuilder bab;
            for( int i = 0; i < _min; ++i ) {
                bab << BSON( "_id" << i );
            }
            for( int i = _max; i < 10; ++i ) {
                bab << BSON( "_id" << i );
            }
            return bab.arr();
        }
        BSONArray docs() const {
            auto_ptr<DBClientCursor> cursor =
                    client.query( ns, Query().hint( BSON( "_id" << 1 ) ) );
            BSONArrayBuilder bab;
            while( cursor->more() ) {
                bab << cursor->next();
            }
            return bab.arr();
        }
        int _min;
        int _max;
    };
    
    class All : public Suite {
    public:
        All() : Suite( "remove" ) {
        }
        void setupTests() {
            add<RemoveRange>();
        }
    } myall;
    
} // namespace RemoveTests