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