GnuCash 2.4.99
test-customer.c
00001 /*********************************************************************
00002  * test-customer.c
00003  * Test the customer object (without Guile/Scheme)
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 "cashobjects.h"
00031 #include "gncCustomerP.h"
00032 #include "gncInvoiceP.h"
00033 #include "gncJobP.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) (GncCustomer *, const char *str),
00041                  const char * (*get)(const GncCustomer *));
00042 
00043 static void
00044 test_numeric_fcn (QofBook *book, const char *message,
00045                   void (*set) (GncCustomer *, gnc_numeric),
00046                   gnc_numeric (*get)(const GncCustomer *));
00047 
00048 static void
00049 test_bool_fcn (QofBook *book, const char *message,
00050                void (*set) (GncCustomer *, gboolean),
00051                gboolean (*get) (const GncCustomer *));
00052 
00053 static void
00054 test_customer (void)
00055 {
00056     QofBook *book;
00057     GncCustomer *customer;
00058 
00059     book = qof_book_new ();
00060 
00061     /* Test creation/destruction */
00062     {
00063         do_test (gncCustomerCreate (NULL) == NULL, "customer create NULL");
00064         customer = gncCustomerCreate (book);
00065         do_test (customer != NULL, "customer create");
00066         do_test (gncCustomerGetBook (customer) == book, "getbook");
00067 
00068         gncCustomerBeginEdit (customer);
00069         gncCustomerDestroy (customer);
00070         success ("create/destroy");
00071     }
00072 
00073     /* Test setting/getting routines; does the active flag get set right? */
00074     {
00075         GncGUID guid;
00076 
00077         test_string_fcn (book, "Id", gncCustomerSetID, gncCustomerGetID);
00078         test_string_fcn (book, "Name", gncCustomerSetName, gncCustomerGetName);
00079         test_string_fcn (book, "Notes", gncCustomerSetNotes, gncCustomerGetNotes);
00080 
00081         //test_string_fcn (book, "Terms", gncCustomerSetTerms, gncCustomerGetTerms);
00082 
00083         test_numeric_fcn (book, "Discount", gncCustomerSetDiscount, gncCustomerGetDiscount);
00084         test_numeric_fcn (book, "Credit", gncCustomerSetCredit, gncCustomerGetCredit);
00085 
00086         test_bool_fcn (book, "Active", gncCustomerSetActive, gncCustomerGetActive);
00087 
00088         do_test (gncCustomerGetAddr (customer) != NULL, "Addr");
00089         do_test (gncCustomerGetShipAddr (customer) != NULL, "ShipAddr");
00090 
00091         guid_new (&guid);
00092         customer = gncCustomerCreate (book);
00093         count++;
00094         gncCustomerSetGUID (customer, &guid);
00095         do_test (guid_equal (&guid, gncCustomerGetGUID (customer)), "guid compare");
00096     }
00097     {
00098         GList *list;
00099 
00100         list = gncBusinessGetList (book, GNC_ID_CUSTOMER, TRUE);
00101         do_test (list != NULL, "getList all");
00102         do_test (g_list_length (list) == count, "correct length: all");
00103         g_list_free (list);
00104 
00105         list = gncBusinessGetList (book, GNC_ID_CUSTOMER, FALSE);
00106         do_test (list != NULL, "getList active");
00107         do_test (g_list_length (list) == 1, "correct length: active");
00108         g_list_free (list);
00109     }
00110     {
00111         const char *str = get_random_string();
00112         const char *res;
00113 
00114         res = NULL;
00115         gncCustomerBeginEdit(customer);
00116         gncCustomerSetName (customer, str);
00117         gncCustomerCommitEdit(customer);
00118         res = qof_object_printable (GNC_ID_CUSTOMER, customer);
00119         do_test (res != NULL, "Printable NULL?");
00120         do_test (safe_strcmp (str, res) == 0, "Printable equals");
00121     }
00122 
00123     do_test (gncCustomerGetJoblist (customer, TRUE) == NULL, "joblist empty");
00124 
00125     /* Test the Entity Table */
00126     {
00127         const GncGUID *guid;
00128 
00129         guid = gncCustomerGetGUID (customer);
00130         do_test (gncCustomerLookup (book, guid) == customer, "Entity Table");
00131     }
00132 
00133     /* Note: JobList is tested from the Job tests */
00134     qof_book_destroy (book);
00135 }
00136 
00137 static void
00138 test_string_fcn (QofBook *book, const char *message,
00139                  void (*set) (GncCustomer *, const char *str),
00140                  const char * (*get)(const GncCustomer *))
00141 {
00142     GncCustomer *customer = gncCustomerCreate (book);
00143     char const *str = get_random_string ();
00144 
00145     do_test (!gncCustomerIsDirty (customer), "test if start dirty");
00146     gncCustomerBeginEdit (customer);
00147     set (customer, str);
00148     /* Customer record should be dirty */
00149     do_test (gncCustomerIsDirty (customer), "test dirty later");
00150     gncCustomerCommitEdit (customer);
00151     /* Customer record should be not dirty */
00152     /* Skip, because will always fail without a backend.
00153      * It's not possible to load a backend in the engine code
00154      * without having circular dependencies.
00155      */
00156     // do_test (!gncCustomerIsDirty (customer), "test dirty after commit");
00157     do_test (safe_strcmp (get (customer), str) == 0, message);
00158     gncCustomerSetActive (customer, FALSE);
00159     count++;
00160 }
00161 
00162 static void
00163 test_numeric_fcn (QofBook *book, const char *message,
00164                   void (*set) (GncCustomer *, gnc_numeric),
00165                   gnc_numeric (*get)(const GncCustomer *))
00166 {
00167     GncCustomer *customer = gncCustomerCreate (book);
00168     gnc_numeric num = gnc_numeric_create (17, 1);
00169 
00170     do_test (!gncCustomerIsDirty (customer), "test if start dirty");
00171     gncCustomerBeginEdit (customer);
00172     set (customer, num);
00173     /* Customer record should be dirty */
00174     do_test (gncCustomerIsDirty (customer), "test dirty later");
00175     gncCustomerCommitEdit (customer);
00176     /* Customer record should be not dirty */
00177     /* Skip, because will always fail without a backend.
00178      * It's not possible to load a backend in the engine code
00179      * without having circular dependencies.
00180      */
00181     // do_test (!gncCustomerIsDirty (customer), "test dirty after commit");
00182     do_test (gnc_numeric_equal (get (customer), num), message);
00183     gncCustomerSetActive (customer, FALSE);
00184     count++;
00185 }
00186 
00187 static void
00188 test_bool_fcn (QofBook *book, const char *message,
00189                void (*set) (GncCustomer *, gboolean),
00190                gboolean (*get) (const GncCustomer *))
00191 {
00192     GncCustomer *customer = gncCustomerCreate (book);
00193     gboolean num = get_random_boolean ();
00194 
00195     do_test (!gncCustomerIsDirty (customer), "test if start dirty");
00196     gncCustomerBeginEdit (customer);
00197     set (customer, FALSE);
00198     set (customer, TRUE);
00199     set (customer, num);
00200     /* Customer record should be dirty */
00201     do_test (gncCustomerIsDirty (customer), "test dirty later");
00202     gncCustomerCommitEdit (customer);
00203     /* Customer record should be not dirty */
00204     /* Skip, because will always fail without a backend.
00205      * It's not possible to load a backend in the engine code
00206      * without having circular dependencies.
00207      */
00208     // do_test (!gncCustomerIsDirty (customer), "test dirty after commit");
00209     do_test (get (customer) == num, message);
00210     gncCustomerSetActive (customer, FALSE);
00211     count++;
00212 }
00213 
00214 int
00215 main (int argc, char **argv)
00216 {
00217     qof_init();
00218     do_test (cashobjects_register(), "Cannot register cash objects");
00219     /* These three registrations are done during cashobjects_register,
00220        so trying to register them again naturally fails. */
00221 #if 0
00222     do_test (gncInvoiceRegister(), "Cannot register GncInvoice");
00223     do_test (gncJobRegister (),  "Cannot register GncJob");
00224     do_test (gncCustomerRegister(), "Cannot register GncCustomer");
00225 #endif
00226     test_customer();
00227     print_test_results();
00228     qof_close ();
00229     return get_rv();
00230 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines