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
00119 {
00120 fail("Mismatched db type.");
00121 }
00122 }
00123
00124 START_TEST(db_test_schema)
00125 {
00126 rtcom_el_db_t db;
00127 GHashTable *map;
00128 GHashTable *typ;
00129 GList *li, *i;
00130 struct schema_test_ctx ctx = { NULL, G_TYPE_INVALID };
00131
00132
00133 db = rtcom_el_db_open (fname);
00134 fail_unless (db != NULL);
00135
00136 rtcom_el_db_schema_get_mappings (NULL, &map, &typ);
00137 fail_unless (g_hash_table_size (map) == g_hash_table_size (typ));
00138
00139
00140
00141 li = g_hash_table_get_keys (map);
00142 for (i = li; i; i = g_list_next (i))
00143 {
00144 gboolean ret;
00145 gchar *field = i->data;
00146 gchar **tmp;
00147
00148 tmp = g_strsplit (g_hash_table_lookup (map, field), ".", 2);
00149 fail_unless (tmp[0] && tmp[1] && !tmp[2]);
00150
00151 ctx.field = field;
00152 ctx.column = tmp[1];
00153 ctx.type = GPOINTER_TO_UINT (g_hash_table_lookup (typ, field));
00154 fail_unless (ctx.type != G_TYPE_INVALID);
00155
00156 ret = rtcom_el_db_exec_printf (db, _verify_column_type, &ctx, NULL,
00157 "PRAGMA table_info(%s);", tmp[0]);
00158 fail_unless (ret == TRUE);
00159 }
00160
00161 g_list_free (li);
00162
00163 rtcom_el_db_close (db);
00164 }
00165 END_TEST
00166
00167 static void
00168 _cause_mischief (gpointer data, gpointer user_data)
00169 {
00170 rtcom_el_db_t db = user_data;
00171
00172 fail_if (rtcom_el_db_transaction (db, TRUE, NULL));
00173 }
00174
00175 START_TEST(db_test_events)
00176 {
00177 rtcom_el_db_t db;
00178 const gchar *sel;
00179 GHashTable *map;
00180 GHashTable *typ;
00181 gint i;
00182
00183 db = rtcom_el_db_open (fname);
00184 fail_unless (db != NULL);
00185
00186 rtcom_el_db_schema_get_mappings (&sel, &map, &typ);
00187 fail_unless (g_hash_table_size (map) == g_hash_table_size (typ));
00188
00189
00190 fail_unless (rtcom_el_db_transaction (db, FALSE, NULL));
00191
00192
00193 fail_if (rtcom_el_db_transaction (db, FALSE, NULL));
00194
00195 for (i = 0; i < 100; i++) {
00196 rtcom_el_db_exec (db, NULL, NULL,
00197 "INSERT INTO Events (service_id, event_type_id, storage_time, " \
00198 "start_time) VALUES (0, 0, 0, 0);", NULL);
00199 }
00200
00201 rtcom_el_db_commit (db, NULL);
00202
00203 i = 0;
00204 rtcom_el_db_exec (db, rtcom_el_db_single_int, &i,
00205 "SELECT COUNT(*) FROM Events", NULL);
00206 fail_unless (i == 100);
00207
00208
00209
00210 rtcom_el_db_exec (db, _cause_mischief, &db,
00211 "SELECT COUNT(*) FROM Events", NULL);
00212
00213 rtcom_el_db_exec (db, NULL, NULL, "DELETE FROM Events;", NULL);
00214 rtcom_el_db_exec (db, rtcom_el_db_single_int, &i,
00215 "SELECT COUNT(*) FROM Events", NULL);
00216 fail_unless (i == 0);
00217
00218
00219 fail_if (rtcom_el_db_commit (db, NULL));
00220
00221 fail_if (rtcom_el_db_rollback (db, NULL));
00222 }
00223 END_TEST
00224
00225 void
00226 db_extend_el_suite (Suite *s)
00227 {
00228 TCase *tc_db = tcase_create ("Database");
00229
00230 g_type_init ();
00231
00232 tcase_add_test (tc_db, db_test_db);
00233 tcase_add_test (tc_db, db_test_schema);
00234 tcase_add_test (tc_db, db_test_events);
00235
00236 suite_add_tcase (s, tc_db);
00237 }
00238