FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
test_fossdbmanagerclass.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015, Siemens AG
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License version 2
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software Foundation,
15  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16  */
17 
18 #include "testUtils.hpp"
19 
20 #include <cppunit/TestFixture.h>
21 #include <cppunit/extensions/HelperMacros.h>
22 
23 #include <vector>
24 
25 extern "C" {
26 #include <libfodbreposysconf.h>
27 }
28 
29 #include <iostream>
30 
32 
42 class FoLibCPPTest : public CPPUNIT_NS::TestFixture {
43 CPPUNIT_TEST_SUITE(FoLibCPPTest);
44  CPPUNIT_TEST(test_runSimpleQuery);
46  CPPUNIT_TEST(test_tableExists);
47  CPPUNIT_TEST(test_runPreparedStatement);
48  CPPUNIT_TEST(test_transactions);
51  CPPUNIT_TEST_SUITE_END();
52 private:
54 
55 public:
59  void setUp() {
60  dbManager = new fo::DbManager(createTestEnvironment("", NULL, 0));
61  }
62 
66  void tearDown() {
67  delete dbManager;
68  // dbManager connection is already closed by destructor
69  dropTestEnvironment(NULL, "", NULL);
70  }
71 
80  fo::DbManager manager = dbManager->spawn();
81  fo::QueryResult result = manager.queryPrintf("CREATE TABLE tbl(%s integer)", "col");
82 
83  CPPUNIT_ASSERT(result);
84  }
85 
97  fo::DbManager manager = dbManager->spawn();
98 
99  CPPUNIT_ASSERT(manager.queryPrintf("CREATE TABLE tbl(col integer)"));
100 
101  CPPUNIT_ASSERT(manager.tableExists("tbl"));
102  CPPUNIT_ASSERT(!manager.tableExists("tb"));
103  }
104 
115  fo::DbManager manager = dbManager->spawn();
116 
117  int val = 17;
118  {
119  CPPUNIT_ASSERT(manager.queryPrintf("CREATE TABLE tbl(col integer)"));
120  CPPUNIT_ASSERT(manager.queryPrintf("INSERT INTO tbl(col) VALUES (%d)", val));
121  }
122 
123  fo::QueryResult result = manager.queryPrintf("SELECT * FROM tbl");
124 
125  CPPUNIT_ASSERT(result);
126 
127  CPPUNIT_ASSERT_EQUAL(1, result.getRowCount());
128 
129  std::vector<std::string> row = result.getRow(0);
130 
131  CPPUNIT_ASSERT_EQUAL(1, (int) row.size());
132  CPPUNIT_ASSERT_EQUAL(std::string("17"), row[0]);
133 
134  std::vector<int> results = result.getSimpleResults(0, atoi);
135  std::vector<int> expected = {17};
136 
137  CPPUNIT_ASSERT_EQUAL(expected, results);
138  };
139 
149  fo::DbManager manager = dbManager->spawn();
150 
151  {
152  CPPUNIT_ASSERT(manager.queryPrintf("CREATE TABLE tbl(col integer)"));
153 
154  fo_dbManager_PreparedStatement* preparedStatement = fo_dbManager_PrepareStamement(
155  manager.getStruct_dbManager(),
156  "test",
157  "INSERT INTO tbl(col) VALUES ($1)",
158  int
159  );
160 
161  manager.begin();
162  for (int i = 0; i < 5; ++i) {
163  manager.execPrepared(preparedStatement, i * 2);
164  }
165  manager.commit();
166  }
167 
168  fo::QueryResult result = manager.queryPrintf("SELECT * FROM tbl");
169 
170  CPPUNIT_ASSERT(result);
171 
172  std::vector<int> results = result.getSimpleResults(0, atoi);
173 
174  std::vector<int> expected = {0, 2, 4, 6, 8};
175 
176  CPPUNIT_ASSERT_EQUAL(expected, results);
177  }
178 
192  fo::DbManager manager1 = dbManager->spawn();
193 
194  CPPUNIT_ASSERT(manager1.queryPrintf("CREATE TABLE tbl(col integer)"));
195  {
196  fo::DbManager manager2 = dbManager->spawn();
197 
198  fo_dbManager_PreparedStatement* preparedStatement1 = fo_dbManager_PrepareStamement(
199  manager1.getStruct_dbManager(),
200  "test",
201  "INSERT INTO tbl(col) VALUES ($1)",
202  int
203  );
204 
205  fo_dbManager_PreparedStatement* preparedStatement2 = fo_dbManager_PrepareStamement(
206  manager2.getStruct_dbManager(),
207  "test",
208  "INSERT INTO tbl(col) VALUES ($1)",
209  int
210  );
211 
212  CPPUNIT_ASSERT(manager1.begin());
213  CPPUNIT_ASSERT(manager2.begin());
214  for (int i = 0; i < 5; ++i) {
215  CPPUNIT_ASSERT(manager1.execPrepared(preparedStatement1, (i + 1) * 2));
216  CPPUNIT_ASSERT(manager2.execPrepared(preparedStatement2, i * 2));
217  }
218  CPPUNIT_ASSERT(manager1.commit());
219  CPPUNIT_ASSERT(manager2.rollback());
220  }
221 
222  fo::QueryResult result = manager1.queryPrintf("SELECT * FROM tbl");
223 
224  CPPUNIT_ASSERT(result);
225 
226  std::vector<int> results = result.getSimpleResults(0, atoi);
227 
228  std::vector<int> expected = {2, 4, 6, 8, 10};
229 
230  CPPUNIT_ASSERT_EQUAL(expected, results);
231  }
232 
242  fo::DbManager manager = dbManager->spawn();
243 
244  // TODO implement fo::DbManager::setLogFile() and check that errors pass through
245  std::cout << std::endl << "expecting errors" << std::endl << "-----" << std::endl;
246 
247  fo::QueryResult result = manager.queryPrintf("CREATE TABLE tbl( integer");
248 
249  std::cout << std::endl << "-----" << std::endl;
250 
251  CPPUNIT_ASSERT(!result);
252  }
253 
264  const char* sysConf = get_sysconfdir(); // [sic]
265 
266  // TODO make this correctly
267  CPPUNIT_ASSERT(system((std::string("install -D ../../../../VERSION '") + sysConf + "/mods-enabled/an agent name/VERSION'").c_str()) >= 0);
268  char const* argv[] = {"an agent name", "-c", sysConf};
269  int argc = 3;
270 
271  fo::DbManager manager(&argc, (char**) argv); // [sic]
272 
273  fo::QueryResult result = manager.queryPrintf("CREATE TABLE tbl()");
274 
275  CPPUNIT_ASSERT(result);
276 
277  // TODO make this correctly
278  CPPUNIT_ASSERT(system((std::string("rm -rf '") + sysConf + "/mods-enabled'").c_str()) >= 0);
279  }
280 
281 };
282 
283 CPPUNIT_TEST_SUITE_REGISTRATION(FoLibCPPTest);
void test_runBadCommandQueryCheckIfError()
DB wrapper for agents.
bool tableExists(const char *tableName) const
QueryResult execPrepared(fo_dbManager_PreparedStatement *stmt,...) const
Execute a prepared statement with new parameters.
void test_runSchedulerConnectConstructor()
DB wrapper for agents.
Utility functions for test.
fo_dbManager * getStruct_dbManager() const
Wrapper for DB result.
bool rollback() const
void test_runCommandQueryCheckIfSuccess()
fo::DbManager * dbManager
Object for DbManager.
QueryResult queryPrintf(const char *queryFormat,...) const
Execute a query in printf format.
char * get_sysconfdir()
get sysconfig dir path just created by create_db_repo_sysconf()
std::vector< T > getSimpleResults(int columnN, T(functionP)(const char *)) const
Get vector of a single column from query result.
Test cases for CPP DB Manager.
DbManager spawn() const