[libgdata] picasaweb: Make the upload tests idempotent



commit ab3a9b5f5c69ad9d716d853ea00610915dda626c
Author: Philip Withnall <philip tecnocode co uk>
Date:   Fri Dec 10 14:11:55 2010 +0000

    picasaweb: Make the upload tests idempotent
    
    This helps towards the goal of making all tests idempotent, but also means
    that the tests will be a lot easier to adapt to a stream-based upload
    mechanism which is landing shortly for the PicasaWeb service.
    
    Helps: bgo#633359

 gdata/tests/picasaweb.c |  292 ++++++++++++++---------------------------------
 1 files changed, 86 insertions(+), 206 deletions(-)
---
diff --git a/gdata/tests/picasaweb.c b/gdata/tests/picasaweb.c
index 423cdcf..abc5e45 100644
--- a/gdata/tests/picasaweb.c
+++ b/gdata/tests/picasaweb.c
@@ -1147,208 +1147,119 @@ test_query_all_albums_async (gconstpointer service)
 	g_main_loop_unref (main_loop);
 }
 
+typedef struct {
+	GDataPicasaWebFile *photo;
+	GDataPicasaWebFile *updated_photo;
+	GFile *photo_file;
+} UploadData;
+
 static void
-test_upload_default_album (gconstpointer service)
+setup_upload (UploadData *data, gconstpointer service)
 {
-	GDataPicasaWebFile *photo, *photo_new;
-	GFile *photo_file;
-	GError *error = NULL;
-	GTimeVal timeval;
-	gchar *xml, *time_str, *summary, *expected_xml, *parsed_time_str;
-	GRegex *regex;
-	GMatchInfo *match_info;
-	guint64 delta;
 	const gchar * const tags[] = { "foo", "bar", ",,baz,baz", NULL };
-	const gchar * const *tags2;
-
-	g_get_current_time (&timeval);
-	time_str = g_time_val_to_iso8601 (&timeval);
-	summary = g_strdup_printf ("Photo Summary (%s)", time_str);
-
-	expected_xml = g_strdup_printf ("<entry "
-						"xmlns='http://www.w3.org/2005/Atom' "
-						"xmlns:gphoto='http://schemas.google.com/photos/2007' "
-						"xmlns:media='http://search.yahoo.com/mrss/' "
-						"xmlns:gd='http://schemas.google.com/g/2005' "
-						"xmlns:exif='http://schemas.google.com/photos/exif/2007' "
-						"xmlns:app='http://www.w3.org/2007/app' "
-						"xmlns:georss='http://www.georss.org/georss' "
-						"xmlns:gml='http://www.opengis.net/gml'>"
-						"<title type='text'>Photo Entry Title</title>"
-						"<summary type='text'>Photo Summary \\(%s\\)</summary>"
-						"<category term='http://schemas.google.com/photos/2007#photo' "
-							"scheme='http://schemas.google.com/g/2005#kind'/>"
-						"<gphoto:timestamp>([0-9]+)</gphoto:timestamp>"
-						"<gphoto:commentingEnabled>true</gphoto:commentingEnabled>"
-						"<media:group>"
-							"<media:title type='plain'>Photo Entry Title</media:title>"
-							"<media:description type='plain'>Photo Summary \\(%s\\)</media:description>"
-							"<media:keywords>foo,bar,%%2C%%2Cbaz%%2Cbaz</media:keywords>"
-						"</media:group>"
-					"</entry>", time_str, time_str);
-	g_free (time_str);
-
-	/* Build a regex to match the timestamp from the XML, since we can't definitely say what it'll be */
-	regex = g_regex_new (expected_xml, 0, 0, NULL);
-	g_free (expected_xml);
 
 	/* Build the photo */
-	photo = gdata_picasaweb_file_new (NULL);
-	gdata_entry_set_title (GDATA_ENTRY (photo), "Photo Entry Title");
-	gdata_picasaweb_file_set_caption (photo, summary);
-	gdata_picasaweb_file_set_tags (photo, tags);
+	data->photo = gdata_picasaweb_file_new (NULL);
+	gdata_entry_set_title (GDATA_ENTRY (data->photo), "Photo Entry Title");
+	gdata_picasaweb_file_set_caption (data->photo, "Photo Summary");
+	gdata_picasaweb_file_set_tags (data->photo, tags);
+	gdata_picasaweb_file_set_coordinates (data->photo, 17.127, -110.35);
 
-	/* Check the XML: match it against the regex built above, then check that the timestamp is within 100ms of the current time at the start of
-	 * the test function. We can't check it exactly, as a few milliseconds may have passed inbetween building the expected_xml and building the XML
-	 * for the photo. */
-	xml = gdata_parsable_get_xml (GDATA_PARSABLE (photo));
-	g_assert (g_regex_match (regex, xml, 0, &match_info) == TRUE);
-	parsed_time_str = g_match_info_fetch (match_info, 1);
-	delta = g_ascii_strtoull (parsed_time_str, NULL, 10) - (((guint64) timeval.tv_sec) * 1000 + ((guint64) timeval.tv_usec) / 1000);
-	g_assert_cmpuint (abs (delta), <, 1000);
+	/* File is public domain: http://en.wikipedia.org/wiki/File:German_garden_gnome_cropped.jpg */
+	data->photo_file = g_file_new_for_path (TEST_FILE_DIR "photo.jpg");
+}
 
-	g_free (parsed_time_str);
-	g_free (xml);
-	g_regex_unref (regex);
-	g_match_info_free (match_info);
+static void
+teardown_upload (UploadData *data, gconstpointer service)
+{
+	/* Delete the uploaded photo (don't worry if this fails) */
+	if (data->updated_photo != NULL) {
+		gdata_service_delete_entry (GDATA_SERVICE (service), GDATA_ENTRY (data->updated_photo), NULL, NULL);
+		g_object_unref (data->updated_photo);
+	}
 
-	gdata_picasaweb_file_set_coordinates (photo, 17.127, -110.35);
+	g_object_unref (data->photo);
+	g_object_unref (data->photo_file);
+}
 
-	/* File is public domain: http://en.wikipedia.org/wiki/File:German_garden_gnome_cropped.jpg */
-	photo_file = g_file_new_for_path (TEST_FILE_DIR "photo.jpg");
+static void
+test_upload_default_album (UploadData *data, gconstpointer service)
+{
+	const gchar * const *tags, * const *tags2;
+	GError *error = NULL;
 
 	/* Upload the photo */
 	/* TODO right now, it will just go to the default album, we want an uploading one :| */
-	photo_new = gdata_picasaweb_service_upload_file (GDATA_PICASAWEB_SERVICE (service), NULL, photo, photo_file, NULL, &error);
+	data->updated_photo = gdata_picasaweb_service_upload_file (GDATA_PICASAWEB_SERVICE (service), NULL, data->photo, data->photo_file, NULL,
+	                                                           &error);
 	g_assert_no_error (error);
-	g_assert (GDATA_IS_PICASAWEB_FILE (photo_new));
+	g_assert (GDATA_IS_PICASAWEB_FILE (data->updated_photo));
 	g_clear_error (&error);
 
 	/* TODO: check entries and feed properties */
-	tags2 = gdata_picasaweb_file_get_tags (photo_new);
-	g_assert_cmpuint (g_strv_length ((gchar**) tags2), ==, 3);
+	tags = gdata_picasaweb_file_get_tags (data->photo);
+	tags2 = gdata_picasaweb_file_get_tags (data->updated_photo);
+	g_assert_cmpuint (g_strv_length ((gchar**) tags2), ==, g_strv_length ((gchar**) tags));
 	g_assert_cmpstr (tags2[0], ==, tags[0]);
 	g_assert_cmpstr (tags2[1], ==, tags[1]);
 	g_assert_cmpstr (tags2[2], ==, tags[2]);
+}
 
-	g_free (summary);
-	g_object_unref (photo);
-	g_object_unref (photo_new);
-	g_object_unref (photo_file);
+typedef struct {
+	UploadData data;
+	GMainLoop *main_loop;
+} UploadAsyncData;
+
+static void
+setup_upload_async (UploadAsyncData *data, gconstpointer service)
+{
+	setup_upload ((UploadData*) data, service);
+	data->main_loop = g_main_loop_new (NULL, TRUE);
 }
 
 static void
-test_upload_default_album_async_cb (GDataPicasaWebService *service, GAsyncResult *result, GMainLoop *main_loop)
+teardown_upload_async (UploadAsyncData *data, gconstpointer service)
+{
+	g_main_loop_unref (data->main_loop);
+	teardown_upload ((UploadData*) data, service);
+}
+
+static void
+test_upload_default_album_async_cb (GDataPicasaWebService *service, GAsyncResult *result, UploadAsyncData *data)
 {
-	GDataPicasaWebFile *photo_new;
 	GError *error = NULL;
 
-	photo_new = gdata_picasaweb_service_upload_file_finish (service, result, &error);
+	data->data.updated_photo = gdata_picasaweb_service_upload_file_finish (service, result, &error);
 	g_assert_no_error (error);
-	g_assert (GDATA_IS_PICASAWEB_FILE (photo_new));
+	g_assert (GDATA_IS_PICASAWEB_FILE (data->data.updated_photo));
 	g_clear_error (&error);
-	g_assert (gdata_entry_is_inserted (GDATA_ENTRY (photo_new)));
+	g_assert (gdata_entry_is_inserted (GDATA_ENTRY (data->data.updated_photo)));
 
-	g_assert_cmpstr (gdata_entry_get_title (GDATA_ENTRY (photo_new)), ==, "Async Photo Entry Title");
+	g_assert_cmpstr (gdata_entry_get_title (GDATA_ENTRY (data->data.updated_photo)), ==, gdata_entry_get_title (GDATA_ENTRY (data->data.photo)));
 
-	g_main_loop_quit (main_loop);
-
-	g_object_unref (photo_new);
+	g_main_loop_quit (data->main_loop);
 }
 
 static void
-test_upload_default_album_async (gconstpointer service)
+test_upload_default_album_async (UploadAsyncData *data, gconstpointer service)
 {
-	GDataPicasaWebFile *photo;
-	GFile *photo_file;
-	GTimeVal timeval;
-	gchar *xml, *time_str, *summary, *expected_xml, *parsed_time_str;
-	GRegex *regex;
-	GMatchInfo *match_info;
-	guint64 delta;
-	const gchar * const tags[] = { "foo", "bar", ",,baz,baz", NULL };
-	GMainLoop *main_loop = g_main_loop_new (NULL, TRUE);
-
-	g_get_current_time (&timeval);
-	time_str = g_time_val_to_iso8601 (&timeval);
-	summary = g_strdup_printf ("Async Photo Summary (%s)", time_str);
-
-	expected_xml = g_strdup_printf ("<entry "
-						"xmlns='http://www.w3.org/2005/Atom' "
-						"xmlns:gphoto='http://schemas.google.com/photos/2007' "
-						"xmlns:media='http://search.yahoo.com/mrss/' "
-						"xmlns:gd='http://schemas.google.com/g/2005' "
-						"xmlns:exif='http://schemas.google.com/photos/exif/2007' "
-						"xmlns:app='http://www.w3.org/2007/app' "
-						"xmlns:georss='http://www.georss.org/georss' "
-						"xmlns:gml='http://www.opengis.net/gml'>"
-						"<title type='text'>Async Photo Entry Title</title>"
-						"<summary type='text'>Async Photo Summary \\(%s\\)</summary>"
-						"<category term='http://schemas.google.com/photos/2007#photo' "
-							"scheme='http://schemas.google.com/g/2005#kind'/>"
-						"<gphoto:timestamp>([0-9]+)</gphoto:timestamp>"
-						"<gphoto:commentingEnabled>true</gphoto:commentingEnabled>"
-						"<media:group>"
-							"<media:title type='plain'>Async Photo Entry Title</media:title>"
-							"<media:description type='plain'>Async Photo Summary \\(%s\\)</media:description>"
-							"<media:keywords>foo,bar,%%2C%%2Cbaz%%2Cbaz</media:keywords>"
-						"</media:group>"
-					"</entry>", time_str, time_str);
-	g_free (time_str);
-
-	/* Build a regex to match the timestamp from the XML, since we can't definitely say what it'll be */
-	regex = g_regex_new (expected_xml, 0, 0, NULL);
-	g_free (expected_xml);
-
-	/* Build the photo */
-	photo = gdata_picasaweb_file_new (NULL);
-	gdata_entry_set_title (GDATA_ENTRY (photo), "Async Photo Entry Title");
-	gdata_picasaweb_file_set_caption (photo, summary);
-	gdata_picasaweb_file_set_tags (photo, tags);
-
-	/* Check the XML: match it against the regex built above, then check that the timestamp is within 100ms of the current time at the start of
-	 * the test function. We can't check it exactly, as a few milliseconds may have passed inbetween building the expected_xml and building the XML
-	 * for the photo. */
-	xml = gdata_parsable_get_xml (GDATA_PARSABLE (photo));
-	g_assert (g_regex_match (regex, xml, 0, &match_info) == TRUE);
-	parsed_time_str = g_match_info_fetch (match_info, 1);
-	delta = g_ascii_strtoull (parsed_time_str, NULL, 10) - (((guint64) timeval.tv_sec) * 1000 + ((guint64) timeval.tv_usec) / 1000);
-	g_assert_cmpuint (abs (delta), <, 1000);
-
-	g_free (parsed_time_str);
-	g_free (xml);
-	g_regex_unref (regex);
-	g_match_info_free (match_info);
-
-	gdata_picasaweb_file_set_coordinates (photo, 17.127, -110.35);
-
-	/* File is public domain: http://en.wikipedia.org/wiki/File:German_garden_gnome_cropped.jpg */
-	photo_file = g_file_new_for_path (TEST_FILE_DIR "photo.jpg");
-
 	/* Upload the photo */
-	gdata_picasaweb_service_upload_file_async (GDATA_PICASAWEB_SERVICE (service), NULL, photo, photo_file, NULL,
-						   (GAsyncReadyCallback) test_upload_default_album_async_cb, main_loop);
-
-	g_main_loop_run (main_loop);
-	g_main_loop_unref (main_loop);
-
-	g_free (summary);
-	g_object_unref (photo);
-	g_object_unref (photo_file);
+	gdata_picasaweb_service_upload_file_async (GDATA_PICASAWEB_SERVICE (service), NULL, data->data.photo, data->data.photo_file, NULL,
+	                                           (GAsyncReadyCallback) test_upload_default_album_async_cb, data);
+	g_main_loop_run (data->main_loop);
 }
 
 static void
-test_upload_default_album_cancellation_cb (GDataPicasaWebService *service, GAsyncResult *async_result, GMainLoop *main_loop)
+test_upload_default_album_cancellation_cb (GDataPicasaWebService *service, GAsyncResult *async_result, UploadAsyncData *data)
 {
-	GDataPicasaWebFile *file;
 	GError *error = NULL;
 
-	file = gdata_picasaweb_service_upload_file_finish (service, async_result, &error);
+	data->data.updated_photo = gdata_picasaweb_service_upload_file_finish (service, async_result, &error);
 	g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
-	g_assert (file == NULL);
+	g_assert (data->data.updated_photo == NULL);
 	g_clear_error (&error);
 
-	g_main_loop_quit (main_loop);
+	g_main_loop_quit (data->main_loop);
 }
 
 static gboolean
@@ -1359,71 +1270,37 @@ test_upload_default_album_cancellation_cancel_cb (GCancellable *cancellable)
 }
 
 static void
-test_upload_default_album_cancellation (gconstpointer service)
+test_upload_default_album_cancellation (UploadAsyncData *data, gconstpointer service)
 {
-	GMainLoop *main_loop;
 	GCancellable *cancellable;
-	GDataPicasaWebFile *photo;
-	GFile *photo_file;
-	const gchar * const tags[] = { "foo", "bar", NULL };
 
-	/* Build the photo */
-	photo = gdata_picasaweb_file_new (NULL);
-	gdata_entry_set_title (GDATA_ENTRY (photo), "Photo Entry Title");
-	gdata_picasaweb_file_set_caption (photo, "Summary");
-	gdata_picasaweb_file_set_tags (photo, tags);
-
-	/* File is public domain: http://en.wikipedia.org/wiki/File:German_garden_gnome_cropped.jpg */
-	photo_file = g_file_new_for_path (TEST_FILE_DIR "photo.jpg");
-
-	/* Create a main loop and an idle function which will cancel the upload */
-	main_loop = g_main_loop_new (NULL, TRUE);
+	/* Create an idle function which will cancel the upload */
 	cancellable = g_cancellable_new ();
 	g_idle_add ((GSourceFunc) test_upload_default_album_cancellation_cancel_cb, cancellable);
 
 	/* Upload the photo */
-	gdata_picasaweb_service_upload_file_async (GDATA_PICASAWEB_SERVICE (service), NULL, photo, photo_file, cancellable,
-	                                           (GAsyncReadyCallback) test_upload_default_album_cancellation_cb, main_loop);
-	g_main_loop_run (main_loop);
+	gdata_picasaweb_service_upload_file_async (GDATA_PICASAWEB_SERVICE (service), NULL, data->data.photo, data->data.photo_file, cancellable,
+	                                           (GAsyncReadyCallback) test_upload_default_album_cancellation_cb, data);
+	g_main_loop_run (data->main_loop);
 
-	g_main_loop_unref (main_loop);
 	g_object_unref (cancellable);
-	g_object_unref (photo);
-	g_object_unref (photo_file);
 }
 
 static void
-test_upload_default_album_cancellation2 (gconstpointer service)
+test_upload_default_album_cancellation2 (UploadAsyncData *data, gconstpointer service)
 {
-	GMainLoop *main_loop;
 	GCancellable *cancellable;
-	GDataPicasaWebFile *photo;
-	GFile *photo_file;
-	const gchar * const tags[] = { "foo", "bar", NULL };
-
-	/* Build the photo */
-	photo = gdata_picasaweb_file_new (NULL);
-	gdata_entry_set_title (GDATA_ENTRY (photo), "Photo Entry Title");
-	gdata_picasaweb_file_set_caption (photo, "Summary");
-	gdata_picasaweb_file_set_tags (photo, tags);
 
-	/* File is public domain: http://en.wikipedia.org/wiki/File:German_garden_gnome_cropped.jpg */
-	photo_file = g_file_new_for_path (TEST_FILE_DIR "photo.jpg");
-
-	/* Create a main loop and an idle function which will cancel the upload */
-	main_loop = g_main_loop_new (NULL, TRUE);
+	/* Create an idle function which will cancel the upload */
 	cancellable = g_cancellable_new ();
 	g_timeout_add (1, (GSourceFunc) test_upload_default_album_cancellation_cancel_cb, cancellable);
 
 	/* Upload the photo */
-	gdata_picasaweb_service_upload_file_async (GDATA_PICASAWEB_SERVICE (service), NULL, photo, photo_file, cancellable,
-	                                           (GAsyncReadyCallback) test_upload_default_album_cancellation_cb, main_loop);
-	g_main_loop_run (main_loop);
+	gdata_picasaweb_service_upload_file_async (GDATA_PICASAWEB_SERVICE (service), NULL, data->data.photo, data->data.photo_file, cancellable,
+	                                           (GAsyncReadyCallback) test_upload_default_album_cancellation_cb, data);
+	g_main_loop_run (data->main_loop);
 
-	g_main_loop_unref (main_loop);
 	g_object_unref (cancellable);
-	g_object_unref (photo);
-	g_object_unref (photo_file);
 }
 
 static void
@@ -1634,10 +1511,13 @@ main (int argc, char *argv[])
 		g_test_add_data_func ("/picasaweb/query/photo_single", service, test_photo_single);
 		g_test_add_data_func ("/picasaweb/query/photo/async", service, test_photo_async);
 
-		g_test_add_data_func ("/picasaweb/upload/default_album", service, test_upload_default_album);
-		g_test_add_data_func ("/picasaweb/upload/default_album/async", service, test_upload_default_album_async);
-		g_test_add_data_func ("/picasaweb/upload/default_album/cancellation", service, test_upload_default_album_cancellation);
-		g_test_add_data_func ("/picasaweb/upload/default_album/cancellation2", service, test_upload_default_album_cancellation2);
+		g_test_add ("/picasaweb/upload/default_album", UploadData, service, setup_upload, test_upload_default_album, teardown_upload);
+		g_test_add ("/picasaweb/upload/default_album/async", UploadAsyncData, service, setup_upload_async, test_upload_default_album_async,
+		            teardown_upload_async);
+		g_test_add ("/picasaweb/upload/default_album/cancellation", UploadAsyncData, service, setup_upload_async,
+		            test_upload_default_album_cancellation, teardown_upload_async);
+		g_test_add ("/picasaweb/upload/default_album/cancellation2", UploadAsyncData, service, setup_upload_async,
+		            test_upload_default_album_cancellation2, teardown_upload_async);
 
 		g_test_add_data_func ("/picasaweb/download/photo", service, test_download);
 		g_test_add_data_func ("/picasaweb/download/thumbnails", service, test_download_thumbnails);



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