39 #include <boost/shared_ptr.hpp>
48 int main(
int argc,
char **argv)
50 bool TEST_INSERTION =
true;
51 bool TEST_DELETION =
true;
52 size_t NUM_OBJECTS = 2;
55 "willow",
"database_test");
58 ROS_ERROR(
"Database failed to connect");
61 ROS_INFO(
"Database connected successfully");
64 std::vector< boost::shared_ptr<database_interface::DatabaseTestObject> > objects;
68 ROS_ERROR(
"Failed to get list of test objects");
72 ROS_INFO(
"Retrieved %zd objects", objects.size());
73 if ( objects.size() != NUM_OBJECTS)
75 ROS_ERROR(
"Expected %d objects", (
int)NUM_OBJECTS);
79 for (
size_t i=0; i<objects.size(); i++)
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++)
90 tags += objects[i]->tags_field_.get().at(t);
93 ROS_INFO(
" Tags: %s", tags.c_str());
97 ROS_ERROR(
"Failed to load binary field for object %zd", i);
102 ROS_INFO(
" Binary field: %zd bytes", objects[i]->binary_field_.get().size());
110 ROS_INFO(
"Clone id %d, double field %f, string field %s, foreign field %d",
117 ROS_ERROR(
"Clone string_field not identical to original: %s", clone.
string_field_.
get().c_str());
120 if ( clone.
binary_field_.
get().size() != objects[0]->binary_field_.get().size() )
122 ROS_ERROR(
"Clone binary field size not identical to original: %d", (
int)clone.
binary_field_.
get().size());
125 ROS_INFO(
"Cloning successful");
127 std::string old_string = objects[0]->string_field_.get();
129 std::string new_string(
"10011001100122");
130 objects[0]->string_field_.fromString(new_string);
133 ROS_ERROR(
"Failed to save field to database");
136 objects[0]->string_field_.get().assign(
"foo");
139 ROS_ERROR(
"Failed to retrieve field from database");
142 std::string test_string;
143 objects[0]->string_field_.toString(test_string);
144 if (test_string != new_string)
146 ROS_ERROR(
"Retrieved field does not match: %s", test_string.c_str());
150 objects[0]->string_field_.get() = old_string;
153 ROS_ERROR(
"Failed to revert field to old value");
157 ROS_INFO(
"Saving and retrieving fields successful");
174 ROS_ERROR(
"Failed to insert new model in database");
178 ROS_INFO(
"New model inserted successfully");
184 ROS_ERROR(
"Failed to delete model from database");
188 ROS_INFO(
"New model deleted successfully");
194 ROS_INFO(
"All tests passed");
DBField< double > double_field_
The C++ version of an object stored in a database.
DBField< std::string > string_field_
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< int > foreign_field_
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.