FOSSology  4.4.0
Open Source License Compliance by Open Source Software
test_fossdbmanagerclass.cc
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2015 Siemens AG
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
7 #include "testUtils.hpp"
8 
9 #include <cppunit/TestFixture.h>
10 #include <cppunit/extensions/HelperMacros.h>
11 
12 #include <vector>
13 
14 extern "C" {
15 #include <libfodbreposysconf.h>
16 }
17 
18 #include <iostream>
19 
21 
31 class FoLibCPPTest : public CPPUNIT_NS::TestFixture {
32 CPPUNIT_TEST_SUITE(FoLibCPPTest);
33  CPPUNIT_TEST(test_runSimpleQuery);
35  CPPUNIT_TEST(test_tableExists);
36  CPPUNIT_TEST(test_runPreparedStatement);
37  CPPUNIT_TEST(test_transactions);
40  CPPUNIT_TEST_SUITE_END();
41 private:
43 
44 public:
45  FoLibCPPTest() : dbManager(){}
46  FoLibCPPTest(const FoLibCPPTest& obj) : dbManager(obj.dbManager){}
47 
51  void setUp() {
52  dbManager = new fo::DbManager(createTestEnvironment("", NULL, 0));
53  }
54 
58  void tearDown() {
59  delete dbManager;
60  // dbManager connection is already closed by destructor
61  dropTestEnvironment(NULL, "", NULL);
62  }
63 
72  fo::DbManager manager = dbManager->spawn();
73  fo::QueryResult result = manager.queryPrintf("CREATE TABLE tbl(%s integer)", "col");
74 
75  CPPUNIT_ASSERT(result);
76  }
77 
89  fo::DbManager manager = dbManager->spawn();
90 
91  CPPUNIT_ASSERT(manager.queryPrintf("CREATE TABLE tbl(col integer)"));
92 
93  CPPUNIT_ASSERT(manager.tableExists("tbl"));
94  CPPUNIT_ASSERT(!manager.tableExists("tb"));
95  }
96 
107  fo::DbManager manager = dbManager->spawn();
108 
109  int val = 17;
110  {
111  CPPUNIT_ASSERT(manager.queryPrintf("CREATE TABLE tbl(col integer)"));
112  CPPUNIT_ASSERT(manager.queryPrintf("INSERT INTO tbl(col) VALUES (%d)", val));
113  }
114 
115  fo::QueryResult result = manager.queryPrintf("SELECT * FROM tbl");
116 
117  CPPUNIT_ASSERT(result);
118 
119  CPPUNIT_ASSERT_EQUAL(1, result.getRowCount());
120 
121  std::vector<std::string> row = result.getRow(0);
122 
123  CPPUNIT_ASSERT_EQUAL(1, (int) row.size());
124  CPPUNIT_ASSERT_EQUAL(std::string("17"), row[0]);
125 
126  std::vector<int> results = result.getSimpleResults(0, atoi);
127  std::vector<int> expected = {17};
128 
129  CPPUNIT_ASSERT_EQUAL(expected, results);
130  };
131 
141  fo::DbManager manager = dbManager->spawn();
142 
143  {
144  CPPUNIT_ASSERT(manager.queryPrintf("CREATE TABLE tbl(col integer)"));
145 
146  fo_dbManager_PreparedStatement* preparedStatement = fo_dbManager_PrepareStamement(
147  manager.getStruct_dbManager(),
148  "test",
149  "INSERT INTO tbl(col) VALUES ($1)",
150  int
151  );
152 
153  manager.begin();
154  for (int i = 0; i < 5; ++i) {
155  manager.execPrepared(preparedStatement, i * 2);
156  }
157  manager.commit();
158  }
159 
160  fo::QueryResult result = manager.queryPrintf("SELECT * FROM tbl");
161 
162  CPPUNIT_ASSERT(result);
163 
164  std::vector<int> results = result.getSimpleResults(0, atoi);
165 
166  std::vector<int> expected = {0, 2, 4, 6, 8};
167 
168  CPPUNIT_ASSERT_EQUAL(expected, results);
169  }
170 
184  fo::DbManager manager1 = dbManager->spawn();
185 
186  CPPUNIT_ASSERT(manager1.queryPrintf("CREATE TABLE tbl(col integer)"));
187  {
188  fo::DbManager manager2 = dbManager->spawn();
189 
190  fo_dbManager_PreparedStatement* preparedStatement1 = fo_dbManager_PrepareStamement(
191  manager1.getStruct_dbManager(),
192  "test",
193  "INSERT INTO tbl(col) VALUES ($1)",
194  int
195  );
196 
197  fo_dbManager_PreparedStatement* preparedStatement2 = fo_dbManager_PrepareStamement(
198  manager2.getStruct_dbManager(),
199  "test",
200  "INSERT INTO tbl(col) VALUES ($1)",
201  int
202  );
203 
204  CPPUNIT_ASSERT(manager1.begin());
205  CPPUNIT_ASSERT(manager2.begin());
206  for (int i = 0; i < 5; ++i) {
207  CPPUNIT_ASSERT(manager1.execPrepared(preparedStatement1, (i + 1) * 2));
208  CPPUNIT_ASSERT(manager2.execPrepared(preparedStatement2, i * 2));
209  }
210  CPPUNIT_ASSERT(manager1.commit());
211  CPPUNIT_ASSERT(manager2.rollback());
212  }
213 
214  fo::QueryResult result = manager1.queryPrintf("SELECT * FROM tbl");
215 
216  CPPUNIT_ASSERT(result);
217 
218  std::vector<int> results = result.getSimpleResults(0, atoi);
219 
220  std::vector<int> expected = {2, 4, 6, 8, 10};
221 
222  CPPUNIT_ASSERT_EQUAL(expected, results);
223  }
224 
234  fo::DbManager manager = dbManager->spawn();
235 
236  // TODO implement fo::DbManager::setLogFile() and check that errors pass through
237  std::cout << std::endl << "expecting errors" << std::endl << "-----" << std::endl;
238 
239  fo::QueryResult result = manager.queryPrintf("CREATE TABLE tbl( integer");
240 
241  std::cout << std::endl << "-----" << std::endl;
242 
243  CPPUNIT_ASSERT(!result);
244  }
245 
256  const char* sysConf = get_sysconfdir(); // [sic]
257 
258  // TODO make this correctly
259 
260  CPPUNIT_ASSERT(system((std::string("install -D ") + BUILDDIR + "/install/VERSION '" + sysConf + "/mods-enabled/an agent name/VERSION'").c_str()) >= 0);
261  char const* argv[] = {"an agent name", "-c", sysConf};
262  int argc = 3;
263 
264  fo::DbManager manager(&argc, (char**) argv); // [sic]
265 
266  fo::QueryResult result = manager.queryPrintf("CREATE TABLE tbl()");
267 
268  CPPUNIT_ASSERT(result);
269 
270  // TODO make this correctly
271  CPPUNIT_ASSERT(system((std::string("rm -rf '") + sysConf + "/mods-enabled'").c_str()) >= 0);
272  }
273 
274 };
275 
276 CPPUNIT_TEST_SUITE_REGISTRATION(FoLibCPPTest);
Test cases for CPP DB Manager.
void test_runSchedulerConnectConstructor()
void test_runBadCommandQueryCheckIfError()
void test_runCommandQueryCheckIfSuccess()
fo::DbManager * dbManager
Object for DbManager.
DB wrapper for agents.
QueryResult execPrepared(fo_dbManager_PreparedStatement *stmt,...) const
Execute a prepared statement with new parameters.
bool tableExists(const char *tableName) const
QueryResult queryPrintf(const char *queryFormat,...) const
Execute a query in printf format.
fo_dbManager * getStruct_dbManager() const
DbManager spawn() const
Wrapper for DB result.
char * get_sysconfdir()
get sysconfig dir path just created by create_db_repo_sysconf()
DB wrapper for agents.
Utility functions for test.