[libgdata] Add contact insertion support and test suite



commit 68676c15218cd44866b6186289f231dad7957cab
Author: Philip Withnall <philip tecnocode co uk>
Date:   Sun Apr 19 18:30:23 2009 +0100

    Add contact insertion support and test suite
    
    Added gdata_contacts_service_insert_contact to allow contacts to be inserted,
    and added a contacts service test suite, testing queries and insertion.
    Also fixed parsing of contacts without "primary" parameters.
---
 gdata/gdata.symbols                              |    1 +
 gdata/services/contacts/gdata-contacts-contact.c |   36 ++--
 gdata/services/contacts/gdata-contacts-service.c |   18 ++
 gdata/services/contacts/gdata-contacts-service.h |    2 +
 gdata/tests/Makefile.am                          |    4 +-
 gdata/tests/contacts.c                           |  194 ++++++++++++++++++++++
 6 files changed, 236 insertions(+), 19 deletions(-)

diff --git a/gdata/gdata.symbols b/gdata/gdata.symbols
index 42068c0..a5b47d3 100644
--- a/gdata/gdata.symbols
+++ b/gdata/gdata.symbols
@@ -245,6 +245,7 @@ gdata_contacts_service_get_type
 gdata_contacts_service_new
 gdata_contacts_service_query_contacts
 gdata_contacts_service_query_contacts_async
+gdata_contacts_service_insert_contact
 gdata_contacts_query_get_type
 gdata_contacts_query_new
 gdata_contacts_query_new_with_limits
diff --git a/gdata/services/contacts/gdata-contacts-contact.c b/gdata/services/contacts/gdata-contacts-contact.c
index 987949c..a6a9f98 100644
--- a/gdata/services/contacts/gdata-contacts-contact.c
+++ b/gdata/services/contacts/gdata-contacts-contact.c
@@ -186,10 +186,10 @@ parse_xml (GDataEntry *entry, xmlDoc *doc, xmlNode *node, GError **error)
 
 		/* Is it the primary e-mail address? */
 		primary = xmlGetProp (node, (xmlChar*) "primary");
-		if (xmlStrcmp (primary, (xmlChar*) "true") == 0)
-			primary_bool = TRUE;
-		else if (xmlStrcmp (primary, (xmlChar*) "false") == 0)
+		if (primary == NULL || xmlStrcmp (primary, (xmlChar*) "false") == 0)
 			primary_bool = FALSE;
+		else if (xmlStrcmp (primary, (xmlChar*) "true") == 0)
+			primary_bool = TRUE;
 		else {
 			gdata_parser_error_unknown_property_value ("gd:email", "primary", (gchar*) primary, error);
 			xmlFree (primary);
@@ -223,10 +223,10 @@ parse_xml (GDataEntry *entry, xmlDoc *doc, xmlNode *node, GError **error)
 
 		/* Is it the primary IM address? */
 		primary = xmlGetProp (node, (xmlChar*) "primary");
-		if (xmlStrcmp (primary, (xmlChar*) "true") == 0)
-			primary_bool = TRUE;
-		else if (xmlStrcmp (primary, (xmlChar*) "false") == 0)
+		if (primary == NULL || xmlStrcmp (primary, (xmlChar*) "false") == 0)
 			primary_bool = FALSE;
+		else if (xmlStrcmp (primary, (xmlChar*) "true") == 0)
+			primary_bool = TRUE;
 		else {
 			gdata_parser_error_unknown_property_value ("gd:im", "primary", (gchar*) primary, error);
 			xmlFree (primary);
@@ -261,10 +261,10 @@ parse_xml (GDataEntry *entry, xmlDoc *doc, xmlNode *node, GError **error)
 
 		/* Is it the primary phone number? */
 		primary = xmlGetProp (node, (xmlChar*) "primary");
-		if (xmlStrcmp (primary, (xmlChar*) "true") == 0)
-			primary_bool = TRUE;
-		else if (xmlStrcmp (primary, (xmlChar*) "false") == 0)
+		if (primary == NULL || xmlStrcmp (primary, (xmlChar*) "false") == 0)
 			primary_bool = FALSE;
+		else if (xmlStrcmp (primary, (xmlChar*) "true") == 0)
+			primary_bool = TRUE;
 		else {
 			gdata_parser_error_unknown_property_value ("gd:phoneNumber", "primary", (gchar*) primary, error);
 			xmlFree (primary);
@@ -298,10 +298,10 @@ parse_xml (GDataEntry *entry, xmlDoc *doc, xmlNode *node, GError **error)
 
 		/* Is it the primary postal address? */
 		primary = xmlGetProp (node, (xmlChar*) "primary");
-		if (xmlStrcmp (primary, (xmlChar*) "true") == 0)
-			primary_bool = TRUE;
-		else if (xmlStrcmp (primary, (xmlChar*) "false") == 0)
+		if (primary == NULL || xmlStrcmp (primary, (xmlChar*) "false") == 0)
 			primary_bool = FALSE;
+		else if (xmlStrcmp (primary, (xmlChar*) "true") == 0)
+			primary_bool = TRUE;
 		else {
 			gdata_parser_error_unknown_property_value ("gd:postalAddress", "primary", (gchar*) primary, error);
 			xmlFree (primary);
@@ -358,10 +358,10 @@ parse_xml (GDataEntry *entry, xmlDoc *doc, xmlNode *node, GError **error)
 
 		/* Is it the primary organisation? */
 		primary = xmlGetProp (node, (xmlChar*) "primary");
-		if (xmlStrcmp (primary, (xmlChar*) "true") == 0)
-			primary_bool = TRUE;
-		else if (xmlStrcmp (primary, (xmlChar*) "false") == 0)
+		if (primary == NULL || xmlStrcmp (primary, (xmlChar*) "false") == 0)
 			primary_bool = FALSE;
+		else if (xmlStrcmp (primary, (xmlChar*) "true") == 0)
+			primary_bool = TRUE;
 		else {
 			gdata_parser_error_unknown_property_value ("gd:organization", "primary", (gchar*) primary, error);
 			xmlFree (primary);
@@ -411,10 +411,10 @@ parse_xml (GDataEntry *entry, xmlDoc *doc, xmlNode *node, GError **error)
 
 		/* Has it been deleted? */
 		deleted = xmlGetProp (node, (xmlChar*) "deleted");
-		if (xmlStrcmp (deleted, (xmlChar*) "true") == 0)
-			deleted_bool = TRUE;
-		else if (xmlStrcmp (deleted, (xmlChar*) "false") == 0)
+		if (deleted == NULL || xmlStrcmp (deleted, (xmlChar*) "false") == 0)
 			deleted_bool = FALSE;
+		else if (xmlStrcmp (deleted, (xmlChar*) "true") == 0)
+			deleted_bool = TRUE;
 		else {
 			gdata_parser_error_unknown_property_value ("gContact:groupMembershipInfo", "deleted", (gchar*) deleted, error);
 			xmlFree (deleted);
diff --git a/gdata/services/contacts/gdata-contacts-service.c b/gdata/services/contacts/gdata-contacts-service.c
index d249ae8..cdb62b3 100644
--- a/gdata/services/contacts/gdata-contacts-service.c
+++ b/gdata/services/contacts/gdata-contacts-service.c
@@ -87,3 +87,21 @@ gdata_contacts_service_query_contacts_async (GDataContactsService *self, GDataCo
 	gdata_service_query_async (GDATA_SERVICE (self), "http://www.google.com/m8/feeds/contacts/default/full";, GDATA_QUERY (query),
 				   GDATA_TYPE_CONTACTS_CONTACT, cancellable, progress_callback, progress_user_data, callback, user_data);
 }
+
+/* TODO: Async variant */
+GDataContactsContact *
+gdata_contacts_service_insert_contact (GDataContactsService *self, GDataContactsContact *contact, GCancellable *cancellable, GError **error)
+{
+	gchar *uri;
+	GDataEntry *entry;
+
+	g_return_val_if_fail (GDATA_IS_CONTACTS_SERVICE (self), NULL);
+	g_return_val_if_fail (GDATA_IS_CONTACTS_CONTACT (contact), NULL);
+
+	uri = g_strdup_printf ("http://www.google.com/m8/feeds/contacts/%s/full";, gdata_service_get_username (GDATA_SERVICE (self)));
+
+	entry = gdata_service_insert_entry (GDATA_SERVICE (self), uri, GDATA_ENTRY (contact), cancellable, error);
+	g_free (uri);
+
+	return GDATA_CONTACTS_CONTACT (entry);
+}
diff --git a/gdata/services/contacts/gdata-contacts-service.h b/gdata/services/contacts/gdata-contacts-service.h
index 3ba3cdc..1bb143d 100644
--- a/gdata/services/contacts/gdata-contacts-service.h
+++ b/gdata/services/contacts/gdata-contacts-service.h
@@ -55,6 +55,8 @@ GDataFeed *gdata_contacts_service_query_contacts (GDataContactsService *self, GD
 void gdata_contacts_service_query_contacts_async (GDataContactsService *self, GDataContactsQuery *query, GCancellable *cancellable,
 						  GDataQueryProgressCallback progress_callback, gpointer progress_user_data,
 						  GAsyncReadyCallback callback, gpointer user_data);
+GDataContactsContact *gdata_contacts_service_insert_contact (GDataContactsService *self, GDataContactsContact *contact,
+							     GCancellable *cancellable, GError **error);
 
 G_END_DECLS
 
diff --git a/gdata/tests/Makefile.am b/gdata/tests/Makefile.am
index 07263b3..b6e647f 100644
--- a/gdata/tests/Makefile.am
+++ b/gdata/tests/Makefile.am
@@ -26,8 +26,10 @@ youtube_SOURCES			 = youtube.c $(TEST_SRCS)
 TEST_PROGS			+= calendar
 calendar_SOURCES		 = calendar.c $(TEST_SRCS)
 
+TEST_PROGS			+= contacts
+contacts_SOURCES		 = contacts.c $(TEST_SRCS)
+
 TEST_PROGS			+= memory
 memory_SOURCES			 = memory.c $(TEST_SRCS)
 
-
 -include $(top_srcdir)/git.mk
diff --git a/gdata/tests/contacts.c b/gdata/tests/contacts.c
new file mode 100644
index 0000000..8877d4c
--- /dev/null
+++ b/gdata/tests/contacts.c
@@ -0,0 +1,194 @@
+/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+/*
+ * GData Client
+ * Copyright (C) Philip Withnall 2009 <philip tecnocode co uk>
+ *
+ * GData Client is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * GData Client is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GData Client.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <glib.h>
+#include <unistd.h>
+
+#include "gdata.h"
+#include "common.h"
+
+/* TODO: probably a better way to do this; some kind of data associated with the test suite? */
+static GDataService *service = NULL;
+static GMainLoop *main_loop = NULL;
+
+static void
+test_authentication (void)
+{
+	gboolean retval;
+	GError *error = NULL;
+
+	/* Create a service */
+	service = GDATA_SERVICE (gdata_contacts_service_new (CLIENT_ID));
+
+	g_assert (service != NULL);
+	g_assert (GDATA_IS_SERVICE (service));
+	g_assert_cmpstr (gdata_service_get_client_id (service), ==, CLIENT_ID);
+
+	/* Log in */
+	retval = gdata_service_authenticate (service, USERNAME, PASSWORD, NULL, &error);
+	g_assert_no_error (error);
+	g_assert (retval == TRUE);
+	g_clear_error (&error);
+
+	/* Check all is as it should be */
+	g_assert (gdata_service_is_authenticated (service) == TRUE);
+	g_assert_cmpstr (gdata_service_get_username (service), ==, USERNAME);
+	g_assert_cmpstr (gdata_service_get_password (service), ==, PASSWORD);
+}
+
+static void
+test_query_all_contacts (void)
+{
+	GDataFeed *feed;
+	GError *error = NULL;
+
+	g_assert (service != NULL);
+
+	feed = gdata_contacts_service_query_contacts (GDATA_CONTACTS_SERVICE (service), NULL, NULL, NULL, NULL, &error);
+	g_assert_no_error (error);
+	g_assert (GDATA_IS_FEED (feed));
+	g_clear_error (&error);
+
+	/* TODO: check entries and feed properties */
+
+	g_object_unref (feed);
+}
+
+static void
+test_query_all_contacts_async_cb (GDataService *service, GAsyncResult *async_result, gpointer user_data)
+{
+	GDataFeed *feed;
+	GError *error = NULL;
+
+	feed = gdata_service_query_finish (service, async_result, &error);
+	g_assert_no_error (error);
+	g_assert (GDATA_IS_FEED (feed));
+	g_clear_error (&error);
+
+	/* TODO: Tests? */
+	g_main_loop_quit (main_loop);
+
+	g_object_unref (feed);
+}
+
+static void
+test_query_all_contacts_async (void)
+{
+	g_assert (service != NULL);
+
+	gdata_contacts_service_query_contacts_async (GDATA_CONTACTS_SERVICE (service), NULL, NULL, NULL,
+						     NULL, (GAsyncReadyCallback) test_query_all_contacts_async_cb, NULL);
+
+	main_loop = g_main_loop_new (NULL, TRUE);
+	g_main_loop_run (main_loop);
+	g_main_loop_unref (main_loop);
+}
+
+static void
+test_insert_simple (void)
+{
+	GDataContactsContact *contact, *new_contact;
+	GDataCategory *category;
+	GDataGDEmailAddress *email_address1, *email_address2;
+	GDataGDPhoneNumber *phone_number1, *phone_number2;
+	GDataGDIMAddress *im_address;
+	GDataGDPostalAddress *postal_address;
+	gchar *xml;
+	GError *error = NULL;
+
+	g_assert (service != NULL);
+
+	contact = gdata_contacts_contact_new (NULL);
+
+	gdata_entry_set_title (GDATA_ENTRY (contact), "Elizabeth Bennet");
+	gdata_entry_set_content (GDATA_ENTRY (contact), "Notes");
+	/* TODO: Have it add this category automatically? Same for GDataCalendarEvent */
+	category = gdata_category_new ("http://schemas.google.com/contact/2008#contact";, "http://schemas.google.com/g/2005#kind";, NULL);
+	gdata_entry_add_category (GDATA_ENTRY (contact), category);
+	email_address1 = gdata_gd_email_address_new ("liz gmail com", "http://schemas.google.com/g/2005#work";, NULL, FALSE);
+	gdata_contacts_contact_add_email_address (contact, email_address1);
+	email_address2 = gdata_gd_email_address_new ("liz example org", "http://schemas.google.com/g/2005#home";, NULL, FALSE);
+	gdata_contacts_contact_add_email_address (contact, email_address2);
+	phone_number1 = gdata_gd_phone_number_new ("(206)555-1212", "http://schemas.google.com/g/2005#work";, NULL, NULL, TRUE);
+	gdata_contacts_contact_add_phone_number (contact, phone_number1);
+	phone_number2 = gdata_gd_phone_number_new ("(206)555-1213", "http://schemas.google.com/g/2005#home";, NULL, NULL, FALSE);
+	gdata_contacts_contact_add_phone_number (contact, phone_number2);
+	im_address = gdata_gd_im_address_new ("liz gmail com", "http://schemas.google.com/g/2005#GOOGLE_TALK";, "http://schemas.google.com/g/2005#home";,
+					      NULL, FALSE);
+	gdata_contacts_contact_add_im_address (contact, im_address);
+	postal_address = gdata_gd_postal_address_new ("1600 Amphitheatre Pkwy Mountain View", "http://schemas.google.com/g/2005#work";, NULL, TRUE);
+	gdata_contacts_contact_add_postal_address (contact, postal_address);
+
+	/* Check the XML */
+	xml = gdata_entry_get_xml (GDATA_ENTRY (contact));
+	g_assert_cmpstr (xml, ==,
+			 "<entry xmlns='http://www.w3.org/2005/Atom' "
+			 	"xmlns:gd='http://schemas.google.com/g/2005' "
+			 	"xmlns:app='http://www.w3.org/2007/app' "
+			 	"xmlns:gContact='http://schemas.google.com/contact/2008'>"
+			 	"<title type='text'>Elizabeth Bennet</title>"
+			 	"<content type='text'>Notes</content>"
+				"<category term='http://schemas.google.com/contact/2008#contact' scheme='http://schemas.google.com/g/2005#kind'/>"
+				"<gd:email address='liz gmail com' rel='http://schemas.google.com/g/2005#work' primary='false'/>"
+				"<gd:email address='liz example org' rel='http://schemas.google.com/g/2005#home' primary='false'/>"
+				"<gd:im address='liz gmail com' protocol='http://schemas.google.com/g/2005#GOOGLE_TALK' "
+					"rel='http://schemas.google.com/g/2005#home' primary='false'/>"
+				"<gd:phoneNumber rel='http://schemas.google.com/g/2005#work' primary='true'>(206)555-1212</gd:phoneNumber>"
+				"<gd:phoneNumber rel='http://schemas.google.com/g/2005#home' primary='false'>(206)555-1213</gd:phoneNumber>"
+				"<gd:postalAddress rel='http://schemas.google.com/g/2005#work' primary='true'>"
+					"1600 Amphitheatre Pkwy Mountain View"
+				"</gd:postalAddress>"
+			 "</entry>");
+	g_free (xml);
+
+	/* Insert the contact */
+	new_contact = gdata_contacts_service_insert_contact (GDATA_CONTACTS_SERVICE (service), contact, NULL, &error);
+	g_assert_no_error (error);
+	g_assert (GDATA_IS_CONTACTS_CONTACT (new_contact));
+	g_clear_error (&error);
+
+	/* TODO: check entries and feed properties */
+
+	g_object_unref (contact);
+	g_object_unref (new_contact);
+}
+
+int
+main (int argc, char *argv[])
+{
+	gint retval;
+
+	g_type_init ();
+	g_thread_init (NULL);
+	g_test_init (&argc, &argv, NULL);
+	g_test_bug_base ("http://bugzilla.gnome.org/show_bug.cgi?id=";);
+
+	g_test_add_func ("/contacts/authentication", test_authentication);
+	g_test_add_func ("/contacts/query/all_contacts", test_query_all_contacts);
+	if (g_test_thorough () == TRUE)
+		g_test_add_func ("/contacts/query/all_contacts_async", test_query_all_contacts_async);
+	if (g_test_slow () == TRUE)
+		g_test_add_func ("/contacts/insert/simple", test_insert_simple);
+
+	retval = g_test_run ();
+	if (service != NULL)
+		g_object_unref (service);
+
+	return retval;
+}



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