[evolution-data-server/openismus-work] Added test case to create a book with custom summary fields and fetch a contact.



commit d0ffe6443a597ddebd22466893ec85900abbd5a5
Author: Tristan Van Berkom <tristanvb openismus com>
Date:   Mon Oct 8 16:43:06 2012 +0900

    Added test case to create a book with custom summary fields and fetch a contact.
    
    This test case configures some summary fields and indexes and then performs
    some queries and asserts the results. Notably the configured summary uses multi valued
    fields E_CONTACT_TEL and E_CONTACT_EMAIL.
    
    Some extra vcards are added to the test data directory in order to have some
    mixed up emails and phone numbers to test with

 tests/libebook/client/Makefile.am                  |    3 +
 tests/libebook/client/test-client-custom-summary.c |  243 ++++++++++++++++++++
 tests/libebook/data/vcards/custom-1.vcf            |    5 +
 tests/libebook/data/vcards/custom-2.vcf            |    6 +
 tests/libebook/data/vcards/custom-3.vcf            |    6 +
 tests/libebook/data/vcards/custom-4.vcf            |    5 +
 tests/libebook/data/vcards/custom-5.vcf            |    5 +
 7 files changed, 273 insertions(+), 0 deletions(-)
---
diff --git a/tests/libebook/client/Makefile.am b/tests/libebook/client/Makefile.am
index 670029b..edb1920 100644
--- a/tests/libebook/client/Makefile.am
+++ b/tests/libebook/client/Makefile.am
@@ -26,6 +26,7 @@ TESTS =								\
 	test-client-refresh					\
 	test-client-add-contact					\
 	test-client-get-contact					\
+	test-client-custom-summary				\
 	test-client-get-revision				\
 	test-client-get-view					\
 	test-client-uid-only-view				\
@@ -73,6 +74,8 @@ test_client_examine_LDADD=$(TEST_LIBS)
 test_client_examine_CPPFLAGS=$(TEST_CPPFLAGS)
 test_client_get_contact_LDADD=$(TEST_LIBS)
 test_client_get_contact_CPPFLAGS=$(TEST_CPPFLAGS)
+test_client_custom_summary_LDADD=$(TEST_LIBS)
+test_client_custom_summary_CPPFLAGS=$(TEST_CPPFLAGS)
 test_client_get_revision_LDADD=$(TEST_LIBS)
 test_client_get_revision_CPPFLAGS=$(TEST_CPPFLAGS)
 test_client_get_view_LDADD=$(TEST_LIBS)
diff --git a/tests/libebook/client/test-client-custom-summary.c b/tests/libebook/client/test-client-custom-summary.c
new file mode 100644
index 0000000..97b3953
--- /dev/null
+++ b/tests/libebook/client/test-client-custom-summary.c
@@ -0,0 +1,243 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+
+#include <stdlib.h>
+#include <libebook/libebook.h>
+
+#include "client-test-utils.h"
+
+
+/****************************** Custom Book Creation *****************************/
+
+
+typedef struct {
+	GMainLoop       *loop;
+	const gchar     *uid;
+	ESourceRegistry *registry;
+	ESource         *scratch;
+	ESource         *source;
+	EBookClient     *book;
+} CreateBookData;
+
+static gboolean
+quit_idle (CreateBookData *data)
+{
+	g_main_loop_quit (data->loop);
+	return FALSE;
+}
+
+static gboolean
+create_book_idle (CreateBookData *data)
+{
+	GError *error = NULL;
+
+	data->source = e_source_registry_ref_source (data->registry, data->uid);
+	if (!data->source)
+		g_error ("Unable to fetch newly created source uid '%s' from the registry", data->uid);
+
+	data->book = e_book_client_new (data->source, &error);
+	if (!data->book)
+		g_error ("Unable to create the book: %s", error->message);
+
+	g_idle_add ((GSourceFunc)quit_idle, data);
+
+	return FALSE;
+}
+
+static gboolean
+register_source_idle (CreateBookData *data)
+{
+	GError *error = NULL;
+	ESourceBackend  *backend;
+	ESourceAddressBookConfig *config;
+
+	data->registry = e_source_registry_new_sync (NULL, &error);
+	if (!data->registry)
+		g_error ("Unable to create the registry: %s", error->message);
+
+	data->scratch = e_source_new_with_uid (data->uid, NULL, &error);
+	if (!data->scratch)
+		g_error ("Failed to create source with uid '%s': %s", data->uid, error->message);
+
+	backend = e_source_get_extension (data->scratch, E_SOURCE_EXTENSION_ADDRESS_BOOK);
+	e_source_backend_set_backend_name (backend, "local");
+
+	g_type_class_unref (g_type_class_ref (E_TYPE_SOURCE_ADDRESS_BOOK_CONFIG));
+	config   = e_source_get_extension (data->scratch, E_SOURCE_EXTENSION_ADDRESS_BOOK_CONFIG);
+	e_source_address_book_config_set_summary_fields  (config,
+							  E_CONTACT_FULL_NAME,
+							  E_CONTACT_FAMILY_NAME,
+							  E_CONTACT_EMAIL_1,
+							  E_CONTACT_TEL,
+							  E_CONTACT_EMAIL,
+							  0);
+	e_source_address_book_config_set_indexed_fields (config,
+							 E_CONTACT_TEL, E_BOOK_INDEX_SUFFIX,
+							 E_CONTACT_FULL_NAME, E_BOOK_INDEX_PREFIX,
+							 E_CONTACT_FULL_NAME, E_BOOK_INDEX_SUFFIX,
+							 E_CONTACT_FAMILY_NAME, E_BOOK_INDEX_PREFIX,
+							 E_CONTACT_FAMILY_NAME, E_BOOK_INDEX_SUFFIX,
+							 0);
+
+
+	if (!e_source_registry_commit_source_sync (data->registry, data->scratch, NULL, &error))
+		g_error ("Unable to add new source to the registry for uid %s: %s", data->uid, error->message);
+
+	/* XXX e_source_registry_commit_source_sync isnt really sync... or else
+	 * we could call e_source_registry_ref_source() immediately
+	 */
+	g_timeout_add (20, (GSourceFunc)create_book_idle, data);
+
+	return FALSE;
+}
+
+static EBookClient *
+ebook_test_utils_book_with_uid (const gchar *uid)
+{
+	CreateBookData data = { 0, };
+
+	data.uid = uid;
+
+	data.loop = g_main_loop_new (NULL, FALSE);
+	g_idle_add ((GSourceFunc)register_source_idle, &data);
+	g_main_loop_run (data.loop);
+	g_main_loop_unref (data.loop);
+
+	g_object_unref (data.scratch);
+	g_object_unref (data.source);
+	g_object_unref (data.registry);
+
+	return data.book;
+}
+
+static EBookClient *
+new_custom_temp_client (gchar **uri)
+{
+	EBookClient     *book;
+	gchar           *uid;
+	guint64          real_time = g_get_real_time ();
+
+	uid  = g_strdup_printf ("test-book-%" G_GINT64_FORMAT, real_time);
+	book = ebook_test_utils_book_with_uid (uid);
+
+	if (uri)
+		*uri = g_strdup (uid);
+
+	g_free (uid);
+
+	return book;
+}
+
+gint
+main (gint argc,
+      gchar **argv)
+{
+	EBookClient *book_client;
+	EContact *contact_final;
+	GError *error = NULL;
+	EBookQuery *query;
+	gchar *sexp;
+	GSList *results = NULL;
+
+	main_initialize ();
+
+	/*
+	 * Setup
+	 */
+	book_client = new_custom_temp_client (NULL);
+	g_return_val_if_fail (book_client != NULL, 1);
+
+	if (!e_client_open_sync (E_CLIENT (book_client), FALSE, NULL, &error)) {
+		report_error ("client open sync", &error);
+		g_object_unref (book_client);
+		return 1;
+	}
+
+	/* Add contacts */
+	if (!add_contact_from_test_case_verify (book_client, "custom-1", &contact_final)) {
+		g_object_unref (book_client);
+		return 1;
+	}
+	if (!add_contact_from_test_case_verify (book_client, "custom-2", &contact_final)) {
+		g_object_unref (book_client);
+		return 1;
+	}
+	if (!add_contact_from_test_case_verify (book_client, "custom-3", &contact_final)) {
+		g_object_unref (book_client);
+		return 1;
+	}
+	if (!add_contact_from_test_case_verify (book_client, "custom-4", &contact_final)) {
+		g_object_unref (book_client);
+		return 1;
+	}
+	if (!add_contact_from_test_case_verify (book_client, "custom-5", &contact_final)) {
+		g_object_unref (book_client);
+		return 1;
+	}
+
+	/* Query exact */
+	query = e_book_query_field_test (E_CONTACT_FULL_NAME, E_BOOK_QUERY_IS, "James Brown");
+	sexp = e_book_query_to_string (query);
+
+	if (!e_book_client_get_contacts_sync (book_client, sexp, &results, NULL, &error)) {
+		report_error ("get contacts", &error);
+		g_object_unref (book_client);
+		return 1;
+	}
+
+	g_assert_cmpint (g_slist_length (results), ==, 1);
+	e_util_free_object_slist (results);
+	e_book_query_unref (query);
+	g_free (sexp);
+
+	/* Query prefix */
+	query = e_book_query_field_test (E_CONTACT_FULL_NAME, E_BOOK_QUERY_BEGINS_WITH, "B");
+	sexp = e_book_query_to_string (query);
+
+	if (!e_book_client_get_contacts_sync (book_client, sexp, &results, NULL, &error)) {
+		report_error ("get contacts", &error);
+		g_object_unref (book_client);
+		return 1;
+	}
+
+	g_assert_cmpint (g_slist_length (results), ==, 2);
+	e_util_free_object_slist (results);
+	e_book_query_unref (query);
+	g_free (sexp);
+
+	/* Query phone number suffix */
+	query = e_book_query_field_test (E_CONTACT_TEL, E_BOOK_QUERY_ENDS_WITH, "999");
+	sexp = e_book_query_to_string (query);
+
+	if (!e_book_client_get_contacts_sync (book_client, sexp, &results, NULL, &error)) {
+		report_error ("get contacts", &error);
+		g_object_unref (book_client);
+		return 1;
+	}
+	e_util_free_object_slist (results);
+	e_book_query_unref (query);
+	g_free (sexp);
+
+	/* Query email suffix */
+	query = e_book_query_field_test (E_CONTACT_EMAIL, E_BOOK_QUERY_ENDS_WITH, "jackson.com");
+	sexp = e_book_query_to_string (query);
+
+	if (!e_book_client_get_contacts_sync (book_client, sexp, &results, NULL, &error)) {
+		report_error ("get contacts", &error);
+		g_object_unref (book_client);
+		return 1;
+	}
+	g_assert_cmpint (g_slist_length (results), ==, 2);
+	e_util_free_object_slist (results);
+	e_book_query_unref (query);
+	g_free (sexp);
+
+	if (!e_client_remove_sync (E_CLIENT (book_client), NULL, &error)) {
+		report_error ("client remove sync", &error);
+		g_object_unref (book_client);
+		return 1;
+	}
+
+	g_object_unref (book_client);
+
+	return 0;
+}
diff --git a/tests/libebook/data/vcards/custom-1.vcf b/tests/libebook/data/vcards/custom-1.vcf
new file mode 100644
index 0000000..bc9a23c
--- /dev/null
+++ b/tests/libebook/data/vcards/custom-1.vcf
@@ -0,0 +1,5 @@
+BEGIN:VCARD
+FN:Micheal Jackson
+TEL;HOME:+1234567
+EMAIL;TYPE=home,work:micheal jackson com
+END:VCARD
diff --git a/tests/libebook/data/vcards/custom-2.vcf b/tests/libebook/data/vcards/custom-2.vcf
new file mode 100644
index 0000000..c267827
--- /dev/null
+++ b/tests/libebook/data/vcards/custom-2.vcf
@@ -0,0 +1,6 @@
+BEGIN:VCARD
+FN:Janet Jackson
+TEL;HOME:+7654321
+EMAIL:janet jackson com
+EMAIL:janny jackson com
+END:VCARD
diff --git a/tests/libebook/data/vcards/custom-3.vcf b/tests/libebook/data/vcards/custom-3.vcf
new file mode 100644
index 0000000..ee19044
--- /dev/null
+++ b/tests/libebook/data/vcards/custom-3.vcf
@@ -0,0 +1,6 @@
+BEGIN:VCARD
+FN:Bobby Brown
+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/custom-4.vcf b/tests/libebook/data/vcards/custom-4.vcf
new file mode 100644
index 0000000..b5529c9
--- /dev/null
+++ b/tests/libebook/data/vcards/custom-4.vcf
@@ -0,0 +1,5 @@
+BEGIN:VCARD
+FN:Big Bobby Brown
+TEL;TYPE=work,pref:+9999999
+EMAIL:big bobby brown org
+END:VCARD
diff --git a/tests/libebook/data/vcards/custom-5.vcf b/tests/libebook/data/vcards/custom-5.vcf
new file mode 100644
index 0000000..3ea4ccc
--- /dev/null
+++ b/tests/libebook/data/vcards/custom-5.vcf
@@ -0,0 +1,5 @@
+BEGIN:VCARD
+FN:James Brown
+TEL;HOME:+6666666
+EMAIL;TYPE=home,work:james brown com
+END:VCARD



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