Mercurial > hg > nsaunier > traffic-intelligence
comparison trajectorymanagement/test/DBSQLiteAccessTest.cpp @ 1159:e1e7acef8eab
moved trajectory management library into Traffic Intelligence
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Mon, 22 Feb 2021 22:09:35 -0500 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1158:7eb972942f22 | 1159:e1e7acef8eab |
|---|---|
| 1 #include "DBSQLiteAccessTest.h" | |
| 2 | |
| 3 void DBSQLiteAccessTest::setUp(void) | |
| 4 { | |
| 5 db = new DBSQLiteAccess(); | |
| 6 | |
| 7 dbName = "XXXXXX.sqlite"; | |
| 8 int res = mkstemps((char*) dbName.c_str(), 7); | |
| 9 CPPUNIT_ASSERT(res != -1); | |
| 10 } | |
| 11 | |
| 12 void DBSQLiteAccessTest::tearDown(void) | |
| 13 { | |
| 14 delete db; | |
| 15 remove(dbName.c_str()); | |
| 16 } | |
| 17 | |
| 18 void DBSQLiteAccessTest::testConnect1(void) | |
| 19 { | |
| 20 CPPUNIT_ASSERT_EQUAL(db->connect(dbName.c_str()), true); | |
| 21 CPPUNIT_ASSERT_EQUAL(db->isConnected(), true); | |
| 22 CPPUNIT_ASSERT_EQUAL(db->connect(dbName.c_str()), false); | |
| 23 CPPUNIT_ASSERT_EQUAL(db->isConnected(), true); | |
| 24 } | |
| 25 | |
| 26 void DBSQLiteAccessTest::testConnect2(void) | |
| 27 { | |
| 28 CPPUNIT_ASSERT_EQUAL(db->connect(dbName.c_str()), true); | |
| 29 CPPUNIT_ASSERT_EQUAL(db->isConnected(), true); | |
| 30 | |
| 31 string dbName2 = "XXXXXX.sqlite"; | |
| 32 int res = mkstemps((char*) dbName2.c_str(), 7); | |
| 33 CPPUNIT_ASSERT(res != -1); | |
| 34 | |
| 35 CPPUNIT_ASSERT_EQUAL(db->connect(dbName2.c_str()), false); | |
| 36 CPPUNIT_ASSERT_EQUAL(db->isConnected(), true); | |
| 37 | |
| 38 remove(dbName2.c_str()); | |
| 39 } | |
| 40 | |
| 41 void DBSQLiteAccessTest::testConnect3(void) | |
| 42 { | |
| 43 CPPUNIT_ASSERT_EQUAL(db->connect("TestRunner/"), false); | |
| 44 CPPUNIT_ASSERT_EQUAL(db->isConnected(), false); | |
| 45 CPPUNIT_ASSERT_EQUAL(db->connect("TestRunner/"), false); | |
| 46 CPPUNIT_ASSERT_EQUAL(db->isConnected(), false); | |
| 47 } | |
| 48 | |
| 49 void DBSQLiteAccessTest::testConnect4(void) | |
| 50 { | |
| 51 CPPUNIT_ASSERT_EQUAL(db->connect(dbName.c_str()), true); | |
| 52 CPPUNIT_ASSERT_EQUAL(db->isConnected(), true); | |
| 53 CPPUNIT_ASSERT_EQUAL(db->connect("TestRunner/"), false); | |
| 54 CPPUNIT_ASSERT_EQUAL(db->isConnected(), true); | |
| 55 } | |
| 56 | |
| 57 void DBSQLiteAccessTest::testConnect5(void) | |
| 58 { | |
| 59 CPPUNIT_ASSERT_EQUAL(db->connect("TestRunner/"), false); | |
| 60 CPPUNIT_ASSERT_EQUAL(db->isConnected(), false); | |
| 61 CPPUNIT_ASSERT_EQUAL(db->connect(dbName.c_str()), true); | |
| 62 CPPUNIT_ASSERT_EQUAL(db->isConnected(), true); | |
| 63 } | |
| 64 | |
| 65 void DBSQLiteAccessTest::testDisconnect1(void) | |
| 66 { | |
| 67 CPPUNIT_ASSERT_EQUAL(false, db->isConnected()); | |
| 68 CPPUNIT_ASSERT_EQUAL(true, db->disconnect()); | |
| 69 CPPUNIT_ASSERT_EQUAL(false, db->isConnected()); | |
| 70 } | |
| 71 | |
| 72 void DBSQLiteAccessTest::testDisconnect2(void) | |
| 73 { | |
| 74 CPPUNIT_ASSERT_EQUAL(db->connect(dbName.c_str()), true); | |
| 75 CPPUNIT_ASSERT_EQUAL(db->isConnected(), true); | |
| 76 CPPUNIT_ASSERT_EQUAL(db->disconnect(), true); | |
| 77 CPPUNIT_ASSERT_EQUAL(db->isConnected(), false); | |
| 78 CPPUNIT_ASSERT_EQUAL(db->connect(dbName.c_str()), true); | |
| 79 CPPUNIT_ASSERT_EQUAL(db->isConnected(), true); | |
| 80 CPPUNIT_ASSERT_EQUAL(db->disconnect(), true); | |
| 81 CPPUNIT_ASSERT_EQUAL(db->isConnected(), false); | |
| 82 } | |
| 83 | |
| 84 void DBSQLiteAccessTest::testDisconnect3(void) | |
| 85 { | |
| 86 string dbName2 = "XXXXXX.sqlite"; | |
| 87 int res = mkstemps((char*) dbName2.c_str(), 7); | |
| 88 CPPUNIT_ASSERT(res != -1); | |
| 89 | |
| 90 CPPUNIT_ASSERT_EQUAL(true, db->connect(dbName.c_str())); | |
| 91 CPPUNIT_ASSERT_EQUAL(true, db->isConnected()); | |
| 92 | |
| 93 CPPUNIT_ASSERT_EQUAL(false, db->connect(dbName2.c_str())); | |
| 94 CPPUNIT_ASSERT_EQUAL(true, db->isConnected()); | |
| 95 | |
| 96 CPPUNIT_ASSERT_EQUAL(true, db->disconnect()); | |
| 97 CPPUNIT_ASSERT_EQUAL(false, db->isConnected()); | |
| 98 | |
| 99 CPPUNIT_ASSERT_EQUAL(true, db->connect(dbName2.c_str())); | |
| 100 CPPUNIT_ASSERT_EQUAL(true, db->isConnected()); | |
| 101 CPPUNIT_ASSERT_EQUAL(true, db->disconnect()); | |
| 102 CPPUNIT_ASSERT_EQUAL(false, db->isConnected()); | |
| 103 | |
| 104 remove(dbName2.c_str()); | |
| 105 } | |
| 106 | |
| 107 void DBSQLiteAccessTest::testSqliteErrCodeMsg1(void) | |
| 108 { | |
| 109 CPPUNIT_ASSERT_EQUAL(false, db->connect("TestRunner/")); | |
| 110 CPPUNIT_ASSERT_EQUAL(SQLITE_MISUSE, db->sqliteErrCode()); | |
| 111 CPPUNIT_ASSERT_EQUAL(*"library routine called out of sequence", *db->sqliteErrMsg()); | |
| 112 } | |
| 113 | |
| 114 void DBSQLiteAccessTest::testSqliteErrCodeMsg2(void) | |
| 115 { | |
| 116 CPPUNIT_ASSERT_EQUAL(true, db->connect(dbName.c_str())); | |
| 117 CPPUNIT_ASSERT_EQUAL(false, db->executeStatement("create table A ();")); | |
| 118 CPPUNIT_ASSERT_EQUAL(SQLITE_ERROR, db->sqliteErrCode()); | |
| 119 CPPUNIT_ASSERT_EQUAL(*"near \")\": syntax error", *db->sqliteErrMsg()); | |
| 120 } | |
| 121 | |
| 122 void DBSQLiteAccessTest::testSqliteErrCodeMsg3(void) | |
| 123 { | |
| 124 CPPUNIT_ASSERT_EQUAL(true, db->connect(dbName.c_str())); | |
| 125 CPPUNIT_ASSERT_EQUAL(true, db->executeStatement("create table A ( A INTEGER );")); | |
| 126 CPPUNIT_ASSERT_EQUAL(false, db->executeStatement("create table A ( A INTEGER );")); | |
| 127 CPPUNIT_ASSERT_EQUAL(SQLITE_ERROR, db->sqliteErrCode()); | |
| 128 CPPUNIT_ASSERT_EQUAL(*"table A already exists", *db->sqliteErrMsg()); | |
| 129 } | |
| 130 | |
| 131 void DBSQLiteAccessTest::testSqliteErrCodeMsg4(void) | |
| 132 { | |
| 133 CPPUNIT_ASSERT_EQUAL(true, db->connect(dbName.c_str())); | |
| 134 CPPUNIT_ASSERT_EQUAL(false, db->executeStatement("select * from A;")); | |
| 135 CPPUNIT_ASSERT_EQUAL(SQLITE_ERROR, db->sqliteErrCode()); | |
| 136 CPPUNIT_ASSERT_EQUAL(*"no such table: A", *db->sqliteErrMsg()); | |
| 137 } | |
| 138 | |
| 139 void DBSQLiteAccessTest::testExecuteStatement1(void) | |
| 140 { | |
| 141 CPPUNIT_ASSERT_EQUAL(true, db->connect(dbName.c_str())); | |
| 142 | |
| 143 int size; | |
| 144 | |
| 145 CPPUNIT_ASSERT_EQUAL(true, db->executeStatement("create table A ( a INTEGER, b INTEGER );")); | |
| 146 CPPUNIT_ASSERT_EQUAL(true, db->executeStatementGetSingleValue("select count(*) from A;", size)); | |
| 147 CPPUNIT_ASSERT_EQUAL(0, size); | |
| 148 | |
| 149 CPPUNIT_ASSERT_EQUAL(true, db->executeStatement("insert into A values(1, 2);")); | |
| 150 CPPUNIT_ASSERT_EQUAL(true, db->executeStatementGetSingleValue("select count(*) from A;", size)); | |
| 151 CPPUNIT_ASSERT_EQUAL(1, size); | |
| 152 | |
| 153 CPPUNIT_ASSERT_EQUAL(true, db->executeStatement("insert into A values(1, 2);")); | |
| 154 CPPUNIT_ASSERT_EQUAL(true, db->executeStatementGetSingleValue("select count(*) from A;", size)); | |
| 155 CPPUNIT_ASSERT_EQUAL(2, size); | |
| 156 } | |
| 157 | |
| 158 void DBSQLiteAccessTest::testExecuteStatement2(void) | |
| 159 { | |
| 160 CPPUNIT_ASSERT_EQUAL(true, db->connect(dbName.c_str())); | |
| 161 | |
| 162 string size; | |
| 163 | |
| 164 CPPUNIT_ASSERT_EQUAL(true, db->executeStatement("create table A ( a INTEGER, b INTEGER );")); | |
| 165 CPPUNIT_ASSERT_EQUAL(true, db->executeStatementGetSingleValue("select count(*) from A;", size)); | |
| 166 CPPUNIT_ASSERT_EQUAL(*"0", *size.c_str()); | |
| 167 | |
| 168 CPPUNIT_ASSERT_EQUAL(true, db->executeStatement("insert into A values(1, 2);")); | |
| 169 CPPUNIT_ASSERT_EQUAL(true, db->executeStatementGetSingleValue("select count(*) from A;", size)); | |
| 170 CPPUNIT_ASSERT_EQUAL(*"1", *size.c_str()); | |
| 171 | |
| 172 CPPUNIT_ASSERT_EQUAL(true, db->executeStatement("insert into A values(1, 2);")); | |
| 173 CPPUNIT_ASSERT_EQUAL(true, db->executeStatementGetSingleValue("select count(*) from A;", size)); | |
| 174 CPPUNIT_ASSERT_EQUAL(*"2", *size.c_str()); | |
| 175 } | |
| 176 | |
| 177 void DBSQLiteAccessTest::testExecuteStatementGetMatrix(void) | |
| 178 { | |
| 179 CPPUNIT_ASSERT_EQUAL(true, db->connect(dbName.c_str())); | |
| 180 CPPUNIT_ASSERT_EQUAL(true, db->executeStatement("create table A ( a INTEGER, b INTEGER, c INTEGER );")); | |
| 181 CPPUNIT_ASSERT_EQUAL(true, db->executeStatement("insert into A values(1, 2, 3);")); | |
| 182 CPPUNIT_ASSERT_EQUAL(true, db->executeStatement("insert into A values(4, 5, 6);")); | |
| 183 CPPUNIT_ASSERT_EQUAL(true, db->executeStatement("insert into A values(7, 8, 9);")); | |
| 184 CPPUNIT_ASSERT_EQUAL(true, db->executeStatement("insert into A values(10, 11, 12);")); | |
| 185 CPPUNIT_ASSERT_EQUAL(true, db->executeStatement("insert into A values(13, 14, 15);")); | |
| 186 CPPUNIT_ASSERT_EQUAL(true, db->executeStatement("insert into A values(16, 17, 18);")); | |
| 187 | |
| 188 const string correctResult[6][3] = | |
| 189 { | |
| 190 { "1", "2", "3" }, | |
| 191 { "4", "5", "6" }, | |
| 192 { "7", "8", "9" }, | |
| 193 { "10", "11", "12" }, | |
| 194 { "13", "14", "15" }, | |
| 195 { "16", "17", "18" } }; | |
| 196 | |
| 197 vector<vector<string> > returnedData; | |
| 198 CPPUNIT_ASSERT_EQUAL(true, db->executeStatementGetMatrix("select * from A;", returnedData)); | |
| 199 | |
| 200 for (unsigned int row = 0; row < 6; ++row) | |
| 201 { | |
| 202 for (unsigned int col = 0; col < 3; ++col) | |
| 203 { | |
| 204 CPPUNIT_ASSERT_EQUAL(correctResult[row][col], returnedData[row][col]); | |
| 205 } | |
| 206 } | |
| 207 } | |
| 208 | |
| 209 void DBSQLiteAccessTest::testTransaction1(void) | |
| 210 { | |
| 211 CPPUNIT_ASSERT_EQUAL(true, db->connect(dbName.c_str())); | |
| 212 CPPUNIT_ASSERT_EQUAL(true, db->begin()); | |
| 213 CPPUNIT_ASSERT_EQUAL(false, db->begin()); | |
| 214 CPPUNIT_ASSERT_EQUAL(false, db->begin()); | |
| 215 } | |
| 216 | |
| 217 void DBSQLiteAccessTest::testTransaction2(void) | |
| 218 { | |
| 219 CPPUNIT_ASSERT_EQUAL(true, db->connect(dbName.c_str())); | |
| 220 CPPUNIT_ASSERT_EQUAL(false, db->rollback()); | |
| 221 CPPUNIT_ASSERT_EQUAL(false, db->rollback()); | |
| 222 } | |
| 223 | |
| 224 void DBSQLiteAccessTest::testTransaction3(void) | |
| 225 { | |
| 226 CPPUNIT_ASSERT_EQUAL(true, db->connect(dbName.c_str())); | |
| 227 CPPUNIT_ASSERT_EQUAL(true, db->begin()); | |
| 228 CPPUNIT_ASSERT_EQUAL(true, db->rollback()); | |
| 229 CPPUNIT_ASSERT_EQUAL(true, db->begin()); | |
| 230 CPPUNIT_ASSERT_EQUAL(true, db->rollback()); | |
| 231 } | |
| 232 | |
| 233 void DBSQLiteAccessTest::testTransaction4(void) | |
| 234 { | |
| 235 CPPUNIT_ASSERT_EQUAL(true, db->connect(dbName.c_str())); | |
| 236 CPPUNIT_ASSERT_EQUAL(true, db->begin()); | |
| 237 CPPUNIT_ASSERT_EQUAL(true, db->commit()); | |
| 238 CPPUNIT_ASSERT_EQUAL(false, db->rollback()); | |
| 239 CPPUNIT_ASSERT_EQUAL(true, db->begin()); | |
| 240 CPPUNIT_ASSERT_EQUAL(true, db->commit()); | |
| 241 CPPUNIT_ASSERT_EQUAL(false, db->rollback()); | |
| 242 } | |
| 243 | |
| 244 void DBSQLiteAccessTest::testTransaction5(void) | |
| 245 { | |
| 246 CPPUNIT_ASSERT_EQUAL(true, db->connect(dbName.c_str())); | |
| 247 CPPUNIT_ASSERT_EQUAL(false, db->commit()); | |
| 248 CPPUNIT_ASSERT_EQUAL(false, db->commit()); | |
| 249 } | |
| 250 | |
| 251 void DBSQLiteAccessTest::testTransaction6(void) | |
| 252 { | |
| 253 CPPUNIT_ASSERT_EQUAL(true, db->connect(dbName.c_str())); | |
| 254 CPPUNIT_ASSERT_EQUAL(true, db->begin()); | |
| 255 CPPUNIT_ASSERT_EQUAL(true, db->commit()); | |
| 256 CPPUNIT_ASSERT_EQUAL(true, db->begin()); | |
| 257 CPPUNIT_ASSERT_EQUAL(true, db->commit()); | |
| 258 } | |
| 259 | |
| 260 void DBSQLiteAccessTest::testTransaction7(void) | |
| 261 { | |
| 262 CPPUNIT_ASSERT_EQUAL(true, db->connect(dbName.c_str())); | |
| 263 CPPUNIT_ASSERT_EQUAL(true, db->begin()); | |
| 264 CPPUNIT_ASSERT_EQUAL(true, db->rollback()); | |
| 265 CPPUNIT_ASSERT_EQUAL(false, db->commit()); | |
| 266 CPPUNIT_ASSERT_EQUAL(true, db->begin()); | |
| 267 CPPUNIT_ASSERT_EQUAL(true, db->rollback()); | |
| 268 CPPUNIT_ASSERT_EQUAL(false, db->commit()); | |
| 269 } | |
| 270 | |
| 271 void DBSQLiteAccessTest::testTransaction8(void) | |
| 272 { | |
| 273 CPPUNIT_ASSERT_EQUAL(true, db->connect(dbName.c_str())); | |
| 274 CPPUNIT_ASSERT_EQUAL(true, db->executeStatement("create table A ( a INTEGER, b INTEGER );")); | |
| 275 CPPUNIT_ASSERT_EQUAL(true, db->begin()); | |
| 276 CPPUNIT_ASSERT_EQUAL(true, db->executeStatement("insert into A values(1, 2);")); | |
| 277 int size; | |
| 278 CPPUNIT_ASSERT_EQUAL(true, db->executeStatementGetSingleValue("select count(*) from A;", size)); | |
| 279 CPPUNIT_ASSERT_EQUAL(1, size); | |
| 280 CPPUNIT_ASSERT_EQUAL(true, db->rollback()); | |
| 281 CPPUNIT_ASSERT_EQUAL(false, db->commit()); | |
| 282 CPPUNIT_ASSERT_EQUAL(true, db->executeStatementGetSingleValue("select count(*) from A;", size)); | |
| 283 CPPUNIT_ASSERT_EQUAL(0, size); | |
| 284 CPPUNIT_ASSERT_EQUAL(true, db->begin()); | |
| 285 } | |
| 286 | |
| 287 void DBSQLiteAccessTest::testTransaction9(void) | |
| 288 { | |
| 289 CPPUNIT_ASSERT_EQUAL(true, db->connect(dbName.c_str())); | |
| 290 CPPUNIT_ASSERT_EQUAL(true, db->executeStatement("create table A ( a INTEGER, b INTEGER );")); | |
| 291 CPPUNIT_ASSERT_EQUAL(true, db->begin()); | |
| 292 CPPUNIT_ASSERT_EQUAL(true, db->executeStatement("insert into A values(1, 2);")); | |
| 293 int size; | |
| 294 CPPUNIT_ASSERT_EQUAL(true, db->executeStatementGetSingleValue("select count(*) from A;", size)); | |
| 295 CPPUNIT_ASSERT_EQUAL(1, size); | |
| 296 CPPUNIT_ASSERT_EQUAL(true, db->commit()); | |
| 297 CPPUNIT_ASSERT_EQUAL(false, db->rollback()); | |
| 298 CPPUNIT_ASSERT_EQUAL(true, db->executeStatementGetSingleValue("select count(*) from A;", size)); | |
| 299 CPPUNIT_ASSERT_EQUAL(1, size); | |
| 300 CPPUNIT_ASSERT_EQUAL(true, db->begin()); | |
| 301 } |
