Belle II Software  release-05-01-25
sqlite.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2019 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Martin Ritter *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <framework/utilities/sqlite.h>
12 #include <framework/geometry/B2Vector3.h>
13 
14 #include <gtest/gtest.h>
15 #include <set>
16 
17 using namespace Belle2;
18 
19 namespace {
21  class SQLiteTest: public ::testing::Test {
22  protected:
24  sqlite3* m_connection{nullptr};
26  void SetUp() override
27  {
28  int result = sqlite3_open_v2(":memory:", &m_connection, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nullptr);
29  ASSERT_EQ(result, SQLITE_OK);
30  std::string sql = R"DOC(
31  CREATE TABLE test(name TEXT, x FLOAT, y FLOAT, z FLOAT);
32  INSERT INTO test VALUES ('IP', 0, 0, 0);
33  INSERT INTO test VALUES ('X', 1, 0, 0);
34  INSERT INTO test VALUES ('Y', 0, 1, 0);
35  INSERT INTO test VALUES ('Z', 0, 0, 1);
36  )DOC";
37  result = sqlite3_exec(m_connection, sql.c_str(), nullptr, nullptr, nullptr);
38  ASSERT_EQ(result, SQLITE_OK);
39  }
40 
42  void TearDown() override
43  {
44  int result = sqlite3_close_v2(m_connection);
45  ASSERT_EQ(result, SQLITE_OK);
46  }
47  };
48 
50  TEST_F(SQLiteTest, Statement)
51  {
52  sqlite::Statement<std::string, double> stmt(m_connection, "SELECT name, x from test ORDER by name DESC", false);
53  std::vector<std::tuple<std::string, double>> expected{{"IP", 0.}, {"X", 1.}, {"Y", 0.}, {"Z", 0.}};
54  for (auto && row : stmt) {
55  ASSERT_FALSE(expected.empty());
56  ASSERT_EQ(row, expected.back());
57  expected.pop_back();
58  }
59  ASSERT_TRUE(expected.empty());
60  }
61 
63  TEST_F(SQLiteTest, StatementTypeConversion)
64  {
65  sqlite::Statement<std::string, std::string> stmt(m_connection, "SELECT name, x from test ORDER by name DESC", false);
66  std::vector<std::tuple<std::string, std::string>> expected{{"IP", "0.0"}, {"X", "1.0"}, {"Y", "0.0"}, {"Z", "0.0"}};
67  for (auto && row : stmt) {
68  ASSERT_FALSE(expected.empty());
69  ASSERT_EQ(row, expected.back());
70  expected.pop_back();
71  }
72  ASSERT_TRUE(expected.empty());
73  }
74 
76  TEST_F(SQLiteTest, StatementTypeConversion2)
77  {
78  sqlite::Statement<std::string, int> stmt(m_connection, "SELECT name, x from test ORDER by name DESC", false);
79  std::vector<std::tuple<std::string, int>> expected{{"IP", 0}, {"X", 1}, {"Y", 0}, {"Z", 0}};
80  for (auto && row : stmt) {
81  ASSERT_FALSE(expected.empty());
82  ASSERT_EQ(row, expected.back());
83  expected.pop_back();
84  }
85  ASSERT_TRUE(expected.empty());
86  }
87 
89  TEST_F(SQLiteTest, SimpleStatement)
90  {
91  sqlite::SimpleStatement<std::string> stmt(m_connection, "SELECT name from test WHERE Y=?", false);
92  std::set<std::string> expected{"IP", "X", "Z"};
93  stmt.execute(0.);
94  std::set<std::string> actual(stmt.begin(), stmt.end());
95  ASSERT_EQ(expected, actual);
96  expected = {"Y"};
97  stmt.execute(1.);
98  actual = std::set<std::string>(stmt.begin(), stmt.end());
99  ASSERT_EQ(expected, actual);
100  }
101 
103  TEST_F(SQLiteTest, SimpleStatementParameterConversion)
104  {
105  sqlite::SimpleStatement<std::string> stmt(m_connection, "SELECT name from test WHERE Y=?", false);
106  std::set<std::string> expected{"IP", "X", "Z"};
107  stmt.execute("0");
108  std::set<std::string> actual(stmt.begin(), stmt.end());
109  ASSERT_EQ(expected, actual);
110  expected = {"Y"};
111  stmt.execute(1);
112  actual = std::set<std::string>(stmt.begin(), stmt.end());
113  ASSERT_EQ(expected, actual);
114  }
115 
117  TEST_F(SQLiteTest, ObjectStatement)
118  {
119  sqlite::ObjectStatement<B2Vector3D, double, double, double> stmt(m_connection, "SELECT x,y,z from test ORDER by name", false);
120  auto it = stmt.begin();
121  ASSERT_NE(it, stmt.end());
122  ASSERT_EQ(*it, B2Vector3D(0, 0, 0));
123  ASSERT_NE(++it, stmt.end());
124  ASSERT_EQ(*it, B2Vector3D(1, 0, 0));
125  ASSERT_NE(++it, stmt.end());
126  ASSERT_EQ(*it, B2Vector3D(0, 1, 0));
127  ASSERT_NE(++it, stmt.end());
128  ASSERT_EQ(*it, B2Vector3D(0, 0, 1));
129  ASSERT_EQ(++it, stmt.end());
130  }
131 
133  TEST_F(SQLiteTest, NotEnoughParams)
134  {
135  sqlite::SimpleStatement<std::string> stmt(m_connection, "SELECT name from test WHERE Y=?", false);
136  ASSERT_THROW(stmt.execute(), std::runtime_error);
137  }
138 
140  TEST_F(SQLiteTest, TooManyParams)
141  {
142  sqlite::SimpleStatement<std::string> stmt(m_connection, "SELECT name from test WHERE Y=?", false);
143  ASSERT_THROW(stmt.execute(0, "foo"), std::runtime_error);
144  }
145 
147  TEST_F(SQLiteTest, NotEnoughColumns)
148  {
149  auto construct = [this]() { return sqlite::Statement<std::string, int>(m_connection, "SELECT name from test ORDER by name DESC", false);};
150  ASSERT_THROW(construct(), std::runtime_error);
151  }
152 
154  TEST_F(SQLiteTest, TooManyColumns)
155  {
156  auto construct = [this]() { return sqlite::Statement<std::string>(m_connection, "SELECT name, x from test ORDER by name DESC", false);};
157  ASSERT_THROW(construct(), std::runtime_error);
158  }
159 }
sqlite::ObjectStatement
SQLite prepared statement wrapper.
Definition: sqlite.h:203
Belle2::B2Vector3D
B2Vector3< double > B2Vector3D
typedef for common usage with double
Definition: B2Vector3.h:507
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TEST_F
TEST_F(GlobalLabelTest, LargeNumberOfTimeDependentParameters)
Test large number of time-dep params for registration and retrieval.
Definition: globalLabel.cc:65