Files
mongo/client/examples/clientTest.cpp

198 lines
6.1 KiB
C++
Raw Normal View History

2009-01-13 15:45:49 -05:00
// clientTest.cpp
/* Copyright 2009 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2009-01-13 15:45:49 -05:00
/**
* a simple test for the c++ driver
*/
#include <iostream>
2009-01-29 18:38:35 -05:00
#include "client/dbclient.h"
2009-01-13 15:45:49 -05:00
using namespace std;
2009-01-14 17:09:51 -05:00
using namespace mongo;
2009-01-13 15:45:49 -05:00
int main( int argc, const char **argv ) {
2009-08-06 14:12:32 -04:00
const char *port = "27017";
if ( argc != 1 ) {
if ( argc != 3 )
throw -12;
port = argv[ 2 ];
}
2009-01-13 15:45:49 -05:00
DBClientConnection conn;
string errmsg;
if ( ! conn.connect( string( "127.0.0.1:" ) + port , errmsg ) ) {
2009-01-13 15:45:49 -05:00
cout << "couldn't connect : " << errmsg << endl;
throw -11;
}
2009-01-14 17:17:24 -05:00
2009-01-13 15:45:49 -05:00
const char * ns = "test.test1";
conn.dropCollection(ns);
2009-01-13 15:45:49 -05:00
// clean up old data from any previous tests
2009-05-06 13:17:07 -04:00
conn.remove( ns, BSONObj() );
assert( conn.findOne( ns , BSONObj() ).isEmpty() );
2009-01-14 17:17:24 -05:00
2009-01-13 15:45:49 -05:00
// test insert
2009-02-09 15:38:26 -05:00
conn.insert( ns ,BSON( "name" << "eliot" << "num" << 1 ) );
2009-05-06 13:17:07 -04:00
assert( ! conn.findOne( ns , BSONObj() ).isEmpty() );
2009-01-14 17:17:24 -05:00
2009-01-13 15:45:49 -05:00
// test remove
2009-05-06 13:17:07 -04:00
conn.remove( ns, BSONObj() );
assert( conn.findOne( ns , BSONObj() ).isEmpty() );
2009-01-14 17:17:24 -05:00
2009-01-13 15:45:49 -05:00
// insert, findOne testing
2009-02-09 15:38:26 -05:00
conn.insert( ns , BSON( "name" << "eliot" << "num" << 1 ) );
2009-01-13 15:45:49 -05:00
{
2009-05-06 13:17:07 -04:00
BSONObj res = conn.findOne( ns , BSONObj() );
2009-01-13 15:45:49 -05:00
assert( strstr( res.getStringField( "name" ) , "eliot" ) );
assert( ! strstr( res.getStringField( "name2" ) , "eliot" ) );
assert( 1 == res.getIntField( "num" ) );
}
2009-01-14 17:17:24 -05:00
2009-01-13 15:45:49 -05:00
// cursor
2009-02-09 15:38:26 -05:00
conn.insert( ns ,BSON( "name" << "sara" << "num" << 2 ) );
2009-01-13 15:45:49 -05:00
{
2009-05-06 13:17:07 -04:00
auto_ptr<DBClientCursor> cursor = conn.query( ns , BSONObj() );
2009-01-13 15:45:49 -05:00
int count = 0;
2009-01-14 17:17:24 -05:00
while ( cursor->more() ) {
2009-01-13 15:45:49 -05:00
count++;
BSONObj obj = cursor->next();
}
assert( count == 2 );
}
2009-01-14 17:17:24 -05:00
2009-01-13 15:45:49 -05:00
{
2009-02-09 15:38:26 -05:00
auto_ptr<DBClientCursor> cursor = conn.query( ns , BSON( "num" << 1 ) );
2009-01-13 15:45:49 -05:00
int count = 0;
2009-01-14 17:17:24 -05:00
while ( cursor->more() ) {
2009-01-13 15:45:49 -05:00
count++;
BSONObj obj = cursor->next();
}
assert( count == 1 );
}
2009-01-14 17:17:24 -05:00
2009-01-13 15:45:49 -05:00
{
2009-02-09 15:38:26 -05:00
auto_ptr<DBClientCursor> cursor = conn.query( ns , BSON( "num" << 3 ) );
2009-01-13 15:45:49 -05:00
int count = 0;
2009-01-14 17:17:24 -05:00
while ( cursor->more() ) {
2009-01-13 15:45:49 -05:00
count++;
BSONObj obj = cursor->next();
}
assert( count == 0 );
}
2009-01-13 16:08:07 -05:00
// update
{
2009-02-09 13:04:32 -05:00
BSONObj res = conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot" ).obj() );
2009-01-13 16:08:07 -05:00
assert( ! strstr( res.getStringField( "name2" ) , "eliot" ) );
2009-01-14 17:17:24 -05:00
2009-02-09 13:04:32 -05:00
BSONObj after = BSONObjBuilder().appendElements( res ).append( "name2" , "h" ).obj();
2009-01-14 17:17:24 -05:00
2009-02-09 13:04:32 -05:00
conn.update( ns , BSONObjBuilder().append( "name" , "eliot2" ).obj() , after );
res = conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot" ).obj() );
2009-01-13 16:08:07 -05:00
assert( ! strstr( res.getStringField( "name2" ) , "eliot" ) );
2009-02-09 13:04:32 -05:00
assert( conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot2" ).obj() ).isEmpty() );
2009-01-14 17:17:24 -05:00
2009-02-09 13:04:32 -05:00
conn.update( ns , BSONObjBuilder().append( "name" , "eliot" ).obj() , after );
res = conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot" ).obj() );
2009-01-13 16:08:07 -05:00
assert( strstr( res.getStringField( "name" ) , "eliot" ) );
assert( strstr( res.getStringField( "name2" ) , "h" ) );
2009-02-09 13:04:32 -05:00
assert( conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot2" ).obj() ).isEmpty() );
2009-01-13 16:08:07 -05:00
// upsert
2009-02-09 13:04:32 -05:00
conn.update( ns , BSONObjBuilder().append( "name" , "eliot2" ).obj() , after , 1 );
assert( ! conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot" ).obj() ).isEmpty() );
2009-01-14 17:17:24 -05:00
2009-01-13 16:08:07 -05:00
}
{ // ensure index
2009-01-29 18:38:35 -05:00
assert( conn.ensureIndex( ns , BSON( "name" << 1 ) ) );
assert( ! conn.ensureIndex( ns , BSON( "name" << 1 ) ) );
}
{ // hint related tests
assert( conn.findOne(ns, "{}")["name"].str() == "sara" );
2009-01-30 14:40:19 -05:00
assert( conn.findOne(ns, "{ name : 'eliot' }")["name"].str() == "eliot" );
assert( conn.getLastError() == "" );
// nonexistent index test
assert( conn.findOne(ns, Query("{name:\"eliot\"}").hint("{foo:1}")).hasElement("$err") );
2009-02-25 12:35:45 -05:00
assert( conn.getLastError() == "bad hint" );
conn.resetError();
assert( conn.getLastError() == "" );
//existing index
2009-01-30 14:40:19 -05:00
assert( conn.findOne(ns, Query("{name:'eliot'}").hint("{name:1}")).hasElement("name") );
2009-02-02 09:36:01 -05:00
// run validate
assert( conn.validate( ns ) );
}
2009-08-06 14:12:32 -04:00
{ // timestamp test
2009-08-06 14:12:32 -04:00
const char * tsns = "test.tstest1";
conn.dropCollection( tsns );
2009-08-06 14:12:32 -04:00
{
mongo::BSONObjBuilder b;
b.appendTimestamp( "ts" );
conn.insert( tsns , b.obj() );
}
2009-08-06 14:12:32 -04:00
2009-03-19 16:23:04 -04:00
mongo::BSONObj out = conn.findOne( tsns , mongo::BSONObj() );
2009-03-30 11:09:51 -04:00
unsigned long long oldTime = out["ts"].timestampTime();
unsigned int oldInc = out["ts"].timestampInc();
2009-08-06 14:12:32 -04:00
{
mongo::BSONObjBuilder b1;
b1.append( out["_id"] );
2009-08-06 14:12:32 -04:00
mongo::BSONObjBuilder b2;
b2.append( out["_id"] );
b2.appendTimestamp( "ts" );
2009-08-06 14:12:32 -04:00
conn.update( tsns , b1.obj() , b2.obj() );
}
2009-08-06 14:12:32 -04:00
2009-03-30 11:09:51 -04:00
BSONObj found = conn.findOne( tsns , mongo::BSONObj() );
assert( ( oldTime < found["ts"].timestampTime() ) ||
( oldInc + 1 == found["ts"].timestampInc() ) );
2009-08-06 14:12:32 -04:00
}
2009-08-06 14:12:32 -04:00
{
list<string> l = conn.getDatabaseNames();
for ( list<string>::iterator i = l.begin(); i != l.end(); i++ ){
cout << "db name : " << *i << endl;
}
l = conn.getCollectionNames( "test" );
for ( list<string>::iterator i = l.begin(); i != l.end(); i++ ){
cout << "coll name : " << *i << endl;
}
}
2009-01-13 15:45:49 -05:00
cout << "client test finished!" << endl;
}