[couchdb-glib/wip/query-response] update tests for new API



commit 0c383a58e2b06b7c8190bee874c5b4eb183ad74e
Author: Krzysztof Klimonda <kklimonda syntaxhighlighted com>
Date:   Wed Jan 19 13:44:14 2011 +0100

    update tests for new API

 tests/test-couchdb-database.c      |  153 ++++++++++++++
 tests/test-couchdb-glib.c          |  399 +++++++++++++++---------------------
 tests/test-couchdb-session.c       |  245 ++++++++++++++++++++++
 tests/test-desktopcouch-database.c |   27 +++
 tests/test-desktopcouch-glib.c     |    1 -
 tests/test-desktopcouch-session.c  |  156 ++++++++++++++
 tests/test-oauth.c                 |    1 -
 7 files changed, 749 insertions(+), 233 deletions(-)
---
diff --git a/tests/test-couchdb-database.c b/tests/test-couchdb-database.c
new file mode 100644
index 0000000..3b90af4
--- /dev/null
+++ b/tests/test-couchdb-database.c
@@ -0,0 +1,153 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2010 Krzysztof Klimonda
+ *
+ * Authors: Krzysztof Klimonda <kklimonda syntaxhighlighted com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU Lesser General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+#include <couchdb-glib.h>
+
+typedef struct {
+	CouchdbSession *session;
+	CouchdbDatabase *database;
+} CouchdbFixture;
+
+static void
+couchdb_fixture_setup (CouchdbFixture * fixture,
+		       gconstpointer user_data)
+{
+	GList *dbnames, *iter;
+	fixture->session = couchdb_session_new (user_data);
+
+	dbnames = couchdb_session_list_databases (fixture->session, NULL);
+	for (iter = dbnames; iter != NULL; iter = g_list_next (iter)) {
+		couchdb_session_delete_database (fixture->session,
+						 iter->data, NULL);
+	}
+
+	fixture->database =
+		couchdb_session_create_database (fixture->session,
+						 "test_couchdb_database", NULL);
+}
+
+static void
+couchdb_fixture_teardown (CouchdbFixture * fixture,
+		          gconstpointer user_data)
+{
+	couchdb_session_delete_database (fixture->session,
+					 "test_couchdb_database", NULL);
+
+	g_object_unref (fixture->database);
+	g_object_unref (fixture->session);
+}
+
+static void
+test_database_put_get_docs (CouchdbFixture * fixture, gconstpointer user_data)
+{
+	JsonObject *object;
+	const gchar *simple_id, *weird_id, *backslash_id;
+	gchar *rev;
+	GError *error;
+
+	error = NULL;
+	simple_id = "test_simple_id";
+	weird_id = "test/simple/id/with/:0+-();\"";
+	backslash_id = "test\\with\\backslashes";
+
+	object = json_object_new ();
+	json_object_set_string_member (object, "_id", simple_id);
+
+	couchdb_database_put_document (fixture->database, object, &error);
+	g_assert (error == NULL);
+	g_assert (json_object_has_member (object, "_rev"));
+
+	rev = g_strdup (json_object_get_string_member (object, "_rev"));
+	json_object_unref (object);
+
+	object = couchdb_database_get_document (fixture->database,
+						simple_id, NULL, &error);
+	g_assert (error == NULL && object != NULL);
+	g_assert_cmpstr (json_object_get_string_member (object, "_rev"), ==, rev);
+
+	json_object_unref (object);
+	g_free (rev);
+
+	object = json_object_new ();
+	json_object_set_string_member (object, "_id", weird_id);
+
+	couchdb_database_put_document (fixture->database, object, &error);
+	g_assert (error == NULL);
+	g_assert (json_object_has_member (object, "_rev"));
+	json_object_unref (object);
+
+	object = json_object_new ();
+	json_object_set_string_member (object, "_id", backslash_id);
+
+	couchdb_database_put_document (fixture->database, object, &error);
+	g_assert (error == NULL);
+	g_assert (json_object_has_member (object, "_rev"));
+	json_object_unref (object);
+}
+
+static void
+test_database_execute_query (CouchdbFixture * fixture, gconstpointer test_data)
+{
+	CouchdbDatabase *database;
+	CouchdbQuery *query;
+	CouchdbResponse *response;
+	GError *error;
+
+	error = NULL;
+	database = NULL;
+
+	/* database have to be escaped in couchdb_database_execute_query so lets
+	   see if that happens */
+	database = couchdb_session_create_database (fixture->session,
+						    "database/with0/_$()+-", NULL);
+	g_assert (database != NULL);
+
+	query = couchdb_query_new_for_path ("/_all_docs");
+	response = couchdb_database_execute_query (database, query, &error);
+	g_assert (error == NULL);
+
+	g_object_unref (response);
+	g_object_unref (query);
+	g_object_unref (database);
+}
+
+int
+main (int argc, char *argv[])
+{
+	CouchdbSession *session;
+	GList *databases, *iter;
+	char *uri;
+
+	g_type_init ();
+	g_thread_init (NULL);
+	g_test_init (&argc, &argv, NULL);
+
+	uri = "http://localhost:5985";;
+
+	g_test_add ("/CouchDB/Database/executeQuery", CouchdbFixture,
+		    uri, couchdb_fixture_setup, test_database_execute_query,
+		    couchdb_fixture_teardown);
+
+	g_test_add ("/CouchDB/Database/putAndGetDocuments", CouchdbFixture,
+		    uri, couchdb_fixture_setup, test_database_put_get_docs,
+		    couchdb_fixture_teardown);
+
+	return g_test_run ();
+}
diff --git a/tests/test-couchdb-glib.c b/tests/test-couchdb-glib.c
index cdd8634..c0cf8fd 100644
--- a/tests/test-couchdb-glib.c
+++ b/tests/test-couchdb-glib.c
@@ -1,8 +1,8 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 /*
- * Copyright (C) 2009 Canonical Services Ltd (www.canonical.com)
+ * Copyright (C) 2010 Krzysztof Klimonda
  *
- * Authors: Rodrigo Moya <rodrigo moya canonical com>
+ * Authors: Krzysztof Klimonda <kklimonda syntaxhighlighted com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of version 2 of the GNU Lesser General Public
@@ -24,290 +24,227 @@
 
 static CouchdbSession *couchdb;
 
-static CouchdbArrayField *
-create_fake_array (void)
-{
-	CouchdbArrayField *array;
-	gint i;
-
-	array = couchdb_array_field_new ();
-	for (i = 0; i < 10; i++)
-		couchdb_array_field_add_int_element (array, i);
-
-	return array;
-}
+typedef struct {
+	CouchdbSession *session;
+	CouchdbDatabase *database;
+} CouchdbFixture;
 
 static void
-test_array_field (void)
+couchdb_fixture_setup (CouchdbFixture * fixture,
+		       gconstpointer user_data)
 {
-	CouchdbArrayField *array, *tmp_array;
-
-	array = couchdb_array_field_new ();
-	g_assert (COUCHDB_IS_ARRAY_FIELD (array));
-
-	tmp_array = create_fake_array ();
-	couchdb_array_field_add_array_element (array, tmp_array);
-	g_assert (couchdb_array_field_get_length (couchdb_array_field_get_array_element (array, couchdb_array_field_get_length (array) - 1))
-		  == couchdb_array_field_get_length (tmp_array));
-	g_object_unref (G_OBJECT (tmp_array));
-
-	couchdb_array_field_add_boolean_element (array, TRUE);
-	g_assert (couchdb_array_field_get_boolean_element (array, couchdb_array_field_get_length (array) - 1) == TRUE);
-
-	couchdb_array_field_add_double_element (array, 1.0);
-	g_assert (couchdb_array_field_get_double_element (array, couchdb_array_field_get_length (array) - 1) == 1.0);
-
-	couchdb_array_field_add_int_element (array, 1);
-	g_assert (couchdb_array_field_get_int_element (array, couchdb_array_field_get_length (array) - 1) == 1);
-
-	couchdb_array_field_add_string_element (array, "hola");
-	g_assert (g_strcmp0 (couchdb_array_field_get_string_element (array, couchdb_array_field_get_length (array) - 1),
-			     "hola") == 0);
-
-	do {
-		gint length = couchdb_array_field_get_length (array);
-
-		couchdb_array_field_remove_element (array, 0);
-		g_assert (couchdb_array_field_get_length (array) == length - 1);
-	} while (couchdb_array_field_get_length (array) > 0);
-
-	g_object_unref (G_OBJECT (array));
+	fixture->session = couchdb_session_new (user_data);
+
+	couchdb_session_create_database (fixture->session,
+					 "couchdb_glib_test_database", NULL);
+	fixture->database =
+		couchdb_session_get_database (fixture->session,
+					      "couchdb_glib_test_database",
+					      NULL);
 }
 
 static void
-test_struct_field (void)
+couchdb_fixture_teardown (CouchdbFixture * fixture,
+		          gconstpointer user_data)
 {
-	CouchdbStructField *sf;
-
-	sf = couchdb_struct_field_new ();
-	g_assert (COUCHDB_IS_STRUCT_FIELD (sf));
+	g_object_unref (fixture->database);
 
-	couchdb_struct_field_set_boolean_field (sf, "boolean", TRUE);
-	g_assert (couchdb_struct_field_get_boolean_field (sf, "boolean") == TRUE);
+	couchdb_session_delete_database (fixture->session,
+					 "couchdb_glib_test_database", NULL);
 
-	couchdb_struct_field_set_double_field (sf, "double", 1.0);
-	g_assert (couchdb_struct_field_get_double_field (sf, "double") == 1.0);
-
-	couchdb_struct_field_set_int_field (sf, "int", 1);
-	g_assert (couchdb_struct_field_get_int_field (sf, "int") == 1);
-
-	couchdb_struct_field_set_string_field (sf, "string", "hola");
-	g_assert (g_strcmp0 (couchdb_struct_field_get_string_field (sf, "string"), "hola") == 0);
-
-	g_object_unref (G_OBJECT (sf));
+	g_object_unref (fixture->session);
 }
 
 static void
-test_list_databases (void)
+test_database_create_and_destroy (gconstpointer user_data)
 {
-	GError *error = NULL;
-	GSList *dblist;
-
-	dblist = couchdb_session_list_databases (couchdb, &error);
-	if (error != NULL) {
-		/* A critical will abort the test case */
-		g_critical ("Error listing databases: %s", error->message);
-		g_error_free (error);
-		error = NULL;		
-	}
+	const char *uri = user_data;
+	CouchdbSession *session;
+	GError *error;
 
-	while (dblist != NULL) {
-		CouchdbDatabaseInfo *dbinfo;
-		CouchdbDatabase *database;
-		GSList *doclist;
+	error = NULL;
 
-		error = NULL;
-		dbinfo = couchdb_session_get_database_info (couchdb, (const char *) dblist->data, &error);
-		g_assert (error == NULL);
-		g_assert (dbinfo != NULL);
-		g_assert (couchdb_database_info_get_dbname (dbinfo) != NULL);
+	/* initialize session */
+	session = couchdb_session_new (uri);
 
-		error = NULL;
-		database = couchdb_session_get_database (couchdb, (const char *) dblist->data, &error);
-		g_assert (error == NULL);
-		g_assert (database != NULL);
+	/* some simple database, just to see if nothing basic is broken */
+	couchdb_session_create_database (session, "simple_database", &error);
+	g_assert (error == NULL);
+	couchdb_session_delete_database (session, "simple_database", &error);
+	g_assert (error == NULL);
 
-		/* Get list of documents to compare against couchdb_database_info_get_documents_count */
-		error = NULL;
-		doclist = couchdb_database_list_documents (database, &error);
-		g_assert (error == NULL);
-		g_assert (g_slist_length (doclist) == couchdb_database_info_get_documents_count (dbinfo));
-		if (doclist)
-			couchdb_database_free_document_list (doclist);
+	/* check whether we can create a database with slashes */
+	couchdb_session_create_database (session, "database/with/slashes", &error);
+	g_assert (error == NULL);
+	couchdb_session_delete_database (session, "database/with/slashes", &error);
+	g_assert (error == NULL);
 
-		dblist = g_slist_remove (dblist, dblist->data);
-		couchdb_database_info_unref (dbinfo);
-		g_object_unref (G_OBJECT (database));
-	}
+	/* check whether we can create a database with other funny
+	   characters */
+	couchdb_session_create_database (session, "database/with0/_$()+-", &error);
+	g_assert (error == NULL);
+	couchdb_session_delete_database (session, "database/with0/_$()+-", &error);
+	g_assert (error == NULL);
+
+	/* see what happens if we try to create a database with prohibited
+	   characters */
+	couchdb_session_create_database (session, "database/with\\backslash", &error);
+	g_assert (error != NULL);
+	g_error_free (error);
 }
 
 static void
-test_list_documents (void)
+test_document_create_and_delete (CouchdbFixture * fixture,
+				 gconstpointer user_data)
 {
-	GError *error = NULL;
-	GSList *dblist;
-
-	dblist = couchdb_session_list_databases (couchdb, &error);
-	g_assert (error == NULL);
-
-	while (dblist != NULL) {
-		GSList *doclist;
-		CouchdbDatabase *database;
-
-		error = NULL;
-		database = couchdb_session_get_database (couchdb, (const char *) dblist->data, &error);
-		g_assert (error == NULL);
-		g_assert (database != NULL);
-
-		error = NULL;
-		doclist = couchdb_database_list_documents (database, &error);
-		g_assert (error == NULL);
-
-		while (doclist) {
-			CouchdbDocumentInfo *doc_info = doclist->data;
-			CouchdbDocument *document;
-			char *str;
-
-			error = NULL;
-			document = couchdb_database_get_document (database,
-								  couchdb_document_info_get_docid (doc_info),
-								  &error);
-			g_assert (error == NULL);
-			g_assert (document != NULL);
-			g_assert (g_strcmp0 (couchdb_document_info_get_docid (doc_info), couchdb_document_get_id (document)) == 0);
-			g_assert (g_strcmp0 (couchdb_document_info_get_revision (doc_info), couchdb_document_get_revision (document)) == 0);
+	JsonObject *document;
+	GError *error;
+	gboolean retval;
 
-			str = couchdb_document_to_string (document);
-			g_assert (str != NULL);
-			g_free (str);
+	error = NULL;
 
-			g_object_unref (G_OBJECT (document));
-
-			doclist = g_slist_remove (doclist, doc_info);
-			couchdb_document_info_unref (doc_info);
-		}
+	/* create an empty document first and see if it has revision and
+	   id set after it's put into the database */
+	document = json_object_new ();
+	couchdb_database_put_document (fixture->database, document, &error);
+	g_assert (error == NULL);
+	g_assert (json_object_has_member (document, "_id") &&
+			json_object_has_member (document, "_rev"));
 
-		dblist = g_slist_remove (dblist, dblist->data);
-		g_object_unref (G_OBJECT (database));
-	}
-}
+	retval = couchdb_database_delete_document (fixture->database,
+						   document, &error);
+	g_assert (error == NULL);
+	g_assert (retval == TRUE);
 
-static void
-doc_changed_cb (CouchdbDatabase *database, CouchdbDocument *document, gpointer user_data)
-{
-	char *doc_str;
+	json_object_unref (document);
 
-	doc_str = couchdb_document_to_string (document);
-	g_print ("Document %s has been %s: %s\n",
-		 couchdb_document_get_id (document),
-		 (const gchar *) user_data,
-		 doc_str);
+	/* create a document with _id set, but not _rev */
+	document = json_object_new ();
+	json_object_set_string_member (document, "_id", "1");
+	
+	couchdb_database_put_document (fixture->database, document, &error);
+	g_assert (error == NULL);
+	g_assert (json_object_has_member (document, "_id") &&
+			json_object_has_member (document, "_rev"));
 
-	g_free (doc_str);
-}
+	retval = couchdb_database_delete_document (fixture->database,
+						   document, &error);
+	g_assert (error == NULL);
+	g_assert (retval == TRUE);
 
-static void
-doc_deleted_cb (CouchdbDatabase *database, const char *docid, gpointer user_data)
-{
-	g_print ("Document %s in database %s has been deleted\n", docid, couchdb_database_get_name (database));
+	json_object_unref (document);
 }
 
 static void
-test_change_databases (void)
+test_document_create_valid_id (CouchdbFixture * fixture,
+                               gconstpointer user_data)
 {
-	char *dbname;
-	gint i;
-	GError *error = NULL;
-	CouchdbDatabase *database;
-
-	dbname = generate_uuid ();
-	g_assert (dbname != NULL);
-
-	/* Database name can not start with a digit
-	 * we will overwrite the first character with 'a' */
-	 dbname[0] = 'a';
-
-	/* Create database */
-	couchdb_session_create_database (couchdb, dbname, &error);
-	if (error) {
-		g_critical ("Error creating database '%s': %s", dbname, error->message);
-		g_error_free (error);
-	}
-
-	database = couchdb_session_get_database (couchdb, dbname, &error);
-	g_signal_connect (G_OBJECT (database), "document_created", G_CALLBACK (doc_changed_cb), "created");
-	g_signal_connect (G_OBJECT (database), "document_updated", G_CALLBACK (doc_changed_cb), "updated");
-	g_signal_connect (G_OBJECT (database), "document_deleted", G_CALLBACK (doc_deleted_cb), NULL);
-
-	couchdb_database_listen_for_changes (database);
+	JsonObject *document;
+	GError *error;
+	gboolean retval;
 
-	/* Create some documents */
-	for (i = 0; i < 10; i++) {
-		CouchdbDocument *document;
-		char *str;
+	error = NULL;
 
-		document = couchdb_document_new ();
-		g_assert (document != NULL);
+	/* check if we can create a document with forward slash */
+	document = json_object_new ();
+	json_object_set_string_member (document, "_id", "id/with/slashes");
 
-		couchdb_document_set_boolean_field (document, "boolean", TRUE);
-		couchdb_document_set_int_field (document, "int", i);
-		couchdb_document_set_double_field (document, "double", (gdouble) i);
-
-		str = g_strdup_printf ("value%d", i);
-		couchdb_document_set_string_field (document, "string", str);
-		g_free (str);
-
-		g_assert (couchdb_database_put_document (database, document, &error));
-		g_assert (error == NULL);
-	}
+	couchdb_database_put_document (fixture->database, document, &error);
+	g_assert (error == NULL);
+	g_assert (json_object_has_member (document, "_id") &&
+			json_object_has_member (document, "_rev"));
+	g_assert_cmpstr (json_object_get_string_member (document, "_id"),
+				==, "id/with/slashes");
 
-	g_object_unref (G_OBJECT (database));
+	retval = couchdb_database_delete_document (fixture->database,
+						   document, &error);
+	g_assert (error == NULL);
+	g_assert (retval == TRUE);
+	
+	json_object_unref (document);
 
-	/* Delete database */
-	g_assert (couchdb_session_delete_database (couchdb, dbname, &error));
+	document = json_object_new ();
+	json_object_set_string_member (document, "_id",
+				       "document/with0/_$()+-:");
+	couchdb_database_put_document (fixture->database, document, &error);
+	g_assert (error == NULL);
+	g_assert (json_object_has_member (document, "_id") &&
+			json_object_has_member (document, "_rev"));
+	g_assert_cmpstr (json_object_get_string_member (document, "_id"),
+				==, "document/with0/_$()+-:");
+	retval = couchdb_database_delete_document (fixture->database,
+						   document, &error);
 	g_assert (error == NULL);
+	g_assert (retval == TRUE);
 
-	/* Free memory */
-	g_free (dbname);
+	json_object_unref (document);
 }
 
 static void
-db_created_cb (CouchdbSession *couchdb, const char *dbname, gpointer user_data)
+test_database_create_and_get_all_docs (CouchdbFixture * fixture,
+	                               gconstpointer user_data)
 {
-	g_print ("Database %s has been created\n", dbname);
-}
+	GList *documents;
+	GError *error;
+	guint i;
+
+	error = NULL;
+
+	/* first put some docs */
+	for (i = 0; i < 4; i++) {
+		JsonObject *document;
+		char *id;
+
+		id = g_strdup_printf ("%i", i);
+		document = json_object_new ();
+		json_object_set_string_member (document, "_id", id);
+		couchdb_database_put_document (fixture->database,
+					       document, &error);
+		g_assert (error == NULL);
+		json_object_unref (document);
+		g_free (id);
+	}
 
-static void
-db_deleted_cb (CouchdbSession *couchdb, const char *dbname, gpointer user_data)
-{
-	g_print ("Database %s has been deleted\n", dbname);
+	/* now get them all */
+	documents = couchdb_database_get_all_documents (fixture->database,
+							&error);
+	g_assert (error == NULL);
+	g_assert_cmpint (g_list_length (documents), ==, 4);
+	
+	g_list_foreach (documents, (GFunc) json_object_unref, NULL);
+	g_list_free (documents);
 }
 
 int
 main (int argc, char *argv[])
 {
+	char *uri;
 	g_type_init ();
 	g_thread_init (NULL);
 	g_test_init (&argc, &argv, NULL);
 
 	/* Initialize data needed for all tests */
-	couchdb =  argc > 1 ? couchdb_session_new (argv[1]) : couchdb_session_new ("http://test:test 127 0 0 1:5985");
-	g_print ("Connecting to Couchdb at %s\n", couchdb_session_get_uri (couchdb));
-	
-	if (!couchdb) {
-		g_print ("Could not create Couchdb object\n");
-		return -1;
-	}
-
-	g_signal_connect (G_OBJECT (couchdb), "database_created", G_CALLBACK (db_created_cb), NULL);
-	g_signal_connect (G_OBJECT (couchdb), "database_deleted", G_CALLBACK (db_deleted_cb), NULL);
-
-	/* Setup test functions */
-	g_test_add_func ("/testcouchdbglib/TestArrayField", test_array_field);
-	g_test_add_func ("/testcouchdbglib/TestStructField", test_struct_field);
-	g_test_add_func ("/testcouchdbglib/ListDatabases", test_list_databases);
-	g_test_add_func ("/testcouchdbglib/ListDocuments", test_list_documents);
-	g_test_add_func ("/testcouchdbglib/ChangeDatabases", test_change_databases);
+	uri = argc > 1 ? g_strdup (argv[1]) : g_strdup ("http://test:test 127 0 0 1:5985");
+
+	/* this test doesn't take Fixture because we basically test things that are used
+	 * in a Fixture setup itself */
+	g_test_add_data_func ("/CouchDB/DatabaseCreationAndDestruction",
+			      uri, test_database_create_and_destroy);
+	/* see if basic document creation and deletion works */
+	g_test_add ("/CouchDB/DocumentCreationAndDeletion", CouchdbFixture,
+		    uri, couchdb_fixture_setup,
+		    test_document_create_and_delete,
+		    couchdb_fixture_teardown);
+	/* create documents with weird IDs */
+	g_test_add ("/CouchDB/DocumentCreateValidID", CouchdbFixture,
+		    uri, couchdb_fixture_setup,
+		    test_document_create_valid_id,
+		    couchdb_fixture_teardown);
+	/* check couchdb_database_get_all_documents */
+	g_test_add ("/CouchDB/CreateAndGetAllDocs", CouchdbFixture,
+		    uri, couchdb_fixture_setup,
+		    test_database_create_and_get_all_docs,
+		    couchdb_fixture_teardown);
 
 	return g_test_run ();
 }
diff --git a/tests/test-couchdb-session.c b/tests/test-couchdb-session.c
new file mode 100644
index 0000000..a4d5b83
--- /dev/null
+++ b/tests/test-couchdb-session.c
@@ -0,0 +1,245 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2010 Krzysztof Klimonda
+ *
+ * Authors: Krzysztof Klimonda <kklimonda syntaxhighlighted com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU Lesser General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include <couchdb-glib.h>
+#include <utils.h>
+
+#include "couchdb-query.h"
+
+typedef struct {
+	CouchdbSession *session;
+} CouchdbFixture;
+
+static void
+couchdb_fixture_setup (CouchdbFixture * fixture,
+		       gconstpointer user_data)
+{
+	fixture->session = couchdb_session_new (user_data);
+
+	couchdb_session_create_database (fixture->session,
+					 "simple_database", NULL);
+	couchdb_session_create_database (fixture->session,
+					 "database/with/slashes", NULL);
+	couchdb_session_create_database (fixture->session,
+					 "database/with0/_$()+-", NULL);
+}
+
+static void
+couchdb_fixture_teardown (CouchdbFixture * fixture,
+		          gconstpointer user_data)
+{
+	couchdb_session_delete_database (fixture->session,
+					 "simple_database", NULL);
+	couchdb_session_delete_database (fixture->session,
+					 "database/with/slashes", NULL);
+	couchdb_session_delete_database (fixture->session,
+					 "database/with0/_$()+-", NULL);
+
+	g_object_unref (fixture->session);
+}
+
+static void
+test_session_create_and_destroy (gconstpointer user_data)
+{
+	const char *uri = user_data;
+	CouchdbSession *session;
+	CouchdbDatabase *database;
+	GError *error;
+
+	error = NULL;
+
+	/* initialize session */
+	session = couchdb_session_new (uri);
+
+	/* some simple database, just to see if nothing basic is broken */
+	database = 
+		couchdb_session_create_database (session,
+						 "simple_database", &error);
+	g_assert (error == NULL);
+	g_assert (database != NULL);
+	g_object_unref (database);
+
+	couchdb_session_delete_database (session, "simple_database", &error);
+	g_assert (error == NULL);
+
+	/* check whether we can create a database with slashes */
+	database =
+		couchdb_session_create_database (session,
+						 "database/with/slashes", &error);
+	g_assert (error == NULL);
+	g_assert (database != NULL);
+	g_object_unref (database);
+
+	couchdb_session_delete_database (session, "database/with/slashes", &error);
+	g_assert (error == NULL);
+
+	/* check whether we can create a database with other funny
+	   characters */
+	database =
+		couchdb_session_create_database (session,
+						 "database/with0/_$()+-", &error);
+	g_assert (error == NULL);
+	g_assert (database != NULL);
+	g_object_unref (database);
+
+	couchdb_session_delete_database (session, "database/with0/_$()+-", &error);
+	g_assert (error == NULL);
+
+	/* see what happens if we try to create a database with prohibited
+	   characters */
+	database =
+		couchdb_session_create_database (session,
+					 "database/with\\backslash", &error);
+	g_assert (error != NULL);
+	g_assert (database == NULL);
+	g_error_free (error);
+}
+
+static void
+test_session_get_database (CouchdbFixture *fixture, gconstpointer user_data)
+{
+	CouchdbDatabase *database;
+	GError * error;
+
+	error = NULL;
+
+	database = couchdb_session_get_database (fixture->session,
+						 "simple_database", &error);
+	g_assert (error == NULL);
+	g_assert_cmpstr (couchdb_database_get_name (database),
+			 ==, "simple_database");
+	g_object_unref (database);
+
+	database = couchdb_session_get_database (fixture->session,
+						 "database/with/slashes", &error);
+	g_assert (error == NULL);
+	g_assert_cmpstr (couchdb_database_get_name (database),
+			 ==, "database/with/slashes");
+	g_object_unref (database);
+
+	database = couchdb_session_get_database (fixture->session,
+						 "database/with0/_$()+-", &error);
+	g_assert (error == NULL);
+	g_assert_cmpstr (couchdb_database_get_name (database),
+			 ==, "database/with0/_$()+-");
+	g_object_unref (database);
+
+	database = NULL;
+	database = couchdb_session_get_database (fixture->session,
+						 "non_existant", &error);
+	g_assert (error != NULL);
+	g_assert (database == NULL);
+	g_error_free (error);
+}
+
+static void
+test_session_list_databases (CouchdbFixture *fixture, gconstpointer user_data)
+{
+	GList *databases;
+	GError *error;
+
+	error = NULL;
+
+	databases = couchdb_session_list_databases (fixture->session, &error);
+	g_assert (error == NULL);
+	g_assert (databases != NULL);
+
+	/* we've created three databases, there is also _users */
+	g_assert_cmpint (g_list_length (databases), ==, 3);
+	
+	g_list_free (databases);
+}
+
+static void
+test_session_execute_simple_queries (gconstpointer user_data)
+{
+	CouchdbSession *session;
+	CouchdbQuery *query;
+	CouchdbResponse *response;
+	JsonObject *object;
+	GError *error;
+
+	session = couchdb_session_new (user_data);
+
+	error = NULL;
+	/* first, test simple query without anything */
+	query = couchdb_query_new ();
+
+	response = couchdb_session_execute_query (session, query, &error);
+	g_assert (response != NULL);
+
+	g_assert_cmpuint (couchdb_response_get_status_code (response), ==, 200);
+	g_assert_cmpuint (couchdb_response_get_content_length (response), ==, 40);
+	g_assert (couchdb_response_get_etag (response) == NULL);
+	g_assert_cmpstr (couchdb_response_get_content_type (response), ==,
+			 "text/plain;charset=utf-8");
+	g_assert (couchdb_response_get_json_object (response) != NULL);
+
+	g_assert (error == NULL);
+	g_object_unref (query);
+	g_object_unref (response);
+
+	query = couchdb_query_new_for_path ("/_all_dbs");
+	response = couchdb_session_execute_query (session, query, &error);
+	g_assert (response != NULL);
+	g_assert (error == NULL);
+
+	object = couchdb_response_get_json_object (response);
+	g_assert (JSON_NODE_HOLDS_ARRAY (json_object_get_member (object, "array")));
+
+	g_object_unref (query);
+	g_object_unref (response);
+}
+
+int
+main (int argc, char *argv[])
+{
+	CouchdbSession *session;
+	GList *databases, *iter;
+	char *uri;
+
+	g_type_init ();
+	g_thread_init (NULL);
+	g_test_init (&argc, &argv, NULL);
+
+	uri = "http://localhost:5985";;
+
+	session = couchdb_session_new (uri);
+	databases = couchdb_session_list_databases (session, NULL);
+	for (iter = databases; iter != NULL; iter = g_list_next (iter)) {
+		couchdb_session_delete_database (session, iter->data, NULL);
+	}
+
+	g_test_add_data_func ("/CouchDB/Session/executeSimpleQueries", uri,
+			      test_session_execute_simple_queries);
+	g_test_add_data_func ("/CouchDB/Session/DatabaseCreationAndDestruction",
+			      uri, test_session_create_and_destroy);
+	g_test_add ("/CouchDB/Session/GetDatabase", CouchdbFixture,
+		    uri, couchdb_fixture_setup,
+		    test_session_get_database,
+		    couchdb_fixture_teardown);
+	g_test_add ("/CouchDB/Session/ListDatabases", CouchdbFixture,
+		    uri, couchdb_fixture_setup,
+		    test_session_list_databases,
+		    couchdb_fixture_teardown);
+
+	return g_test_run ();
+}
diff --git a/tests/test-desktopcouch-database.c b/tests/test-desktopcouch-database.c
new file mode 100644
index 0000000..abc4f6a
--- /dev/null
+++ b/tests/test-desktopcouch-database.c
@@ -0,0 +1,27 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2010 Krzysztof Klimonda
+ *
+ * Authors: Krzysztof Klimonda <kklimonda syntaxhighlighted com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU Lesser General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+#include <couchdb-glib.h>
+#include <desktopcouch-glib.h>
+
+typedef struct {
+	DesktopcouchSession *session;
+	DesktopcouchDatabase *database;
+} DesktopcouchFixture;
diff --git a/tests/test-desktopcouch-glib.c b/tests/test-desktopcouch-glib.c
index a99c328..92c31a7 100644
--- a/tests/test-desktopcouch-glib.c
+++ b/tests/test-desktopcouch-glib.c
@@ -49,7 +49,6 @@ test_list_databases (void)
 		g_print ("Found database %s\n", (const char *) sl->data);
 
 	/* Free memory */
-	couchdb_session_free_database_list (dblist);
 }
 
 int
diff --git a/tests/test-desktopcouch-session.c b/tests/test-desktopcouch-session.c
new file mode 100644
index 0000000..e0f75d7
--- /dev/null
+++ b/tests/test-desktopcouch-session.c
@@ -0,0 +1,156 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2010 Krzysztof Klimonda
+ *
+ * Authors: Krzysztof Klimonda <kklimonda syntaxhighlighted com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU Lesser General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+#include <couchdb-glib.h>
+#include <desktopcouch-glib.h>
+
+typedef struct {
+	DesktopcouchSession *session;
+	DesktopcouchDatabase *database;
+} DesktopcouchFixture;
+
+static void
+desktopcouch_fixture_setup (DesktopcouchFixture * fixture,
+			    gconstpointer user_data)
+{
+	fixture->session =
+		desktopcouch_session_new (user_data,
+					  COUCHDB_CREDENTIALS_TYPE_DESKTOPCOUCH);
+	fixture->database =
+		desktopcouch_session_create_database (fixture->session,
+						      "desktopcouch_test_db",
+						      DESKTOPCOUCH_TYPE_DOCUMENT_CONTACT,
+						      NULL);
+}
+
+static void
+desktopcouch_fixture_teardown (DesktopcouchFixture * fixture,
+			       gconstpointer user_data)
+{
+	g_object_unref (fixture->database)
+
+	couchdb_session_delete_database (fixture->session,
+					 "desktopcouch_test_db");
+	g_object_unref (fixture->session);
+}
+
+static void
+desktopcouch_test_connect_create_delete (gconstpointer test_data)
+{
+	DesktopcouchSession *session;
+	DesktopcouchDatabase *database;
+	GError *error;
+	gboolean ret;
+
+	error = NULL;
+
+	session = desktopcouch_session_new (uri,
+					    COUCHDB_CREDENTIALS_TYPE_DESKTOPCOUCH);
+	couchdb_session_connect (session, &error);
+	g_assert (error == NULL);
+
+	database =
+		desktopcouch_session_create_database (session,
+						      "desktopcouch_test_db",
+						      DESKTOPCOUCH_TYPE_DOCUMENT_CONTACT,
+						      &error);
+	g_assert (error == NULL);
+	
+	g_assert_cmp (desktopcouch_database_get_document_type (database),
+		      ==, DESKTOPCOUCH_TYPE_DOCUMENT_CONTACT);
+	g_assert_cmpstr (couchdb_database_get_name (database),
+			 ==, "desktopcouch_test_db");
+
+	ret = couchdb_session_delete_database (session,
+					       "desktopcouch_test_db", &error);
+	g_assert (error == NULL && ret == TRUE);
+
+	g_object_unref (database);
+	g_object_unref (session);
+}
+
+static void
+test_documents_add_remove (DesktopcouchFixture * fixture,
+			   gconstpointer user_data)
+{
+	DesktopcouchDocument *document;
+	DesktopcouchDocumentContact *contact;
+	GError *error;
+	const gchar *id, *first_name;
+
+	error = NULL;
+	id = "test-id";
+
+	document = desktopcouch_document_contact_new ();
+	/* upcasting because all DesktopcouchDocument creators should
+	 * return DesktopcouchDocument casted objects. That's because
+	 * other functions expect it */
+	contact = DESKTOPCOUCH_DOCUMENT_CONTACT (document);
+	dekstopcouch_document_set_id (document, id);
+	desktopcouch_document_contact_set_first_name (contact, first_name);
+
+	desktopcouch_database_put_document (fixture->database,
+					    document, &error);
+	g_object_unref (document);
+	document = desktopcouch_database_get_document (fixture->database,
+						       id, &error);
+	g_assert (error == NULL);
+	g_assert (DESKTOPCOUCH_IS_DOCUMENT_CONTACT (document));
+	g_assert_cmpstr (desktopcouch_document_get_id (document), ==, id);
+
+	contact = DESKTOPCOUCH_DOCUMENT_CONTACT (document);
+	g_assert_cmpstr (desktopcouch_document_contact_get_first_name (contact),
+			 ==, first_name);
+
+	g_object_unref (document);
+}
+
+int
+main (int argc, char ** argv)
+{
+	CouchdbSession *session;
+	GList *dbnames, *iter;
+
+	g_type_init ();
+	g_thread_init (NULL);
+	g_test_init (&argc, &argv, NULL);
+
+	uri = "http://localhost:5985";;
+
+	session = couchdb_session_new (uri);
+
+	couchdb_session_connect (session, &error);
+	if (error != NULL) {
+		g_error ("Couldn't connect to database");
+	}
+
+	dbnames = couchdb_session_list_databases (session, NULL);
+	for (iter = dbnames; iter != NULL; iter = g_list_next (iter)) {
+		couchdb_session_delete_database (session, iter->data, NULL);
+	}
+
+	g_test_add_data_func ("/DesktopCouch/Session/connectCreateAndDelete",
+			      uri, desktopcouch_test_connect_create_delete);
+	g_test_add ("/DesktopCouch/Session/addAndRemoveDocuments",
+		    DesktopcouchFixture, uri, desktopcouch_fixture_setup,
+		    test_documents_add_remove, desktopcouch_fixture_teardown);
+
+	return g_test_run ();
+}
diff --git a/tests/test-oauth.c b/tests/test-oauth.c
index 34c3073..bd26480 100644
--- a/tests/test-oauth.c
+++ b/tests/test-oauth.c
@@ -125,7 +125,6 @@ main (int argc, char *argv[])
 			g_print ("Found database %s\n", (const char *) sl->data);
 		}
 
-		couchdb_session_free_database_list (db_list);
 	} else if (error != NULL) {
 		g_print ("Could not get list of databases: %s\n", error->message);
 		g_error_free (error);



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