00001 #include <cstdio> 00002 #include "../sqlite/sqlitewrap.h" 00003 #include <boost/test/unit_test.hpp> 00004 #include <boost/filesystem.hpp> 00005 #include <iostream> 00006 #include <vector> 00007 #include <exception> 00008 #include <algorithm> 00009 00010 void cleanup() 00011 { 00012 remove("asdf.db"); 00013 } 00014 00015 sqlite::Database get_test_db() 00016 { 00017 BOOST_REQUIRE_MESSAGE(!boost::filesystem::exists("test.db"), "test.db exists, aborting"); 00018 return sqlite::Database(":memory:"); 00019 } 00020 00021 void prepare_test_db(sqlite::Database& db) 00022 { 00023 sqlite::Statement st("CREATE TABLE test (ID INTEGER)", db); 00024 BOOST_REQUIRE_MESSAGE(!st.has_row(), "st has row"); 00025 BOOST_REQUIRE_MESSAGE(!st.done(), "st is done"); 00026 st.step(); 00027 BOOST_REQUIRE_MESSAGE(!st.has_row(), "st has row2"); 00028 BOOST_REQUIRE_MESSAGE(st.done(), "st is not done"); 00029 st = sqlite::Statement("CREATE TABLE empty(ID INTEGER PRIMARY KEY)", db); 00030 st.step(); 00031 st = sqlite::Statement("CREATE TABLE empty2(ID INTEGER PRIMARY KEY, A REAL)", db); 00032 st.step(); 00033 } 00034 void insert_data(sqlite::Database& db) 00035 { 00036 sqlite::Statement st("INSERT INTO test (ID) VALUES (1)", db); 00037 st.step(); 00038 st = sqlite::Statement("INSERT INTO test (ID) VALUES (2)", db); 00039 st.step(); 00040 st = sqlite::Statement("INSERT INTO test (ID) VALUES (3)", db); 00041 st.step(); 00042 } 00043 00044 class SimpleFixture 00045 { 00046 public: 00047 sqlite::Database db; 00048 SimpleFixture(): 00049 db(get_test_db()) 00050 { 00051 try 00052 { 00053 prepare_test_db(db); 00054 insert_data(db); 00055 } 00056 catch (...) 00057 { 00058 cleanup(); 00059 throw; 00060 } 00061 } 00062 ~SimpleFixture() 00063 { 00064 cleanup(); 00065 } 00066 }; 00067 00068 BOOST_FIXTURE_TEST_SUITE(SqliteSimpleTests, SimpleFixture) 00069 00070 BOOST_AUTO_TEST_CASE(fixture) 00071 { 00072 } 00073 00074 BOOST_AUTO_TEST_CASE(basic_usage) 00075 { 00076 sqlite::Statement st("SELECT ID FROM test ORDER BY ID", db); 00077 sqlite::Statement st3; 00078 sqlite::Statement st2(" ", db); 00079 st.step(); 00080 BOOST_REQUIRE_MESSAGE(st.has_row(), "not has row"); 00081 BOOST_REQUIRE_MESSAGE(!st.done(), "is done"); 00082 BOOST_CHECK_EQUAL(1, st.val_int(0)); 00083 st.step(); 00084 BOOST_REQUIRE(!st.done()); 00085 BOOST_REQUIRE(st.has_row()); 00086 BOOST_CHECK_EQUAL(2, st.val_int(0)); 00087 st.reset(); 00088 BOOST_REQUIRE(!st.done()); 00089 BOOST_REQUIRE(!st.has_row()); 00090 std::vector<int> exp {1, 2, 3}; 00091 std::vector<int> vals; 00092 while (!st.done()) 00093 { 00094 st.step(); 00095 if (st.has_row()) 00096 vals.push_back(st.val_int(0)); 00097 } 00098 BOOST_CHECK_MESSAGE(vals == exp, "vals == exp"); 00099 BOOST_REQUIRE_MESSAGE(!st.has_row(), "has row 3"); 00100 BOOST_REQUIRE_MESSAGE(st.done(), "not done 2"); 00101 } 00102 00103 BOOST_AUTO_TEST_CASE(sqlite_exception) 00104 { 00105 BOOST_CHECK_THROW(sqlite::Statement st("SELECT ID FROM asdf", db), sqlite::SqliteException); 00106 } 00107 00108 BOOST_AUTO_TEST_CASE(invalid_use_exception) 00109 { 00110 sqlite::Statement st("SELECT ID FROM test", db); 00111 st.step(); 00112 BOOST_CHECK_THROW(st.val_int(2), sqlite::InvalidUseException); 00113 BOOST_CHECK_THROW(st.val_int(-1), sqlite::InvalidUseException); 00114 } 00115 00116 BOOST_AUTO_TEST_CASE(step_failure) 00117 { 00118 { 00119 sqlite::Statement st1("INSERT INTO empty (ID) VALUES(1)", db); 00120 st1.step(); 00121 st1.reset(); 00122 BOOST_REQUIRE_THROW(st1.step(), sqlite::SqliteException); 00123 BOOST_CHECK(!st1.done()); 00124 BOOST_CHECK(!st1.has_row()); 00125 sqlite::Statement st2("DELETE FROM empty", db); 00126 st2.step(); 00127 BOOST_CHECK_NO_THROW(st1.step()); 00128 st2 = sqlite::Statement("SELECT ID FROM empty", db); 00129 st2.step(); 00130 BOOST_CHECK(st2.has_row()); 00131 } 00132 BOOST_CHECK_EQUAL(0, db.unfinalized()); 00133 } 00134 00135 BOOST_AUTO_TEST_CASE(bind) 00136 { 00137 sqlite::Statement st("INSERT INTO empty2 (ID, A) VALUES(?,?)", db); 00138 st.bind_int(1, 20); 00139 st.bind_double(2, 3); 00140 BOOST_CHECK_THROW(st.bind_double(0, 19), sqlite::SqliteException); 00141 BOOST_CHECK_THROW(st.bind_double(3, 29), sqlite::SqliteException); 00142 st.step(); 00143 sqlite::Statement st2("SELECT ID, A FROM empty2", db); 00144 st2.step(); 00145 BOOST_CHECK(st2.has_row()); 00146 BOOST_CHECK_EQUAL(st2.val_int(0), 20); 00147 BOOST_CHECK_CLOSE(st2.val_double(1), 3, 0.0001); 00148 st2.step(); 00149 BOOST_CHECK(st2.done()); 00150 } 00151 00152 BOOST_AUTO_TEST_CASE(exec) 00153 { 00154 sqlite::execute_sql("CREATE TABLE empty3 (A INTEGER, B INTEGER, C INTEGER)", db); 00155 sqlite::execute_sql("INSERT INTO empty3 (A, B, C) VALUES(1, 2, 3)", db); 00156 sqlite::execute_sql("INSERT INTO empty3 (A, B, C) VALUES(4, 5, 6)", db); 00157 sqlite::Statement st("INSERT INTO empty3 (A, B, C) VALUES(?, ?, ?)", db); 00158 st.bind(7, 8, 9); 00159 st.step(); 00160 st.bind(10, 11, 12); 00161 st.step(); 00162 std::vector<std::tuple<int, int, int> > exp {std::make_tuple(1, 2, 3), std::make_tuple(4, 5, 6), std::make_tuple(7, 8, 9), std::make_tuple(10, 11, 12)}; 00163 std::vector<std::tuple<int, int, int> > ret = sqlite::query_sql(std::string("SELECT A, B, C FROM empty3"), db, sqlite::colint(), sqlite::colint(), sqlite::colint()); 00164 std::sort(ret.begin(), ret.end()); 00165 BOOST_CHECK(ret == exp); 00166 } 00167 00168 BOOST_AUTO_TEST_CASE(int64) 00169 { 00170 sqlite::Statement st("INSERT INTO empty (ID) VALUES (?)", db); 00171 st.bind(100000000000); 00172 st.step(); 00173 std::vector<std::tuple<int64_t> > v {std::make_tuple(100000000000)}; 00174 std::vector<std::tuple<int64_t> > v2 = sqlite::query_sql("SELECT ID FROM empty", db, sqlite::colint64()); 00175 BOOST_CHECK(v == v2); 00176 } 00177 00178 BOOST_AUTO_TEST_CASE(db_new) 00179 { 00180 { 00181 sqlite::Database db("asdf.db"); 00182 BOOST_CHECK(db.is_new()); 00183 } 00184 { 00185 sqlite::Database db("asdf.db"); 00186 BOOST_CHECK(!db.is_new()); 00187 } 00188 } 00189 00190 BOOST_AUTO_TEST_SUITE_END()