[evolution-data-server/openismus-work-master: 48/76] Added tests to ensure descending order works properly with the Cursor API.
- From: Tristan Van Berkom <tvb src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [evolution-data-server/openismus-work-master: 48/76] Added tests to ensure descending order works properly with the Cursor API.
- Date: Tue, 30 Jul 2013 21:38:27 +0000 (UTC)
commit 3c80c9df28308dce25efd0b5f89d05fdee04ed35
Author: Tristan Van Berkom <tristanvb openismus com>
Date: Fri Jun 14 21:23:47 2013 +0900
Added tests to ensure descending order works properly with the Cursor API.
tests/libedata-book/data-test-utils.c | 33 +++-
tests/libedata-book/data-test-utils.h | 6 +-
tests/libedata-book/test-sqlite-cursor-calculate.c | 221 ++++++++++++++++++-
.../test-sqlite-cursor-move-by-en-US.c | 17 ++
4 files changed, 261 insertions(+), 16 deletions(-)
---
diff --git a/tests/libedata-book/data-test-utils.c b/tests/libedata-book/data-test-utils.c
index 3379934..ea49211 100644
--- a/tests/libedata-book/data-test-utils.c
+++ b/tests/libedata-book/data-test-utils.c
@@ -225,7 +225,7 @@ e_sqlitedb_cursor_fixture_setup (EbSdbCursorFixture *fixture,
ESqliteDBFixture *ebsdb_fixture = (ESqliteDBFixture *)fixture;
EbSdbCursorClosure *data = (EbSdbCursorClosure *)user_data;
EContactField sort_fields[] = { E_CONTACT_FAMILY_NAME, E_CONTACT_GIVEN_NAME };
- EBookSortType sort_types[] = { E_BOOK_SORT_ASCENDING, E_BOOK_SORT_ASCENDING };
+ EBookSortType sort_types[] = { data->sort_type, data->sort_type };
EBookClient *book_client;
GSList *contacts = NULL;
GError *error = NULL;
@@ -495,6 +495,7 @@ move_by_test_new_internal (const gchar *test_path,
data->parent.parent.type = E_TEST_SERVER_ADDRESS_BOOK;
data->parent.parent.customize = e_sqlitedb_cursor_fixture_setup_book;
data->parent.locale = g_strdup (locale);
+ data->parent.sort_type = E_BOOK_SORT_ASCENDING;
data->path = g_strdup (test_path);
data->struct_size = struct_size;
@@ -520,6 +521,19 @@ move_by_test_new (const gchar *test_path,
return move_by_test_new_internal (test_path, locale, sizeof (MoveByData));
}
+MoveByData *
+move_by_test_new_full (const gchar *test_path,
+ const gchar *locale,
+ EBookSortType sort_type)
+{
+ MoveByData *data;
+
+ data = move_by_test_new_internal (test_path, locale, sizeof (MoveByData));
+ data->parent.sort_type = sort_type;
+
+ return data;
+}
+
static void
test_cursor_move_teardown (EbSdbCursorFixture *fixture,
gconstpointer user_data)
@@ -538,10 +552,20 @@ assert_move_by (EbSdbCursorFixture *fixture,
GSList *results)
{
GSList *uids = NULL;
- gint j;
+ gint j, expected = 0;
+
+ /* Count the number of really expected results */
+ for (j = 0; j < ABS (data->counts[i]); j++) {
+ gint index = data->expected[i][j];
+
+ if (index < 0)
+ break;
+
+ expected++;
+ }
/* Assert the exact amount of requested results */
- g_assert_cmpint (g_slist_length (results), ==, ABS (data->counts[i]));
+ g_assert_cmpint (g_slist_length (results), ==, expected);
#if DEBUG_FIXTURE
g_print ("%s: Constructing expected result list for a fetch of %d: ",
@@ -551,6 +575,9 @@ assert_move_by (EbSdbCursorFixture *fixture,
gint index = data->expected[i][j];
gchar *uid;
+ if (index < 0)
+ break;
+
uid = (gchar *)e_contact_get_const (fixture->contacts[index], E_CONTACT_UID);
uids = g_slist_append (uids, uid);
diff --git a/tests/libedata-book/data-test-utils.h b/tests/libedata-book/data-test-utils.h
index 38ecda1..6e86dfb 100644
--- a/tests/libedata-book/data-test-utils.h
+++ b/tests/libedata-book/data-test-utils.h
@@ -90,7 +90,8 @@ typedef struct {
typedef struct {
ETestServerClosure parent;
- const gchar *locale;
+ const gchar *locale;
+ EBookSortType sort_type;
} EbSdbCursorClosure;
typedef struct {
@@ -147,6 +148,9 @@ void move_by_test_add_assertion (MoveByData *data,
...);
MoveByData *move_by_test_new (const gchar *test_path,
const gchar *locale);
+MoveByData *move_by_test_new_full (const gchar *test_path,
+ const gchar *locale,
+ EBookSortType sort_type);
void move_by_test_add (MoveByData *data,
gboolean filtered);
diff --git a/tests/libedata-book/test-sqlite-cursor-calculate.c
b/tests/libedata-book/test-sqlite-cursor-calculate.c
index d895e3d..c60e726 100644
--- a/tests/libedata-book/test-sqlite-cursor-calculate.c
+++ b/tests/libedata-book/test-sqlite-cursor-calculate.c
@@ -6,7 +6,17 @@
#include "data-test-utils.h"
-static EbSdbCursorClosure book_closure = { { E_TEST_SERVER_ADDRESS_BOOK,
e_sqlitedb_cursor_fixture_setup_book, 0 }, FALSE };
+static EbSdbCursorClosure ascending_closure = {
+ { E_TEST_SERVER_ADDRESS_BOOK, e_sqlitedb_cursor_fixture_setup_book, 0 },
+ NULL,
+ E_BOOK_SORT_ASCENDING
+};
+
+static EbSdbCursorClosure descending_closure = {
+ { E_TEST_SERVER_ADDRESS_BOOK, e_sqlitedb_cursor_fixture_setup_book, 0 },
+ NULL,
+ E_BOOK_SORT_DESCENDING
+};
static void
test_cursor_calculate_initial (EbSdbCursorFixture *fixture,
@@ -410,6 +420,168 @@ test_cursor_calculate_filtered_after_modification (EbSdbCursorFixture *fixture,
g_assert_cmpint (total, ==, 13);
}
+static void
+test_cursor_calculate_descending_move_forward (EbSdbCursorFixture *fixture,
+ gconstpointer user_data)
+{
+ GSList *results;
+ GError *error = NULL;
+ gint position = 0, total = 0;
+
+ /* Move cursor */
+ results = e_book_backend_sqlitedb_cursor_move_by (((ESqliteDBFixture *) fixture)->ebsdb,
+ fixture->cursor,
+ EBSDB_CURSOR_ORIGIN_CURRENT,
+ 5, &error);
+
+ if (error)
+ g_error ("Error fetching cursor results: %s", error->message);
+
+ /* Assert the first 5 contacts in en_US order */
+ g_assert_cmpint (g_slist_length (results), ==, 5);
+ assert_contacts_order (results,
+ "sorted-20",
+ "sorted-19",
+ "sorted-9",
+ "sorted-13",
+ "sorted-12",
+ NULL);
+ g_slist_foreach (results, (GFunc)e_book_backend_sqlitedb_search_data_free, NULL);
+ g_slist_free (results);
+
+ /* Check new position */
+ if (!e_book_backend_sqlitedb_cursor_calculate (((ESqliteDBFixture *) fixture)->ebsdb,
+ fixture->cursor, &total, &position, &error))
+ g_error ("Error calculating cursor: %s", error->message);
+
+ /* results 0 + 5 = position 5, result index 4 (results[0, 1, 2, 3, 4]) */
+ g_assert_cmpint (position, ==, 5);
+ g_assert_cmpint (total, ==, 20);
+}
+
+static void
+test_cursor_calculate_descending_move_backwards (EbSdbCursorFixture *fixture,
+ gconstpointer user_data)
+{
+ GSList *results;
+ GError *error = NULL;
+ gint position = 0, total = 0;
+
+ /* Move cursor */
+ results = e_book_backend_sqlitedb_cursor_move_by (((ESqliteDBFixture *) fixture)->ebsdb,
+ fixture->cursor,
+ EBSDB_CURSOR_ORIGIN_CURRENT,
+ -5, &error);
+
+ if (error)
+ g_error ("Error fetching cursor results: %s", error->message);
+
+ /* Assert the last 5 contacts in en_US order */
+ g_assert_cmpint (g_slist_length (results), ==, 5);
+ assert_contacts_order (results,
+ "sorted-11",
+ "sorted-1",
+ "sorted-2",
+ "sorted-5",
+ "sorted-6",
+ NULL);
+ g_slist_foreach (results, (GFunc)e_book_backend_sqlitedb_search_data_free, NULL);
+ g_slist_free (results);
+
+ /* Check new position */
+ if (!e_book_backend_sqlitedb_cursor_calculate (((ESqliteDBFixture *) fixture)->ebsdb,
+ fixture->cursor, &total, &position, &error))
+ g_error ("Error calculating cursor: %s", error->message);
+
+ /* results 20 - 5 = position 16 result index 15 (results[20, 19, 18, 17, 16]) */
+ g_assert_cmpint (position, ==, 16);
+ g_assert_cmpint (total, ==, 20);
+}
+
+static void
+test_cursor_calculate_descending_partial_target (EbSdbCursorFixture *fixture,
+ gconstpointer user_data)
+{
+ GError *error = NULL;
+ gint position = 0, total = 0;
+ ECollator *collator;
+ gint n_labels;
+ const gchar *const *labels;
+
+ /* First verify our test... in en_US locale the label 'C' should exist with the index 3 */
+ collator = e_book_backend_sqlitedb_ref_collator (((ESqliteDBFixture *) fixture)->ebsdb);
+ labels = e_collator_get_index_labels (collator, &n_labels, NULL, NULL, NULL);
+ g_assert_cmpstr (labels[3], ==, "C");
+ e_collator_unref (collator);
+
+ /* Set the cursor at the start of family names beginning with 'C' */
+ e_book_backend_sqlitedb_cursor_set_target_alphabetic_index (((ESqliteDBFixture *) fixture)->ebsdb,
+ fixture->cursor, 3);
+
+ /* Check new position */
+ if (!e_book_backend_sqlitedb_cursor_calculate (((ESqliteDBFixture *) fixture)->ebsdb,
+ fixture->cursor, &total, &position, &error))
+ g_error ("Error calculating cursor: %s", error->message);
+
+ /* Position is 7, there are 7 contacts leading up to the last 'C' in en_US locale
+ * (when sorting in descending order) */
+ g_assert_cmpint (position, ==, 7);
+ g_assert_cmpint (total, ==, 20);
+}
+
+static void
+test_cursor_calculate_descending_after_modification (EbSdbCursorFixture *fixture,
+ gconstpointer user_data)
+{
+ EBookClient *book_client;
+ GError *error = NULL;
+ gint position = 0, total = 0;
+ GSList *results;
+
+ book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);
+
+ /* Set the cursor to point exactly 'Bät' (which is the 12th contact in descending order) */
+ results = e_book_backend_sqlitedb_cursor_move_by (((ESqliteDBFixture *) fixture)->ebsdb,
+ fixture->cursor,
+ EBSDB_CURSOR_ORIGIN_CURRENT,
+ 12, &error);
+ if (error)
+ g_error ("Error fetching cursor results: %s", error->message);
+
+ g_slist_foreach (results, (GFunc)e_book_backend_sqlitedb_search_data_free, NULL);
+ g_slist_free (results);
+
+ /* Check new position */
+ if (!e_book_backend_sqlitedb_cursor_calculate (((ESqliteDBFixture *) fixture)->ebsdb,
+ fixture->cursor, &total, &position, &error))
+ g_error ("Error calculating cursor: %s", error->message);
+
+ /* 'Bät' is at position 12 in en_US locale (descending order) */
+ g_assert_cmpint (position, ==, 12);
+ g_assert_cmpint (total, ==, 20);
+
+ /* Rename Muffler -> Jacob Appelbaum */
+ e_contact_set (fixture->contacts[19 - 1], E_CONTACT_FAMILY_NAME, "Appelbaum");
+ e_contact_set (fixture->contacts[19 - 1], E_CONTACT_GIVEN_NAME, "Jacob");
+ if (!e_book_client_modify_contact_sync (book_client, fixture->contacts[19 - 1], NULL, &error))
+ g_error ("modify contact sync: %s", error->message);
+
+ /* Rename Müller -> Sade Adu */
+ e_contact_set (fixture->contacts[20 - 1], E_CONTACT_FAMILY_NAME, "Adu");
+ e_contact_set (fixture->contacts[20 - 1], E_CONTACT_GIVEN_NAME, "Sade");
+ if (!e_book_client_modify_contact_sync (book_client, fixture->contacts[20 - 1], NULL, &error))
+ g_error ("modify contact sync: %s", error->message);
+
+ /* Check new position */
+ if (!e_book_backend_sqlitedb_cursor_calculate (((ESqliteDBFixture *) fixture)->ebsdb,
+ fixture->cursor, &total, &position, &error))
+ g_error ("Error calculating cursor: %s", error->message);
+
+ /* 'Bät' is now at position 10 in descending order after moving 2 contacts to begin with 'A' */
+ g_assert_cmpint (position, ==, 10);
+ g_assert_cmpint (total, ==, 20);
+}
+
gint
main (gint argc,
gchar **argv)
@@ -419,51 +591,76 @@ main (gint argc,
#endif
g_test_init (&argc, &argv, NULL);
- g_test_add ("/EbSdbCursor/Calculate/Initial", EbSdbCursorFixture, &book_closure,
+ g_test_add ("/EbSdbCursor/Calculate/Initial", EbSdbCursorFixture, &ascending_closure,
e_sqlitedb_cursor_fixture_setup,
test_cursor_calculate_initial,
e_sqlitedb_cursor_fixture_teardown);
- g_test_add ("/EbSdbCursor/Calculate/MoveForward", EbSdbCursorFixture, &book_closure,
+ g_test_add ("/EbSdbCursor/Calculate/MoveForward", EbSdbCursorFixture, &ascending_closure,
e_sqlitedb_cursor_fixture_setup,
test_cursor_calculate_move_forward,
e_sqlitedb_cursor_fixture_teardown);
- g_test_add ("/EbSdbCursor/Calculate/MoveBackwards", EbSdbCursorFixture, &book_closure,
+ g_test_add ("/EbSdbCursor/Calculate/MoveBackwards", EbSdbCursorFixture, &ascending_closure,
e_sqlitedb_cursor_fixture_setup,
test_cursor_calculate_move_backwards,
e_sqlitedb_cursor_fixture_teardown);
- g_test_add ("/EbSdbCursor/Calculate/BackAndForth", EbSdbCursorFixture, &book_closure,
+ g_test_add ("/EbSdbCursor/Calculate/BackAndForth", EbSdbCursorFixture, &ascending_closure,
e_sqlitedb_cursor_fixture_setup,
test_cursor_calculate_back_and_forth,
e_sqlitedb_cursor_fixture_teardown);
- g_test_add ("/EbSdbCursor/Calculate/AlphabeticTarget", EbSdbCursorFixture, &book_closure,
+ g_test_add ("/EbSdbCursor/Calculate/AlphabeticTarget", EbSdbCursorFixture, &ascending_closure,
e_sqlitedb_cursor_fixture_setup,
test_cursor_calculate_partial_target,
e_sqlitedb_cursor_fixture_teardown);
- g_test_add ("/EbSdbCursor/Calculate/AfterModification", EbSdbCursorFixture, &book_closure,
+ g_test_add ("/EbSdbCursor/Calculate/AfterModification", EbSdbCursorFixture, &ascending_closure,
e_sqlitedb_cursor_fixture_setup,
test_cursor_calculate_after_modification,
e_sqlitedb_cursor_fixture_teardown);
- g_test_add ("/EbSdbCursor/Calculate/Filtered/Initial", EbSdbCursorFixture, &book_closure,
+ g_test_add ("/EbSdbCursor/Calculate/Filtered/Initial", EbSdbCursorFixture, &ascending_closure,
e_sqlitedb_cursor_fixture_filtered_setup,
test_cursor_calculate_filtered_initial,
e_sqlitedb_cursor_fixture_teardown);
- g_test_add ("/EbSdbCursor/Calculate/Filtered/MoveForward", EbSdbCursorFixture, &book_closure,
+ g_test_add ("/EbSdbCursor/Calculate/Filtered/MoveForward", EbSdbCursorFixture, &ascending_closure,
e_sqlitedb_cursor_fixture_filtered_setup,
test_cursor_calculate_filtered_move_forward,
e_sqlitedb_cursor_fixture_teardown);
- g_test_add ("/EbSdbCursor/Calculate/Filtered/MoveBackwards", EbSdbCursorFixture, &book_closure,
+ g_test_add ("/EbSdbCursor/Calculate/Filtered/MoveBackwards", EbSdbCursorFixture, &ascending_closure,
e_sqlitedb_cursor_fixture_filtered_setup,
test_cursor_calculate_filtered_move_backwards,
e_sqlitedb_cursor_fixture_teardown);
- g_test_add ("/EbSdbCursor/Calculate/Filtered/AlphabeticTarget", EbSdbCursorFixture, &book_closure,
+ g_test_add ("/EbSdbCursor/Calculate/Filtered/AlphabeticTarget", EbSdbCursorFixture,
&ascending_closure,
e_sqlitedb_cursor_fixture_filtered_setup,
test_cursor_calculate_filtered_partial_target,
e_sqlitedb_cursor_fixture_teardown);
- g_test_add ("/EbSdbCursor/Calculate/Filtered/AfterModification", EbSdbCursorFixture, &book_closure,
+ g_test_add ("/EbSdbCursor/Calculate/Filtered/AfterModification", EbSdbCursorFixture,
&ascending_closure,
e_sqlitedb_cursor_fixture_filtered_setup,
test_cursor_calculate_filtered_after_modification,
e_sqlitedb_cursor_fixture_teardown);
+ g_test_add ("/EbSdbCursor/Calculate/Descending/Initial", EbSdbCursorFixture, &descending_closure,
+ e_sqlitedb_cursor_fixture_setup,
+ test_cursor_calculate_initial,
+ e_sqlitedb_cursor_fixture_teardown);
+ g_test_add ("/EbSdbCursor/Calculate/Descending/MoveForward", EbSdbCursorFixture, &descending_closure,
+ e_sqlitedb_cursor_fixture_setup,
+ test_cursor_calculate_descending_move_forward,
+ e_sqlitedb_cursor_fixture_teardown);
+ g_test_add ("/EbSdbCursor/Calculate/Descending/MoveBackwards", EbSdbCursorFixture,
&descending_closure,
+ e_sqlitedb_cursor_fixture_setup,
+ test_cursor_calculate_descending_move_backwards,
+ e_sqlitedb_cursor_fixture_teardown);
+ g_test_add ("/EbSdbCursor/Calculate/Descending/BackAndForth", EbSdbCursorFixture, &descending_closure,
+ e_sqlitedb_cursor_fixture_setup,
+ test_cursor_calculate_back_and_forth,
+ e_sqlitedb_cursor_fixture_teardown);
+ g_test_add ("/EbSdbCursor/Calculate/Descending/AlphabeticTarget", EbSdbCursorFixture,
&descending_closure,
+ e_sqlitedb_cursor_fixture_setup,
+ test_cursor_calculate_descending_partial_target,
+ e_sqlitedb_cursor_fixture_teardown);
+ g_test_add ("/EbSdbCursor/Calculate/Descending/AfterModification", EbSdbCursorFixture,
&descending_closure,
+ e_sqlitedb_cursor_fixture_setup,
+ test_cursor_calculate_descending_after_modification,
+ e_sqlitedb_cursor_fixture_teardown);
+
return e_test_server_utils_run ();
}
diff --git a/tests/libedata-book/test-sqlite-cursor-move-by-en-US.c
b/tests/libedata-book/test-sqlite-cursor-move-by-en-US.c
index 5411b08..dc8f4c0 100644
--- a/tests/libedata-book/test-sqlite-cursor-move-by-en-US.c
+++ b/tests/libedata-book/test-sqlite-cursor-move-by-en-US.c
@@ -42,5 +42,22 @@ main (gint argc,
move_by_test_add_assertion (data, -8, 16, 17, 8, 3, 5, 2, 1, 11);
move_by_test_add (data, TRUE);
+ data = move_by_test_new_full ("/EbSdbCursor/en_US/Move/Descending/Forward", "en_US.UTF-8",
+ E_BOOK_SORT_DESCENDING);
+ move_by_test_add_assertion (data, 5, 20, 19, 9, 13, 12);
+ move_by_test_add_assertion (data, 5, 14, 10, 18, 16, 17);
+ move_by_test_add_assertion (data, 5, 15, 8, 7, 3, 4);
+ move_by_test_add_assertion (data, 5, 6, 5, 2, 1, 11);
+ move_by_test_add (data, FALSE);
+
+ data = move_by_test_new_full ("/EbSdbCursor/en_US/Move/Descending/Forward/Loop", "en_US.UTF-8",
+ E_BOOK_SORT_DESCENDING);
+ move_by_test_add_assertion (data, 10, 20, 19, 9, 13, 12, 14, 10, 18, 16, 17);
+ move_by_test_add_assertion (data, 11, 15, 8, 7, 3, 4, 6, 5, 2, 1, 11, 0);
+
+ move_by_test_add_assertion (data, 10, 20, 19, 9, 13, 12, 14, 10, 18, 16, 17);
+ move_by_test_add_assertion (data, 10, 15, 8, 7, 3, 4, 6, 5, 2, 1, 11);
+ move_by_test_add (data, FALSE);
+
return e_test_server_utils_run ();
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]