0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-30 09:06:21 +01:00
mongodb/dbtests/namespacetests.cpp

258 lines
6.5 KiB
C++
Raw Normal View History

// indexdetailstests.cpp : IndexDetails unit tests.
//
/**
* Copyright (C) 2008 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/>.
*/
2008-12-11 21:20:28 +01:00
// Where IndexDetails defined.
#include "../db/namespace.h"
#include "../db/db.h"
#include "../db/json.h"
#include "dbtests.h"
2008-12-11 21:20:28 +01:00
namespace NamespaceTests {
namespace IndexDetailsTests {
class Base {
public:
Base() {
dblock lk;
setClient( ns() );
2008-12-11 23:35:11 +01:00
}
~Base() {
if( id_.info.isNull() )
return;
theDataFileMgr.deleteRecord( ns(), id_.info.rec(), id_.info );
ASSERT( theDataFileMgr.findAll( ns() )->eof() );
}
protected:
void create() {
2008-12-11 21:20:28 +01:00
BSONObjBuilder builder;
builder.append( "ns", ns() );
builder.append( "name", "testIndex" );
builder.append( "key", key() );
BSONObj bobj = builder.done();
id_.info = theDataFileMgr.insert( ns(), bobj.objdata(), bobj.objsize() );
// head not needed for current tests
2008-12-11 23:35:11 +01:00
// idx_.head = BtreeBucket::addHead( id_ );
2008-12-11 21:20:28 +01:00
}
static const char* ns() { return "sys.unittest.indexdetailstests"; }
const IndexDetails& id() { return id_; }
virtual BSONObj key() const {
BSONObjBuilder k;
k.append( "a", 1 );
return k.doneAndDecouple();
}
BSONObj aDotB() const {
2008-12-11 23:35:11 +01:00
BSONObjBuilder k;
k.append( "a.b", 1 );
return k.doneAndDecouple();
}
BSONObj aAndB() const {
BSONObjBuilder k;
k.append( "a", 1 );
k.append( "b", 1 );
return k.doneAndDecouple();
}
static vector< int > shortArray() {
vector< int > a;
a.push_back( 1 );
a.push_back( 2 );
a.push_back( 3 );
return a;
2008-12-11 21:20:28 +01:00
}
private:
IndexDetails id_;
};
class Create : public Base {
public:
void run() {
2008-12-11 23:35:11 +01:00
create();
ASSERT_EQUALS( "testIndex", id().indexName() );
ASSERT_EQUALS( ns(), id().parentNS() );
2008-12-11 21:20:28 +01:00
// check equal
ASSERT_EQUALS( key(), id().keyPattern() );
2008-12-11 21:20:28 +01:00
}
};
class GetKeysFromObjectSimple : public Base {
public:
void run() {
2008-12-11 23:35:11 +01:00
create();
2008-12-11 21:20:28 +01:00
BSONObjBuilder b, e;
b.append( "b", 4 );
b.append( "a", 5 );
e.append( "", 5 );
2008-12-11 21:20:28 +01:00
set< BSONObj > keys;
id().getKeysFromObject( b.done(), keys );
ASSERT_EQUALS( 1, keys.size() );
ASSERT_EQUALS( e.done(), *keys.begin() );
2008-12-11 21:20:28 +01:00
}
};
class GetKeysFromObjectDotted : public Base {
public:
void run() {
2008-12-11 23:35:11 +01:00
create();
2008-12-11 21:20:28 +01:00
BSONObjBuilder a, e, b;
b.append( "b", 4 );
a.append( "a", b.done() );
a.append( "c", "foo" );
e.append( "", 4 );
2008-12-11 21:20:28 +01:00
set< BSONObj > keys;
id().getKeysFromObject( a.done(), keys );
ASSERT_EQUALS( 1, keys.size() );
ASSERT_EQUALS( e.done(), *keys.begin() );
2008-12-11 21:20:28 +01:00
}
private:
virtual BSONObj key() const { return aDotB(); }
};
class GetKeysFromArraySimple : public Base {
public:
void run() {
2008-12-11 23:35:11 +01:00
create();
2008-12-11 21:20:28 +01:00
BSONObjBuilder b;
2008-12-11 23:35:11 +01:00
b.append( "a", shortArray()) ;
2008-12-11 21:20:28 +01:00
set< BSONObj > keys;
id().getKeysFromObject( b.done(), keys );
ASSERT_EQUALS( 3, keys.size() );
2008-12-11 21:20:28 +01:00
int j = 1;
for( set< BSONObj >::iterator i = keys.begin(); i != keys.end(); ++i, ++j ) {
BSONObjBuilder b;
b.append( "", j );
ASSERT_EQUALS( b.done(), *i );
2008-12-11 21:20:28 +01:00
}
}
};
2008-12-11 23:35:11 +01:00
class GetKeysFromArrayFirstElement : public Base {
2008-12-11 21:20:28 +01:00
public:
void run() {
2008-12-11 23:35:11 +01:00
create();
2008-12-11 21:20:28 +01:00
BSONObjBuilder b;
2008-12-11 23:35:11 +01:00
b.append( "a", shortArray() );
b.append( "b", 2 );
2008-12-11 21:20:28 +01:00
set< BSONObj > keys;
2008-12-11 23:35:11 +01:00
id().getKeysFromObject( b.done(), keys );
ASSERT_EQUALS( 3, keys.size() );
2008-12-11 21:20:28 +01:00
int j = 1;
for( set< BSONObj >::iterator i = keys.begin(); i != keys.end(); ++i, ++j ) {
BSONObjBuilder b;
b.append( "", j );
b.append( "", 2 );
ASSERT_EQUALS( b.done(), *i );
2008-12-11 23:35:11 +01:00
}
}
private:
virtual BSONObj key() const { return aAndB(); }
};
class GetKeysFromArraySecondElement : public Base {
public:
void run() {
create();
BSONObjBuilder b;
b.append( "first", 5 );
b.append( "a", shortArray()) ;
set< BSONObj > keys;
id().getKeysFromObject( b.done(), keys );
ASSERT_EQUALS( 3, keys.size() );
int j = 1;
for( set< BSONObj >::iterator i = keys.begin(); i != keys.end(); ++i, ++j ) {
BSONObjBuilder b;
b.append( "", 5 );
b.append( "", j );
ASSERT_EQUALS( b.done(), *i );
2008-12-11 23:35:11 +01:00
}
}
private:
virtual BSONObj key() const {
BSONObjBuilder k;
k.append( "first", 1 );
k.append( "a", 1 );
return k.doneAndDecouple();
}
};
class GetKeysFromSecondLevelArray : public Base {
public:
void run() {
create();
BSONObjBuilder b;
b.append( "b", shortArray() );
BSONObjBuilder a;
a.append( "a", b.done() );
set< BSONObj > keys;
id().getKeysFromObject( a.done(), keys );
ASSERT_EQUALS( 3, keys.size() );
int j = 1;
for( set< BSONObj >::iterator i = keys.begin(); i != keys.end(); ++i, ++j ) {
BSONObjBuilder b;
b.append( "", j );
ASSERT_EQUALS( b.done(), *i );
2008-12-11 21:20:28 +01:00
}
}
private:
virtual BSONObj key() const { return aDotB(); }
};
2008-12-11 23:35:11 +01:00
class ParallelArraysBasic : public Base {
public:
void run() {
create();
BSONObjBuilder b;
b.append( "a", shortArray() );
b.append( "b", shortArray() );
set< BSONObj > keys;
ASSERT_EXCEPTION( id().getKeysFromObject( b.done(), keys ),
UserAssertionException );
}
private:
virtual BSONObj key() const { return aAndB(); }
};
2008-12-11 21:20:28 +01:00
} // namespace IndexDetailsTests
2008-12-11 23:35:11 +01:00
// TODO
// array subelement
// parallel arrays complex
// allowed multi array indexes
class All : public UnitTest::Suite {
public:
All() {
2008-12-11 21:20:28 +01:00
add< IndexDetailsTests::Create >();
add< IndexDetailsTests::GetKeysFromObjectSimple >();
add< IndexDetailsTests::GetKeysFromObjectDotted >();
add< IndexDetailsTests::GetKeysFromArraySimple >();
2008-12-11 23:35:11 +01:00
add< IndexDetailsTests::GetKeysFromArrayFirstElement >();
add< IndexDetailsTests::GetKeysFromArraySecondElement >();
add< IndexDetailsTests::GetKeysFromSecondLevelArray >();
add< IndexDetailsTests::ParallelArraysBasic >();
}
};
}
2008-12-11 21:20:28 +01:00
UnitTest::TestPtr namespaceTests() {
return UnitTest::createSuite< NamespaceTests::All >();
}