[evolution-data-server/openismus-work-master: 5/37] Added test case test-sqlite-cursor-move-by



commit b20fcfd3390d1fff4aee35be8f41f2ecaebab03b
Author: Tristan Van Berkom <tristanvb openismus com>
Date:   Mon Apr 15 20:46:01 2013 +0900

    Added test case test-sqlite-cursor-move-by
    
    This test case asserts that e_book_backend_sqlite_cursor_move_by()
    works as expected. Consequently, a hand full of additional test
    vcards were added for this.

 tests/libebook/Makefile.am                       |   11 +
 tests/libebook/data/vcards/sorted-1.vcf          |    6 +
 tests/libebook/data/vcards/sorted-10.vcf         |    6 +
 tests/libebook/data/vcards/sorted-11.vcf         |    6 +
 tests/libebook/data/vcards/sorted-2.vcf          |    7 +
 tests/libebook/data/vcards/sorted-3.vcf          |    7 +
 tests/libebook/data/vcards/sorted-4.vcf          |    6 +
 tests/libebook/data/vcards/sorted-5.vcf          |    6 +
 tests/libebook/data/vcards/sorted-6.vcf          |    5 +
 tests/libebook/data/vcards/sorted-7.vcf          |    5 +
 tests/libebook/data/vcards/sorted-8.vcf          |    6 +
 tests/libebook/data/vcards/sorted-9.vcf          |    6 +
 tests/libedata-book/Makefile.am                  |    5 +-
 tests/libedata-book/data-test-utils.c            |   70 ++++
 tests/libedata-book/data-test-utils.h            |    5 +
 tests/libedata-book/test-sqlite-cursor-move-by.c |  451 ++++++++++++++++++++++
 16 files changed, 607 insertions(+), 1 deletions(-)
---
diff --git a/tests/libebook/Makefile.am b/tests/libebook/Makefile.am
index 325b8f7..fdf7a48 100644
--- a/tests/libebook/Makefile.am
+++ b/tests/libebook/Makefile.am
@@ -62,6 +62,17 @@ EXTRA_DIST = \
        $(srcdir)/data/vcards/name-only.vcf     \
        $(srcdir)/data/vcards/simple-1.vcf      \
        $(srcdir)/data/vcards/simple-2.vcf      \
+       $(srcdir)/data/vcards/sorted-1.vcf      \
+       $(srcdir)/data/vcards/sorted-2.vcf      \
+       $(srcdir)/data/vcards/sorted-3.vcf      \
+       $(srcdir)/data/vcards/sorted-4.vcf      \
+       $(srcdir)/data/vcards/sorted-5.vcf      \
+       $(srcdir)/data/vcards/sorted-6.vcf      \
+       $(srcdir)/data/vcards/sorted-7.vcf      \
+       $(srcdir)/data/vcards/sorted-8.vcf      \
+       $(srcdir)/data/vcards/sorted-9.vcf      \
+       $(srcdir)/data/vcards/sorted-10.vcf     \
+       $(srcdir)/data/vcards/sorted-11.vcf     \
        $(srcdir)/data/vcards/custom-1.vcf      \
        $(srcdir)/data/vcards/custom-2.vcf      \
        $(srcdir)/data/vcards/custom-3.vcf      \
diff --git a/tests/libebook/data/vcards/sorted-1.vcf b/tests/libebook/data/vcards/sorted-1.vcf
new file mode 100644
index 0000000..eaa7f4b
--- /dev/null
+++ b/tests/libebook/data/vcards/sorted-1.vcf
@@ -0,0 +1,6 @@
+BEGIN:VCARD
+UID:sorted-1
+N:Jackson;Micheal
+TEL;HOME:+1-221-5423789
+EMAIL;TYPE=home,work:micheal jackson com
+END:VCARD
diff --git a/tests/libebook/data/vcards/sorted-10.vcf b/tests/libebook/data/vcards/sorted-10.vcf
new file mode 100644
index 0000000..20042ae
--- /dev/null
+++ b/tests/libebook/data/vcards/sorted-10.vcf
@@ -0,0 +1,6 @@
+BEGIN:VCARD
+UID:sorted-10
+N:J;Mister
+TEL;HOME:+1-221-5423789
+EMAIL;TYPE=home,work:mister jackson com
+END:VCARD
diff --git a/tests/libebook/data/vcards/sorted-11.vcf b/tests/libebook/data/vcards/sorted-11.vcf
new file mode 100644
index 0000000..b820cc8
--- /dev/null
+++ b/tests/libebook/data/vcards/sorted-11.vcf
@@ -0,0 +1,6 @@
+BEGIN:VCARD
+UID:sorted-11
+FN:Ye Nameless One
+TEL;HOME:+1-221-5423789
+EMAIL;TYPE=home,work:name less com
+END:VCARD
diff --git a/tests/libebook/data/vcards/sorted-2.vcf b/tests/libebook/data/vcards/sorted-2.vcf
new file mode 100644
index 0000000..a4ba4b1
--- /dev/null
+++ b/tests/libebook/data/vcards/sorted-2.vcf
@@ -0,0 +1,7 @@
+BEGIN:VCARD
+UID:sorted-2
+N:Jackson;Janet
+TEL;HOME:7654321
+EMAIL:janet jackson com
+EMAIL:janny jackson com
+END:VCARD
diff --git a/tests/libebook/data/vcards/sorted-3.vcf b/tests/libebook/data/vcards/sorted-3.vcf
new file mode 100644
index 0000000..74bc136
--- /dev/null
+++ b/tests/libebook/data/vcards/sorted-3.vcf
@@ -0,0 +1,7 @@
+BEGIN:VCARD
+UID:sorted-3
+N:Brown;Bobby
+TEL;HOME:+9999999
+EMAIL;TYPE=work:bobby brown org
+EMAIL;TYPE=home,work:bobby brown com
+END:VCARD
diff --git a/tests/libebook/data/vcards/sorted-4.vcf b/tests/libebook/data/vcards/sorted-4.vcf
new file mode 100644
index 0000000..289ac33
--- /dev/null
+++ b/tests/libebook/data/vcards/sorted-4.vcf
@@ -0,0 +1,6 @@
+BEGIN:VCARD
+UID:sorted-4
+N:Brown;Big Bobby
+TEL;TYPE=work,pref:+9999999
+EMAIL:big bobby brown org
+END:VCARD
diff --git a/tests/libebook/data/vcards/sorted-5.vcf b/tests/libebook/data/vcards/sorted-5.vcf
new file mode 100644
index 0000000..7798496
--- /dev/null
+++ b/tests/libebook/data/vcards/sorted-5.vcf
@@ -0,0 +1,6 @@
+BEGIN:VCARD
+UID:sorted-5
+N:Brown;James
+TEL;HOME:+6666666
+EMAIL;TYPE=home,work:james brown com
+END:VCARD
diff --git a/tests/libebook/data/vcards/sorted-6.vcf b/tests/libebook/data/vcards/sorted-6.vcf
new file mode 100644
index 0000000..fbcdfe9
--- /dev/null
+++ b/tests/libebook/data/vcards/sorted-6.vcf
@@ -0,0 +1,5 @@
+BEGIN:VCARD
+UID:sorted-6
+N:%Strange Name;Mister
+TEL;HOME:ask Jenny for Lisa's number
+END:VCARD
diff --git a/tests/libebook/data/vcards/sorted-7.vcf b/tests/libebook/data/vcards/sorted-7.vcf
new file mode 100644
index 0000000..861cd88
--- /dev/null
+++ b/tests/libebook/data/vcards/sorted-7.vcf
@@ -0,0 +1,5 @@
+BEGIN:VCARD
+UID:sorted-7
+N:Goose;Purple
+TEL;HOME:+49-89-7888 99
+END:VCARD
diff --git a/tests/libebook/data/vcards/sorted-8.vcf b/tests/libebook/data/vcards/sorted-8.vcf
new file mode 100644
index 0000000..ba8b06e
--- /dev/null
+++ b/tests/libebook/data/vcards/sorted-8.vcf
@@ -0,0 +1,6 @@
+BEGIN:VCARD
+UID:sorted-8
+N:Pony;Purple
+TEL;HOME:+31-221-5423789
+EMAIL;TYPE=home,work:purple pony com
+END:VCARD
diff --git a/tests/libebook/data/vcards/sorted-9.vcf b/tests/libebook/data/vcards/sorted-9.vcf
new file mode 100644
index 0000000..685420f
--- /dev/null
+++ b/tests/libebook/data/vcards/sorted-9.vcf
@@ -0,0 +1,6 @@
+BEGIN:VCARD
+UID:sorted-9
+N:Pony;Pink
+TEL;HOME:514-845-8436
+EMAIL;TYPE=home,work:pink pony com
+END:VCARD
diff --git a/tests/libedata-book/Makefile.am b/tests/libedata-book/Makefile.am
index 76e3d97..ce89961 100644
--- a/tests/libedata-book/Makefile.am
+++ b/tests/libedata-book/Makefile.am
@@ -37,7 +37,8 @@ libdata_test_utils_la_LIBADD =                                \
 # Should be kept ordered approximately from least to most difficult/complex
 TESTS =                                        \
        test-sqlite-get-contact         \
-       test-sqlite-create-cursor
+       test-sqlite-create-cursor       \
+       test-sqlite-cursor-move-by
 
 noinst_PROGRAMS = $(TESTS)
 
@@ -57,5 +58,7 @@ test_sqlite_get_contact_LDADD=$(TEST_LIBS)
 test_sqlite_get_contact_CPPFLAGS=$(TEST_CPPFLAGS)
 test_sqlite_create_cursor_LDADD=$(TEST_LIBS)
 test_sqlite_create_cursor_CPPFLAGS=$(TEST_CPPFLAGS)
+test_sqlite_cursor_move_by_LDADD=$(TEST_LIBS)
+test_sqlite_cursor_move_by_CPPFLAGS=$(TEST_CPPFLAGS)
 
 -include $(top_srcdir)/git.mk
diff --git a/tests/libedata-book/data-test-utils.c b/tests/libedata-book/data-test-utils.c
index 015e1c8..00749a6 100644
--- a/tests/libedata-book/data-test-utils.c
+++ b/tests/libedata-book/data-test-utils.c
@@ -184,3 +184,73 @@ e_sqlitedb_fixture_teardown (ESqliteDBFixture *fixture,
        g_object_unref (fixture->ebsdb);
        e_test_server_utils_teardown ((ETestServerFixture *)fixture, user_data);
 }
+
+
+static gint
+find_contact_data (EbSdbSearchData *data,
+                  const gchar     *uid)
+{
+       return g_strcmp0 (data->uid, uid);
+}
+
+void
+assert_contacts_order (GSList      *results,
+                      const gchar *first_uid,
+                      ...)
+{
+       GSList *uids = NULL, *link, *l;
+       gint position = -1;
+       gchar *uid;
+       va_list args;
+
+       g_assert (first_uid);
+
+       uids = g_slist_append (uids, (gpointer)first_uid);
+
+       va_start (args, first_uid);
+       uid = va_arg (args, gchar*);
+       while (uid) {
+               uids = g_slist_append (uids, uid);
+               uid = va_arg (args, gchar*);
+       }
+       va_end (args);
+
+       /* Assert that all passed UIDs are found in the
+        * results, and that those UIDs are in the
+        * specified order.
+        */
+       for (l = uids; l; l = l->next) {
+               gint new_position;
+
+               uid = l->data;
+
+               link = g_slist_find_custom (results, uid, (GCompareFunc)find_contact_data);
+               if (!link)
+                       g_error ("Specified uid was not found in results");
+
+               new_position = g_slist_position (results, link);
+               g_assert_cmpint (new_position, >, position);
+               position = new_position;
+       }
+
+       g_slist_free (uids);
+}
+
+void
+print_results (GSList      *results)
+{
+       GSList *l;
+
+       if (g_getenv ("TEST_DEBUG") == NULL)
+               return;
+
+       g_print ("\nPRINTING RESULTS:\n");
+
+       for (l = results; l; l = l->next) {
+               EbSdbSearchData *data = l->data;
+
+               g_print ("\n%s\n", data->vcard);
+       }
+
+       g_print ("\nRESULT LIST_FINISHED\n");
+}
diff --git a/tests/libedata-book/data-test-utils.h b/tests/libedata-book/data-test-utils.h
index e55bd78..a89059a 100644
--- a/tests/libedata-book/data-test-utils.h
+++ b/tests/libedata-book/data-test-utils.h
@@ -46,5 +46,10 @@ gboolean add_contact_from_test_case_verify (EBookClient *book_client,
                                            const gchar *case_name,
                                            EContact   **contact);
 
+void     assert_contacts_order             (GSList      *results,
+                                           const gchar *first_uid,
+                                           ...) G_GNUC_NULL_TERMINATED;
+
+void     print_results                     (GSList      *results);
 
 #endif /* DATA_TEST_UTILS_H */
diff --git a/tests/libedata-book/test-sqlite-cursor-move-by.c 
b/tests/libedata-book/test-sqlite-cursor-move-by.c
new file mode 100644
index 0000000..e993bc1
--- /dev/null
+++ b/tests/libedata-book/test-sqlite-cursor-move-by.c
@@ -0,0 +1,451 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+
+#include <stdlib.h>
+#include <locale.h>
+#include <libebook/libebook.h>
+
+#include "data-test-utils.h"
+
+/* Optimize queries, just so we can run with BOOKSQL_DEBUG=2 and check the
+ * indexes are properly leverage for cursor queries
+ */
+static void
+setup_custom_book (ESource            *scratch,
+                  ETestServerClosure *closure)
+{
+       ESourceBackendSummarySetup *setup;
+
+       g_type_class_unref (g_type_class_ref (E_TYPE_SOURCE_BACKEND_SUMMARY_SETUP));
+       setup = e_source_get_extension (scratch, E_SOURCE_EXTENSION_BACKEND_SUMMARY_SETUP);
+       e_source_backend_summary_setup_set_summary_fields (setup,
+                                                          E_CONTACT_FAMILY_NAME,
+                                                          E_CONTACT_GIVEN_NAME,
+                                                          E_CONTACT_EMAIL,
+                                                          0);
+       e_source_backend_summary_setup_set_indexed_fields (setup,
+                                                          E_CONTACT_FAMILY_NAME, E_BOOK_INDEX_PREFIX,
+                                                          E_CONTACT_GIVEN_NAME, E_BOOK_INDEX_PREFIX,
+                                                          E_CONTACT_EMAIL, E_BOOK_INDEX_PREFIX,
+                                                          0);
+}
+
+static ETestServerClosure book_closure = { E_TEST_SERVER_ADDRESS_BOOK, setup_custom_book, 0 };
+
+typedef struct {
+       ESqliteDBFixture parent_fixture;
+
+       EbSdbCursor  *cursor;
+
+       EContact *contacts[11];
+
+       EBookQuery *query;
+} CursorFixture;
+
+static void
+cursor_fixture_setup (CursorFixture *fixture,
+                     gconstpointer  user_data)
+{
+       EContactField sort_fields[] = { E_CONTACT_FAMILY_NAME, E_CONTACT_GIVEN_NAME };
+       EBookSortType sort_types[] = { E_BOOK_SORT_ASCENDING, E_BOOK_SORT_ASCENDING };
+       GError       *error = NULL;
+       EBookClient  *book_client;
+       EContact    **it = fixture->contacts;
+       gchar        *sexp = NULL;
+
+       e_sqlitedb_fixture_setup ((ESqliteDBFixture *)fixture, user_data);
+
+       book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);
+
+       /* Add contacts... */
+       if (/* N:Jackson;Micheal */
+           !add_contact_from_test_case_verify (book_client, "sorted-1", it++) ||
+           /* N:Jackson;Janet */
+           !add_contact_from_test_case_verify (book_client, "sorted-2", it++) ||
+           /* N:Brown;Bobby */
+           !add_contact_from_test_case_verify (book_client, "sorted-3", it++) ||
+           /* N:Brown;Big Bobby */
+           !add_contact_from_test_case_verify (book_client, "sorted-4", it++) ||
+           /* N:Brown;James */
+           !add_contact_from_test_case_verify (book_client, "sorted-5", it++) ||
+           /* N:%Strange Name;Mister */
+           !add_contact_from_test_case_verify (book_client, "sorted-6", it++) ||
+           /* N:Goose;Purple */
+           !add_contact_from_test_case_verify (book_client, "sorted-7", it++) ||
+           /* N:Pony;Purple */
+           !add_contact_from_test_case_verify (book_client, "sorted-8", it++) ||
+           /* N:Pony;Pink */
+           !add_contact_from_test_case_verify (book_client, "sorted-9", it++) ||
+           /* N:J;Mister */
+           !add_contact_from_test_case_verify (book_client, "sorted-10", it++) ||
+           /* FN:Ye Nameless One */
+           !add_contact_from_test_case_verify (book_client, "sorted-11", it++)) {
+               g_error ("Failed to add contacts");
+       }
+
+       /* Allow a surrounding fixture setup to add a query here */
+       if (fixture->query) {
+               sexp = e_book_query_to_string (fixture->query);
+               e_book_query_unref (fixture->query);
+               fixture->query = NULL;
+       }
+
+       fixture->cursor = e_book_backend_sqlitedb_cursor_new (((ESqliteDBFixture *) fixture)->ebsdb,
+                                                             SQLITEDB_FOLDER_ID,
+                                                             sexp, sort_fields, sort_types, 2, &error);
+
+
+       g_free (sexp);
+
+       g_assert (fixture->cursor != NULL);
+}
+
+static void
+cursor_fixture_teardown (CursorFixture *fixture,
+                        gconstpointer  user_data)
+{
+       gint i;
+
+       for (i = 0; i < G_N_ELEMENTS (fixture->contacts); ++i) {
+               if (fixture->contacts[i])
+                       g_object_unref (fixture->contacts[i]);
+       }
+
+       e_book_backend_sqlitedb_cursor_free (((ESqliteDBFixture *) fixture)->ebsdb, fixture->cursor);
+       e_sqlitedb_fixture_teardown ((ESqliteDBFixture *)fixture, user_data);
+}
+
+/********************** Move forward **********************/
+static void
+test_cursor_move_forward (CursorFixture *fixture,
+                         gconstpointer  user_data)
+{
+       GSList *results;
+       GError *error = NULL;
+
+       /* First batch */
+       results = e_book_backend_sqlitedb_cursor_move_by (((ESqliteDBFixture *) fixture)->ebsdb,
+                                                         fixture->cursor, 5, &error);
+
+       if (error)
+               g_error ("Error fetching cursor results: %s", error->message);
+
+       print_results (results);
+
+       g_assert_cmpint (g_slist_length (results), ==, 5);
+
+       /* Assert that we got the results ordered as:
+        *   "Ye Nameless One"
+        *   "Big Bobby Brown"
+        *   "Bobby Brown"
+        *   "James Brown"
+        */
+       assert_contacts_order (results,
+                              e_contact_get_const (fixture->contacts[10], E_CONTACT_UID),
+                              e_contact_get_const (fixture->contacts[3], E_CONTACT_UID),
+                              e_contact_get_const (fixture->contacts[2], E_CONTACT_UID),
+                              e_contact_get_const (fixture->contacts[4], E_CONTACT_UID),
+                              NULL);
+
+       g_slist_foreach (results, (GFunc)e_book_backend_sqlitedb_search_data_free, NULL);
+       g_slist_free (results);
+
+       /* Second batch */
+       results = e_book_backend_sqlitedb_cursor_move_by (((ESqliteDBFixture *) fixture)->ebsdb,
+                                                         fixture->cursor, 6, &error);
+
+       if (error)
+               g_error ("Error fetching cursor results: %s", error->message);
+
+       print_results (results);
+
+       /* We asked for 5, but only expect 4 remaining results in the query */
+       g_assert_cmpint (g_slist_length (results), ==, 6);
+
+       /* Assert that we got the results ordered as:
+        *   "Janet Jackson"
+        *   "Micheal Jackson"
+        *   "Pink Pony"
+        */
+       assert_contacts_order (results,
+                              e_contact_get_const (fixture->contacts[1], E_CONTACT_UID),
+                              e_contact_get_const (fixture->contacts[0], E_CONTACT_UID),
+                              e_contact_get_const (fixture->contacts[8], E_CONTACT_UID),
+                              NULL);
+
+       g_slist_foreach (results, (GFunc)e_book_backend_sqlitedb_search_data_free, NULL);
+       g_slist_free (results);
+}
+
+/********************** Move forward on nameless **********************/
+static void
+test_cursor_move_forward_on_nameless (CursorFixture *fixture,
+                                     gconstpointer  user_data)
+{
+       GSList *results;
+       GError *error = NULL;
+
+       /* First batch */
+       results = e_book_backend_sqlitedb_cursor_move_by (((ESqliteDBFixture *) fixture)->ebsdb,
+                                                         fixture->cursor, 1, &error);
+
+       if (error)
+               g_error ("Error fetching cursor results: %s", error->message);
+
+       print_results (results);
+
+       g_assert_cmpint (g_slist_length (results), ==, 1);
+
+       /* Assert that we got the results ordered as:
+        *   "Ye Nameless One"
+        */
+       assert_contacts_order (results,
+                              e_contact_get_const (fixture->contacts[10], E_CONTACT_UID),
+                              NULL);
+
+       g_slist_foreach (results, (GFunc)e_book_backend_sqlitedb_search_data_free, NULL);
+       g_slist_free (results);
+
+       /* Second batch */
+       results = e_book_backend_sqlitedb_cursor_move_by (((ESqliteDBFixture *) fixture)->ebsdb,
+                                                         fixture->cursor, 3, &error);
+
+       if (error)
+               g_error ("Error fetching cursor results: %s", error->message);
+
+       print_results (results);
+
+       g_assert_cmpint (g_slist_length (results), ==, 3);
+
+       /* Assert that we got the results ordered as:
+        *   "Big Bobby Brown"
+        *   "Bobby Brown"
+        *   "James Brown"
+        */
+       assert_contacts_order (results,
+                              e_contact_get_const (fixture->contacts[3], E_CONTACT_UID),
+                              e_contact_get_const (fixture->contacts[2], E_CONTACT_UID),
+                              e_contact_get_const (fixture->contacts[4], E_CONTACT_UID),
+                              NULL);
+
+       g_slist_foreach (results, (GFunc)e_book_backend_sqlitedb_search_data_free, NULL);
+       g_slist_free (results);
+}
+
+/********************** Move backwards **********************/
+static void
+test_cursor_move_backwards (CursorFixture *fixture,
+                           gconstpointer  user_data)
+{
+       GSList *results;
+       GError *error = NULL;
+
+       /* First batch (starts from the end of the results) */
+       results = e_book_backend_sqlitedb_cursor_move_by (((ESqliteDBFixture *) fixture)->ebsdb,
+                                                         fixture->cursor, -5, &error);
+
+       if (error)
+               g_error ("Error fetching cursor results: %s", error->message);
+
+       print_results (results);
+
+       g_assert_cmpint (g_slist_length (results), ==, 5);
+
+       /* Assert that we got the results ordered as:
+        *   "Pink Pony"
+        *   "Micheal Jackson"
+        *   "Janet Jackson"
+        */
+       assert_contacts_order (results,
+                              e_contact_get_const (fixture->contacts[8], E_CONTACT_UID),
+                              e_contact_get_const (fixture->contacts[0], E_CONTACT_UID),
+                              e_contact_get_const (fixture->contacts[1], E_CONTACT_UID),
+                              NULL);
+
+       g_slist_foreach (results, (GFunc)e_book_backend_sqlitedb_search_data_free, NULL);
+       g_slist_free (results);
+
+       /* Second batch */
+       results = e_book_backend_sqlitedb_cursor_move_by (((ESqliteDBFixture *) fixture)->ebsdb,
+                                                         fixture->cursor, -6, &error);
+
+       if (error)
+               g_error ("Error fetching cursor results: %s", error->message);
+
+       print_results (results);
+
+       /* We asked for 5, but only expect 4 remaining results in the query */
+       g_assert_cmpint (g_slist_length (results), ==, 6);
+
+       /* Assert that we got the results ordered as:
+        *   "Big Bobby Brown", 
+        *   "Bobby Brown",
+        *   "James Brown"
+        */
+       assert_contacts_order (results,
+                              e_contact_get_const (fixture->contacts[4], E_CONTACT_UID),
+                              e_contact_get_const (fixture->contacts[2], E_CONTACT_UID),
+                              e_contact_get_const (fixture->contacts[3], E_CONTACT_UID),
+                              e_contact_get_const (fixture->contacts[10], E_CONTACT_UID),
+                              NULL);
+
+       g_slist_foreach (results, (GFunc)e_book_backend_sqlitedb_search_data_free, NULL);
+       g_slist_free (results);
+}
+
+/********************** Move forward filtered **********************/
+static void
+test_cursor_move_filtered_setup (CursorFixture *fixture,
+                                gconstpointer  user_data)
+{
+       fixture->query = e_book_query_field_test (E_CONTACT_EMAIL, E_BOOK_QUERY_ENDS_WITH, ".com");
+
+       cursor_fixture_setup (fixture, user_data);
+}
+
+static void
+test_cursor_move_forward_filtered (CursorFixture *fixture,
+                                  gconstpointer  user_data)
+{
+       GSList *results;
+       GError *error = NULL;
+
+       /* First batch */
+       results = e_book_backend_sqlitedb_cursor_move_by (((ESqliteDBFixture *) fixture)->ebsdb,
+                                                         fixture->cursor, 5, &error);
+
+       if (error)
+               g_error ("Error fetching cursor results: %s", error->message);
+
+       print_results (results);
+
+       g_assert_cmpint (g_slist_length (results), ==, 5);
+
+       /* Assert that we got the results ordered as:
+        *   "Bobby Brown"
+        *   "James Brown"
+        */
+       assert_contacts_order (results,
+                              e_contact_get_const (fixture->contacts[2], E_CONTACT_UID),
+                              e_contact_get_const (fixture->contacts[4], E_CONTACT_UID),
+                              NULL);
+
+       g_slist_foreach (results, (GFunc)e_book_backend_sqlitedb_search_data_free, NULL);
+       g_slist_free (results);
+
+       /* Second batch */
+       results = e_book_backend_sqlitedb_cursor_move_by (((ESqliteDBFixture *) fixture)->ebsdb,
+                                                         fixture->cursor, 5, &error);
+
+       if (error)
+               g_error ("Error fetching cursor results: %s", error->message);
+
+       print_results (results);
+
+       /* We asked for 5, but only expect 3 remaining filtered result in the query */
+       g_assert_cmpint (g_slist_length (results), ==, 3);
+
+       /* Assert the last result is:
+        *   "Micheal Jackson"
+        *   "Pink Pony"
+        *   "Purple Pony"
+        */
+       assert_contacts_order (results,
+                              e_contact_get_const (fixture->contacts[0], E_CONTACT_UID),
+                              e_contact_get_const (fixture->contacts[8], E_CONTACT_UID),
+                              e_contact_get_const (fixture->contacts[7], E_CONTACT_UID),
+                              NULL);
+
+       g_slist_foreach (results, (GFunc)e_book_backend_sqlitedb_search_data_free, NULL);
+       g_slist_free (results);
+}
+
+/********************** Move backwards filtered **********************/
+static void
+test_cursor_move_backwards_filtered (CursorFixture *fixture,
+                                    gconstpointer  user_data)
+{
+       GSList *results;
+       GError *error = NULL;
+
+       /* First batch */
+       results = e_book_backend_sqlitedb_cursor_move_by (((ESqliteDBFixture *) fixture)->ebsdb,
+                                                         fixture->cursor, -5, &error);
+
+       if (error)
+               g_error ("Error fetching cursor results: %s", error->message);
+
+       print_results (results);
+
+       g_assert_cmpint (g_slist_length (results), ==, 5);
+
+       /* Assert that we got the results ordered as:
+        *   "Purple Pony"
+        *   "Pink Pony"
+        *   "Micheal Jackson"
+        */
+       assert_contacts_order (results,
+                              e_contact_get_const (fixture->contacts[7], E_CONTACT_UID),
+                              e_contact_get_const (fixture->contacts[8], E_CONTACT_UID),
+                              e_contact_get_const (fixture->contacts[0], E_CONTACT_UID),
+                              NULL);
+
+       g_slist_foreach (results, (GFunc)e_book_backend_sqlitedb_search_data_free, NULL);
+       g_slist_free (results);
+
+       /* Second batch */
+       results = e_book_backend_sqlitedb_cursor_move_by (((ESqliteDBFixture *) fixture)->ebsdb,
+                                                         fixture->cursor, -5, &error);
+
+       if (error)
+               g_error ("Error fetching cursor results: %s", error->message);
+
+       print_results (results);
+
+       /* We asked for 5, but only expect 3 remaining filtered result in the query */
+       g_assert_cmpint (g_slist_length (results), ==, 3);
+
+       /* Assert the last result is:
+        *   "James Brown"
+        *   "Bobby Brown"
+        *   "Ye Nameless One"
+        */
+       assert_contacts_order (results,
+                              e_contact_get_const (fixture->contacts[4], E_CONTACT_UID),
+                              e_contact_get_const (fixture->contacts[2], E_CONTACT_UID),
+                              e_contact_get_const (fixture->contacts[10], E_CONTACT_UID),
+                              NULL);
+
+       g_slist_foreach (results, (GFunc)e_book_backend_sqlitedb_search_data_free, NULL);
+       g_slist_free (results);
+}
+
+gint
+main (gint argc,
+      gchar **argv)
+{
+#if !GLIB_CHECK_VERSION (2, 35, 1)
+       g_type_init ();
+#endif
+       g_test_init (&argc, &argv, NULL);
+
+       /* Ensure that the client and server get the same locale */
+       g_assert (g_setenv ("LC_ALL", "en_US.UTF-8", TRUE));
+       setlocale (LC_ALL, "");
+
+       g_test_add ("/EbSdbCursor/MoveBy/MoveForward", CursorFixture, &book_closure,
+                   cursor_fixture_setup, test_cursor_move_forward, cursor_fixture_teardown);
+       g_test_add ("/EbSdbCursor/MoveBy/MoveForwardOnNameless", CursorFixture, &book_closure,
+                   cursor_fixture_setup, test_cursor_move_forward_on_nameless, cursor_fixture_teardown);
+       g_test_add ("/EbSdbCursor/MoveBy/MoveBackwards", CursorFixture, &book_closure,
+                   cursor_fixture_setup, test_cursor_move_backwards, cursor_fixture_teardown);
+       g_test_add ("/EbSdbCursor/MoveBy/Filtered/MoveForward", CursorFixture, &book_closure,
+                   test_cursor_move_filtered_setup,
+                   test_cursor_move_forward_filtered,
+                   cursor_fixture_teardown);
+       g_test_add ("/EbSdbCursor/MoveBy/Filtered/MoveBackwards", CursorFixture, &book_closure,
+                   test_cursor_move_filtered_setup,
+                   test_cursor_move_backwards_filtered,
+                   cursor_fixture_teardown);
+
+       return e_test_server_utils_run ();
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]