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.

sqlitepp_test.cc 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright (C) 2014--2015 Robin Krahl <robin.krahl@ireas.org>
  2. // MIT license -- http://opensource.org/licenses/MIT
  3. #include <stdexcept>
  4. #include <fstream>
  5. #include <iostream>
  6. #include "gtest/gtest.h"
  7. #include "sqlitepp/sqlitepp.h"
  8. TEST(Database, openClose) {
  9. sqlitepp::Database database;
  10. EXPECT_FALSE(database.isOpen());
  11. database.open("/tmp/test.db");
  12. EXPECT_TRUE(database.isOpen());
  13. database.close();
  14. EXPECT_FALSE(database.isOpen());
  15. database.open("/tmp/test2.db");
  16. EXPECT_TRUE(database.isOpen());
  17. database.close();
  18. EXPECT_FALSE(database.isOpen());
  19. sqlitepp::Database database2("/tmp/test.db");
  20. EXPECT_TRUE(database2.isOpen());
  21. EXPECT_THROW(database2.open("/tmp/test2.db"), std::logic_error);
  22. EXPECT_TRUE(database2.isOpen());
  23. database2.close();
  24. EXPECT_FALSE(database2.isOpen());
  25. std::ifstream testStream("/tmp/test.db");
  26. EXPECT_TRUE(testStream.good());
  27. testStream.close();
  28. testStream.open("/tmp/test2.db");
  29. EXPECT_TRUE(testStream.good());
  30. testStream.close();
  31. }
  32. TEST(Database, copy) {
  33. sqlitepp::Database database;
  34. // MUST NOT COMPILE:
  35. // sqlitepp::Database database2 = database;
  36. database.close();
  37. sqlitepp::Database database3;
  38. // MUST NOT COMPILE:
  39. // database3 = database;
  40. database3.close();
  41. }
  42. TEST(Database, prepare) {
  43. sqlitepp::Database database("/tmp/test.db");
  44. std::shared_ptr<sqlitepp::Statement> statement = database.prepare(
  45. "CREATE TABLE IF NOT EXISTS test (id, value);");
  46. EXPECT_TRUE(statement->isOpen());
  47. statement->close();
  48. EXPECT_FALSE(statement->isOpen());
  49. database.close();
  50. }
  51. TEST(Database, execute) {
  52. sqlitepp::Database database("/tmp/test.db");
  53. database.execute("CREATE TABLE IF NOT EXISTS test (id, value);");
  54. }
  55. TEST(Database, insert) {
  56. sqlitepp::Database database("/tmp/test.db");
  57. std::shared_ptr<sqlitepp::Statement> statement = database.prepare(
  58. "INSERT INTO test (id, value) VALUES (:id, ?)");
  59. statement->bind(":id", 1);
  60. statement->bind(2, "test value");
  61. statement->execute();
  62. statement->reset();
  63. statement->bind(":id", 2);
  64. statement->bind(2, "other value");
  65. statement->execute();
  66. }
  67. TEST(Database, query) {
  68. sqlitepp::Database database("/tmp/test.db");
  69. std::shared_ptr<sqlitepp::Statement> statement = database.prepare(
  70. "SELECT id, value FROM test;");
  71. sqlitepp::ResultSet resultSet = statement->execute();
  72. EXPECT_TRUE(resultSet.canRead());
  73. EXPECT_EQ(2, resultSet.columnCount());
  74. int id = resultSet.readInt(0);
  75. std::string value = resultSet.readString(1);
  76. EXPECT_EQ(1, id);
  77. EXPECT_EQ("test value", value);
  78. EXPECT_TRUE(resultSet.next());
  79. EXPECT_TRUE(resultSet.canRead());
  80. id = resultSet.readInt(0);
  81. value = resultSet.readString(1);
  82. EXPECT_EQ(2, id);
  83. EXPECT_EQ("other value", value);
  84. EXPECT_FALSE(resultSet.next());
  85. EXPECT_FALSE(resultSet.canRead());
  86. }
  87. TEST(Database, cleanup) {
  88. sqlitepp::Database database("/tmp/test.db");
  89. database.execute("DROP TABLE test;");
  90. database.close();
  91. }