GnuCash 2.4.99
test-job.c
00001 /*********************************************************************
00002  * test-job.c
00003  * Test the job object.
00004  *
00005  * Copyright (c) 2001 Derek Atkins <warlord@MIT.EDU>
00006  * Copyright (c) 2005 Neil Williams <linux@codehelp.co.uk>
00007  *
00008  * This program is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU General Public License as
00010  * published by the Free Software Foundation; either version 2 of
00011  * the License, or (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, contact:
00020  *
00021  * Free Software Foundation           Voice:  +1-617-542-5942
00022  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
00023  * Boston, MA  02110-1301,  USA       gnu@gnu.org
00024  *
00025  *********************************************************************/
00026 
00027 #include "config.h"
00028 #include <glib.h>
00029 #include "qof.h"
00030 #include "gncJobP.h"
00031 #include "gncInvoiceP.h"
00032 #include "gncCustomerP.h"
00033 #include "gncOwner.h"
00034 #include "test-stuff.h"
00035 
00036 static int count = 0;
00037 
00038 static void
00039 test_string_fcn (QofBook *book, const char *message,
00040                  void (*set) (GncJob *, const char *str),
00041                  const char * (*get)(const GncJob *));
00042 
00043 #if 0
00044 static void
00045 test_numeric_fcn (QofBook *book, const char *message,
00046                   void (*set) (GncJob *, gnc_numeric),
00047                   gnc_numeric (*get)(const GncJob *));
00048 #endif
00049 
00050 static void
00051 test_bool_fcn (QofBook *book, const char *message,
00052                void (*set) (GncJob *, gboolean),
00053                gboolean (*get) (const GncJob *));
00054 
00055 #if 0
00056 static void
00057 test_gint_fcn (QofBook *book, const char *message,
00058                void (*set) (GncJob *, gint),
00059                gint (*get) (const GncJob *));
00060 #endif
00061 
00062 static void
00063 test_job (void)
00064 {
00065     QofBook *book;
00066     GncJob *job;
00067 
00068     book = qof_book_new();
00069 
00070     /* Test creation/destruction */
00071     {
00072         do_test (gncJobCreate (NULL) == NULL, "job create NULL");
00073         job = gncJobCreate (book);
00074         do_test (job != NULL, "job create");
00075         do_test (qof_instance_get_book(QOF_INSTANCE(job)) == book,
00076                  "getbook");
00077 
00078         gncJobBeginEdit (job);
00079         gncJobDestroy (job);
00080         success ("create/destroy");
00081     }
00082 
00083     /* Test setting/getting routines; does the active flag get set right? */
00084     {
00085         GncGUID guid;
00086 
00087         test_string_fcn (book, "Id", gncJobSetID, gncJobGetID);
00088         test_string_fcn (book, "Name", gncJobSetName, gncJobGetName);
00089         test_string_fcn (book, "Reference", gncJobSetReference, gncJobGetReference);
00090 
00091         test_bool_fcn (book, "Active", gncJobSetActive, gncJobGetActive);
00092 
00093         guid_new (&guid);
00094         job = gncJobCreate (book);
00095         count++;
00096         gncJobSetGUID (job, &guid);
00097         do_test (guid_equal (&guid, qof_instance_get_guid(QOF_INSTANCE(job))), "guid compare");
00098     }
00099 #if 0
00100     {
00101         GList *list;
00102 
00103         list = gncBusinessGetList (book, GNC_ID_JOB, TRUE);
00104         do_test (list != NULL, "getList all");
00105         do_test (g_list_length (list) == count, "correct length: all");
00106         g_list_free (list);
00107 
00108         list = gncBusinessGetList (book, GNC_ID_JOB, FALSE);
00109         do_test (list != NULL, "getList active");
00110         do_test (g_list_length (list) == 1, "correct length: active");
00111         g_list_free (list);
00112     }
00113 #endif
00114     {
00115         const char *str = get_random_string();
00116         const char *res;
00117 
00118         gncJobSetName (job, str);
00119         res = qof_object_printable (GNC_ID_JOB, job);
00120         do_test (res != NULL, "Printable NULL?");
00121         do_test (safe_strcmp (str, res) == 0, "Printable equals");
00122     }
00123     {
00124         GList *list;
00125         GncOwner owner;
00126         GncCustomer *cust = gncCustomerCreate (book);
00127 
00128         gncOwnerInitCustomer (&owner, cust);
00129 
00130         do_test (gncCustomerGetJoblist (cust, TRUE) == NULL, "empty list at start");
00131         gncJobSetOwner (job, &owner);
00132         list = gncCustomerGetJoblist (cust, FALSE);
00133         do_test (list != NULL, "added to cust");
00134         do_test (g_list_length (list) == 1, "correct joblist length");
00135         do_test (list->data == job, "verify job in list");
00136         gncJobSetActive (job, FALSE);
00137         list = gncCustomerGetJoblist (cust, FALSE);
00138         do_test (list == NULL, "no active jobs");
00139         list = gncCustomerGetJoblist (cust, TRUE);
00140         do_test (list != NULL, "all jobs");
00141         gncJobBeginEdit (job);
00142         gncJobDestroy (job);
00143         list = gncCustomerGetJoblist (cust, TRUE);
00144         do_test (list == NULL, "no more jobs");
00145     }
00146 
00147     qof_book_destroy (book);
00148 }
00149 
00150 static void
00151 test_string_fcn (QofBook *book, const char *message,
00152                  void (*set) (GncJob *, const char *str),
00153                  const char * (*get)(const GncJob *))
00154 {
00155     GncJob *job = gncJobCreate (book);
00156     char const *str = get_random_string ();
00157 
00158     do_test (!qof_instance_is_dirty (QOF_INSTANCE(job)), "test if start dirty");
00159     gncJobBeginEdit (job);
00160     set (job, str);
00161     /* Job record should be dirty */
00162     do_test (qof_instance_is_dirty (QOF_INSTANCE(job)), "test dirty later");
00163     gncJobCommitEdit (job);
00164     /* Job record should be not dirty */
00165     /* Skip, because will always fail without a backend.
00166      * It's not possible to load a backend in the engine code
00167      * without having circular dependencies.
00168      */
00169     // do_test (!qof_instance_is_dirty (QOF_INSTANCE(job)), "test dirty after commit");
00170     do_test (safe_strcmp (get (job), str) == 0, message);
00171     gncJobSetActive (job, FALSE);
00172     count++;
00173 }
00174 
00175 #if 0
00176 static void
00177 test_numeric_fcn (QofBook *book, const char *message,
00178                   void (*set) (GncJob *, gnc_numeric),
00179                   gnc_numeric (*get)(const GncJob *))
00180 {
00181     GncJob *job = gncJobCreate (book);
00182     gnc_numeric num = gnc_numeric_create (17, 1);
00183 
00184     do_test (!qof_instance_is_dirty (QOF_INSTANCE(job)), "test if start dirty");
00185     gncJobBeginEdit (job);
00186     set (job, num);
00187     /* Job record should be dirty */
00188     do_test (qof_instance_is_dirty (QOF_INSTANCE(job)), "test dirty later");
00189     gncJobCommitEdit (job);
00190     /* Job record should be not dirty */
00191     /* Skip, because will always fail without a backend.
00192      * It's not possible to load a backend in the engine code
00193      * without having circular dependencies.
00194      */
00195     // do_test (!qof_instance_is_dirty (QOF_INSTANCE(job)), "test dirty after commit");
00196     do_test (gnc_numeric_equal (get (job), num), message);
00197     gncJobSetActive (job, FALSE);
00198     count++;
00199 }
00200 #endif
00201 
00202 static void
00203 test_bool_fcn (QofBook *book, const char *message,
00204                void (*set) (GncJob *, gboolean),
00205                gboolean (*get) (const GncJob *))
00206 {
00207     GncJob *job = gncJobCreate (book);
00208     gboolean num = get_random_boolean ();
00209 
00210     do_test (!qof_instance_is_dirty (QOF_INSTANCE(job)), "test if start dirty");
00211     gncJobBeginEdit (job);
00212     set (job, FALSE);
00213     set (job, TRUE);
00214     set (job, num);
00215     /* Job record should be dirty */
00216     do_test (qof_instance_is_dirty (QOF_INSTANCE(job)), "test dirty later");
00217     gncJobCommitEdit (job);
00218     /* Job record should be not dirty */
00219     /* Skip, because will always fail without a backend.
00220      * It's not possible to load a backend in the engine code
00221      * without having circular dependencies.
00222      */
00223     // do_test (!qof_instance_is_dirty (QOF_INSTANCE(job)), "test dirty after commit");
00224     do_test (get (job) == num, message);
00225     gncJobSetActive (job, FALSE);
00226     count++;
00227 }
00228 
00229 #if 0
00230 static void
00231 test_gint_fcn (QofBook *book, const char *message,
00232                void (*set) (GncJob *, gint),
00233                gint (*get) (const GncJob *))
00234 {
00235     GncJob *job = gncJobCreate (book);
00236     gint num = 17;
00237 
00238     do_test (!qof_instance_is_dirty (QOF_INSTANCE(job)), "test if start dirty");
00239     gncJobBeginEdit (job);
00240     set (job, num);
00241     /* Job record should be dirty */
00242     do_test (qof_instance_is_dirty (QOF_INSTANCE(job)), "test dirty later");
00243     gncJobCommitEdit (job);
00244     /* Job record should be not dirty */
00245     /* Skip, because will always fail without a backend.
00246      * It's not possible to load a backend in the engine code
00247      * without having circular dependencies.
00248      */
00249     // do_test (!qof_instance_is_dirty (QOF_INSTANCE(job)), "test dirty after commit");
00250     do_test (get (job) == num, message);
00251     gncJobSetActive (job, FALSE);
00252     count++;
00253 }
00254 #endif
00255 
00256 int
00257 main (int argc, char **argv)
00258 {
00259     qof_init();
00260     do_test (gncInvoiceRegister(), "Cannot register GncInvoice");
00261     do_test (gncJobRegister (),  "Cannot register GncJob");
00262     do_test (gncCustomerRegister(), "Cannot register GncCustomer");
00263     test_job();
00264     print_test_results();
00265     qof_close();
00266     return get_rv();
00267 }
00268 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines