SchemaVersion.cpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: BSD-3-Clause
2 
3 /*
4  * Copyright (c) 2020, Bjarne von Horn
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  * * Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  * * Neither the name of the copyright holder nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL BJARNE VON HORN BE LIABLE FOR ANY DIRECT,
22  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <gtest/gtest.h>
34 #include <sqlite3.h>
35 
36 class SchemaVersion : public ::testing::Test
37 {
38 protected:
39  void SetUp() override
40  {
41  sqlite3* db = nullptr;
42  ASSERT_EQ(sqlite3_open(":memory:", &db), SQLITE_OK);
44  }
45 
47 };
48 
50 
51 TEST_F(SchemaVersion, WrongSchemaVersion)
52 {
53  const int wrong_schema = 2;
54  static_assert(wrong_schema != warehouse_ros_sqlite::schema::VERSION, "schema version is not invalid");
55 
56  const std::string query = "PRAGMA user_version = " + std::to_string(wrong_schema) + ";";
57  ASSERT_EQ(sqlite3_exec(db_.get(), query.c_str(), nullptr, nullptr, nullptr), SQLITE_OK);
58 
60  try
61  {
62  conn.connect();
63  FAIL() << "connect() didn't throw any exception";
64  }
66  {
68  EXPECT_EQ(m.version_in_database_, wrong_schema);
69  }
70  catch (...)
71  {
72  FAIL() << "connect() threw the wrong exception type";
73  }
74 }
75 
76 TEST_F(SchemaVersion, NoSchemaVersionButTable)
77 {
78  const int default_schema = 0;
79  sqlite3_stmt* stmt = nullptr;
80  ASSERT_EQ(sqlite3_prepare_v2(db_.get(), "PRAGMA user_version;", -1, &stmt, nullptr), SQLITE_OK);
82  ASSERT_EQ(sqlite3_step(stmt), SQLITE_ROW);
83  ASSERT_EQ(sqlite3_column_int(stmt, 0), default_schema);
84 
85  const std::string query =
86  std::string("CREATE TABLE ") + warehouse_ros_sqlite::schema::M_D5_TABLE_NAME + "(ID INTEGER PRIMARY KEY);";
87  ASSERT_EQ(sqlite3_exec(db_.get(), query.c_str(), nullptr, nullptr, nullptr), SQLITE_OK);
88 
90  EXPECT_THROW(conn.connect(), warehouse_ros_sqlite::InternalError);
91 }
92 
93 TEST_F(SchemaVersion, CorrectSet)
94 {
95  {
97  ASSERT_NO_THROW(conn.connect());
98  }
99  {
101  ASSERT_NO_THROW(conn.connect());
102  }
103  sqlite3_stmt* stmt = nullptr;
104  ASSERT_EQ(sqlite3_prepare_v2(db_.get(), "PRAGMA user_version;", -1, &stmt, nullptr), SQLITE_OK);
106  ASSERT_EQ(sqlite3_step(stmt), SQLITE_ROW);
107  ASSERT_EQ(sqlite3_column_int(stmt, 0), warehouse_ros_sqlite::schema::VERSION);
108 }
109 
110 int main(int argc, char** argv)
111 {
112  ros::Time::init();
113  ::testing::InitGoogleTest(&argc, argv);
114  return RUN_ALL_TESTS();
115 }
constexpr const char * M_D5_TABLE_NAME
Definition: utils.h:87
void SetUp() override
static void init()
TEST_F(SchemaVersion, WrongSchemaVersion)
int main(int argc, char **argv)
static warehouse_ros_sqlite::sqlite3_ptr db_
std::shared_ptr< sqlite3 > sqlite3_ptr
Definition: utils.h:51
std::unique_ptr< sqlite3_stmt, Sqlite3StmtDeleter > sqlite3_stmt_ptr
Definition: utils.h:50
WAREHOUSE_ROS_SQLITE_EXPORT void sqlite3_delete(sqlite3 *db)


warehouse_ros_sqlite
Author(s): Bjarne von Horn
autogenerated on Fri Nov 11 2022 03:44:33