/home/martin/workspace/OpenStreetNav/src/test/psql.cpp
Go to the documentation of this file.
00001 #include <boost/test/unit_test.hpp>
00002 
00003 #include "../psql/psql.h"
00004 #include "../sqllib/sqllib.h"
00005 #include <tuple>
00006 
00007 class PSqlFixture
00008 {
00009 public:
00010     psql::Database db;
00011     PSqlFixture():
00012         db("", true)
00013     {
00014         psql::Statement<psql::BindTypes<>, psql::RetTypes<> > st("CREATE SCHEMA testing", db);
00015         st.execute();
00016         st = psql::Statement<psql::BindTypes<>, psql::RetTypes<> >("SET search_path TO testing", db);
00017         st.execute();
00018     }
00019     ~PSqlFixture()
00020     {
00021         try
00022         {
00023             psql::Statement<psql::BindTypes<>, psql::RetTypes<> > st("DROP SCHEMA testing CASCADE", db);
00024             st.execute();
00025         }
00026         catch (psql::PgSqlException& ex)
00027         {
00028             std::cout << "WARNING: problem deleting testing schema " << ex.what() << std::endl;
00029         }
00030     }
00031 };
00032 
00033 BOOST_AUTO_TEST_SUITE(psql_test)
00034 
00035 BOOST_AUTO_TEST_CASE(db)
00036 {
00037     psql::Database db("");
00038     db.get_db();
00039 }
00040 
00041 BOOST_AUTO_TEST_CASE(sync)
00042 {
00043     psql::Database db("", true);
00044     db.get_db();
00045 }
00046 
00047 BOOST_AUTO_TEST_CASE(dbfail)
00048 {
00049     BOOST_CHECK_THROW(psql::Database db("asdf"), psql::PgSqlException);
00050 }
00051 
00052 BOOST_AUTO_TEST_CASE(dbfail2)
00053 {
00054     BOOST_CHECK_THROW(psql::Database("user=asdfgasdafgasdghafasasa", true), psql::PgSqlException);
00055 }
00056 
00057 BOOST_AUTO_TEST_CASE(dbfailasync2)
00058 {
00059     psql::Database db("user=asdfgasdafgasdghafasasa", false);
00060     BOOST_CHECK_THROW(db.get_db(), psql::PgSqlException);
00061 }
00062 
00063 BOOST_AUTO_TEST_SUITE_END()
00064 
00065 BOOST_FIXTURE_TEST_SUITE(psql2, PSqlFixture)
00066 
00067 BOOST_AUTO_TEST_CASE(statement)
00068 {
00069     auto st1(sqllib::get_create_test_table(db));
00070     st1.execute();
00071     auto st2(sqllib::get_insert_test_table(db));
00072     st2.execute(32, "asdf", 52341093);
00073     auto st3(sqllib::get_test_select(db));
00074     st3.execute(32);
00075     auto res(st3.get_row(0));
00076     std::tuple<int, std::string, int64_t> tup(32,  "asdf", 52341093);
00077     BOOST_CHECK(res == tup);
00078 }
00079 
00080 BOOST_AUTO_TEST_CASE(named_statement)
00081 {
00082     auto st1(sqllib::get_create_test_table(db, true, "asdf"));
00083     st1.execute();
00084     auto st2(sqllib::get_insert_test_table(db, true, "bvba"));
00085     st2.execute(32, "asdf", 52341093);
00086     auto st3(sqllib::get_test_select(db, true, "agsdf"));
00087     st3.execute(32);
00088     auto res(st3.get_row(0));
00089     std::tuple<int, std::string, int64_t> tup(32, "asdf", 52341093);
00090     BOOST_CHECK(res == tup);
00091 }
00092 
00093 BOOST_AUTO_TEST_CASE(utils)
00094 {
00095     auto st1(sqllib::get_create_test_table(db));
00096     st1.execute();
00097     auto st2(sqllib::get_insert_test_table(db));
00098     st2.execute(32, "asdf", 52341093);
00099     auto st3(sqllib::get_test_select(db));
00100     auto ret = psql::exec_statement(st3, 32);
00101     std::tuple<int, std::string, int64_t> tup(32, "asdf", 52341093);
00102     std::vector<std::tuple<int, std::string, int64_t> > vect {tup};
00103     std::vector<int64_t> vect2 {52341093};
00104     auto ret2 = psql::exec_statement_col<2>(st3, 32);
00105     BOOST_CHECK(ret == vect);
00106     BOOST_CHECK(ret2 == vect2);
00107 }
00108 
00109 BOOST_AUTO_TEST_CASE(copy)
00110 {
00111     auto st1(sqllib::get_create_test_table(db));
00112     st1.execute();
00113     auto st2(sqllib::get_copy_test_table(db));
00114     BOOST_CHECK(!st2.copying());
00115     st2.execute();
00116     BOOST_CHECK(st2.copying());
00117     st2.copy_data(32, "asdf", 52341093);
00118     st2.end_copy();
00119     BOOST_CHECK(!st2.copying());
00120     auto st3(sqllib::get_test_select(db));
00121     st3.execute(32);
00122     BOOST_CHECK(st3.row_count() == 1);
00123     auto res(st3.get_row(0));
00124     std::tuple<int, std::string, int64_t> tup(32,  "asdf", 52341093);
00125     BOOST_CHECK(res == tup);
00126 }
00127 
00128 BOOST_AUTO_TEST_CASE(cursor)
00129 {
00130     auto st1(sqllib::get_create_test_table(db));
00131     st1.execute();
00132     auto st2(sqllib::get_insert_test_table(db));
00133     st2.execute(32, "asdf", 52341093);
00134     st2.execute(34, "asdfa", 52341093);
00135     st2.execute(35, "asdfb", 52341093);
00136     st2.execute(36, "asdfc", 52341093);
00137     st2.execute(37, "asdfd", 52341093);
00138     auto st3(sqllib::get_test_select_gt(db));
00139     db.begin_transaction();
00140     try
00141     {
00142         auto curs = psql::make_cursor(db, "test_crs", st3);
00143         curs.open(34);
00144         curs.fetch(3);
00145         std::vector<std::tuple<int, std::string, int64_t> > exp
00146         {
00147             std::make_tuple(35, "asdfb", 52341093),
00148             std::make_tuple(36, "asdfc", 52341093),
00149             std::make_tuple(37, "asdfd", 52341093)
00150         };
00151         BOOST_CHECK(curs.get_buffer() == exp);
00152         curs.close();
00153         db.rollback_transaction();
00154     }
00155     catch (...)
00156     {
00157         try
00158         {
00159             db.rollback_transaction();
00160         }
00161         catch (...)
00162         {
00163             std::cout << "WARNING: Failed to rollback" << std::endl;
00164         }
00165         throw;
00166     }
00167 }
00168 
00169 BOOST_AUTO_TEST_SUITE_END()
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines