00001
00020 #include "rtcom-eventlogger/eventlogger.h"
00021 #include "rtcom-eventlogger/db.h"
00022 #include "rtcom-eventlogger/eventlogger-types.h"
00023
00024 #include <glib.h>
00025 #include <glib/gstdio.h>
00026 #include <glib-object.h>
00027 #include <stdlib.h>
00028 #include <stdio.h>
00029 #include <check.h>
00030
00031 static const gchar *fname = "/tmp/check_db.sqlite";
00032
00033 START_TEST(db_test_db)
00034 {
00035 FILE *fp;
00036 rtcom_el_db_t db;
00037 gint cnt = -1;
00038 GError *err = NULL;
00039 GHashTable *t;
00040
00041
00042
00043 g_unlink (fname);
00044 fp = fopen (fname, "w");
00045 fprintf (fp, "illegaldata000000000000000000000000000000illegaldata");
00046 fclose (fp);
00047
00048
00049 db = rtcom_el_db_open (fname);
00050 fail_unless (db != NULL);
00051 rtcom_el_db_close (db);
00052
00053 db = rtcom_el_db_open (fname);
00054 fail_unless (db != NULL);
00055
00056
00057 rtcom_el_db_exec (db, rtcom_el_db_single_int, &cnt,
00058 "SELECT COUNT(*) FROM Events;", NULL);
00059 fail_unless (cnt == 0);
00060
00061
00062 fail_if (rtcom_el_db_exec (db, NULL, NULL, "BOGUS;", &err));
00063 fail_unless (err != NULL);
00064 fail_unless (err->domain == RTCOM_EL_ERROR);
00065 fail_unless (err->code == RTCOM_EL_INTERNAL_ERROR);
00066 g_error_free (err);
00067
00068 rtcom_el_db_transaction (db, FALSE, NULL);
00069
00070
00071 rtcom_el_db_exec (db, NULL, NULL, "DELETE FROM Services", NULL);
00072 rtcom_el_db_exec (db, NULL, NULL,
00073 "INSERT INTO SERVICES (id, name) VALUES (1, 'One');", NULL);
00074 rtcom_el_db_exec (db, NULL, NULL,
00075 "INSERT INTO SERVICES (id, name) VALUES (2, 'Two');", NULL);
00076 rtcom_el_db_exec (db, NULL, NULL,
00077 "INSERT INTO SERVICES (id, name) VALUES (3, 'Three');", NULL);
00078
00079 rtcom_el_db_commit (db, NULL);
00080
00081 t = rtcom_el_db_cache_lookup_table (db, "Services");
00082 fail_unless (t != NULL);
00083 fail_unless (g_hash_table_size (t) == 3);
00084 fail_unless (GPOINTER_TO_INT (g_hash_table_lookup (t, "Three")) == 3);
00085 g_hash_table_destroy (t);
00086
00087 rtcom_el_db_close (db);
00088 }
00089 END_TEST
00090
00091 struct schema_test_ctx {
00092 gchar *field;
00093 gchar *column;
00094 GType type;
00095 };
00096
00097 static void
00098 _verify_column_type (gpointer data, gpointer user_data)
00099 {
00100 rtcom_el_db_stmt_t stmt = data;
00101 struct schema_test_ctx *ctx = user_data;
00102 const gchar *dbtyp;
00103
00104 if (g_strcmp0 ((const gchar *) sqlite3_column_text (stmt, 1),
00105 ctx->column))
00106 return;
00107
00108 dbtyp = (const gchar *) sqlite3_column_text (stmt, 2);
00109 if (!g_strcmp0 (dbtyp, "INTEGER"))
00110 {
00111 fail_unless ((ctx->type == G_TYPE_INT) ||
00112 (ctx->type == G_TYPE_BOOLEAN));
00113 }
00114 else if (!g_strcmp0 (dbtyp, "TEXT"))
00115 {
00116 fail_unless (ctx->type == G_TYPE_STRING);
00117 }
00118 else if (!g_strcmp0 (dbtyp, "BOOL"))
00119 {
00120 fail_unless (ctx->type == G_TYPE_BOOLEAN);
00121 }
00122 else
00123 {
00124 fail("Mismatched db type.");
00125 }
00126 }
00127
00128 START_TEST(db_test_schema)
00129 {
00130 rtcom_el_db_t db;
00131 GHashTable *map;
00132 GHashTable *typ;
00133 GList *li, *i;
00134 struct schema_test_ctx ctx = { NULL, G_TYPE_INVALID };
00135
00136
00137 db = rtcom_el_db_open (fname);
00138 fail_unless (db != NULL);
00139
00140 rtcom_el_db_schema_get_mappings (NULL, &map, &typ);
00141 fail_unless (g_hash_table_size (map) == g_hash_table_size (typ));
00142
00143
00144
00145 li = g_hash_table_get_keys (map);
00146 for (i = li; i; i = g_list_next (i))
00147 {
00148 gboolean ret;
00149 gchar *field = i->data;
00150 gchar **tmp;
00151
00152 tmp = g_strsplit (g_hash_table_lookup (map, field), ".", 2);
00153 fail_unless (tmp[0] && tmp[1] && !tmp[2]);
00154
00155 ctx.field = field;
00156 ctx.column = tmp[1];
00157 ctx.type = GPOINTER_TO_UINT (g_hash_table_lookup (typ, field));
00158 fail_unless (ctx.type != G_TYPE_INVALID);
00159
00160 ret = rtcom_el_db_exec_printf (db, _verify_column_type, &ctx, NULL,
00161 "PRAGMA table_info(%s);", tmp[0]);
00162 fail_unless (ret == TRUE);
00163
00164 g_strfreev (tmp);
00165 }
00166
00167 g_list_free (li);
00168
00169 rtcom_el_db_close (db);
00170 }
00171 END_TEST
00172
00173 static void
00174 _cause_mischief (gpointer data, gpointer user_data)
00175 {
00176 rtcom_el_db_t db = user_data;
00177
00178 fail_if (rtcom_el_db_transaction (db, TRUE, NULL));
00179 }
00180
00181 START_TEST(db_test_events)
00182 {
00183 rtcom_el_db_t db;
00184 const gchar *sel;
00185 GHashTable *map;
00186 GHashTable *typ;
00187 gint i;
00188
00189 db = rtcom_el_db_open (fname);
00190 fail_unless (db != NULL);
00191
00192 rtcom_el_db_schema_get_mappings (&sel, &map, &typ);
00193 fail_unless (g_hash_table_size (map) == g_hash_table_size (typ));
00194
00195
00196 fail_unless (rtcom_el_db_transaction (db, FALSE, NULL));
00197
00198
00199 fail_if (rtcom_el_db_transaction (db, FALSE, NULL));
00200
00201 for (i = 0; i < 100; i++) {
00202 rtcom_el_db_exec (db, NULL, NULL,
00203 "INSERT INTO Events (service_id, event_type_id, storage_time, " \
00204 "start_time) VALUES (0, 0, 0, 0);", NULL);
00205 }
00206
00207 rtcom_el_db_commit (db, NULL);
00208
00209 i = 0;
00210 rtcom_el_db_exec (db, rtcom_el_db_single_int, &i,
00211 "SELECT COUNT(*) FROM Events", NULL);
00212 fail_unless (i == 100);
00213
00214
00215
00216 rtcom_el_db_exec (db, _cause_mischief, &db,
00217 "SELECT COUNT(*) FROM Events", NULL);
00218
00219 rtcom_el_db_exec (db, NULL, NULL, "DELETE FROM Events;", NULL);
00220 rtcom_el_db_exec (db, rtcom_el_db_single_int, &i,
00221 "SELECT COUNT(*) FROM Events", NULL);
00222 fail_unless (i == 0);
00223
00224
00225 fail_if (rtcom_el_db_commit (db, NULL));
00226
00227 fail_if (rtcom_el_db_rollback (db, NULL));
00228
00229 rtcom_el_db_close (db);
00230 }
00231 END_TEST
00232
00233 void
00234 db_extend_el_suite (Suite *s)
00235 {
00236 TCase *tc_db = tcase_create ("Database");
00237
00238 tcase_add_test (tc_db, db_test_db);
00239 tcase_add_test (tc_db, db_test_schema);
00240 tcase_add_test (tc_db, db_test_events);
00241
00242 suite_add_tcase (s, tc_db);
00243 }
00244