00001
00025 #include "rtcom-eventlogger/eventlogger.h"
00026
00027 #include <glib.h>
00028 #include <glib/gstdio.h>
00029
00030 #include <check.h>
00031
00032 #include <sqlite3.h>
00033
00034 #include <stdlib.h>
00035 #include <string.h>
00036 #include <sys/types.h>
00037 #include <sys/stat.h>
00038 #include <fcntl.h>
00039 #include <unistd.h>
00040
00041 #include "canned-data.h"
00042 #include "fail.h"
00043
00044 #define SERVICE "RTCOM_EL_SERVICE_TEST"
00045 #define EVENT_TYPE "RTCOM_EL_EVENTTYPE_TEST_ET1"
00046 #define FLAGS 0
00047 #define BYTES_SENT 10
00048 #define BYTES_RECEIVED 9
00049 #define REMOTE_EBOOK_UID "ebook-uid-1"
00050 #define LOCAL_UID "ext-salvatore.iovene@nokia.com"
00051 #define LOCAL_NAME "Salvatore Iovene"
00052
00053
00054 #define REMOTE_UID "1@foo.org"
00055 #define REMOTE_NAME "1,2"
00056
00057 #define CHANNEL "chavo"
00058 #define FREE_TEXT "Test free_text"
00059
00060 #define HEADER_KEY "Foo"
00061 #define HEADER_VAL "Bar"
00062
00063 #define ATTACH_DESC "Foo attachment."
00064
00065 #define REMOTE_EBOOK_UID_2 "ebook-uid-2"
00066 #define REMOTE_UID_2 "ext-salvatore.iovene-2@nokia.com"
00067 #define REMOTE_NAME_2 "Salvatore Iovene 2"
00068
00069 #define REMOTE_EBOOK_UID_3 "ebook-uid-3"
00070 #define REMOTE_UID_3 "ext-salvatore.iovene-3@nokia.com"
00071 #define REMOTE_NAME_3 "Salvatore Iovene 3"
00072
00073 static RTComEl *el = NULL;
00074
00075 static RTComElEvent *
00076 event_new_lite (void)
00077 {
00078 RTComElEvent *ev;
00079
00080 ev = rtcom_el_event_new();
00081 g_return_val_if_fail (ev != NULL, NULL);
00082
00083 RTCOM_EL_EVENT_SET_FIELD(ev, service, g_strdup (SERVICE));
00084 RTCOM_EL_EVENT_SET_FIELD(ev, event_type, g_strdup (EVENT_TYPE));
00085 RTCOM_EL_EVENT_SET_FIELD(ev, local_uid, g_strdup (LOCAL_UID));
00086 RTCOM_EL_EVENT_SET_FIELD(ev, start_time, time(NULL));
00087
00088 return ev;
00089 }
00090
00091 static RTComElEvent *
00092 event_new_full (time_t t)
00093 {
00094 RTComElEvent *ev;
00095
00096 ev = rtcom_el_event_new();
00097 g_return_val_if_fail (ev != NULL, NULL);
00098
00099
00100
00101
00102
00103
00104
00105 RTCOM_EL_EVENT_SET_FIELD(ev, service, g_strdup (SERVICE));
00106 RTCOM_EL_EVENT_SET_FIELD(ev, event_type, g_strdup (EVENT_TYPE));
00107 RTCOM_EL_EVENT_SET_FIELD(ev, start_time, t);
00108 RTCOM_EL_EVENT_SET_FIELD(ev, end_time, t);
00109 RTCOM_EL_EVENT_SET_FIELD(ev, flags, FLAGS);
00110 RTCOM_EL_EVENT_SET_FIELD(ev, bytes_sent, BYTES_SENT);
00111 RTCOM_EL_EVENT_SET_FIELD(ev, bytes_received, BYTES_RECEIVED);
00112 RTCOM_EL_EVENT_SET_FIELD(ev, local_uid, g_strdup (LOCAL_UID));
00113 RTCOM_EL_EVENT_SET_FIELD(ev, local_name, g_strdup (LOCAL_NAME));
00114 RTCOM_EL_EVENT_SET_FIELD(ev, remote_uid, g_strdup (REMOTE_UID));
00115 RTCOM_EL_EVENT_SET_FIELD(ev, remote_name, g_strdup (REMOTE_NAME));
00116 RTCOM_EL_EVENT_SET_FIELD(ev, channel, g_strdup (CHANNEL));
00117 RTCOM_EL_EVENT_SET_FIELD(ev, free_text, g_strdup (FREE_TEXT));
00118
00119 return ev;
00120 }
00121
00122 static void
00123 core_setup (void)
00124 {
00125 g_type_init ();
00126 const gchar *home;
00127 gchar *fn;
00128
00129 home = g_getenv ("RTCOM_EL_HOME");
00130 if (!home)
00131 home = g_get_home_dir();
00132
00133 fn = g_build_filename (home, ".rtcom-eventlogger", "el-v1.db", NULL);
00134 g_unlink (fn);
00135 g_free (fn);
00136
00137
00138 el = rtcom_el_new ();
00139 g_assert (el != NULL);
00140
00141
00142
00143 add_canned_events (el);
00144 }
00145
00146 static void
00147 core_teardown (void)
00148 {
00149
00150 g_assert (el != NULL);
00151 g_object_unref (el);
00152 el = NULL;
00153 }
00154
00155 static gint
00156 iter_count_results (RTComElIter *it)
00157 {
00158 gint i = 1;
00159
00160 if (it == NULL)
00161 {
00162 return 0;
00163 }
00164
00165 if (!rtcom_el_iter_first (it))
00166 {
00167 return 0;
00168 }
00169
00170 while (rtcom_el_iter_next (it))
00171 {
00172 i += 1;
00173 }
00174
00175 return i;
00176 }
00177
00178 START_TEST(test_add_event)
00179 {
00180 RTComElQuery * query = NULL;
00181 RTComElEvent * ev = NULL;
00182 gint event_id = -1;
00183 gint service_id = -1;
00184 gint event_type_id = -1;
00185 RTComElIter * it = NULL;
00186 GHashTable * values = NULL;
00187 time_t t = 0;
00188
00189 t = time (NULL);
00190
00191 ev = event_new_full (t);
00192 if(!ev)
00193 {
00194 fail("Failed to create event.");
00195 }
00196
00197 event_id = rtcom_el_add_event(el, ev, NULL);
00198 fail_if (event_id < 0, "Failed to add event");
00199
00200 query = rtcom_el_query_new(el);
00201
00202
00203 {
00204 RTComEl *q_el = NULL;
00205 gboolean q_is_caching = 0xDEADBEEF;
00206 gint q_limit = -42;
00207 gint q_offset = -42;
00208 gint q_group_by = -42;
00209
00210 g_object_get (query,
00211 "el", &q_el,
00212 "is-caching", &q_is_caching,
00213 "limit", &q_limit,
00214 "offset", &q_offset,
00215 "group-by", &q_group_by,
00216 NULL);
00217
00218 fail_unless (q_el == el);
00219 fail_unless (q_is_caching == FALSE);
00220 fail_unless (q_limit == -1);
00221 fail_unless (q_offset == 0);
00222 fail_unless (q_group_by == RTCOM_EL_QUERY_GROUP_BY_NONE);
00223 }
00224
00225 if(!rtcom_el_query_prepare(
00226 query,
00227 "id", event_id, RTCOM_EL_OP_EQUAL,
00228 NULL))
00229 {
00230 fail("Failed to prepare the query.");
00231 }
00232
00233 it = rtcom_el_get_events(el, query);
00234 g_object_unref(query);
00235
00236 fail_unless(it != NULL, "Failed to get iterator");
00237
00238 fail_unless(rtcom_el_iter_get_values(it, "service-id",
00239 &service_id, "event-type-id", &event_type_id, NULL));
00240
00241 fail_unless (rtcom_el_get_service_id(el, SERVICE) == service_id);
00242 fail_unless (rtcom_el_get_eventtype_id(el, EVENT_TYPE) == event_type_id);
00243
00244
00245 {
00246 gpointer el_db = NULL;
00247 RTComEl *it_el = NULL;
00248 gpointer it_query = NULL;
00249 gpointer it_db = NULL;
00250 gpointer it_stmt = NULL;
00251 gchar *it_sql = NULL;
00252 GHashTable *it_plugins = NULL;
00253 gboolean it_atomic = 0xDEADBEEF;
00254
00255 g_object_get (el,
00256 "db", &el_db,
00257 NULL);
00258
00259 g_object_get (it,
00260 "el", &it_el,
00261 "query", &it_query,
00262 "sqlite3-database", &it_db,
00263 "sqlite3-statement", &it_stmt,
00264 "plugins-table", &it_plugins,
00265 "atomic", &it_atomic,
00266 NULL);
00267
00268 fail_unless (it_el == el);
00269 fail_unless (it_query != NULL);
00270 fail_unless (it_db != NULL);
00271 fail_unless (it_db == el_db);
00272 fail_unless (it_stmt != NULL);
00273 fail_unless (it_plugins != NULL);
00274 fail_unless (it_atomic == TRUE || it_atomic == FALSE);
00275
00276 g_free (it_sql);
00277 }
00278
00279 fail_unless(rtcom_el_iter_first(it), "Failed to start iterator");
00280
00281 values = rtcom_el_iter_get_value_map(
00282 it,
00283 "start-time",
00284 "end-time",
00285 "flags",
00286 "bytes-sent",
00287 "bytes-received",
00288 "local-uid",
00289 "local-name",
00290 "remote-uid",
00291 "remote-name",
00292 "channel",
00293 "free-text",
00294 NULL);
00295
00296 if(!values)
00297 {
00298 fail("Failed to get values.");
00299 }
00300
00301 rtcom_fail_unless_intcmp(t, ==,
00302 g_value_get_int(g_hash_table_lookup(values, "start-time")));
00303 rtcom_fail_unless_intcmp(t, ==,
00304 g_value_get_int(g_hash_table_lookup(values, "end-time")));
00305
00306 rtcom_fail_unless_intcmp(FLAGS, ==,
00307 g_value_get_int(g_hash_table_lookup(values, "flags")));
00308
00309 rtcom_fail_unless_intcmp(BYTES_SENT, ==,
00310 g_value_get_int(g_hash_table_lookup(values, "bytes-sent")));
00311 rtcom_fail_unless_intcmp(BYTES_RECEIVED, ==,
00312 g_value_get_int(g_hash_table_lookup(values, "bytes-received")));
00313 rtcom_fail_unless_strcmp(LOCAL_UID, ==,
00314 g_value_get_string(g_hash_table_lookup(values, "local-uid")));
00315 rtcom_fail_unless_strcmp(LOCAL_NAME, ==,
00316 g_value_get_string(g_hash_table_lookup(values, "local-name")));
00317 rtcom_fail_unless_strcmp(REMOTE_UID, ==,
00318 g_value_get_string(g_hash_table_lookup(values, "remote-uid")));
00319 rtcom_fail_unless_strcmp(REMOTE_NAME, ==,
00320 g_value_get_string(g_hash_table_lookup(values, "remote-name")));
00321 rtcom_fail_unless_strcmp(CHANNEL, ==,
00322 g_value_get_string(g_hash_table_lookup(values, "channel")));
00323 rtcom_fail_unless_strcmp(FREE_TEXT, ==,
00324 g_value_get_string(g_hash_table_lookup(values, "free-text")));
00325
00326 fail_if(rtcom_el_iter_next(it), "Iterator should only return one row");
00327
00328 g_hash_table_destroy(values);
00329 g_object_unref(it);
00330 rtcom_el_event_free_contents (ev);
00331 rtcom_el_event_free (ev);
00332 }
00333 END_TEST
00334
00335 START_TEST(test_add_full)
00336 {
00337 const time_t time = 1000000;
00338 RTComElEvent *ev;
00339 RTComElQuery *query;
00340 RTComElIter *it;
00341 RTComElAttachIter *att_it;
00342 RTComElAttachment *att;
00343 GHashTable *headers;
00344 GList *attachments = NULL;
00345 gint fd;
00346 gint event_id;
00347 gchar *path1;
00348 gchar *path2;
00349 gchar *contents;
00350 gsize length;
00351
00352 headers = g_hash_table_new_full (g_str_hash, g_str_equal,
00353 g_free, g_free);
00354 g_hash_table_insert (headers, g_strdup (HEADER_KEY),
00355 g_strdup ("add_full"));
00356
00357 fd = g_file_open_tmp ("attachment1.XXXXXX", &path1, NULL);
00358 fail_unless (fd >= 0);
00359 fail_unless (path1 != NULL);
00360 close (fd);
00361 fail_unless (g_file_set_contents (path1, "some text\n", -1, NULL));
00362
00363 fd = g_file_open_tmp ("attachment2.XXXXXX", &path2, NULL);
00364 fail_unless (fd >= 0);
00365 fail_unless (path2 != NULL);
00366 close (fd);
00367 fail_unless (g_file_set_contents (path2, "other text\n", -1, NULL));
00368
00369 attachments = g_list_prepend (attachments,
00370 rtcom_el_attachment_new (path1, "some file"));
00371 attachments = g_list_prepend (attachments,
00372 rtcom_el_attachment_new (path2, NULL));
00373
00374 ev = event_new_full (time);
00375 fail_unless (ev != NULL, "Failed to create event.");
00376
00377 event_id = rtcom_el_add_event_full (el, ev,
00378 headers, attachments, NULL);
00379
00380 fail_if (event_id < 0, "Failed to add event");
00381
00382 g_unlink (path1);
00383 g_unlink (path2);
00384 g_hash_table_destroy (headers);
00385 headers = NULL;
00386 g_list_foreach (attachments, (GFunc) rtcom_el_free_attachment, NULL);
00387 g_list_free (attachments);
00388 attachments = NULL;
00389
00390
00391
00392 query = rtcom_el_query_new(el);
00393 fail_unless (rtcom_el_query_prepare (query,
00394 "id", event_id, RTCOM_EL_OP_EQUAL,
00395 NULL));
00396
00397 it = rtcom_el_get_events(el, query);
00398 g_object_unref(query);
00399
00400 fail_unless (it != NULL, "Failed to get iterator");
00401 fail_unless (rtcom_el_iter_first(it), "Failed to start iterator");
00402
00403 fail_unless (rtcom_el_iter_get_values (it, HEADER_KEY, &contents, NULL));
00404 rtcom_fail_unless_strcmp ("add_full", ==, contents);
00405 g_free (contents);
00406
00407 att_it = rtcom_el_iter_get_attachments(it);
00408 fail_unless (att_it != NULL, "Failed to get attachment iterator");
00409
00410 g_object_unref (it);
00411
00412 fail_unless (rtcom_el_attach_iter_first(att_it),
00413 "Failed to start attachment iterator");
00414
00415 att = rtcom_el_attach_iter_get (att_it);
00416 fail_if (att == NULL, "failed to get attachment data");
00417
00418 fail_unless (event_id == att->event_id,
00419 "attachment event ID doesn't match");
00420 rtcom_fail_unless_strcmp (g_basename (path2), ==,
00421 g_basename (att->path));
00422 rtcom_fail_unless_strcmp (NULL, ==, att->desc);
00423 fail_unless (g_file_get_contents (att->path, &contents, &length, NULL));
00424 rtcom_fail_unless_uintcmp (length, ==, strlen ("other text\n"));
00425 rtcom_fail_unless_strcmp (contents, ==, "other text\n");
00426 g_free (contents);
00427 rtcom_el_free_attachment (att);
00428
00429 fail_unless (rtcom_el_attach_iter_next (att_it),
00430 "Failed to advance attachment iterator");
00431
00432 att = rtcom_el_attach_iter_get (att_it);
00433 fail_if (att == NULL, "failed to get attachment data");
00434
00435 fail_unless (event_id == att->event_id,
00436 "attachment event ID doesn't match");
00437 rtcom_fail_unless_strcmp (g_basename (path1), ==,
00438 g_basename (att->path));
00439 rtcom_fail_unless_strcmp ("some file", ==, att->desc);
00440 fail_unless (g_file_get_contents (att->path, &contents, &length, NULL));
00441 rtcom_fail_unless_uintcmp (length, ==, strlen ("some text\n"));
00442 rtcom_fail_unless_strcmp (contents, ==, "some text\n");
00443 g_free (contents);
00444 rtcom_el_free_attachment (att);
00445
00446 fail_if (rtcom_el_attach_iter_next (att_it));
00447
00448 g_object_unref (att_it);
00449
00450 g_free (path1);
00451 g_free (path2);
00452
00453 rtcom_el_event_free_contents (ev);
00454 rtcom_el_event_free (ev);
00455 }
00456 END_TEST
00457
00458 START_TEST(test_header)
00459 {
00460 RTComElQuery * query = NULL;
00461 RTComElEvent * ev = NULL;
00462 gint event_id = -1;
00463 gint header_id = -1;
00464 RTComElIter * it = NULL;
00465 GHashTable *headers;
00466 gchar *contents;
00467
00468 ev = event_new_lite ();
00469 if(!ev)
00470 {
00471 fail("Failed to create event.");
00472 }
00473
00474 event_id = rtcom_el_add_event(el, ev, NULL);
00475 fail_if (event_id < 0, "Failed to add event");
00476
00477 header_id = rtcom_el_add_header(
00478 el, event_id,
00479 HEADER_KEY,
00480 HEADER_VAL,
00481 NULL);
00482 fail_if (header_id < 0, "Failed to add header");
00483
00484 query = rtcom_el_query_new(el);
00485 if(!rtcom_el_query_prepare(
00486 query,
00487 "id", event_id, RTCOM_EL_OP_EQUAL,
00488 NULL))
00489 {
00490 fail("Failed to prepare the query.");
00491 }
00492
00493 it = rtcom_el_get_events(el, query);
00494 g_object_unref(query);
00495
00496 fail_unless(it != NULL, "Failed to get iterator");
00497 fail_unless(rtcom_el_iter_first(it), "Failed to start iterator");
00498
00499 fail_unless(rtcom_el_iter_get_values(it, HEADER_KEY, &contents, NULL));
00500 rtcom_fail_unless_strcmp(HEADER_VAL, ==, contents);
00501 g_free (contents);
00502
00503 fail_if(rtcom_el_iter_next(it), "Iterator should only return one row");
00504
00505 g_object_unref(it);
00506 rtcom_el_event_free_contents (ev);
00507 rtcom_el_event_free (ev);
00508
00509 headers = rtcom_el_fetch_event_headers (el, event_id);
00510
00511 fail_unless (headers != NULL);
00512 rtcom_fail_unless_intcmp (g_hash_table_size (headers), ==, 1);
00513 rtcom_fail_unless_strcmp (g_hash_table_lookup (headers, HEADER_KEY),
00514 ==, HEADER_VAL);
00515
00516 g_hash_table_destroy (headers);
00517 }
00518 END_TEST
00519
00520 START_TEST(test_attach)
00521 {
00522 RTComElQuery * query = NULL;
00523 RTComElEvent * ev = NULL;
00524 gint event_id = -1;
00525 gint attachment_id = -1;
00526 RTComElIter * it = NULL;
00527 RTComElAttachIter * att_it = NULL;
00528 RTComElAttachment *att = NULL;
00529 gchar *contents;
00530 gsize length;
00531 GError *error = NULL;
00532 gchar *attach_path;
00533 gint fd;
00534
00535 ev = event_new_lite ();
00536 fail_if (ev == NULL, "Failed to create event");
00537
00538 event_id = rtcom_el_add_event(el, ev, NULL);
00539 fail_if (event_id < 0, "Failed to add event");
00540
00541 attachment_id = rtcom_el_add_attachment(
00542 el, event_id,
00543 "/nonexistent", ATTACH_DESC,
00544 &error);
00545 fail_if (attachment_id != -1, "Should have failed to add nonexistent "
00546 "attachment");
00547 rtcom_fail_unless_uintcmp (error->domain, ==, RTCOM_EL_ERROR);
00548 rtcom_fail_unless_intcmp (error->code, ==, RTCOM_EL_INTERNAL_ERROR);
00549 g_clear_error (&error);
00550
00551 query = rtcom_el_query_new(el);
00552 if(!rtcom_el_query_prepare(
00553 query,
00554 "id", event_id, RTCOM_EL_OP_EQUAL,
00555 NULL))
00556 {
00557 fail("Failed to prepare the query.");
00558 }
00559
00560 it = rtcom_el_get_events(el, query);
00561 g_object_unref(query);
00562
00563 att_it = rtcom_el_iter_get_attachments(it);
00564 fail_unless (att_it == NULL, "Should start with no attachments");
00565
00566 g_object_unref(it);
00567
00568 fd = g_file_open_tmp ("attachment.XXXXXX", &attach_path, NULL);
00569 fail_unless (fd >= 0);
00570 fail_unless (attach_path != NULL);
00571 close (fd);
00572 fail_unless (g_file_set_contents (attach_path, "lalala", 6, NULL));
00573
00574 attachment_id = rtcom_el_add_attachment(
00575 el, event_id,
00576 attach_path, ATTACH_DESC,
00577 NULL);
00578 fail_if (attachment_id < 0, "Failed to add attachment");
00579
00580 g_unlink (attach_path);
00581
00582 query = rtcom_el_query_new(el);
00583 if(!rtcom_el_query_prepare(
00584 query,
00585 "id", event_id, RTCOM_EL_OP_EQUAL,
00586 NULL))
00587 {
00588 fail("Failed to prepare the query.");
00589 }
00590
00591 it = rtcom_el_get_events(el, query);
00592 g_object_unref(query);
00593
00594 fail_unless (it != NULL, "Failed to get iterator");
00595 fail_unless (rtcom_el_iter_first(it), "Failed to start iterator");
00596
00597 att_it = rtcom_el_iter_get_attachments(it);
00598 fail_unless (att_it != NULL, "Failed to get attachment iterator");
00599
00600
00601 {
00602 gpointer el_db;
00603 gpointer ai_db;
00604 gpointer ai_stmt;
00605
00606 g_object_get (el,
00607 "db", &el_db,
00608 NULL);
00609
00610 g_object_get (att_it,
00611 "sqlite3-database", &ai_db,
00612 "sqlite3-statement", &ai_stmt,
00613 NULL);
00614
00615 fail_unless (ai_db != NULL);
00616 fail_unless (ai_db == el_db);
00617 fail_unless (ai_stmt != NULL);
00618 }
00619
00620 fail_unless (rtcom_el_attach_iter_first(att_it),
00621 "Failed to start attachment iterator");
00622
00623 att = rtcom_el_attach_iter_get (att_it);
00624 fail_if (att == NULL, "failed to get attachment data");
00625
00626 fail_unless (event_id == att->event_id,
00627 "attachment event ID doesn't match");
00628 rtcom_fail_unless_strcmp(g_basename(attach_path), ==,
00629 g_basename(att->path));
00630 rtcom_fail_unless_strcmp(ATTACH_DESC, ==, att->desc);
00631 fail_unless (g_file_get_contents (att->path, &contents, &length, NULL));
00632 rtcom_fail_unless_uintcmp (length, ==, 6);
00633 rtcom_fail_unless_strcmp (contents, ==, "lalala");
00634 g_free (contents);
00635
00636 fail_if (rtcom_el_attach_iter_next (att_it));
00637
00638 g_free (attach_path);
00639 rtcom_el_free_attachment (att);
00640 g_object_unref(att_it);
00641 g_object_unref(it);
00642 rtcom_el_event_free_contents (ev);
00643 rtcom_el_event_free (ev);
00644 }
00645 END_TEST
00646
00647 START_TEST(test_read)
00648 {
00649 RTComElQuery * query = NULL;
00650 RTComElEvent * ev = NULL;
00651 gint event_id = -1;
00652 RTComElIter * it = NULL;
00653 gint i;
00654 gboolean is_read;
00655 gint count;
00656 gint ids[4] = { 0, 0, 0, 0 };
00657
00658 ev = event_new_full (time (NULL));
00659 if(!ev)
00660 {
00661 fail("Failed to create event.");
00662 }
00663
00664 event_id = rtcom_el_add_event(el, ev, NULL);
00665 fail_if (event_id < 0, "Failed to add event");
00666
00667
00668
00669 rtcom_el_set_read_event(el, event_id, TRUE, NULL);
00670
00671 query = rtcom_el_query_new(el);
00672 rtcom_el_query_set_limit(query, 5);
00673 if(!rtcom_el_query_prepare(
00674 query,
00675 "is-read", TRUE, RTCOM_EL_OP_EQUAL,
00676 NULL))
00677 {
00678 fail("Failed to prepare the query.");
00679 }
00680
00681 it = rtcom_el_get_events(el, query);
00682 g_object_unref(query);
00683
00684 fail_unless(it != NULL, "Failed to get iterator");
00685 fail_unless(rtcom_el_iter_first(it), "Failed to start iterator");
00686
00687 fail_unless(rtcom_el_iter_get_values(it, "is-read", &is_read, NULL));
00688 fail_unless(is_read == TRUE, "is-read flag doesn't match");
00689
00690
00691 count = iter_count_results (it);
00692 rtcom_fail_unless_intcmp (count, ==, 1);
00693
00694 g_object_unref(it);
00695
00696
00697
00698 query = rtcom_el_query_new (el);
00699 rtcom_el_query_set_limit (query, 3);
00700 fail_unless (rtcom_el_query_prepare(query,
00701 NULL));
00702 it = rtcom_el_get_events(el, query);
00703 g_object_unref (query);
00704
00705 fail_unless (it != NULL);
00706 fail_unless (rtcom_el_iter_first (it));
00707 i = 0;
00708
00709 for (i = 0; i < 3; i++)
00710 {
00711 if (i > 0)
00712 {
00713 fail_unless (rtcom_el_iter_next (it));
00714 }
00715
00716 fail_unless (rtcom_el_iter_get_values (it, "id", ids + i, NULL));
00717 }
00718
00719 fail_if (rtcom_el_iter_next (it), "Iterator should run out after 3");
00720
00721 rtcom_fail_unless_intcmp (ids[0], ==, event_id);
00722
00723 g_object_unref(it);
00724
00725 rtcom_fail_unless_intcmp (rtcom_el_set_read_events (el, ids, TRUE, NULL),
00726 ==, 0);
00727
00728 query = rtcom_el_query_new (el);
00729 fail_unless (rtcom_el_query_prepare(query,
00730 "is-read", TRUE, RTCOM_EL_OP_EQUAL,
00731 NULL));
00732 it = rtcom_el_get_events(el, query);
00733 g_object_unref (query);
00734 count = iter_count_results (it);
00735
00736 rtcom_fail_unless_intcmp (count, ==, 3);
00737 g_object_unref(it);
00738
00739 rtcom_fail_unless_intcmp (rtcom_el_set_read_events (el, ids, FALSE, NULL),
00740 ==, 0);
00741
00742 query = rtcom_el_query_new (el);
00743 fail_unless (rtcom_el_query_prepare(query,
00744 "is-read", TRUE, RTCOM_EL_OP_EQUAL,
00745 NULL));
00746 it = rtcom_el_get_events(el, query);
00747 g_object_unref (query);
00748 fail_unless (it == NULL, "all read flags should have been unset");
00749
00750 rtcom_el_event_free_contents (ev);
00751 rtcom_el_event_free (ev);
00752 }
00753 END_TEST
00754
00755 START_TEST(test_flags)
00756 {
00757 RTComElQuery * query = NULL;
00758 RTComElEvent * ev = NULL;
00759 gint event_id = -1;
00760 RTComElIter * it = NULL;
00761 gint test_flag1 = 0;
00762 gint flags = 0;
00763
00764 ev = event_new_full (time (NULL));
00765 if(!ev)
00766 {
00767 fail("Failed to create event.");
00768 }
00769
00770 event_id = rtcom_el_add_event(el, ev, NULL);
00771 fail_if (event_id < 0, "Failed to add event");
00772
00773 rtcom_el_set_event_flag(el, event_id, "RTCOM_EL_FLAG_TEST_FLAG1", NULL);
00774
00775 query = rtcom_el_query_new(el);
00776 rtcom_el_query_set_limit(query, 5);
00777 if(!rtcom_el_query_prepare(
00778 query,
00779 "id", event_id, RTCOM_EL_OP_EQUAL,
00780 NULL))
00781 {
00782 fail("Failed to prepare the query.");
00783 }
00784
00785 it = rtcom_el_get_events(el, query);
00786 g_object_unref(query);
00787
00788 fail_unless(it != NULL, "Failed to get iterator");
00789 fail_unless(rtcom_el_iter_first(it), "Failed to start iterator");
00790
00791 fail_unless(rtcom_el_iter_get_values(it, "flags", &flags, NULL));
00792
00793 test_flag1 = rtcom_el_get_flag_value(el, "RTCOM_EL_FLAG_TEST_FLAG1");
00794
00795 fail_if ((flags & test_flag1) == 0, "flags don't match");
00796
00797 g_object_unref(it);
00798 rtcom_el_event_free_contents (ev);
00799 rtcom_el_event_free (ev);
00800 }
00801 END_TEST
00802
00803
00804 START_TEST(test_get)
00805 {
00806 RTComElQuery * query = NULL;
00807 RTComElEvent * ev = NULL;
00808 RTComElEvent *result = NULL;
00809 gint event_id = -1;
00810 RTComElIter * it = NULL;
00811
00812 ev = event_new_full (time (NULL));
00813 if(!ev)
00814 {
00815 fail("Failed to create event.");
00816 }
00817
00818 event_id = rtcom_el_add_event(el, ev, NULL);
00819 fail_if (event_id < 0, "Fail to add event");
00820
00821 query = rtcom_el_query_new(el);
00822 if(!rtcom_el_query_prepare(
00823 query,
00824 "id", event_id, RTCOM_EL_OP_EQUAL,
00825 NULL))
00826 {
00827 fail("Failed to prepare the query.");
00828 }
00829
00830 it = rtcom_el_get_events(el, query);
00831 g_object_unref(query);
00832
00833 fail_unless(it != NULL, "Failed to get iterator");
00834 fail_unless(rtcom_el_iter_first(it), "Failed to start iterator");
00835
00836 result = rtcom_el_event_new ();
00837 fail_unless (result != NULL, "failed to create result event");
00838
00839 fail_unless (rtcom_el_iter_get_full (it, result),
00840 "Failed to get event from iterator");
00841
00842 fail_unless (rtcom_el_event_equals (ev, result),
00843 "Retrieved event doesn't match created one");
00844
00845 g_object_unref(it);
00846 rtcom_el_event_free_contents (result);
00847 rtcom_el_event_free (result);
00848 rtcom_el_event_free_contents (ev);
00849 rtcom_el_event_free (ev);
00850 }
00851 END_TEST
00852
00853 START_TEST(test_unique_remotes)
00854 {
00855 RTComElEvent * ev = NULL;
00856 gint event_id = -1;
00857 GList * remote_ebook_uids = NULL;
00858 GList * remote_uids = NULL;
00859 GList * remote_names = NULL;
00860 GList *iter = NULL;
00861
00862 ev = event_new_full (time (NULL));
00863 if(!ev)
00864 {
00865 fail("Failed to create event.");
00866 }
00867
00868 RTCOM_EL_EVENT_SET_FIELD(ev, remote_ebook_uid, g_strdup (REMOTE_EBOOK_UID));
00869
00870 event_id = rtcom_el_add_event(el, ev, NULL);
00871 fail_if (event_id < 0, "Fail to add event");
00872
00873
00874 g_free (RTCOM_EL_EVENT_GET_FIELD(ev, remote_ebook_uid));
00875 g_free (RTCOM_EL_EVENT_GET_FIELD(ev, remote_uid));
00876 g_free (RTCOM_EL_EVENT_GET_FIELD(ev, remote_name));
00877 RTCOM_EL_EVENT_SET_FIELD(ev, remote_ebook_uid, g_strdup (REMOTE_EBOOK_UID_2));
00878 RTCOM_EL_EVENT_SET_FIELD(ev, remote_uid, g_strdup (REMOTE_UID_2));
00879 RTCOM_EL_EVENT_SET_FIELD(ev, remote_name, g_strdup (REMOTE_NAME_2));
00880
00881
00882
00883
00884 g_free(RTCOM_EL_EVENT_GET_FIELD(ev, group_uid));
00885 RTCOM_EL_EVENT_UNSET_FIELD(ev, group_uid);
00886
00887 event_id = rtcom_el_add_event(el, ev, NULL);
00888 fail_if (event_id < 0, "Fail to add event");
00889
00890 g_free (RTCOM_EL_EVENT_GET_FIELD(ev, remote_ebook_uid));
00891 g_free (RTCOM_EL_EVENT_GET_FIELD(ev, remote_uid));
00892 g_free (RTCOM_EL_EVENT_GET_FIELD(ev, remote_name));
00893 RTCOM_EL_EVENT_SET_FIELD(ev, remote_ebook_uid, g_strdup (REMOTE_EBOOK_UID_3));
00894 RTCOM_EL_EVENT_SET_FIELD(ev, remote_uid, g_strdup (REMOTE_UID_3));
00895 RTCOM_EL_EVENT_SET_FIELD(ev, remote_name, g_strdup (REMOTE_NAME_3));
00896
00897
00898
00899 g_free(RTCOM_EL_EVENT_GET_FIELD(ev, group_uid));
00900 RTCOM_EL_EVENT_UNSET_FIELD(ev, group_uid);
00901
00902 event_id = rtcom_el_add_event(el, ev, NULL);
00903 fail_if (event_id < 0, "Fail to add event");
00904
00905 remote_ebook_uids = rtcom_el_get_unique_remote_ebook_uids(el);
00906 fail_if (remote_ebook_uids == NULL, "Fail to get unique remote_ebook_uids");
00907
00908 fail_if (g_list_length(remote_ebook_uids) < 2,
00909 "remote_ebook_uids's length doesn't match");
00910
00911 for (iter = remote_ebook_uids; iter != NULL; iter = iter->next)
00912 g_debug("Unique remote_ebook_uid: %s", (const guchar *) iter->data);
00913
00914 remote_uids = rtcom_el_get_unique_remote_uids(el);
00915 fail_if (remote_uids == NULL, "Fail to get unique remote_uids");
00916
00917 fail_if (g_list_length(remote_uids) < 2, "remote_uids's length doesn't match");
00918
00919 for (iter = remote_uids; iter != NULL; iter = iter->next)
00920 g_debug("Unique remote_uid: %s", (const guchar *) iter->data);
00921
00922 remote_names = rtcom_el_get_unique_remote_names(el);
00923 fail_if (remote_names == NULL, "Fail to get unique remote_names");
00924
00925 fail_if (g_list_length(remote_names) < 2,
00926 "remote_names's length doesn't match");
00927
00928 for (iter = remote_names; iter != NULL; iter = iter->next)
00929 g_debug("Unique remote_name: %s", (const guchar *) iter->data);
00930
00931 rtcom_el_event_free_contents (ev);
00932 rtcom_el_event_free (ev);
00933 g_list_foreach(remote_ebook_uids, (GFunc) g_free, NULL);
00934 g_list_free(remote_ebook_uids);
00935 g_list_foreach(remote_uids, (GFunc) g_free, NULL);
00936 g_list_free(remote_uids);
00937 g_list_foreach(remote_names, (GFunc) g_free, NULL);
00938 g_list_free(remote_names);
00939 }
00940 END_TEST
00941
00942 START_TEST(test_get_string)
00943 {
00944 RTComElQuery * query = NULL;
00945 RTComElEvent * ev = NULL;
00946 gint event_id = -1;
00947 RTComElIter * it = NULL;
00948 gint header_id;
00949 gchar *bar;
00950
00951 ev = event_new_full (time (NULL));
00952 if(!ev)
00953 {
00954 fail("Failed to create event.");
00955 }
00956
00957 event_id = rtcom_el_add_event(el, ev, NULL);
00958 fail_if (event_id < 0, "Fail to add event");
00959
00960 header_id = rtcom_el_add_header(
00961 el, event_id,
00962 HEADER_KEY,
00963 HEADER_VAL,
00964 NULL);
00965 fail_if (header_id < 0, "Failed to add header");
00966
00967 query = rtcom_el_query_new(el);
00968 if(!rtcom_el_query_prepare(
00969 query,
00970 "id", event_id, RTCOM_EL_OP_EQUAL,
00971 NULL))
00972 {
00973 fail("Failed to prepare the query.");
00974 }
00975
00976 it = rtcom_el_get_events(el, query);
00977 g_object_unref(query);
00978
00979 fail_unless(it != NULL, "Failed to get iterator");
00980 fail_unless(rtcom_el_iter_first(it), "Failed to start iterator");
00981
00982 bar = GUINT_TO_POINTER (0xDEADBEEF);
00983 fail_if(rtcom_el_iter_get_values(it, "there is no such key", &bar, NULL),
00984 "Shouldn't be able to get a missing value as a string");
00985 fail_unless(bar == GUINT_TO_POINTER (0xDEADBEEF),
00986 "bar should be left untouched in this case");
00987
00988 fail_unless(rtcom_el_iter_get_values(it, HEADER_KEY, &bar, NULL));
00989 fail_if(bar == NULL);
00990 fail_if(bar == GUINT_TO_POINTER (0xDEADBEEF));
00991 rtcom_fail_unless_strcmp(bar, ==, HEADER_VAL);
00992
00993 g_free(bar);
00994 g_object_unref(it);
00995 rtcom_el_event_free_contents (ev);
00996 rtcom_el_event_free (ev);
00997 }
00998 END_TEST
00999
01000 START_TEST(test_get_int)
01001 {
01002 RTComElQuery * query = NULL;
01003 RTComElEvent * ev = NULL;
01004 gint event_id = -1;
01005 RTComElIter * it = NULL;
01006 gint retrieved;
01007
01008 ev = event_new_full (time (NULL));
01009 if(!ev)
01010 {
01011 fail("Failed to create event.");
01012 }
01013
01014 event_id = rtcom_el_add_event(el, ev, NULL);
01015 fail_if (event_id < 0, "Fail to add event");
01016
01017 query = rtcom_el_query_new(el);
01018 if(!rtcom_el_query_prepare(
01019 query,
01020 "id", event_id, RTCOM_EL_OP_EQUAL,
01021 NULL))
01022 {
01023 fail("Failed to prepare the query.");
01024 }
01025
01026 it = rtcom_el_get_events(el, query);
01027 g_object_unref(query);
01028
01029 fail_unless(it != NULL, "Failed to get iterator");
01030 fail_unless(rtcom_el_iter_first(it), "Failed to start iterator");
01031
01032 fail_unless(rtcom_el_iter_get_values(it, "bytes-sent", &retrieved, NULL),
01033 "Failed to get bytes-sent");
01034 rtcom_fail_unless_intcmp(retrieved, ==, BYTES_SENT);
01035
01036 g_object_unref(it);
01037 rtcom_el_event_free_contents (ev);
01038 rtcom_el_event_free (ev);
01039 }
01040 END_TEST
01041
01042 START_TEST(test_ends_with)
01043 {
01044 RTComElQuery * query = NULL;
01045 RTComElIter * it = NULL;
01046 gchar *contents;
01047 time_t t = 0;
01048
01049 t = time (NULL);
01050
01051 query = rtcom_el_query_new(el);
01052 if(!rtcom_el_query_prepare(
01053 query,
01054 "remote-name", "ve", RTCOM_EL_OP_STR_ENDS_WITH,
01055 NULL))
01056 {
01057 fail("Failed to prepare the query.");
01058 }
01059
01060 it = rtcom_el_get_events(el, query);
01061 g_object_unref(query);
01062
01063 fail_unless(it != NULL, "Failed to get iterator");
01064 fail_unless(rtcom_el_iter_first(it), "Failed to start iterator");
01065
01066 fail_unless(rtcom_el_iter_get_values(it, "free-text", &contents, NULL));
01067
01068
01069
01070
01071 rtcom_fail_unless_strcmp("I am online", ==, contents);
01072 g_free(contents);
01073
01074 fail_unless (rtcom_el_iter_next (it));
01075
01076 fail_unless(rtcom_el_iter_get_values(it, "free-text", &contents, NULL));
01077
01078 rtcom_fail_unless_strcmp("Hello from Dave", ==, contents);
01079
01080 g_free(contents);
01081
01082 fail_if (rtcom_el_iter_next (it));
01083
01084 g_object_unref(it);
01085 }
01086 END_TEST
01087
01088 START_TEST(test_delete_events)
01089 {
01090 RTComElQuery * query = NULL;
01091 RTComElEvent * ev = NULL;
01092 RTComElIter *it;
01093 gint event_id = -1;
01094 gint count;
01095 gboolean success;
01096
01097
01098 query = rtcom_el_query_new(el);
01099 fail_unless (rtcom_el_query_prepare (query,
01100 NULL));
01101 it = rtcom_el_get_events (el, query);
01102 g_object_unref (query);
01103 fail_unless (it != NULL);
01104 fail_unless (RTCOM_IS_EL_ITER (it));
01105 count = iter_count_results (it);
01106 rtcom_fail_unless_intcmp (count, ==, num_canned_events ());
01107 g_object_unref (it);
01108
01109 ev = event_new_full (time (NULL));
01110 if(!ev)
01111 {
01112 fail("Failed to create event.");
01113 }
01114
01115 event_id = rtcom_el_add_event(el, ev, NULL);
01116 fail_if (event_id < 0, "Failed to add event");
01117
01118 query = rtcom_el_query_new(el);
01119 if(!rtcom_el_query_prepare(
01120 query,
01121 "id", event_id, RTCOM_EL_OP_EQUAL,
01122 NULL))
01123 {
01124 fail("Failed to prepare the query.");
01125 }
01126
01127 success = rtcom_el_delete_events(el, query, NULL);
01128 g_object_unref(query);
01129
01130 fail_unless (success, "Failed to delete stuff");
01131
01132
01133
01134 rtcom_el_event_free_contents (ev);
01135 rtcom_el_event_free (ev);
01136
01137 query = rtcom_el_query_new(el);
01138 fail_unless (rtcom_el_query_prepare (query,
01139 "id", event_id, RTCOM_EL_OP_EQUAL,
01140 NULL));
01141 it = rtcom_el_get_events (el, query);
01142 g_object_unref (query);
01143 fail_unless (it == NULL);
01144 count = iter_count_results (it);
01145 rtcom_fail_unless_intcmp (count, ==, 0);
01146
01147 query = rtcom_el_query_new(el);
01148 fail_unless (rtcom_el_query_prepare (query,
01149 NULL));
01150 it = rtcom_el_get_events (el, query);
01151 g_object_unref (query);
01152 fail_unless (it != NULL);
01153 fail_unless (RTCOM_IS_EL_ITER (it));
01154 count = iter_count_results (it);
01155 rtcom_fail_unless_intcmp (count, ==, num_canned_events ());
01156 g_object_unref (it);
01157 }
01158 END_TEST
01159
01160 START_TEST(test_in_strv)
01161 {
01162 RTComElQuery * query = NULL;
01163 RTComElIter * it = NULL;
01164 time_t t = 0;
01165 gchar *contents;
01166 const gchar * const interesting_people[] = { "Chris", "Dave", NULL };
01167
01168 t = time (NULL);
01169
01170 query = rtcom_el_query_new(el);
01171 if(!rtcom_el_query_prepare(
01172 query,
01173 "remote-name", &interesting_people, RTCOM_EL_OP_IN_STRV,
01174 NULL))
01175 {
01176 fail("Failed to prepare the query.");
01177 }
01178
01179 it = rtcom_el_get_events(el, query);
01180 g_object_unref(query);
01181
01182 fail_unless(it != NULL, "Failed to get iterator");
01183 fail_unless(rtcom_el_iter_first(it), "Failed to start iterator");
01184
01185 fail_unless(rtcom_el_iter_get_values(it, "free-text", &contents, NULL));
01186
01187
01188
01189
01190
01191 rtcom_fail_unless_strcmp("Hello from Dave", ==, contents);
01192
01193 g_free (contents);
01194
01195 fail_unless (rtcom_el_iter_next (it));
01196
01197 fail_unless(rtcom_el_iter_get_values(it, "free-text", &contents, NULL));
01198
01199 rtcom_fail_unless_strcmp("Hello from Chris", ==, contents);
01200
01201 fail_if (rtcom_el_iter_next (it));
01202
01203 g_free (contents);
01204 g_object_unref(it);
01205 }
01206 END_TEST
01207
01208 START_TEST(test_delete_event)
01209 {
01210 RTComElQuery * query = NULL;
01211 RTComElEvent * ev = NULL;
01212 RTComElIter *it;
01213 gint event_id = -1;
01214 gint count;
01215 gint ret;
01216
01217
01218 query = rtcom_el_query_new(el);
01219 fail_unless (rtcom_el_query_prepare (query,
01220 NULL));
01221 it = rtcom_el_get_events (el, query);
01222 g_object_unref (query);
01223 fail_unless (it != NULL);
01224 fail_unless (RTCOM_IS_EL_ITER (it));
01225 count = iter_count_results (it);
01226 rtcom_fail_unless_intcmp (count, ==, num_canned_events ());
01227 g_object_unref (it);
01228
01229 ev = event_new_full (time (NULL));
01230 if(!ev)
01231 {
01232 fail("Failed to create event.");
01233 }
01234
01235 event_id = rtcom_el_add_event(el, ev, NULL);
01236 fail_if (event_id < 0, "Failed to add event");
01237
01238 ret = rtcom_el_delete_event(el, event_id, NULL);
01239 rtcom_fail_unless_intcmp (ret, ==, 0);
01240
01241
01242
01243 rtcom_el_event_free_contents (ev);
01244 rtcom_el_event_free (ev);
01245
01246 query = rtcom_el_query_new(el);
01247 fail_unless (rtcom_el_query_prepare (query,
01248 "id", event_id, RTCOM_EL_OP_EQUAL,
01249 NULL));
01250 it = rtcom_el_get_events (el, query);
01251 g_object_unref (query);
01252 fail_unless (it == NULL);
01253 count = iter_count_results (it);
01254 rtcom_fail_unless_intcmp (count, ==, 0);
01255
01256 query = rtcom_el_query_new(el);
01257 fail_unless (rtcom_el_query_prepare (query,
01258 NULL));
01259 it = rtcom_el_get_events (el, query);
01260 g_object_unref (query);
01261 fail_unless (it != NULL);
01262 fail_unless (RTCOM_IS_EL_ITER (it));
01263 count = iter_count_results (it);
01264 rtcom_fail_unless_intcmp (count, ==, num_canned_events ());
01265 g_object_unref (it);
01266 }
01267 END_TEST
01268
01269 START_TEST(test_string_equals)
01270 {
01271 RTComElQuery * query = NULL;
01272 RTComElIter * it = NULL;
01273 gchar *contents;
01274 time_t t = 0;
01275
01276 t = time (NULL);
01277
01278 query = rtcom_el_query_new(el);
01279 if(!rtcom_el_query_prepare(
01280 query,
01281 "local-uid", "butterfly/msn/alice", RTCOM_EL_OP_NOT_EQUAL,
01282 "remote-name", "Bob", RTCOM_EL_OP_EQUAL,
01283 NULL))
01284 {
01285 fail("Failed to prepare the query.");
01286 }
01287
01288 it = rtcom_el_get_events(el, query);
01289 g_object_unref(query);
01290
01291 fail_unless(it != NULL, "Failed to get iterator");
01292 fail_unless(rtcom_el_iter_first(it), "Failed to start iterator");
01293
01294 fail_unless(rtcom_el_iter_get_values(it, "free-text", &contents, NULL),
01295 "Failed to get values.");
01296
01297
01298
01299
01300
01301 rtcom_fail_unless_strcmp("Are you there?", ==, contents);
01302 g_free(contents);
01303
01304 fail_unless (rtcom_el_iter_next (it));
01305
01306 fail_unless(rtcom_el_iter_get_values(it, "free-text", &contents, NULL),
01307 "Failed to get values.");
01308
01309 rtcom_fail_unless_strcmp("Hi Alice", ==, contents);
01310 g_free(contents);
01311
01312 g_object_unref(it);
01313 }
01314 END_TEST
01315
01316 START_TEST(test_int_ranges)
01317 {
01318 RTComElQuery * query = NULL;
01319 RTComElIter * it = NULL;
01320 gchar *contents;
01321 time_t t = 0;
01322
01323 t = time (NULL);
01324
01325 query = rtcom_el_query_new(el);
01326 if(!rtcom_el_query_prepare(
01327 query,
01328 "start-time", 0, RTCOM_EL_OP_GREATER,
01329 "start-time", 4000, RTCOM_EL_OP_LESS_EQUAL,
01330 NULL))
01331 {
01332 fail("Failed to prepare the query.");
01333 }
01334
01335 it = rtcom_el_get_events(el, query);
01336 g_object_unref(query);
01337
01338 fail_unless(it != NULL, "Failed to get iterator");
01339 fail_unless(rtcom_el_iter_first(it), "Failed to start iterator");
01340
01341 fail_unless(rtcom_el_iter_get_values(it, "free-text", &contents, NULL),
01342 "Failed to get values");
01343
01344
01345
01346
01347 rtcom_fail_unless_strcmp("Are you there?", ==, contents);
01348 g_free(contents);
01349
01350 fail_unless (rtcom_el_iter_next (it));
01351
01352 fail_unless(rtcom_el_iter_get_values(it, "free-text", &contents, NULL),
01353 "Failed to get values");
01354
01355 rtcom_fail_unless_strcmp("Hello from Dave", ==, contents);
01356 g_free(contents);
01357
01358 fail_unless (rtcom_el_iter_next (it));
01359
01360 fail_unless(rtcom_el_iter_get_values(it, "free-text", &contents, NULL),
01361 "Failed to get values");
01362
01363 rtcom_fail_unless_strcmp("Hello from Chris", ==, contents);
01364 g_free(contents);
01365
01366 fail_unless (rtcom_el_iter_next (it));
01367
01368 fail_unless(rtcom_el_iter_get_values(it, "free-text", &contents, NULL),
01369 "Failed to get values");
01370
01371 rtcom_fail_unless_strcmp("Hi Alice", ==, contents);
01372 g_free(contents);
01373
01374 fail_if (rtcom_el_iter_next (it));
01375
01376 g_object_unref(it);
01377 }
01378 END_TEST
01379
01380 START_TEST(test_group_by_uids)
01381 {
01382 RTComElQuery * query = NULL;
01383 RTComElIter * it = NULL;
01384 gchar *s;
01385
01386 query = rtcom_el_query_new(el);
01387 rtcom_el_query_set_group_by (query, RTCOM_EL_QUERY_GROUP_BY_UIDS);
01388 fail_unless(rtcom_el_query_prepare(query,
01389 "remote-uid", "f", RTCOM_EL_OP_LESS,
01390 NULL));
01391
01392 it = rtcom_el_get_events(el, query);
01393 g_object_unref(query);
01394
01395 fail_unless(it != NULL, "Failed to get iterator");
01396
01397
01398
01399
01400 fail_unless(rtcom_el_iter_first(it), "Failed to start iterator");
01401 fail_unless (rtcom_el_iter_get_values (it, "remote-uid", &s, NULL));
01402 rtcom_fail_unless_strcmp("bob@example.com", ==, s);
01403 g_free (s);
01404 fail_unless (rtcom_el_iter_get_values (it, "local-uid", &s, NULL));
01405 rtcom_fail_unless_strcmp("butterfly/msn/alice", ==, s);
01406 g_free (s);
01407
01408 fail_unless (rtcom_el_iter_next (it));
01409 fail_unless (rtcom_el_iter_get_values (it, "remote-uid", &s, NULL));
01410 rtcom_fail_unless_strcmp("christine@msn.invalid", ==, s);
01411 g_free (s);
01412
01413 fail_unless (rtcom_el_iter_next (it));
01414 fail_unless (rtcom_el_iter_get_values (it, "remote-uid", &s, NULL));
01415 rtcom_fail_unless_strcmp("eve@example.com", ==, s);
01416 g_free (s);
01417
01418 fail_unless (rtcom_el_iter_next (it));
01419 fail_unless (rtcom_el_iter_get_values (it, "remote-uid", &s, NULL));
01420 rtcom_fail_unless_strcmp("bob@example.com", ==, s);
01421 g_free (s);
01422 fail_unless (rtcom_el_iter_get_values (it, "free-text", &s, NULL));
01423 rtcom_fail_unless_strcmp("Are you there?", ==, s);
01424 g_free (s);
01425
01426 fail_unless (rtcom_el_iter_next (it));
01427 fail_unless (rtcom_el_iter_get_values (it, "remote-uid", &s, NULL));
01428 rtcom_fail_unless_strcmp("dave@example.com", ==, s);
01429 g_free (s);
01430
01431 fail_unless (rtcom_el_iter_next (it));
01432 fail_unless (rtcom_el_iter_get_values (it, "remote-uid", &s, NULL));
01433 rtcom_fail_unless_strcmp("chris@example.com", ==, s);
01434 g_free (s);
01435
01436
01437
01438 fail_if (rtcom_el_iter_next (it), "Iterator should have expired");
01439
01440 g_object_unref(it);
01441 }
01442 END_TEST
01443
01444 START_TEST(test_group_by_metacontacts)
01445 {
01446 RTComElQuery * query = NULL;
01447 RTComElIter * it = NULL;
01448 gchar *s;
01449
01450 query = rtcom_el_query_new(el);
01451 rtcom_el_query_set_group_by (query, RTCOM_EL_QUERY_GROUP_BY_CONTACT);
01452 fail_unless(rtcom_el_query_prepare(query,
01453 "remote-uid", "f", RTCOM_EL_OP_LESS,
01454 NULL));
01455
01456 it = rtcom_el_get_events(el, query);
01457 g_object_unref(query);
01458
01459 fail_unless(it != NULL, "Failed to get iterator");
01460
01461
01462
01463
01464 fail_unless(rtcom_el_iter_first(it), "Failed to start iterator");
01465 fail_unless (rtcom_el_iter_get_values (it, "remote-uid", &s, NULL));
01466 rtcom_fail_unless_strcmp("bob@example.com", ==, s);
01467 g_free (s);
01468 fail_unless (rtcom_el_iter_get_values (it, "local-uid", &s, NULL));
01469 rtcom_fail_unless_strcmp("butterfly/msn/alice", ==, s);
01470 g_free (s);
01471
01472 fail_unless (rtcom_el_iter_next (it));
01473 fail_unless (rtcom_el_iter_get_values (it, "remote-uid", &s, NULL));
01474 rtcom_fail_unless_strcmp("christine@msn.invalid", ==, s);
01475 g_free (s);
01476
01477 fail_unless (rtcom_el_iter_next (it));
01478 fail_unless (rtcom_el_iter_get_values (it, "remote-uid", &s, NULL));
01479 rtcom_fail_unless_strcmp("eve@example.com", ==, s);
01480 g_free (s);
01481
01482
01483
01484
01485
01486
01487
01488
01489 fail_unless (rtcom_el_iter_next (it));
01490 fail_unless (rtcom_el_iter_get_values (it, "remote-uid", &s, NULL));
01491 rtcom_fail_unless_strcmp("bob@example.com", ==, s);
01492 g_free (s);
01493 fail_unless (rtcom_el_iter_get_values (it, "local-uid", &s, NULL));
01494 rtcom_fail_unless_strcmp("gabble/jabber/alice", ==, s);
01495 g_free (s);
01496
01497 fail_unless (rtcom_el_iter_next (it));
01498 fail_unless (rtcom_el_iter_get_values (it, "remote-uid", &s, NULL));
01499 rtcom_fail_unless_strcmp("dave@example.com", ==, s);
01500 g_free (s);
01501
01502
01503
01504
01505
01506
01507
01508 fail_if (rtcom_el_iter_next (it), "Iterator should have expired");
01509
01510 g_object_unref(it);
01511 }
01512 END_TEST
01513
01514 START_TEST(test_group_by_group)
01515 {
01516 RTComElQuery * query = NULL;
01517 RTComElIter * it = NULL;
01518 gchar *s;
01519
01520 query = rtcom_el_query_new(el);
01521 rtcom_el_query_set_group_by (query, RTCOM_EL_QUERY_GROUP_BY_GROUP);
01522 fail_unless(rtcom_el_query_prepare(query,
01523
01524 "remote-uid", "b", RTCOM_EL_OP_GREATER_EQUAL,
01525 "remote-uid", "g", RTCOM_EL_OP_LESS,
01526 NULL));
01527
01528 it = rtcom_el_get_events(el, query);
01529 g_object_unref(query);
01530
01531 fail_unless(it != NULL, "Failed to get iterator");
01532
01533
01534
01535
01536 fail_unless(rtcom_el_iter_first(it), "Failed to start iterator");
01537 fail_unless (rtcom_el_iter_get_values (it, "remote-uid", &s, NULL));
01538 rtcom_fail_unless_strcmp("bob@example.com", ==, s);
01539 g_free (s);
01540 fail_unless (rtcom_el_iter_get_values (it, "local-uid", &s, NULL));
01541 rtcom_fail_unless_strcmp("butterfly/msn/alice", ==, s);
01542 g_free (s);
01543 fail_unless (rtcom_el_iter_get_values (it, "group-uid", &s, NULL));
01544 rtcom_fail_unless_strcmp("group(bob)", ==, s);
01545 g_free (s);
01546
01547 fail_unless (rtcom_el_iter_next (it));
01548 fail_unless (rtcom_el_iter_get_values (it, "remote-uid", &s, NULL));
01549 rtcom_fail_unless_strcmp("frank@msn.invalid", ==, s);
01550 g_free (s);
01551 fail_unless (rtcom_el_iter_get_values (it, "local-uid", &s, NULL));
01552 rtcom_fail_unless_strcmp("butterfly/msn/alice", ==, s);
01553 g_free (s);
01554 fail_unless (rtcom_el_iter_get_values (it, "group-uid", &s, NULL));
01555 rtcom_fail_unless_strcmp("group(chris+frank)", ==, s);
01556 g_free (s);
01557
01558
01559
01560
01561
01562 fail_if (rtcom_el_iter_next (it), "Iterator should have expired");
01563
01564 g_object_unref(it);
01565 }
01566 END_TEST
01567
01568 START_TEST(test_update_remote_contact)
01569 {
01570 RTComElQuery *query_by_abook;
01571 RTComElQuery *query_by_name;
01572 RTComElIter *it;
01573 gint count;
01574
01575
01576 fail_unless (rtcom_eventlogger_update_remote_contact (el,
01577 "gabble/jabber/alice", "bob@example.com",
01578 "abook-bob", "Robert", NULL));
01579
01580 query_by_abook = rtcom_el_query_new(el);
01581 fail_unless(rtcom_el_query_prepare(query_by_abook,
01582 "remote-ebook-uid", "abook-bob", RTCOM_EL_OP_EQUAL,
01583 NULL));
01584
01585
01586 it = rtcom_el_get_events (el, query_by_abook);
01587 fail_unless (it != NULL, "Failed to get iterator");
01588 count = iter_count_results (it);
01589 rtcom_fail_unless_intcmp (count, ==, 2);
01590
01591
01592 fail_unless (rtcom_eventlogger_update_remote_contact (el,
01593 "butterfly/msn/alice", "bob@example.com",
01594 "abook-bob", "Robert", NULL));
01595
01596 g_object_unref (it);
01597 it = rtcom_el_get_events (el, query_by_abook);
01598 fail_unless (it != NULL, "Failed to get iterator");
01599
01600
01601 count = iter_count_results (it);
01602 rtcom_fail_unless_intcmp (count, ==, 3);
01603
01604 g_object_unref (it);
01605
01606
01607 query_by_name = rtcom_el_query_new(el);
01608 fail_unless(rtcom_el_query_prepare(query_by_name,
01609 "remote-name", "Robert", RTCOM_EL_OP_EQUAL,
01610 NULL));
01611
01612 it = rtcom_el_get_events (el, query_by_name);
01613 fail_unless (it != NULL, "Failed to get iterator");
01614 count = iter_count_results (it);
01615 rtcom_fail_unless_intcmp (count, ==, 3);
01616
01617
01618 fail_unless (rtcom_eventlogger_update_remote_contact (el,
01619 "gabble/jabber/alice", "bob@example.com",
01620 NULL, "Robert", NULL));
01621 fail_unless (rtcom_eventlogger_update_remote_contact (el,
01622 "butterfly/msn/alice", "bob@example.com",
01623 NULL, "Robert", NULL));
01624
01625 g_object_unref (it);
01626 it = rtcom_el_get_events (el, query_by_name);
01627 fail_unless (it != NULL, "Failed to get iterator");
01628
01629 count = iter_count_results (it);
01630 rtcom_fail_unless_intcmp (count, ==, 3);
01631
01632 g_object_unref (it);
01633
01634 g_object_unref (query_by_abook);
01635 g_object_unref (query_by_name);
01636 }
01637 END_TEST
01638
01639 extern void db_extend_el_suite (Suite *s);
01640
01641 Suite *
01642 el_suite(void)
01643 {
01644 Suite * s = suite_create ("rtcom-eventlogger");
01645
01646
01647 db_extend_el_suite(s);
01648
01649
01650 TCase * tc_core = tcase_create("Core");
01651 tcase_add_checked_fixture(tc_core, core_setup, core_teardown);
01652 tcase_add_test(tc_core, test_add_event);
01653 tcase_add_test(tc_core, test_add_full);
01654 tcase_add_test(tc_core, test_header);
01655 tcase_add_test(tc_core, test_attach);
01656 tcase_add_test(tc_core, test_read);
01657 tcase_add_test(tc_core, test_flags);
01658 tcase_add_test(tc_core, test_get);
01659 tcase_add_test(tc_core, test_unique_remotes);
01660 tcase_add_test(tc_core, test_get_int);
01661 tcase_add_test(tc_core, test_get_string);
01662 tcase_add_test(tc_core, test_ends_with);
01663 tcase_add_test(tc_core, test_delete_events);
01664 tcase_add_test(tc_core, test_delete_event);
01665 tcase_add_test(tc_core, test_in_strv);
01666 tcase_add_test(tc_core, test_string_equals);
01667 tcase_add_test(tc_core, test_int_ranges);
01668 tcase_add_test(tc_core, test_group_by_uids);
01669 tcase_add_test(tc_core, test_group_by_metacontacts);
01670 tcase_add_test(tc_core, test_group_by_group);
01671 tcase_add_test(tc_core, test_update_remote_contact);
01672
01673 suite_add_tcase(s, tc_core);
01674
01675 return s;
01676 }
01677
01678 int main(void)
01679 {
01680 int number_failed;
01681 Suite * s = el_suite();
01682 SRunner * sr = srunner_create(s);
01683
01684 srunner_set_xml(sr, "/tmp/result.xml");
01685 srunner_run_all(sr, CK_NORMAL);
01686 number_failed = srunner_ntests_failed(sr);
01687 srunner_free (sr);
01688
01689 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
01690 }
01691
01692
01693