summaryrefslogtreecommitdiff
path: root/jstests/unique2.js
blob: 1c2828830f4f386227c3eca81b9b3c90f34ba15e (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
85
86
87
88
89
90
91
92
93
94
// Test unique and dropDups index options.

function checkNprev( np ) {
    // getPrevError() is not available sharded.
    if ( typeof( myShardingTest ) == 'undefined' ) {
        assert.eq( np, db.getPrevError().nPrev );
    }
}

t = db.jstests_unique2;

t.drop();

/* test for good behavior when indexing multikeys */

t.insert({k:3});
t.insert({k:[2,3]});
t.insert({k:[4,3]});

t.ensureIndex({k:1}, {unique:true, dropDups:true});

assert( t.count() == 1 ) ;
assert( t.find().sort({k:1}).toArray().length == 1 ) ;
assert( t.find().sort({k:1}).count() == 1 ) ;

t.drop();

t.ensureIndex({k:1}, {unique:true});

t.insert({k:3});
t.insert({k:[2,3]});
assert( db.getLastError() );
t.insert({k:[4,3]});
assert( db.getLastError() );

assert( t.count() == 1 ) ;
assert( t.find().sort({k:1}).toArray().length == 1 ) ;
assert( t.find().sort({k:1}).count() == 1 ) ;

t.dropIndexes();

t.insert({k:[2,3]});
t.insert({k:[4,3]});
assert( t.count() == 3 ) ;

// Trigger an error, so we can test n of getPrevError() later.
assert.throws( function() { t.find( {$where:'aaa'} ).itcount(); } );
assert( db.getLastError() );
checkNprev( 1 );

t.ensureIndex({k:1}, {unique:true, dropDups:true});
// Check error flag was not set SERVER-2054.
assert( !db.getLastError() );
// Check that offset of previous error is correct.
checkNprev( 2 );

// Check the dups were dropped.
assert( t.count() == 1 ) ;
assert( t.find().sort({k:1}).toArray().length == 1 ) ;
assert( t.find().sort({k:1}).count() == 1 ) ;

// Check that a new conflicting insert will cause an error.
t.insert({k:[2,3]});
assert( db.getLastError() );

t.drop();

t.insert({k:3});
t.insert({k:[2,3]});
t.insert({k:[4,3]});
assert( t.count() == 3 ) ;


// Now try with a background index op.

// Trigger an error, so we can test n of getPrevError() later.
assert.throws( function() { t.find( {$where:'aaa'} ).itcount(); } );
assert( db.getLastError() );
checkNprev( 1 );

t.ensureIndex({k:1}, {background:true, unique:true, dropDups:true});
// Check error flag was not set SERVER-2054.
assert( !db.getLastError() );
// Check that offset of pervious error is correct.
checkNprev( 2 );

// Check the dups were dropped.
assert( t.count() == 1 ) ;
assert( t.find().sort({k:1}).toArray().length == 1 ) ;
assert( t.find().sort({k:1}).count() == 1 ) ;

// Check that a new conflicting insert will cause an error.
t.insert({k:[2,3]});
assert( db.getLastError() );