00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00069 #define BOOST_TEST_MODULE "DB_Facade"
00070 #include <boost/test/floating_point_comparison.hpp>
00071 #include <boost/test/included/unit_test.hpp>
00072 #include "dbfacade.h"
00073
00074 #define EPSILON 1E-7
00075
00076 using namespace dbfacade;
00077 using namespace boost::unit_test;
00078 using std::string;
00079
00081 void prepareDatabase(DatabasePtr& db);
00082
00084 void deleteDatabase(DatabasePtr& db);
00085
00086 BOOST_AUTO_TEST_SUITE(dbfacade)
00087
00088 struct Fixture
00089 {
00090 Fixture()
00091 {
00092 prepareDatabase(db);
00093 }
00094 ~Fixture()
00095 {
00096 deleteDatabase(db);
00097 }
00098 DatabasePtr db;
00099 };
00100
00101 BOOST_FIXTURE_TEST_CASE(escape, Fixture)
00102 {
00103 string n("\n");
00104 string r("\r");
00105 string z(1, 26);
00106 string dq("\"");
00107 string sq("\'");
00108 string bs("\\");
00109 string c;
00110 for (int i=32; i<128; ++i)
00111 if (i != 34 && i != 39 && i != 92)
00112 c += string(1, i);
00113
00114
00115
00116 BOOST_CHECK_EQUAL(db->escape(n.c_str(), n.length()), "\\n");
00117 BOOST_CHECK_EQUAL(db->escape(r.c_str(), r.length()), "\\r");
00118 BOOST_CHECK_EQUAL(db->escape(z.c_str(), z.length()), "\\Z");
00119 BOOST_CHECK_EQUAL(db->escape(dq.c_str(), dq.length()), "\\\"");
00120 BOOST_CHECK_EQUAL(db->escape(bs.c_str(), bs.length()), "\\\\");
00121 BOOST_CHECK_EQUAL(db->escape(sq.c_str(), sq.length()), "\\'");
00122 BOOST_CHECK_EQUAL(db->escape(c.c_str(), c.length()), c);
00123 }
00124
00125 BOOST_FIXTURE_TEST_CASE(ResultPtr_RowPtr, Fixture)
00126 {
00127 ResultPtr res;
00128 RowPtr row;
00129 res = db->sqlSelect(
00130 "SELECT "
00131 "id, "
00132 "misc_boolean, "
00133 "misc_double, "
00134 "misc_int, "
00135 "misc_string "
00136 "FROM misc_data ORDER BY id");
00137 BOOST_CHECK(res);
00138
00139 BOOST_TEST_CHECKPOINT("first row");
00140 row = res->getNextRow();
00141 BOOST_CHECK(row);
00142 BOOST_CHECK_EQUAL(row->hasValueAt(0), true);
00143 BOOST_CHECK_EQUAL(row->hasValueAt(1), false);
00144 BOOST_CHECK_EQUAL(row->hasValueAt(2), true);
00145 BOOST_CHECK_EQUAL(row->hasValueAt(3), true);
00146 BOOST_CHECK_EQUAL(row->hasValueAt(4), true);
00147 BOOST_CHECK_EQUAL(row->hasValueAt(5), false);
00148 BOOST_CHECK_EQUAL(row->getIntAt(0), 1);
00149 BOOST_CHECK_THROW(row->getIntAt(1), EmptyResultSetException);
00150 BOOST_CHECK_CLOSE(row->getDoubleAt(2), 3.14, EPSILON);
00151 BOOST_CHECK_EQUAL(row->getIntAt(3), 42);
00152 BOOST_CHECK_EQUAL(row->getStringAt(4), "hello world");
00153
00154 BOOST_TEST_CHECKPOINT("second row");
00155 row = res->getNextRow();
00156 BOOST_CHECK(row);
00157 BOOST_CHECK_EQUAL(row->hasValueAt(0), true);
00158 BOOST_CHECK_EQUAL(row->hasValueAt(1), true);
00159 BOOST_CHECK_EQUAL(row->hasValueAt(2), false);
00160 BOOST_CHECK_EQUAL(row->hasValueAt(3), true);
00161 BOOST_CHECK_EQUAL(row->hasValueAt(4), true);
00162 BOOST_CHECK_EQUAL(row->hasValueAt(5), false);
00163 BOOST_CHECK_EQUAL(row->getIntAt(0), 2);
00164 BOOST_CHECK_EQUAL(row->getBooleanAt(1), true);
00165 BOOST_CHECK_THROW(row->getIntAt(2), EmptyResultSetException);
00166 BOOST_CHECK_EQUAL(row->getIntAt(3), 0);
00167 BOOST_CHECK_EQUAL(row->getStringAt(4), "");
00168
00169 BOOST_TEST_CHECKPOINT("third row");
00170 row = res->getNextRow();
00171 BOOST_CHECK(row);
00172 BOOST_CHECK_EQUAL(row->hasValueAt(0), true);
00173 BOOST_CHECK_EQUAL(row->hasValueAt(1), true);
00174 BOOST_CHECK_EQUAL(row->hasValueAt(2), true);
00175 BOOST_CHECK_EQUAL(row->hasValueAt(3), false);
00176 BOOST_CHECK_EQUAL(row->hasValueAt(4), true);
00177 BOOST_CHECK_EQUAL(row->hasValueAt(5), false);
00178 BOOST_CHECK_EQUAL(row->getIntAt(0), 3);
00179 BOOST_CHECK_EQUAL(row->getBooleanAt(1), false);
00180 BOOST_CHECK_CLOSE(row->getDoubleAt(2), 42., EPSILON);
00181 BOOST_CHECK_THROW(row->getIntAt(3), EmptyResultSetException);
00182 BOOST_CHECK_EQUAL(row->getStringAt(4), "123");
00183 BOOST_CHECK_THROW(row->getIntAt(5), EmptyResultSetException);
00184
00185 BOOST_TEST_CHECKPOINT("fourth row");
00186 row = res->getNextRow();
00187 BOOST_CHECK(row);
00188 BOOST_CHECK_EQUAL(row->hasValueAt(0), true);
00189 BOOST_CHECK_EQUAL(row->hasValueAt(1), true);
00190 BOOST_CHECK_EQUAL(row->hasValueAt(2), true);
00191 BOOST_CHECK_EQUAL(row->hasValueAt(3), true);
00192 BOOST_CHECK_EQUAL(row->hasValueAt(4), false);
00193 BOOST_CHECK_EQUAL(row->hasValueAt(5), false);
00194 BOOST_CHECK_EQUAL(row->getIntAt(0), 4);
00195 BOOST_CHECK_EQUAL(row->getBooleanAt(1), true);
00196 BOOST_CHECK_CLOSE(row->getDoubleAt(2), 0., EPSILON);
00197 BOOST_CHECK_EQUAL(row->getIntAt(3), 27);
00198 BOOST_CHECK_THROW(row->getIntAt(4), EmptyResultSetException);
00199 BOOST_CHECK_THROW(row->getIntAt(5), EmptyResultSetException);
00200
00201 BOOST_TEST_CHECKPOINT("fifth row");
00202 row = res->getNextRow();
00203 BOOST_CHECK(!row);
00204 }
00205
00206 BOOST_FIXTURE_TEST_CASE(sqlSelect, Fixture)
00207 {
00208 BOOST_CHECK_THROW(db->sqlSelect(
00209 "broken statement"),
00210 Exception);
00211 BOOST_CHECK_THROW(db->sqlSelect(
00212 "SELECT * FROM imaginary_table"),
00213 Exception);
00214 }
00215
00216 BOOST_FIXTURE_TEST_CASE(execute, Fixture)
00217 {
00218 BOOST_CHECK_THROW(db->execute(
00219 "INSERT INTO units(name) VALUES('first unit')"),
00220 UniqueConstraintException);
00221 BOOST_CHECK_THROW(db->execute(
00222 "INSERT INTO units_in_groups(unit_id, group_id) "
00223 "VALUES(4, 1)"),
00224 ForeignKeyConstraintException);
00225 BOOST_CHECK_THROW(db->execute(
00226 "UPDATE units_in_groups SET group_id=2 "
00227 "WHERE unit_id=2 AND group_id=1"),
00228 UniqueConstraintException);
00229 BOOST_CHECK_THROW(db->execute(
00230 "UPDATE units_in_groups SET group_id=4 "
00231 "WHERE unit_id=2 AND group_id=1"),
00232 ForeignKeyConstraintException);
00233 }
00234
00235 BOOST_FIXTURE_TEST_CASE(sqlInsert, Fixture)
00236 {
00237 db->sqlInsert(
00238 "INSERT INTO units(name) VALUES('fourth unit')");
00239 db->sqlInsert(
00240 "INSERT INTO groups(name) VALUES('fourth group')");
00241 db->execute(
00242 "INSERT INTO units_in_groups(unit_id, group_id) "
00243 "VALUES(4, 1)");
00244 db->execute(
00245 "INSERT INTO units_in_groups(unit_id, group_id) "
00246 "VALUES(4, 2)");
00247 db->execute(
00248 "INSERT INTO units_in_groups(unit_id, group_id) "
00249 "VALUES(4, 3)");
00250 db->execute(
00251 "INSERT INTO units_in_groups(unit_id, group_id) "
00252 "VALUES(4, 4)");
00253 BOOST_CHECK_EQUAL(db->getFirstString(
00254 "SELECT name FROM units WHERE id=4"),
00255 "fourth unit");
00256 BOOST_CHECK_EQUAL(db->getFirstString(
00257 "SELECT name FROM groups WHERE id=4"),
00258 "fourth group");
00259 BOOST_CHECK_EQUAL(db->getFirstInt(
00260 "SELECT count(*) FROM units_in_groups "
00261 "WHERE unit_id=4"),
00262 4);
00263
00264
00265
00266 }
00267
00268 BOOST_FIXTURE_TEST_CASE(sqlUpdate, Fixture)
00269 {
00270 BOOST_CHECK_EQUAL(db->sqlUpdate(
00271 "UPDATE units SET name='test' WHERE id=4"),
00272 0);
00273 BOOST_CHECK_EQUAL(db->sqlUpdate(
00274 "UPDATE units SET name='test' WHERE id=3"),
00275 1);
00276 BOOST_CHECK_EQUAL(db->sqlUpdate(
00277 "UPDATE misc_data SET misc_int=27 WHERE id IN (1,2)"),
00278 2);
00279 BOOST_CHECK_THROW(db->sqlUpdate(
00280 "UPDATE units SET id=1 WHERE id <3"),
00281 UniqueConstraintException);
00282 BOOST_CHECK_THROW(db->sqlUpdate(
00283 "UPDATE units SET name='test' WHERE id < 3"),
00284 UniqueConstraintException);
00285 BOOST_CHECK_THROW(db->sqlUpdate(
00286 "UPDATE units_in_groups SET group_id=3 "
00287 "WHERE unit_id=3 and group_id=1"),
00288 UniqueConstraintException);
00289 BOOST_CHECK_THROW(db->sqlUpdate(
00290 "UPDATE units_in_groups SET group_id=5 "
00291 "WHERE unit_id=3 and group_id=1"),
00292 ForeignKeyConstraintException);
00293 }
00294
00295 BOOST_FIXTURE_TEST_CASE(getFirstBoolean, Fixture)
00296 {
00297 BOOST_CHECK_THROW(db->getFirstBoolean(
00298 "SELECT misc_boolean FROM misc_data "
00299 "WHERE id=1"),
00300 EmptyResultSetException);
00301 BOOST_CHECK_EQUAL(db->getFirstBoolean(
00302 "SELECT misc_boolean FROM misc_data "
00303 "WHERE id=2"),
00304 true);
00305 BOOST_CHECK_EQUAL(db->getFirstBoolean(
00306 "SELECT misc_boolean FROM misc_data "
00307 "WHERE id=3"),
00308 false);
00309 BOOST_CHECK_EQUAL(db->getFirstBoolean(
00310 "SELECT misc_boolean FROM misc_data "
00311 "WHERE id=4"),
00312 true);
00313 BOOST_CHECK_THROW(db->getFirstBoolean(
00314 "SELECT misc_boolean FROM misc_data "
00315 "WHERE id=5"),
00316 EmptyResultSetException);
00317 }
00318
00319 BOOST_FIXTURE_TEST_CASE(getFirstDouble, Fixture)
00320 {
00321 BOOST_CHECK_CLOSE(db->getFirstDouble(
00322 "SELECT misc_double FROM misc_data "
00323 "WHERE id=1"),
00324 3.14, EPSILON);
00325 BOOST_CHECK_THROW(db->getFirstDouble(
00326 "SELECT misc_double FROM misc_data "
00327 "WHERE id=2"),
00328 EmptyResultSetException);
00329 BOOST_CHECK_CLOSE(db->getFirstDouble(
00330 "SELECT misc_double FROM misc_data "
00331 "WHERE id=3"),
00332 42.0, EPSILON);
00333 BOOST_CHECK_CLOSE(db->getFirstDouble(
00334 "SELECT misc_double FROM misc_data "
00335 "WHERE id=4"),
00336 0., EPSILON);
00337 BOOST_CHECK_THROW(db->getFirstDouble(
00338 "SELECT misc_double FROM misc_data "
00339 "WHERE id=5"),
00340 EmptyResultSetException);
00341 }
00342
00343 BOOST_FIXTURE_TEST_CASE(getFirstInt, Fixture)
00344 {
00345 BOOST_CHECK_EQUAL(db->getFirstInt(
00346 "SELECT misc_int FROM misc_data "
00347 "WHERE id=1"),
00348 42);
00349 BOOST_CHECK_EQUAL(db->getFirstInt(
00350 "SELECT misc_int FROM misc_data "
00351 "WHERE id=2"),
00352 0);
00353 BOOST_CHECK_THROW(db->getFirstInt(
00354 "SELECT misc_int FROM misc_data "
00355 "WHERE id=3"),
00356 EmptyResultSetException);
00357 BOOST_CHECK_EQUAL(db->getFirstInt(
00358 "SELECT misc_int FROM misc_data "
00359 "WHERE id=4"),
00360 27);
00361 BOOST_CHECK_THROW(db->getFirstInt(
00362 "SELECT misc_int FROM misc_data "
00363 "WHERE id=5"),
00364 EmptyResultSetException);
00365 }
00366
00367 BOOST_FIXTURE_TEST_CASE(getFirstString, Fixture)
00368 {
00369 BOOST_CHECK_EQUAL(db->getFirstString(
00370 "SELECT misc_string FROM misc_data "
00371 "WHERE id=1"),
00372 "hello world");
00373 BOOST_CHECK_EQUAL(db->getFirstString(
00374 "SELECT misc_string FROM misc_data "
00375 "WHERE id=2"),
00376 "");
00377 BOOST_CHECK_EQUAL(db->getFirstString(
00378 "SELECT misc_string FROM misc_data "
00379 "WHERE id=3"),
00380 "123");
00381 BOOST_CHECK_THROW(db->getFirstString(
00382 "SELECT misc_string FROM misc_data "
00383 "WHERE id=4"),
00384 EmptyResultSetException);
00385 BOOST_CHECK_THROW(db->getFirstString(
00386 "SELECT misc_string FROM misc_data "
00387 "WHERE id=5"),
00388 EmptyResultSetException);
00389 }
00390
00391 BOOST_AUTO_TEST_SUITE_END()
00392