postgresql_interface_test.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2009, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the Willow Garage nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *********************************************************************/
00034 
00035 // Author(s): Matei Ciocarlie
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 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends


database_interface
Author(s): Matei Ciocarlie and Lorenz Mosenlechner
autogenerated on Wed Jan 16 2013 13:32:14