postgresql_interface_test.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2009, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 // Author(s): Matei Ciocarlie
36 
37 #include <algorithm>
38 #include <vector>
39 #include <boost/shared_ptr.hpp>
40 
42 
44 
45 #include <ros/ros.h>
46 
48 int main(int argc, char **argv)
49 {
50  bool TEST_INSERTION = true;
51  bool TEST_DELETION = true;
52  size_t NUM_OBJECTS = 2;
53 
54  database_interface::PostgresqlDatabase database("wgs36", "5432", "willow",
55  "willow", "database_test");
56  if (!database.isConnected())
57  {
58  ROS_ERROR("Database failed to connect");
59  return -1;
60  }
61  ROS_INFO("Database connected successfully");
62 
63 
64  std::vector< boost::shared_ptr<database_interface::DatabaseTestObject> > objects;
65 
66  if (!database.getList(objects))
67  {
68  ROS_ERROR("Failed to get list of test objects");
69  return -1;
70  }
71 
72  ROS_INFO("Retrieved %zd objects", objects.size());
73  if ( objects.size() != NUM_OBJECTS)
74  {
75  ROS_ERROR("Expected %d objects", (int)NUM_OBJECTS);
76  return -1;
77  }
78 
79  for (size_t i=0; i<objects.size(); i++)
80  {
81  ROS_INFO("Object id %d, double field %f, string field %s, foreign field %d",
82  objects[i]->pk_field_.get(),
83  objects[i]->double_field_.get(),
84  objects[i]->string_field_.get().c_str(),
85  objects[i]->foreign_field_.get());
86  std::string tags("{");
87  for (size_t t=0; t<objects[i]->tags_field_.get().size(); t++)
88  {
89  if (t!=0) tags+= ",";
90  tags += objects[i]->tags_field_.get().at(t);
91  }
92  tags += "}";
93  ROS_INFO(" Tags: %s", tags.c_str());
94 
95  if (!database.loadFromDatabase(&(objects[i]->binary_field_)) )
96  {
97  ROS_ERROR("Failed to load binary field for object %zd", i);
98  return -1;
99  }
100  else
101  {
102  ROS_INFO(" Binary field: %zd bytes", objects[i]->binary_field_.get().size());
103  }
104  }
105 
106 
107  if (NUM_OBJECTS)
108  {
109  database_interface::DatabaseTestObject clone(objects[0].get());
110  ROS_INFO("Clone id %d, double field %f, string field %s, foreign field %d",
111  clone.pk_field_.get(),
112  clone.double_field_.get(),
113  clone.string_field_.get().c_str(),
114  clone.foreign_field_.get());
115  if ( clone.string_field_.get() != objects[0]->string_field_.get())
116  {
117  ROS_ERROR("Clone string_field not identical to original: %s", clone.string_field_.get().c_str());
118  return -1;
119  }
120  if ( clone.binary_field_.get().size() != objects[0]->binary_field_.get().size() )
121  {
122  ROS_ERROR("Clone binary field size not identical to original: %d", (int)clone.binary_field_.get().size());
123  return -1;
124  }
125  ROS_INFO("Cloning successful");
126 
127  std::string old_string = objects[0]->string_field_.get();
128 
129  std::string new_string("10011001100122");
130  objects[0]->string_field_.fromString(new_string);
131  if ( !database.saveToDatabase(&(objects[0]->string_field_)) )
132  {
133  ROS_ERROR("Failed to save field to database");
134  return -1;
135  }
136  objects[0]->string_field_.get().assign("foo");
137  if (!database.loadFromDatabase(&(objects[0]->string_field_)) )
138  {
139  ROS_ERROR("Failed to retrieve field from database");
140  return -1;
141  }
142  std::string test_string;
143  objects[0]->string_field_.toString(test_string);
144  if (test_string != new_string)
145  {
146  ROS_ERROR("Retrieved field does not match: %s", test_string.c_str());
147  return -1;
148  }
149 
150  objects[0]->string_field_.get() = old_string;
151  if ( !database.saveToDatabase(&(objects[0]->string_field_)) )
152  {
153  ROS_ERROR("Failed to revert field to old value");
154  return -1;
155  }
156 
157  ROS_INFO("Saving and retrieving fields successful");
158  }
159 
160 
161  if (TEST_INSERTION)
162  {
164 
165  new_object.double_field_.get()=3.5;
166  new_object.string_field_.get()="object3_string";
167  new_object.tags_field_.get().push_back("object3_tag1");
168  new_object.tags_field_.get().push_back("object3_tag2");
169  new_object.tags_field_.get().push_back("object3_tag3");
170  new_object.foreign_field_.get() = 300;
171 
172  if (!database.insertIntoDatabase(&new_object))
173  {
174  ROS_ERROR("Failed to insert new model in database");
175  }
176  else
177  {
178  ROS_INFO("New model inserted successfully");
179 
180  if (TEST_DELETION)
181  {
182  if (!database.deleteFromDatabase(&new_object))
183  {
184  ROS_ERROR("Failed to delete model from database");
185  }
186  else
187  {
188  ROS_INFO("New model deleted successfully");
189  }
190  }
191  }
192  }
193 
194  ROS_INFO("All tests passed");
195  return 0;
196 }
const T & get() const
Definition: db_field.h:321
The C++ version of an object stored in a database.
int main(int argc, char **argv)
A little test program for the model database.
bool getList(std::vector< boost::shared_ptr< T > > &vec, const T &example, std::string where_clause) const
Retreives the list of objects of a certain type from the database.
DBField< std::vector< std::string > > tags_field_
bool insertIntoDatabase(DBClass *instance)
Inserts a new instance of a DBClass into the database.
bool deleteFromDatabase(DBClass *instance)
Deletes an instance of a DBClass from the database.
DBField< std::vector< char > > binary_field_
bool saveToDatabase(const DBFieldBase *field)
Writes the value of one particular field of a DBClass to the database.
bool loadFromDatabase(DBFieldBase *field) const
Reads the value of one particular fields of a DBClass from the database.
bool isConnected() const
Returns true if the interface is connected to the database and ready to go.


database_interface
Author(s): Matei Ciocarlie and Lorenz Mosenlechner
autogenerated on Mon Mar 2 2015 01:31:34