00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00039
00040
00041 #include "test_mongo_helpers.h"
00042 #include <mongo_ros/message_collection.h>
00043 #include <mongo_ros/exceptions.h>
00044 #include <ros/ros.h>
00045 #include <boost/foreach.hpp>
00046 #include <gtest/gtest.h>
00047
00048 namespace mr=mongo_ros;
00049 namespace gm=geometry_msgs;
00050 using std::vector;
00051 using std::string;
00052 using std::cout;
00053
00054 typedef mr::MessageWithMetadata<gm::Pose> PoseWithMetadata;
00055 typedef PoseWithMetadata::ConstPtr PoseMetaPtr;
00056
00057
00058
00059
00060 mr::Metadata makeMetadata (const gm::Pose& p, const string& v)
00061 {
00062 return mr::Metadata("x", p.position.x, "y", p.position.y, "name", v);
00063 }
00064
00065 TEST(MongoRos, MongoRos)
00066 {
00067
00068
00069 using mr::LT;
00070 using mr::GT;
00071
00072
00073 mr::dropDatabase("my_db", "localhost", 27019, 60.0);
00074
00075
00076 mr::MessageCollection<gm::Pose> coll("my_db", "poses", "localhost",
00077 27019, 60.0);
00078
00079
00080 coll.ensureIndex("name");
00081 coll.ensureIndex("x");
00082
00083
00084 const gm::Pose p1 = makePose(24, 42, 0);
00085 const gm::Pose p2 = makePose(10, 532, 3);
00086 const gm::Pose p3 = makePose(53, 22, 5);
00087 const gm::Pose p4 = makePose(22, -5, 33);
00088 coll.insert(p1, makeMetadata(p1, "bar"));
00089 coll.insert(p2, makeMetadata(p2, "baz"));
00090 coll.insert(p3, makeMetadata(p3, "qux"));
00091 coll.insert(p1, makeMetadata(p1, "oof"));
00092 coll.insert(p4, makeMetadata(p4, "ooof"));
00093 EXPECT_EQ(5u, coll.count());
00094
00095
00096
00097 vector<PoseMetaPtr> res = coll.pullAllResults(mr::Query("name", "qux"), true);
00098 EXPECT_EQ(1u, res.size());
00099 EXPECT_EQ("qux", res[0]->lookupString("name"));
00100 EXPECT_DOUBLE_EQ(53, res[0]->lookupDouble("x"));
00101
00102
00103
00104
00105
00106 mr::Query q = mr::Query().append("x", mr::LT, 40).append("y", mr::GT, 0);
00107 vector<PoseMetaPtr> poses = coll.pullAllResults(q, false, "name", false);
00108
00109
00110 EXPECT_EQ(3u, poses.size());
00111 EXPECT_EQ(p1, *poses[0]);
00112 EXPECT_EQ(p2, *poses[1]);
00113 EXPECT_EQ(p1, *poses[2]);
00114
00115 EXPECT_EQ("oof", poses[0]->lookupString("name"));
00116 EXPECT_EQ("baz", poses[1]->lookupString("name"));
00117 EXPECT_EQ("bar", poses[2]->lookupString("name"));
00118
00119
00120 mr::Query q2 ("y", mr::LT, 30);
00121
00122 EXPECT_EQ(5u, coll.count());
00123 EXPECT_EQ(2u, coll.removeMessages(q2));
00124 EXPECT_EQ(3u, coll.count());
00125
00126
00127 mr::Query q4("name", "bar");
00128 EXPECT_EQ(p1, *coll.findOne(q4, false));
00129 EXPECT_DOUBLE_EQ(24, coll.findOne(q4, true)->lookupDouble("x"));
00130
00131 mr::Query q5("name", "barbar");
00132 EXPECT_THROW(coll.findOne(q5, true), mr::NoMatchingMessageException);
00133 EXPECT_THROW(coll.findOne(q5, false), mr::NoMatchingMessageException);
00134
00135
00136 coll.modifyMetadata(q4, mr::Metadata("name", "barbar"));
00137 EXPECT_EQ(3u, coll.count());
00138 EXPECT_THROW(coll.findOne(q4, false), mr::NoMatchingMessageException);
00139 ROS_INFO("here");
00140 EXPECT_EQ(p1, *coll.findOne(q5, false));
00141
00142
00143 boost::shared_ptr<mongo::DBClientConnection> conn =
00144 mr::makeDbConnection(ros::NodeHandle());
00145 EXPECT_EQ("geometry_msgs/Pose", mr::messageType(*conn, "my_db", "poses"));
00146 }
00147
00148
00149 int main (int argc, char** argv)
00150 {
00151 ros::init(argc, argv, "client_test");
00152 ros::NodeHandle nh;
00153 testing::InitGoogleTest(&argc, argv);
00154 return RUN_ALL_TESTS();
00155 }
00156
00157