00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include <stdlib.h>
00044
00045 #include "CUnit/Basic.h"
00046
00047 #include "ksm/database.h"
00048 #include "ksm/database_statement.h"
00049 #include "test_routines.h"
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 static void TestDbExecuteSql(void)
00061 {
00062 DB_RESULT result;
00063 char* sql;
00064 int status;
00065
00066 sql = DqsCountInit("TEST_BASIC");
00067 DqsEnd(&sql);
00068 status = DbExecuteSql(DbHandle(), sql, &result);
00069 CU_ASSERT_EQUAL(status, 0);
00070 DqsFree(sql);
00071
00072 DbFreeResult(result);
00073
00074 return;
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 static void TestDatabaseAccess(void)
00087 {
00088 DB_RESULT result;
00089 DB_ROW row;
00090 char* sql;
00091 int status;
00092 char* string;
00093
00094 sql = DqsInit("TEST_BASIC");
00095 DqsOrderBy(&sql, "SVALUE");
00096 DqsEnd(&sql);
00097 status = DbExecuteSql(DbHandle(), sql, &result);
00098 CU_ASSERT_EQUAL(status, 0);
00099 DqsFree(sql);
00100
00101
00102
00103
00104
00105
00106
00107 status = DbFetchRow(result, &row);
00108 CU_ASSERT_EQUAL(status, 0);
00109 status = DbString(row, 2, &string);
00110 CU_ASSERT_EQUAL(status, 0);
00111 CU_ASSERT_PTR_NULL(string);
00112 DbFreeRow(row);
00113
00114
00115
00116 status = DbFetchRow(result, &row);
00117 CU_ASSERT_EQUAL(status, 0);
00118 status = DbString(row, 2, &string);
00119 CU_ASSERT_EQUAL(status, 0);
00120 CU_ASSERT_STRING_EQUAL(string, "ABC");
00121 DbStringFree(string);
00122 DbFreeRow(row);
00123
00124
00125
00126 status = DbFetchRow(result, &row);
00127 CU_ASSERT_EQUAL(status, 0);
00128 status = DbString(row, 2, &string);
00129 CU_ASSERT_EQUAL(status, 0);
00130 CU_ASSERT_STRING_EQUAL(string, "DEF");
00131 DbStringFree(string);
00132 DbFreeRow(row);
00133
00134
00135
00136 status = DbFetchRow(result, &row);
00137 CU_ASSERT_EQUAL(status, -1);
00138
00139
00140
00141
00142
00143 DbFreeResult(result);
00144
00145 return;
00146 }
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157 static void TestDbExecuteSqlNoResult(void)
00158 {
00159 int rowcount;
00160 char* sql;
00161 int status;
00162 int where = 0;
00163
00164 sql = DisInit("TEST_BASIC");
00165 DisAppendInt(&sql, 400);
00166 DisAppendString(&sql, "GHI");
00167 DisAppendString(&sql, NULL);
00168 DisEnd(&sql);
00169 status = DbExecuteSqlNoResult(DbHandle(), sql);
00170 CU_ASSERT_EQUAL(status, 0);
00171 DisFree(sql);
00172
00173
00174
00175 sql = DqsCountInit("TEST_BASIC");
00176 DqsConditionInt(&sql, "IVALUE", DQS_COMPARE_EQ, 400, where++);
00177 DqsEnd(&sql);
00178 status = DbIntQuery(DbHandle(), &rowcount, sql);
00179 CU_ASSERT_EQUAL(status, 0);
00180 DqsFree(sql);
00181
00182 CU_ASSERT_EQUAL(rowcount, 1);
00183
00184 return;
00185 }
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196 static void TestDbIntQuery(void)
00197 {
00198 int rowcount;
00199 char* sql;
00200 int status;
00201 int where = 0;
00202
00203
00204
00205 sql = DqsCountInit("TEST_BASIC");
00206 DqsConditionInt(&sql, "IVALUE", DQS_COMPARE_EQ, 200, where++);
00207 DqsEnd(&sql);
00208 status = DbIntQuery(DbHandle(), &rowcount, sql);
00209 DqsFree(sql);
00210
00211 CU_ASSERT_EQUAL(status, 0);
00212
00213 CU_ASSERT_EQUAL(rowcount, 1);
00214
00215 return;
00216 }
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226 static void TestDbStringBuffer(void)
00227 {
00228 char buffer[128];
00229 DB_RESULT result;
00230 DB_ROW row;
00231 char* sql;
00232 int status;
00233 int where = 0;
00234
00235 sql = DqsInit("TEST_BASIC");
00236 DqsConditionString(&sql, "SVALUE", DQS_COMPARE_EQ, "ABC", where++);
00237 DqsEnd(&sql);
00238 status = DbExecuteSql(DbHandle(), sql, &result);
00239 CU_ASSERT_EQUAL(status, 0);
00240 DqsFree(sql);
00241
00242
00243
00244 status = DbFetchRow(result, &row);
00245 CU_ASSERT_EQUAL(status, 0);
00246 status = DbStringBuffer(row, 2, buffer, sizeof(buffer));
00247 CU_ASSERT_EQUAL(status, 0);
00248 CU_ASSERT_STRING_EQUAL(buffer, "ABC");
00249 DbFreeRow(row);
00250
00251
00252
00253 status = DbFetchRow(result, &row);
00254 CU_ASSERT_EQUAL(status, -1);
00255
00256
00257
00258
00259
00260 DbFreeResult(result);
00261
00262 return;
00263 }
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275 static void TestDbLastRowId(void)
00276 {
00277 DB_ID first_id = 0;
00278 DB_ID second_id = 0;
00279 char* sql = NULL;
00280 int status;
00281
00282
00283
00284 sql = DisInit("TEST_BASIC");
00285 CU_ASSERT_PTR_NOT_NULL(sql);
00286
00287 DisAppendInt(&sql, 500);
00288 DisAppendString(&sql, "XYZZY");
00289 DisAppendString(&sql, "20090101");
00290 DisEnd(&sql);
00291
00292
00293
00294 status = DbExecuteSqlNoResult(DbHandle(), sql);
00295 CU_ASSERT_EQUAL(status, 0);
00296
00297 status = DbLastRowId(DbHandle(), &first_id);
00298 CU_ASSERT_EQUAL(status, 0);
00299 CU_ASSERT_NOT_EQUAL(first_id, 0);
00300
00301 status = DbExecuteSqlNoResult(DbHandle(), sql);
00302 CU_ASSERT_EQUAL(status, 0);
00303
00304 status = DbLastRowId(DbHandle(), &second_id);
00305 CU_ASSERT_EQUAL(status, 0);
00306 CU_ASSERT_EQUAL(second_id, (first_id + 1));
00307
00308
00309
00310 DisFree(sql);
00311
00312 return;
00313
00314 }
00315
00316 static void TestDbCommit(void)
00317 {
00318 int rowcount;
00319 char* sql;
00320 int status;
00321 int where = 0;
00322
00323 status = DbBeginTransaction();
00324 CU_ASSERT_EQUAL(status, 0);
00325
00326 sql = DisInit("TEST_BASIC");
00327 DisAppendInt(&sql, 600);
00328 DisAppendString(&sql, "JKL");
00329 DisAppendString(&sql, NULL);
00330 DisEnd(&sql);
00331 status = DbExecuteSqlNoResult(DbHandle(), sql);
00332 CU_ASSERT_EQUAL(status, 0);
00333 DisFree(sql);
00334
00335 status = DbCommit();
00336 CU_ASSERT_EQUAL(status, 0);
00337
00338
00339
00340 sql = DqsCountInit("TEST_BASIC");
00341 DqsConditionInt(&sql, "IVALUE", DQS_COMPARE_EQ, 600, where++);
00342 DqsEnd(&sql);
00343 status = DbIntQuery(DbHandle(), &rowcount, sql);
00344 CU_ASSERT_EQUAL(status, 0);
00345 DqsFree(sql);
00346
00347 CU_ASSERT_EQUAL(rowcount, 1);
00348
00349 return;
00350 }
00351
00352 static void TestDbRollback(void)
00353 {
00354 int rowcount;
00355 char* sql;
00356 int status;
00357 int where = 0;
00358
00359 status = DbBeginTransaction();
00360 CU_ASSERT_EQUAL(status, 0);
00361
00362 sql = DisInit("TEST_BASIC");
00363 DisAppendInt(&sql, 700);
00364 DisAppendString(&sql, "MNO");
00365 DisAppendString(&sql, NULL);
00366 DisEnd(&sql);
00367 status = DbExecuteSqlNoResult(DbHandle(), sql);
00368 CU_ASSERT_EQUAL(status, 0);
00369 DisFree(sql);
00370
00371
00372 sql = DqsCountInit("TEST_BASIC");
00373 DqsConditionInt(&sql, "IVALUE", DQS_COMPARE_EQ, 700, where++);
00374 DqsEnd(&sql);
00375 status = DbIntQuery(DbHandle(), &rowcount, sql);
00376 CU_ASSERT_EQUAL(status, 0);
00377 CU_ASSERT_EQUAL(rowcount, 1);
00378
00379
00380 status = DbRollback();
00381 CU_ASSERT_EQUAL(status, 0);
00382
00383
00384 status = DbIntQuery(DbHandle(), &rowcount, sql);
00385 CU_ASSERT_EQUAL(status, 0);
00386 CU_ASSERT_EQUAL(rowcount, 0);
00387 DqsFree(sql);
00388
00389 return;
00390 }
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408 int TestDb(void);
00409 int TestDb(void)
00410 {
00411 struct test_testdef tests[] = {
00412 {"TestDbExecuteSql", TestDbExecuteSql},
00413 {"TestDatabaseAccess", TestDatabaseAccess},
00414 {"TestDbExecuteSqlNoResult", TestDbExecuteSqlNoResult},
00415 {"TestDbIntQuery", TestDbIntQuery},
00416 {"TestDbStringBuffer", TestDbStringBuffer},
00417 {"TestDbLastRowId", TestDbLastRowId},
00418 {"TestDbCommit", TestDbCommit},
00419 {"TestDbRollback", TestDbRollback},
00420 {NULL, NULL}
00421 };
00422
00423 return TcuCreateSuite("Db", TdbSetup, TdbTeardown, tests);
00424 }