Go to the documentation of this file.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
00031
00032
00033
00034
00035
00036
00037 #include <algorithm>
00038 #include <vector>
00039 #include <boost/shared_ptr.hpp>
00040
00041 #include "database_interface/postgresql_database.h"
00042
00043 #include "database_interface/database_test_object.h"
00044
00045 #include <ros/ros.h>
00046
00048 int main(int argc, char **argv)
00049 {
00050 bool TEST_INSERTION = true;
00051 bool TEST_DELETION = true;
00052 size_t NUM_OBJECTS = 2;
00053
00054 database_interface::PostgresqlDatabase database("wgs36", "5432", "willow",
00055 "willow", "database_test");
00056 if (!database.isConnected())
00057 {
00058 ROS_ERROR("Database failed to connect");
00059 return -1;
00060 }
00061 ROS_INFO("Database connected successfully");
00062
00063
00064 std::vector< boost::shared_ptr<database_interface::DatabaseTestObject> > objects;
00065
00066 if (!database.getList(objects))
00067 {
00068 ROS_ERROR("Failed to get list of test objects");
00069 return -1;
00070 }
00071
00072 ROS_INFO("Retrieved %zd objects", objects.size());
00073 if ( objects.size() != NUM_OBJECTS)
00074 {
00075 ROS_ERROR("Expected %d objects", (int)NUM_OBJECTS);
00076 return -1;
00077 }
00078
00079 for (size_t i=0; i<objects.size(); i++)
00080 {
00081 ROS_INFO("Object id %d, double field %f, string field %s, foreign field %d",
00082 objects[i]->pk_field_.get(),
00083 objects[i]->double_field_.get(),
00084 objects[i]->string_field_.get().c_str(),
00085 objects[i]->foreign_field_.get());
00086 std::string tags("{");
00087 for (size_t t=0; t<objects[i]->tags_field_.get().size(); t++)
00088 {
00089 if (t!=0) tags+= ",";
00090 tags += objects[i]->tags_field_.get().at(t);
00091 }
00092 tags += "}";
00093 ROS_INFO(" Tags: %s", tags.c_str());
00094
00095 if (!database.loadFromDatabase(&(objects[i]->binary_field_)) )
00096 {
00097 ROS_ERROR("Failed to load binary field for object %zd", i);
00098 return -1;
00099 }
00100 else
00101 {
00102 ROS_INFO(" Binary field: %zd bytes", objects[i]->binary_field_.get().size());
00103 }
00104 }
00105
00106
00107 if (NUM_OBJECTS)
00108 {
00109 database_interface::DatabaseTestObject clone(objects[0].get());
00110 ROS_INFO("Clone id %d, double field %f, string field %s, foreign field %d",
00111 clone.pk_field_.get(),
00112 clone.double_field_.get(),
00113 clone.string_field_.get().c_str(),
00114 clone.foreign_field_.get());
00115 if ( clone.string_field_.get() != objects[0]->string_field_.get())
00116 {
00117 ROS_ERROR("Clone string_field not identical to original: %s", clone.string_field_.get().c_str());
00118 return -1;
00119 }
00120 if ( clone.binary_field_.get().size() != objects[0]->binary_field_.get().size() )
00121 {
00122 ROS_ERROR("Clone binary field size not identical to original: %d", (int)clone.binary_field_.get().size());
00123 return -1;
00124 }
00125 ROS_INFO("Cloning successful");
00126
00127 std::string old_string = objects[0]->string_field_.get();
00128
00129 std::string new_string("10011001100122");
00130 objects[0]->string_field_.fromString(new_string);
00131 if ( !database.saveToDatabase(&(objects[0]->string_field_)) )
00132 {
00133 ROS_ERROR("Failed to save field to database");
00134 return -1;
00135 }
00136 objects[0]->string_field_.get().assign("foo");
00137 if (!database.loadFromDatabase(&(objects[0]->string_field_)) )
00138 {
00139 ROS_ERROR("Failed to retrieve field from database");
00140 return -1;
00141 }
00142 std::string test_string;
00143 objects[0]->string_field_.toString(test_string);
00144 if (test_string != new_string)
00145 {
00146 ROS_ERROR("Retrieved field does not match: %s", test_string.c_str());
00147 return -1;
00148 }
00149
00150 objects[0]->string_field_.get() = old_string;
00151 if ( !database.saveToDatabase(&(objects[0]->string_field_)) )
00152 {
00153 ROS_ERROR("Failed to revert field to old value");
00154 return -1;
00155 }
00156
00157 ROS_INFO("Saving and retrieving fields successful");
00158 }
00159
00160
00161 if (TEST_INSERTION)
00162 {
00163 database_interface::DatabaseTestObject new_object;
00164
00165 new_object.double_field_.get()=3.5;
00166 new_object.string_field_.get()="object3_string";
00167 new_object.tags_field_.get().push_back("object3_tag1");
00168 new_object.tags_field_.get().push_back("object3_tag2");
00169 new_object.tags_field_.get().push_back("object3_tag3");
00170 new_object.foreign_field_.get() = 300;
00171
00172 if (!database.insertIntoDatabase(&new_object))
00173 {
00174 ROS_ERROR("Failed to insert new model in database");
00175 }
00176 else
00177 {
00178 ROS_INFO("New model inserted successfully");
00179
00180 if (TEST_DELETION)
00181 {
00182 if (!database.deleteFromDatabase(&new_object))
00183 {
00184 ROS_ERROR("Failed to delete model from database");
00185 }
00186 else
00187 {
00188 ROS_INFO("New model deleted successfully");
00189 }
00190 }
00191 }
00192 }
00193
00194 ROS_INFO("All tests passed");
00195 return 0;
00196 }