File: uniqueness.js

package info (click to toggle)
mongodb 1%3A2.4.10-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 82,464 kB
  • sloc: cpp: 740,225; ansic: 152,098; sh: 13,820; python: 11,864; makefile: 1,012; perl: 922; pascal: 617; java: 452; lisp: 222; asm: 174
file content (58 lines) | stat: -rw-r--r-- 1,701 bytes parent folder | download | duplicates (4)
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

t = db.jstests_uniqueness;

t.drop();

// test uniqueness of _id

t.save( { _id : 3 } );
assert( !db.getLastError(), 1 );

// this should yield an error
t.insert( { _id : 3 } );
assert( db.getLastError() , 2);
assert( t.count() == 1, "hmmm");

t.insert( { _id : 4, x : 99 } );
assert( !db.getLastError() , 3);

// this should yield an error
t.update( { _id : 4 } , { _id : 3, x : 99 } );
assert( db.getLastError() , 4);
assert( t.findOne( {_id:4} ), 5 );

// Check for an error message when we index and there are dups 
db.jstests_uniqueness2.drop();
db.jstests_uniqueness2.insert({a:3});
db.jstests_uniqueness2.insert({a:3});
assert( db.jstests_uniqueness2.count() == 2 , 6) ;
db.resetError();
db.jstests_uniqueness2.ensureIndex({a:1}, true);
assert( db.getLastError() , 7);
assert( db.getLastError().match( /E11000/ ) );

// Check for an error message when we index in the background and there are dups 
db.jstests_uniqueness2.drop();
db.jstests_uniqueness2.insert({a:3});
db.jstests_uniqueness2.insert({a:3});
assert( db.jstests_uniqueness2.count() == 2 , 6) ;
assert( !db.getLastError() );
db.resetError();
db.jstests_uniqueness2.ensureIndex({a:1}, {unique:true,background:true});
assert( db.getLastError() , 7);
assert( db.getLastError().match( /E11000/ ) );

/* Check that if we update and remove _id, it gets added back by the DB */

/* - test when object grows */
t.drop();
t.save( { _id : 'Z' } );
t.update( {}, { k : 2 } );
assert( t.findOne()._id == 'Z', "uniqueness.js problem with adding back _id" );

/* - test when doesn't grow */
t.drop();
t.save( { _id : 'Z', k : 3 } );
t.update( {}, { k : 2 } );
assert( t.findOne()._id == 'Z', "uniqueness.js problem with adding back _id (2)" );