[evolution-data-server/openismus-work-master: 7/118] Added libedata-book test directory and added first test.



commit bbd6e6d5ec20bf372a67503947b0935cee3266c9
Author: Tristan Van Berkom <tristanvb openismus com>
Date:   Fri Apr 12 15:37:27 2013 +0900

    Added libedata-book test directory and added first test.
    
    In this new directory we can add tests for libedata-book backend
    components, currently there is a custom fixture which opens an
    EBookBackendSqliteDB on the same addressbook that an EBookClient
    opens, to test EBookBackendSqliteDB APIs directly on the addressbook.
    
    Conflicts:
    
        tests/Makefile.am

 configure.ac                                  |    1 +
 tests/Makefile.am                             |    2 +-
 tests/libedata-book/Makefile.am               |   58 ++++++++
 tests/libedata-book/data-test-utils.c         |  186 +++++++++++++++++++++++++
 tests/libedata-book/data-test-utils.h         |   50 +++++++
 tests/libedata-book/test-sqlite-get-contact.c |   56 ++++++++
 6 files changed, 352 insertions(+), 1 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index d788f26..72f78fa 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1768,6 +1768,7 @@ services/evolution-calendar-factory/Makefile
 services/evolution-source-registry/Makefile
 services/evolution-user-prompter/Makefile
 tests/Makefile
+tests/libedata-book/Makefile
 tests/libebook/Makefile
 tests/libebook-contacts/Makefile
 tests/libebook/client/Makefile
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 53b8924..db8304b 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,4 +1,4 @@
-SUBDIRS = test-server-utils libedataserver libebook-contacts libebook book-migration libecal libedata-cal
+SUBDIRS = test-server-utils libedataserver libebook-contacts libedata-book libebook book-migration libecal 
libedata-cal
 
 @GNOME_CODE_COVERAGE_RULES@
 
diff --git a/tests/libedata-book/Makefile.am b/tests/libedata-book/Makefile.am
new file mode 100644
index 0000000..84f6ba0
--- /dev/null
+++ b/tests/libedata-book/Makefile.am
@@ -0,0 +1,58 @@
+noinst_LTLIBRARIES = libdata-test-utils.la
+
+libdata_test_utils_la_SOURCES =                                        \
+       data-test-utils.c                                       \
+       data-test-utils.h                                       \
+       $(NULL)
+
+libdata_test_utils_la_CPPFLAGS =                               \
+       $(AM_CPPFLAGS)                                          \
+       -I$(top_srcdir)                                         \
+       -I$(top_builddir)                                       \
+       -I$(top_srcdir)/addressbook                             \
+       -I$(top_builddir)/addressbook                           \
+       -I$(top_srcdir)/calendar                                \
+       -I$(top_builddir)/calendar                              \
+       -I$(top_srcdir)/tests/libedataserver                    \
+       -I$(top_builddir)/tests/libedataserver                  \
+       -I$(top_srcdir)/tests/test-server-utils                 \
+       -I$(top_builddir)/tests/test-server-utils               \
+       -DBACKENDDIR=\"$(ebook_backenddir)\"                    \
+       -DDATADIR=\"$(datadir)\"                                \
+       -DSRCDIR=\""$(abs_srcdir)"\"                            \
+       -DBUILDDIR=\""$(abs_topbuilddir)"\"                     \
+       $(EVOLUTION_ADDRESSBOOK_CFLAGS)                         \
+       $(CAMEL_CFLAGS)                                         \
+       $(NULL)
+
+libdata_test_utils_la_LIBADD =                                 \
+       $(top_builddir)/addressbook/libebook-contacts/libebook-contacts-1.2.la          \
+       $(top_builddir)/addressbook/libedata-book/libedata-book-1.2.la  \
+       $(top_builddir)/addressbook/libebook/libebook-1.2.la    \
+       $(top_builddir)/tests/test-server-utils/libetestserverutils.la  \
+       $(EVOLUTION_ADDRESSBOOK_LIBS)                           \
+       $(CAMEL_LIBS)                                           \
+       $(NULL)
+
+# Should be kept ordered approximately from least to most difficult/complex
+TESTS =                                \
+       test-sqlite-get-contact
+
+noinst_PROGRAMS = $(TESTS)
+
+TEST_CPPFLAGS =                                                        \
+       $(libdata_test_utils_la_CPPFLAGS)                       \
+       $(EVOLUTION_ADDRESSBOOK_CPPFLAGS)                       \
+       $(NULL)
+
+TEST_LIBS =                                                    \
+       $(libdata_test_utils_la_LIBADD)                 \
+       libdata-test-utils.la                                   \
+       $(top_builddir)/addressbook/libebook/libebook-1.2.la    \
+       $(EVOLUTION_ADDRESSBOOK_LIBS)                           \
+       $(NULL)
+
+test_sqlite_get_contact_LDADD=$(TEST_LIBS)
+test_sqlite_get_contact_CPPFLAGS=$(TEST_CPPFLAGS)
+
+-include $(top_srcdir)/git.mk
diff --git a/tests/libedata-book/data-test-utils.c b/tests/libedata-book/data-test-utils.c
new file mode 100644
index 0000000..015e1c8
--- /dev/null
+++ b/tests/libedata-book/data-test-utils.c
@@ -0,0 +1,186 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2013, Openismus GmbH
+ *
+ * This program 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 program; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ * Authors: Tristan Van Berkom <tristanvb openismus com>
+ */
+
+#include <locale.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "data-test-utils.h"
+
+/* This forces the GType to be registered in a way that
+ * avoids a "statement with no effect" compiler warning.
+ * FIXME Use g_type_ensure() once we require GLib 2.34. */
+#define REGISTER_TYPE(type) \
+       (g_type_class_unref (g_type_class_ref (type)))
+
+
+#define SQLITEDB_EMAIL_ID    "addressbook localbackend com"
+#define SQLITEDB_FOLDER_NAME "folder"
+
+gchar *
+new_vcard_from_test_case (const gchar *case_name)
+{
+       gchar *filename;
+       gchar *case_filename;
+       GFile * file;
+       GError *error = NULL;
+       gchar *vcard;
+
+       case_filename = g_strdup_printf ("%s.vcf", case_name);
+       filename = g_build_filename (SRCDIR, "..", "libebook", "data", "vcards", case_filename, NULL);
+       file = g_file_new_for_path (filename);
+       if (!g_file_load_contents (file, NULL, &vcard, NULL, NULL, &error))
+               g_error ("failed to read test contact file '%s': %s",
+                        filename, error->message);
+
+       g_free (case_filename);
+       g_free (filename);
+       g_object_unref (file);
+
+       return vcard;
+}
+
+static gboolean
+contacts_are_equal_shallow (EContact *a,
+                            EContact *b)
+{
+       const gchar *uid_a, *uid_b;
+
+        /* Avoid warnings if one or more are NULL, to make this function
+         * "NULL-friendly" */
+       if (!a && !b)
+               return TRUE;
+
+       if (!E_IS_CONTACT (a) || !E_IS_CONTACT (b))
+               return FALSE;
+
+       uid_a = e_contact_get_const (a, E_CONTACT_UID);
+       uid_b = e_contact_get_const (b, E_CONTACT_UID);
+
+       return g_strcmp0 (uid_a, uid_b) == 0;
+}
+
+gboolean
+add_contact_from_test_case_verify (EBookClient *book_client,
+                                   const gchar *case_name,
+                                   EContact **contact)
+{
+       gchar *vcard;
+       EContact *contact_orig;
+       EContact *contact_final;
+       gchar *uid;
+       GError *error = NULL;
+
+       vcard = new_vcard_from_test_case (case_name);
+       contact_orig = e_contact_new_from_vcard (vcard);
+       g_free (vcard);
+       if (!e_book_client_add_contact_sync (book_client, contact_orig, &uid, NULL, &error))
+               g_error ("Failed to add contact: %s", error->message);
+
+       e_contact_set (contact_orig, E_CONTACT_UID, uid);
+
+       if (!e_book_client_get_contact_sync (book_client, uid, &contact_final, NULL, &error))
+               g_error ("Failed to get contact: %s", error->message);
+
+        /* verify the contact was added "successfully" (not thorough) */
+       g_assert (contacts_are_equal_shallow (contact_orig, contact_final));
+
+       if (contact)
+                *contact = contact_final;
+       else
+               g_object_unref (contact_final);
+       g_object_unref (contact_orig);
+       g_free (uid);
+
+       return TRUE;
+}
+
+static gchar *
+get_addressbook_directory (ESourceRegistry *registry,
+                          ESource         *source)
+{
+       ESource *builtin_source;
+       const gchar *user_data_dir;
+       const gchar *uid;
+       gchar *filename = NULL;
+
+       uid = e_source_get_uid (source);
+       g_return_val_if_fail (uid != NULL, NULL);
+
+       user_data_dir = e_get_user_data_dir ();
+
+       builtin_source = e_source_registry_ref_builtin_address_book (registry);
+
+       /* Special case directory for the builtin addressbook source */
+       if (builtin_source != NULL && e_source_equal (source, builtin_source))
+               uid = "system";
+
+       filename = g_build_filename (user_data_dir, "addressbook", uid, NULL);
+
+       if (builtin_source)
+               g_object_unref (builtin_source);
+
+       return filename;
+}
+
+static EBookBackendSqliteDB *
+open_sqlitedb (ESourceRegistry *registry,
+              ESource         *source)
+{
+       EBookBackendSqliteDB *ebsdb;
+       GError *error;
+       gchar *dirname;
+
+       dirname = get_addressbook_directory (registry, source);
+       ebsdb   = e_book_backend_sqlitedb_new (dirname,
+                                              SQLITEDB_EMAIL_ID,
+                                              SQLITEDB_FOLDER_ID,
+                                              SQLITEDB_FOLDER_NAME,
+                                              TRUE, &error);
+
+       if (!ebsdb)
+               g_error ("Failed to open SQLite backend: %s", error->message);
+
+       g_free (dirname);
+
+       return ebsdb;
+}
+
+void
+e_sqlitedb_fixture_setup (ESqliteDBFixture *fixture,
+                         gconstpointer     user_data)
+{
+       EBookClient *book_client;
+
+       e_test_server_utils_setup ((ETestServerFixture *)fixture, user_data);
+
+       book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);
+       fixture->ebsdb = open_sqlitedb (((ETestServerFixture *)fixture)->registry,
+                                       e_client_get_source (E_CLIENT (book_client)));
+}
+
+void
+e_sqlitedb_fixture_teardown (ESqliteDBFixture *fixture,
+                            gconstpointer     user_data)
+{
+       g_object_unref (fixture->ebsdb);
+       e_test_server_utils_teardown ((ETestServerFixture *)fixture, user_data);
+}
diff --git a/tests/libedata-book/data-test-utils.h b/tests/libedata-book/data-test-utils.h
new file mode 100644
index 0000000..e55bd78
--- /dev/null
+++ b/tests/libedata-book/data-test-utils.h
@@ -0,0 +1,50 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2013, Openismus GmbH
+ *
+ * This program 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 program; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ * Authors: Tristan Van Berkom <tristanvb openismus com>
+ */
+
+#ifndef DATA_TEST_UTILS_H
+#define DATA_TEST_UTILS_H
+
+#include <libebook/libebook.h>
+#include <libedata-book/libedata-book.h>
+#include "e-test-server-utils.h"
+
+#define SQLITEDB_FOLDER_ID   "folder_id"
+
+typedef struct {
+       ETestServerFixture parent_fixture;
+
+       EBookBackendSqliteDB *ebsdb;
+} ESqliteDBFixture;
+
+void     e_sqlitedb_fixture_setup          (ESqliteDBFixture *fixture,
+                                           gconstpointer     user_data);
+void     e_sqlitedb_fixture_teardown       (ESqliteDBFixture *fixture,
+                                           gconstpointer     user_data);
+
+
+gchar   *new_vcard_from_test_case          (const gchar *case_name);
+
+gboolean add_contact_from_test_case_verify (EBookClient *book_client,
+                                           const gchar *case_name,
+                                           EContact   **contact);
+
+
+#endif /* DATA_TEST_UTILS_H */
diff --git a/tests/libedata-book/test-sqlite-get-contact.c b/tests/libedata-book/test-sqlite-get-contact.c
new file mode 100644
index 0000000..6736567
--- /dev/null
+++ b/tests/libedata-book/test-sqlite-get-contact.c
@@ -0,0 +1,56 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+
+#include <stdlib.h>
+#include <locale.h>
+#include <libebook/libebook.h>
+
+#include "data-test-utils.h"
+
+static ETestServerClosure book_closure = { E_TEST_SERVER_ADDRESS_BOOK, NULL, 0 };
+
+static void
+test_get_contact (ESqliteDBFixture *fixture,
+                 gconstpointer     user_data)
+{
+       EBookClient *book_client;
+       EContact *contact = NULL;
+       EContact *other;
+       GError *error = NULL;
+
+       book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);
+
+       if (!add_contact_from_test_case_verify (book_client, "simple-1", &contact)) {
+               g_error ("Failed to get contact");
+       }
+
+       other = e_book_backend_sqlitedb_get_contact (fixture->ebsdb, SQLITEDB_FOLDER_ID,
+                                                    (const gchar *)e_contact_get_const (contact, 
E_CONTACT_UID),
+                                                    NULL, NULL, &error);
+
+       if (!other)
+               g_error ("Failed to get contact with uid '%s': %s",
+                        (const gchar *)e_contact_get_const (contact, E_CONTACT_UID),
+                        error->message);
+
+       g_object_unref (contact);
+       g_object_unref (other);
+}
+
+gint
+main (gint argc,
+      gchar **argv)
+{
+#if !GLIB_CHECK_VERSION (2, 35, 1)
+       g_type_init ();
+#endif
+       g_test_init (&argc, &argv, NULL);
+
+       /* Ensure that the client and server get the same locale */
+       g_assert (g_setenv ("LC_ALL", "en_US.UTF-8", TRUE));
+       setlocale (LC_ALL, "");
+
+       g_test_add ("/EBookBackendSqliteDB/GetContact", ESqliteDBFixture, &book_closure,
+                   e_sqlitedb_fixture_setup, test_get_contact, e_sqlitedb_fixture_teardown);
+
+       return e_test_server_utils_run ();
+}


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