mirror of
https://github.com/pocoproject/poco.git
synced 2024-12-18 20:24:04 +01:00
commit
5de564c63c
@ -27,6 +27,7 @@
|
||||
#include "Poco/Nullable.h"
|
||||
#include "Poco/NumberFormatter.h"
|
||||
#include "Poco/DateTimeFormatter.h"
|
||||
#include "Poco/UTF8String.h"
|
||||
#include "Poco/MongoDB/MongoDB.h"
|
||||
#include "Poco/MongoDB/BSONReader.h"
|
||||
#include "Poco/MongoDB/BSONWriter.h"
|
||||
@ -79,7 +80,7 @@ inline std::string Element::name() const
|
||||
typedef std::list<Element::Ptr> ElementSet;
|
||||
|
||||
|
||||
template<typename T>
|
||||
template<typename T>
|
||||
struct ElementTraits
|
||||
{
|
||||
};
|
||||
@ -109,51 +110,11 @@ struct ElementTraits<std::string>
|
||||
|
||||
static std::string toString(const std::string& value, int indent = 0)
|
||||
{
|
||||
std::ostringstream oss;
|
||||
|
||||
oss << '"';
|
||||
|
||||
for(std::string::const_iterator it = value.begin(); it != value.end(); ++it)
|
||||
{
|
||||
switch (*it)
|
||||
{
|
||||
case '"':
|
||||
oss << "\\\"";
|
||||
break;
|
||||
case '\\':
|
||||
oss << "\\\\";
|
||||
break;
|
||||
case '\b':
|
||||
oss << "\\b";
|
||||
break;
|
||||
case '\f':
|
||||
oss << "\\f";
|
||||
break;
|
||||
case '\n':
|
||||
oss << "\\n";
|
||||
break;
|
||||
case '\r':
|
||||
oss << "\\r";
|
||||
break;
|
||||
case '\t':
|
||||
oss << "\\t";
|
||||
break;
|
||||
default:
|
||||
{
|
||||
if ( *it > 0 && *it <= 0x1F )
|
||||
{
|
||||
oss << "\\u" << std::hex << std::uppercase << std::setfill('0') << std::setw(4) << static_cast<int>(*it);
|
||||
}
|
||||
else
|
||||
{
|
||||
oss << *it;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
oss << '"';
|
||||
return oss.str();
|
||||
std::string result;
|
||||
result.append(1, '"');
|
||||
result.append(UTF8::escape(value));
|
||||
result.append(1, '"');
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
@ -231,7 +192,11 @@ struct ElementTraits<Timestamp>
|
||||
|
||||
static std::string toString(const Timestamp& value, int indent = 0)
|
||||
{
|
||||
return DateTimeFormatter::format(value, "%Y-%m-%dT%H:%M:%s%z");
|
||||
std::string result;
|
||||
result.append(1, '"');
|
||||
result.append(DateTimeFormatter::format(value, "%Y-%m-%dT%H:%M:%s%z"));
|
||||
result.append(1, '"');
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
@ -308,7 +273,7 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
T value() const
|
||||
{
|
||||
return _value;
|
||||
@ -320,7 +285,7 @@ public:
|
||||
return ElementTraits<T>::toString(_value, indent);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int type() const
|
||||
{
|
||||
return ElementTraits<T>::TypeId;
|
||||
|
@ -30,46 +30,22 @@
|
||||
|
||||
using namespace Poco::MongoDB;
|
||||
|
||||
|
||||
bool MongoDBTest::_connected = false;
|
||||
Poco::MongoDB::Connection MongoDBTest::_mongo;
|
||||
Poco::MongoDB::Connection::Ptr MongoDBTest::_mongo;
|
||||
|
||||
|
||||
MongoDBTest::MongoDBTest(const std::string& name):
|
||||
CppUnit::TestCase("MongoDB"),
|
||||
_host("localhost"),
|
||||
_port(27017)
|
||||
MongoDBTest::MongoDBTest(const std::string& name):
|
||||
CppUnit::TestCase("MongoDB")
|
||||
{
|
||||
if (!_connected)
|
||||
{
|
||||
try
|
||||
{
|
||||
_mongo.connect(_host, _port);
|
||||
_connected = true;
|
||||
std::cout << "Connected to [" << _host << ':' << _port << ']' << std::endl;
|
||||
}
|
||||
catch (Poco::Net::ConnectionRefusedException& e)
|
||||
{
|
||||
std::cout << "Couldn't connect to " << e.message() << ". " << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
MongoDBTest::~MongoDBTest()
|
||||
{
|
||||
if (_connected)
|
||||
{
|
||||
_mongo.disconnect();
|
||||
_connected = false;
|
||||
std::cout << "Disconnected from [" << _host << ':' << _port << ']' << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void MongoDBTest::setUp()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -80,12 +56,6 @@ void MongoDBTest::tearDown()
|
||||
|
||||
void MongoDBTest::testInsertRequest()
|
||||
{
|
||||
if (!_connected)
|
||||
{
|
||||
std::cout << "Not connected, test skipped." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
Poco::MongoDB::Document::Ptr player = new Poco::MongoDB::Document();
|
||||
player->add("lastname", std::string("Braem"));
|
||||
player->add("firstname", std::string("Franky"));
|
||||
@ -105,24 +75,18 @@ void MongoDBTest::testInsertRequest()
|
||||
|
||||
Poco::MongoDB::InsertRequest request("team.players");
|
||||
request.documents().push_back(player);
|
||||
_mongo.sendRequest(request);
|
||||
_mongo->sendRequest(request);
|
||||
}
|
||||
|
||||
void MongoDBTest::testQueryRequest()
|
||||
{
|
||||
if (!_connected)
|
||||
{
|
||||
std::cout << "Not connected, test skipped." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
Poco::MongoDB::QueryRequest request("team.players");
|
||||
request.selector().add("lastname" , std::string("Braem"));
|
||||
request.setNumberToReturn(1);
|
||||
|
||||
Poco::MongoDB::ResponseMessage response;
|
||||
|
||||
_mongo.sendRequest(request, response);
|
||||
_mongo->sendRequest(request, response);
|
||||
|
||||
if ( response.documents().size() > 0 )
|
||||
{
|
||||
@ -158,18 +122,12 @@ void MongoDBTest::testQueryRequest()
|
||||
|
||||
void MongoDBTest::testDBQueryRequest()
|
||||
{
|
||||
if (!_connected)
|
||||
{
|
||||
std::cout << "Not connected, test skipped." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
Database db("team");
|
||||
Poco::SharedPtr<Poco::MongoDB::QueryRequest> request = db.createQueryRequest("players");
|
||||
request->selector().add("lastname" , std::string("Braem"));
|
||||
|
||||
Poco::MongoDB::ResponseMessage response;
|
||||
_mongo.sendRequest(*request, response);
|
||||
_mongo->sendRequest(*request, response);
|
||||
|
||||
if ( response.documents().size() > 0 )
|
||||
{
|
||||
@ -204,25 +162,19 @@ void MongoDBTest::testDBQueryRequest()
|
||||
|
||||
void MongoDBTest::testCountCommand()
|
||||
{
|
||||
if (!_connected)
|
||||
{
|
||||
std::cout << "Not connected, test skipped." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
Poco::MongoDB::QueryRequest request("team.$cmd");
|
||||
request.setNumberToReturn(1);
|
||||
request.selector().add("count", std::string("players"));
|
||||
|
||||
Poco::MongoDB::ResponseMessage response;
|
||||
|
||||
_mongo.sendRequest(request, response);
|
||||
_mongo->sendRequest(request, response);
|
||||
|
||||
if ( response.documents().size() > 0 )
|
||||
{
|
||||
Poco::MongoDB::Document::Ptr doc = response.documents()[0];
|
||||
std::cout << doc->toString() << std::endl;
|
||||
int count = doc->get<int>("n");
|
||||
double count = doc->get<double>("n");
|
||||
assert(count == 1);
|
||||
}
|
||||
else
|
||||
@ -234,22 +186,16 @@ void MongoDBTest::testCountCommand()
|
||||
|
||||
void MongoDBTest::testDBCountCommand()
|
||||
{
|
||||
if (!_connected)
|
||||
{
|
||||
std::cout << "Not connected, test skipped." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
Poco::MongoDB::Database db("team");
|
||||
Poco::SharedPtr<Poco::MongoDB::QueryRequest> request = db.createCountRequest("players");
|
||||
|
||||
Poco::MongoDB::ResponseMessage response;
|
||||
_mongo.sendRequest(*request, response);
|
||||
_mongo->sendRequest(*request, response);
|
||||
|
||||
if ( response.documents().size() > 0 )
|
||||
{
|
||||
Poco::MongoDB::Document::Ptr doc = response.documents()[0];
|
||||
int count = doc->get<int>("n");
|
||||
double count = doc->get<double>("n");
|
||||
assert(count == 1);
|
||||
}
|
||||
else
|
||||
@ -261,41 +207,23 @@ void MongoDBTest::testDBCountCommand()
|
||||
|
||||
void MongoDBTest::testDBCount2Command()
|
||||
{
|
||||
if (!_connected)
|
||||
{
|
||||
std::cout << "Not connected, test skipped." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
Poco::MongoDB::Database db("team");
|
||||
Poco::Int64 count = db.count(_mongo, "players");
|
||||
Poco::Int64 count = db.count(*_mongo, "players");
|
||||
assert(count == 1);
|
||||
}
|
||||
|
||||
|
||||
void MongoDBTest::testDeleteRequest()
|
||||
{
|
||||
if (!_connected)
|
||||
{
|
||||
std::cout << "Not connected, test skipped." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
Poco::MongoDB::DeleteRequest request("team.players");
|
||||
request.selector().add("lastname", std::string("Braem"));
|
||||
|
||||
_mongo.sendRequest(request);
|
||||
_mongo->sendRequest(request);
|
||||
}
|
||||
|
||||
|
||||
void MongoDBTest::testCursorRequest()
|
||||
{
|
||||
if (!_connected)
|
||||
{
|
||||
std::cout << "Not connected, test skipped." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
Poco::MongoDB::Database db("team");
|
||||
Poco::SharedPtr<Poco::MongoDB::InsertRequest> insertRequest = db.createInsertRequest("numbers");
|
||||
for(int i = 0; i < 10000; ++i)
|
||||
@ -304,22 +232,22 @@ void MongoDBTest::testCursorRequest()
|
||||
doc->add("number", i);
|
||||
insertRequest->documents().push_back(doc);
|
||||
}
|
||||
_mongo.sendRequest(*insertRequest);
|
||||
_mongo->sendRequest(*insertRequest);
|
||||
|
||||
Poco::Int64 count = db.count(_mongo, "numbers");
|
||||
Poco::Int64 count = db.count(*_mongo, "numbers");
|
||||
std::cout << "count= " << count << std::endl;
|
||||
assert(count == 10000);
|
||||
|
||||
Poco::MongoDB::Cursor cursor("team", "numbers");
|
||||
|
||||
int n = 0;
|
||||
Poco::MongoDB::ResponseMessage& response = cursor.next(_mongo);
|
||||
Poco::MongoDB::ResponseMessage& response = cursor.next(*_mongo);
|
||||
while(1)
|
||||
{
|
||||
n += response.documents().size();
|
||||
if ( response.cursorID() == 0 )
|
||||
break;
|
||||
response = cursor.next(_mongo);
|
||||
response = cursor.next(*_mongo);
|
||||
}
|
||||
std::cout << "n= " << n << std::endl;
|
||||
assert(n == 10000);
|
||||
@ -329,7 +257,7 @@ void MongoDBTest::testCursorRequest()
|
||||
drop.selector().add("drop", std::string("numbers"));
|
||||
|
||||
Poco::MongoDB::ResponseMessage responseDrop;
|
||||
_mongo.sendRequest(drop, responseDrop);
|
||||
_mongo->sendRequest(drop, responseDrop);
|
||||
|
||||
if ( responseDrop.documents().size() > 0 )
|
||||
{
|
||||
@ -340,12 +268,6 @@ void MongoDBTest::testCursorRequest()
|
||||
|
||||
void MongoDBTest::testBuildInfo()
|
||||
{
|
||||
if (!_connected)
|
||||
{
|
||||
std::cout << "Not connected, test skipped." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
Poco::MongoDB::QueryRequest request("team.$cmd");
|
||||
request.setNumberToReturn(1);
|
||||
request.selector().add("buildInfo", 1);
|
||||
@ -354,7 +276,7 @@ void MongoDBTest::testBuildInfo()
|
||||
|
||||
try
|
||||
{
|
||||
_mongo.sendRequest(request, response);
|
||||
_mongo->sendRequest(request, response);
|
||||
}
|
||||
catch(Poco::NotImplementedException& nie)
|
||||
{
|
||||
@ -376,7 +298,7 @@ void MongoDBTest::testBuildInfo()
|
||||
|
||||
void MongoDBTest::testConnectionPool()
|
||||
{
|
||||
Poco::Net::SocketAddress sa(_host, _port);
|
||||
Poco::Net::SocketAddress sa("localhost", 27017);
|
||||
Poco::PoolableObjectFactory<Poco::MongoDB::Connection, Poco::MongoDB::Connection::Ptr> factory(sa);
|
||||
Poco::ObjectPool<Poco::MongoDB::Connection, Poco::MongoDB::Connection::Ptr> pool(factory, 10, 15);
|
||||
|
||||
@ -392,7 +314,7 @@ void MongoDBTest::testConnectionPool()
|
||||
if ( response.documents().size() > 0 )
|
||||
{
|
||||
Poco::MongoDB::Document::Ptr doc = response.documents()[0];
|
||||
int count = doc->get<int>("n");
|
||||
double count = doc->get<double>("n");
|
||||
assert(count == 1);
|
||||
}
|
||||
else
|
||||
@ -419,7 +341,7 @@ void MongoDBTest::testCommand() {
|
||||
.add("size", 1024);
|
||||
|
||||
Poco::MongoDB::ResponseMessage response;
|
||||
_mongo.sendRequest(*command, response);
|
||||
_mongo->sendRequest(*command, response);
|
||||
if ( response.documents().size() > 0 )
|
||||
{
|
||||
Poco::MongoDB::Document::Ptr doc = response.documents()[0];
|
||||
@ -427,7 +349,7 @@ void MongoDBTest::testCommand() {
|
||||
}
|
||||
else
|
||||
{
|
||||
Poco::MongoDB::Document::Ptr lastError = db.getLastErrorDoc(_mongo);
|
||||
Poco::MongoDB::Document::Ptr lastError = db.getLastErrorDoc(*_mongo);
|
||||
std::cout << "LastError: " << lastError->toString(2) << std::endl;
|
||||
fail("Didn't get a response from the command");
|
||||
}
|
||||
@ -435,6 +357,17 @@ void MongoDBTest::testCommand() {
|
||||
|
||||
CppUnit::Test* MongoDBTest::suite()
|
||||
{
|
||||
try
|
||||
{
|
||||
_mongo = new Poco::MongoDB::Connection("localhost", 27017);
|
||||
std::cout << "Connected to [localhost:27017]" << std::endl;
|
||||
}
|
||||
catch (Poco::Net::ConnectionRefusedException& e)
|
||||
{
|
||||
std::cout << "Couldn't connect to " << e.message() << ". " << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MongoDBTest");
|
||||
|
||||
CppUnit_addTest(pSuite, MongoDBTest, testInsertRequest);
|
||||
|
@ -49,10 +49,7 @@ public:
|
||||
|
||||
private:
|
||||
|
||||
std::string _host;
|
||||
unsigned _port;
|
||||
static bool _connected;
|
||||
static Poco::MongoDB::Connection _mongo;
|
||||
static Poco::MongoDB::Connection::Ptr _mongo;
|
||||
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user