0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00

Move over some more js tests

This commit is contained in:
Aaron 2009-01-27 14:18:33 -05:00
parent 5622e758fa
commit 66768ae3f7
4 changed files with 165 additions and 0 deletions

11
jstests/capped.js Normal file
View File

@ -0,0 +1,11 @@
db=connect("test");
db.capped.drop();
db.createCollection("cptest", {capped:true, size:30000});
t = db.cptest;
t.save({x:1});
t.save({x:2});
assert( t.find().sort({$natural:1})[0].x == 1 );
assert( t.find().sort({$natural:-1})[0].x == 2 );

52
jstests/capped2.js Normal file
View File

@ -0,0 +1,52 @@
db=connect("test");
db.capped2.drop();
db._dbCommand( { create: "capped2", capped: true, size: 1000, $nExtents: 11 } );
t = db.capped2;
var val = new Array( 1000 );
var c = "";
for( i = 0; i < 1000; ++i, c += "-" ) {
val[ i ] = { a: c };
}
function checkIncreasing( i ) {
res = t.find().sort( { $natural: -1 } );
assert( res.hasNext() );
var j = i;
while( res.hasNext() ) {
assert.eq( val[ j-- ].a, res.next().a );
}
res = t.find().sort( { $natural: 1 } );
assert( res.hasNext() );
while( res.hasNext() )
assert.eq( val[ ++j ].a, res.next().a );
assert.eq( j, i );
}
function checkDecreasing( i ) {
res = t.find().sort( { $natural: -1 } );
assert( res.hasNext() );
var j = i;
while( res.hasNext() ) {
assert.eq( val[ j++ ].a, res.next().a );
}
res = t.find().sort( { $natural: 1 } );
assert( res.hasNext() );
while( res.hasNext() )
assert.eq( val[ --j ].a, res.next().a );
assert.eq( j, i );
}
for( i = 0 ;; ++i ) {
t.save( val[ i ] );
if ( t.count() == 0 ) {
assert( i > 100 );
break;
}
checkIncreasing( i );
}
for( i = 600 ; i >= 0 ; --i ) {
t.save( val[ i ] );
checkDecreasing( i );
}

89
jstests/cursor6.js Normal file
View File

@ -0,0 +1,89 @@
// Test different directions for compound indexes
function eq( one, two ) {
assert.eq( one.a, two.a );
assert.eq( one.b, two.b );
}
function checkExplain( e, idx, reverse, nScanned ) {
if ( !reverse ) {
if ( idx ) {
assert.eq( "BtreeCursor a_1_b_-1", e.cursor );
} else {
assert.eq( "BasicCursor", e.cursor );
}
} else {
if ( idx ) {
assert.eq( "BtreeCursor a_1_b_-1 reverse", e.cursor );
} else {
assert( false );
}
}
assert.eq( nScanned, e.nscanned );
}
function check( indexed ) {
e = r.find().sort( { a: 1, b: 1 } ).explain();
checkExplain( e, false, false, 4 );
f = r.find().sort( { a: 1, b: 1 } );
eq( z[ 0 ], f[ 0 ] );
eq( z[ 1 ], f[ 1 ] );
eq( z[ 2 ], f[ 2 ] );
eq( z[ 3 ], f[ 3 ] );
e = r.find().sort( { a: 1, b: -1 } ).explain();
checkExplain( e, true && indexed, false, 4 );
f = r.find().sort( { a: 1, b: -1 } );
eq( z[ 1 ], f[ 0 ] );
eq( z[ 0 ], f[ 1 ] );
eq( z[ 3 ], f[ 2 ] );
eq( z[ 2 ], f[ 3 ] );
e = r.find().sort( { a: -1, b: 1 } ).explain();
checkExplain( e, true && indexed, true && indexed, 4 );
f = r.find().sort( { a: -1, b: 1 } );
eq( z[ 2 ], f[ 0 ] );
eq( z[ 3 ], f[ 1 ] );
eq( z[ 0 ], f[ 2 ] );
eq( z[ 1 ], f[ 3 ] );
e = r.find( { a: { $gte: 2 } } ).sort( { a: 1, b: -1 } ).explain();
checkExplain( e, true && indexed, false, indexed ? 2 : 4 );
f = r.find( { a: { $gte: 2 } } ).sort( { a: 1, b: -1 } );
eq( z[ 3 ], f[ 0 ] );
eq( z[ 2 ], f[ 1 ] );
e = r.find( { a : { $gte: 2 } } ).sort( { a: -1, b: 1 } ).explain();
checkExplain( e, true && indexed, true && indexed, indexed ? 2 : 4 );
f = r.find( { a: { $gte: 2 } } ).sort( { a: -1, b: 1 } );
eq( z[ 2 ], f[ 0 ] );
eq( z[ 3 ], f[ 1 ] );
e = r.find().sort( { a: -1, b: -1 } ).explain();
checkExplain( e, false, false, 4 );
f = r.find().sort( { a: -1, b: -1 } );
eq( z[ 3 ], f[ 0 ] );
eq( z[ 2 ], f[ 1 ] );
eq( z[ 1 ], f[ 2 ] );
eq( z[ 0 ], f[ 3 ] );
}
db = connect( "test" );
db.setProfilingLevel( 1 );
r = db.ed_db_cursor6;
r.drop();
z = [ { a: 1, b: 1 },
{ a: 1, b: 2 },
{ a: 2, b: 1 },
{ a: 2, b: 2 } ];
for( i = 0; i < z.length; ++i )
r.save( z[ i ] );
check( false );
r.ensureIndex( { a: 1, b: -1 } );
check( true );
assert.eq( "BasicCursor", r.find().sort( { a: 1, b: -1, z: 1 } ).explain().cursor );

13
jstests/update2.js Normal file
View File

@ -0,0 +1,13 @@
db = connect( "test" );
f = db.ed_db_update2;
f.drop();
f.save( { a: 4 } );
f.update( { a: 4 }, { $inc: { a: 2 } } );
assert.eq( 6, f.findOne().a );
f.drop();
f.save( { a: 4 } );
f.ensureIndex( { a: 1 } );
f.update( { a: 4 }, { $inc: { a: 2 } } );
assert.eq( 4, f.findOne().a );