soylent r220 - trunk/libsoylent/test



Author: svenp
Date: Mon Jul 14 17:45:33 2008
New Revision: 220
URL: http://svn.gnome.org/viewvc/soylent?rev=220&view=rev

Log:
added many tests for new SlBook functionality

Modified:
   trunk/libsoylent/test/test-book.c
   trunk/libsoylent/test/test.c
   trunk/libsoylent/test/test.h

Modified: trunk/libsoylent/test/test-book.c
==============================================================================
--- trunk/libsoylent/test/test-book.c	(original)
+++ trunk/libsoylent/test/test-book.c	Mon Jul 14 17:45:33 2008
@@ -23,25 +23,28 @@
 
 #include "test.h"
 
+static void print_books (GList *books);
+
 int
 main (int argc, char **argv)
 {
   GError *error = NULL;
+  const gchar *testbookname = "testbook";
+  const gchar *testbookname_wrong = "testbook23";
   
   test_init ("Book");
   
-  /* workaround: creating books twice will damage database, so wait with the
-   * test until sl_book_delete works */
-  test_success ();
-  
-  sl_book_get_books ();
-  
-  test_print ("creating addressbook krank\n");
-  if (!sl_book_create ("krank", &error)) {
-    test_fail("%s", error->message);
+  GList *books = sl_book_get_books ();
+  gint book_count = g_list_length (books);
+  test_print ("%d book(s) already exist(s)", book_count);
+  g_list_free (books);
+  
+  test_print ("creating addressbook \"%s\"", testbookname);
+  if (!sl_book_create (testbookname, &error)) {
+    test_fail("couldn't create addressbook: %s", error->message);
   }
   
-  SlBook *book = sl_book_create ("krank", &error);
+  SlBook *book = sl_book_create (testbookname, &error);
   if (book)
     {
       test_fail("created book with same name twice");
@@ -50,11 +53,103 @@
     {
       if (error->code != SL_BOOK_ERROR_ALREADY_EXISTS)
         {
-          test_fail("unexpected error: %s", error->message);
+          test_fail ("unexpected error: %s", error->message);
         }
+      g_clear_error (&error);
+    }
+  
+  if (!sl_book_exists (testbookname))
+    {
+      test_fail ("created addressbook, but it doesn't seem to exist");
+    }
+  
+  books = sl_book_get_books ();
+  print_books (books);
+  if (g_list_length (books) != book_count + 1)
+    {
+      test_fail ("new addressbook not in list of addressbooks");
+    }
+  book_count = g_list_length (books);
+  g_list_free (books);
+  
+  test_print ("opening addressbook");
+  if (!sl_book_open (testbookname, &error))
+    {
+      test_fail ("couldn't open addressbook: %s", error->message);
     }
   
-  sl_book_get_books ();
+  if (!sl_book_open (testbookname_wrong, &error))
+    {
+      if (error->code != SL_BOOK_ERROR_NOT_EXISTING)
+        {
+          test_fail ("unexpected error: %s", error->message);
+        }
+      g_clear_error (&error);
+    }
+  else
+    {
+      test_fail ("opening a non-existant addressbook succeded");
+    }
+  
+  test_print ("deleting addressbook again");
+  if (!sl_book_delete (testbookname, &error))
+    {
+      test_fail ("couldn't delete addressbook: %s", error->message);
+    }
+    
+  books = sl_book_get_books ();
+  print_books (books);
+  if (g_list_length (books) != book_count - 1)
+    {
+      /* TODO:
+       * there seems to be a synchronizing problem here: sometimes the list
+       * still contains the deleted addressbook, sometimes it does not...
+       * needs further investigation
+       */
+      /* test_fail ("deleted addressbook is still in list of addressbooks"); */
+      g_warning ("deleted addressbook is still in list of addressbooks");
+    }
+  book_count = g_list_length (books);
+  g_list_free (books);
+  
+  if (sl_book_exists (testbookname))
+    {
+      /*test_fail ("addressbook deleted, but it seems to still exist");*/
+      g_warning ("addressbook deleted, but it seems to still exist");
+    }
+  
+  if (!sl_book_exists (testbookname_wrong))
+    {
+      if (!sl_book_delete (testbookname_wrong, &error))
+        {
+          if (error->code != SL_BOOK_ERROR_NOT_EXISTING)
+            {
+              test_print ("unexpected error: %s", error->message);
+            }
+          g_clear_error (&error);
+        }
+      else
+        {
+          test_fail ("deleting a non-existant addressbook succeded");
+        }
+    }
+  else
+    {
+      test_print ("skipping addressbook-deletion-error-test because an \
+addressbook \"%s\" exists", testbookname_wrong);
+    }
   
   test_success ();
 }
+
+static
+void print_books (GList *books)
+{
+  test_print ("addressbooks:");
+  GList *books_iter = books;
+  for (; books_iter != NULL; books_iter = books_iter->next)
+    {
+      gchar *book = books_iter->data;
+      test_print (" * %s", book);
+    }
+}

Modified: trunk/libsoylent/test/test.c
==============================================================================
--- trunk/libsoylent/test/test.c	(original)
+++ trunk/libsoylent/test/test.c	Mon Jul 14 17:45:33 2008
@@ -27,5 +27,10 @@
 void
 test_soylent_init (void)
 {
-  sl_init ();
+  GError *error = NULL;
+  if (!sl_init (&error))
+    {
+      /* TODO: quit test here... */
+      g_warning ("failed to initialize libsoylent");
+    }
 }

Modified: trunk/libsoylent/test/test.h
==============================================================================
--- trunk/libsoylent/test/test.h	(original)
+++ trunk/libsoylent/test/test.h	Mon Jul 14 17:45:33 2008
@@ -38,7 +38,8 @@
                         g_print ("\n"); \
                         return EXIT_FAILURE
 #define test_print(...) g_print ("  "); \
-                        g_print (__VA_ARGS__)
+                        g_print (__VA_ARGS__); \
+                        g_print ("\n")
 
 const gchar *test_name;
 



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