C++ binding for the SQLite library https://code.ireas.org/sqlitepp/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sqlitepptest.cpp 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * (C) 2014 Robin Krahl
  3. * MIT license -- http://opensource.org/licenses/MIT
  4. */
  5. #include "sqlitepp.h"
  6. #define BOOST_TEST_DYN_LINK
  7. #define BOOST_TEST_MODULE SQLitePPTest
  8. #include <boost/test/unit_test.hpp>
  9. #include <fstream>
  10. #include <iostream>
  11. #include <stdexcept>
  12. BOOST_AUTO_TEST_CASE(openClose) {
  13. sqlitepp::Database database;
  14. BOOST_CHECK(!database.isOpen());
  15. database.open("/tmp/test.db");
  16. BOOST_CHECK(database.isOpen());
  17. database.close();
  18. BOOST_CHECK(!database.isOpen());
  19. database.open("/tmp/test2.db");
  20. BOOST_CHECK(database.isOpen());
  21. database.close();
  22. BOOST_CHECK(!database.isOpen());
  23. sqlitepp::Database database2("/tmp/test.db");
  24. BOOST_CHECK(database2.isOpen());
  25. try {
  26. database2.open("/tmp/test2.db");
  27. BOOST_ERROR("Calling open() to an open database does not throw an exception.");
  28. } catch (std::logic_error &) {
  29. // everything fine
  30. }
  31. BOOST_CHECK(database2.isOpen());
  32. database2.close();
  33. BOOST_CHECK(!database2.isOpen());
  34. std::ifstream testStream("/tmp/test.db");
  35. BOOST_CHECK(testStream.good());
  36. testStream.close();
  37. testStream.open("/tmp/test2.db");
  38. BOOST_CHECK(testStream.good());
  39. testStream.close();
  40. }
  41. BOOST_AUTO_TEST_CASE(copy) {
  42. sqlitepp::Database database;
  43. // MUST NOT COMPILE:
  44. // sqlitepp::Database database2 = database;
  45. database.close();
  46. sqlitepp::Database database3;
  47. // MUST NOT COMPILE:
  48. // database3 = database;
  49. database3.close();
  50. }
  51. BOOST_AUTO_TEST_CASE(prepare) {
  52. sqlitepp::Database database("/tmp/test.db");
  53. sqlitepp::Statement statement(database, "CREATE TABLE IF NOT EXISTS test (id, value);");
  54. // TODO check std::logic_error
  55. BOOST_CHECK(statement.isOpen());
  56. statement.finalize();
  57. BOOST_CHECK(!statement.isOpen());
  58. database.close();
  59. }
  60. BOOST_AUTO_TEST_CASE(execute) {
  61. sqlitepp::Database database("/tmp/test.db");
  62. database.execute("CREATE TABLE IF NOT EXISTS test (id, value);");
  63. sqlitepp::Statement statement(database, "INSERT INTO test (id, value) VALUES (:id, ?)");
  64. statement.bindInt(":id", 1);
  65. statement.bindString(2, "test value");
  66. statement.step();
  67. statement.reset();
  68. statement.bindInt(":id", 2);
  69. statement.bindString(2, "other value");
  70. statement.step();
  71. statement.finalize();
  72. }
  73. BOOST_AUTO_TEST_CASE(query) {
  74. sqlitepp::Database database("/tmp/test.db");
  75. sqlitepp::Statement statement(database, "SELECT id, value FROM test;");
  76. bool hasNext = statement.step();
  77. BOOST_CHECK(hasNext);
  78. BOOST_CHECK_EQUAL(statement.columnCount(), 2);
  79. int id = statement.readInt(0);
  80. std::string value = statement.readString(1);
  81. BOOST_CHECK_EQUAL(id, 1);
  82. BOOST_CHECK_EQUAL(value, "test value");
  83. hasNext = statement.step();
  84. BOOST_CHECK(hasNext);
  85. id = statement.readInt(0);
  86. value = statement.readString(1);
  87. BOOST_CHECK_EQUAL(id, 2);
  88. BOOST_CHECK_EQUAL(value, "other value");
  89. hasNext = statement.step();
  90. BOOST_CHECK(!hasNext);
  91. statement.finalize();
  92. database.close();
  93. }
  94. BOOST_AUTO_TEST_CASE(cleanup) {
  95. sqlitepp::Database database("/tmp/test.db");
  96. database.execute("DROP TABLE test;");
  97. database.close();
  98. }