GnuCash 2.4.99
test-vendor.c
00001 /*********************************************************************
00002  * test-vendor.c
00003  * Test the vendor 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 "gncInvoiceP.h"
00030 #include "gncCustomerP.h"
00031 #include "gncJobP.h"
00032 #include "gncVendorP.h"
00033 #include "test-stuff.h"
00034 
00035 static int count = 0;
00036 
00037 static void
00038 test_string_fcn (QofBook *book, const char *message,
00039                  void (*set) (GncVendor *, const char *str),
00040                  const char * (*get)(const GncVendor *));
00041 
00042 #if 0
00043 static void
00044 test_numeric_fcn (QofBook *book, const char *message,
00045                   void (*set) (GncVendor *, gnc_numeric),
00046                   gnc_numeric (*get)(const GncVendor *));
00047 #endif
00048 
00049 static void
00050 test_bool_fcn (QofBook *book, const char *message,
00051                void (*set) (GncVendor *, gboolean),
00052                gboolean (*get) (const GncVendor *));
00053 
00054 #if 0
00055 static void
00056 test_gint_fcn (QofBook *book, const char *message,
00057                void (*set) (GncVendor *, gint),
00058                gint (*get) (const GncVendor *));
00059 #endif
00060 
00061 static void
00062 test_vendor (void)
00063 {
00064     QofBackend *be;
00065     QofSession *session;
00066     QofBook *book;
00067     GncVendor *vendor;
00068 
00069     book = qof_book_new();
00070 
00071     /* Test creation/destruction */
00072     {
00073         do_test (gncVendorCreate (NULL) == NULL, "vendor create NULL");
00074         vendor = gncVendorCreate (book);
00075         do_test (vendor != NULL, "vendor create");
00076         do_test (qof_instance_get_book (QOF_INSTANCE(vendor)) == book,
00077                  "getbook");
00078 
00079         gncVendorBeginEdit (vendor);
00080         gncVendorDestroy (vendor);
00081         success ("create/destroy");
00082     }
00083 
00084     /* Test setting/getting routines; does the active flag get set right? */
00085     {
00086         GncGUID guid;
00087 
00088         test_string_fcn (book, "Id", gncVendorSetID, gncVendorGetID);
00089         test_string_fcn (book, "Name", gncVendorSetName, gncVendorGetName);
00090         test_string_fcn (book, "Notes", gncVendorSetNotes, gncVendorGetNotes);
00091 
00092         //test_string_fcn (book, "Terms", gncVendorSetTerms, gncVendorGetTerms);
00093 
00094         //test_bool_fcn (book, "TaxIncluded", gncVendorSetTaxIncluded, gncVendorGetTaxIncluded);
00095         test_bool_fcn (book, "Active", gncVendorSetActive, gncVendorGetActive);
00096 
00097         do_test (gncVendorGetAddr (vendor) != NULL, "Addr");
00098 
00099         guid_new (&guid);
00100         vendor = gncVendorCreate (book);
00101         count++;
00102         gncVendorSetGUID (vendor, &guid);
00103         do_test (guid_equal (&guid, qof_instance_get_guid(QOF_INSTANCE(vendor))), "guid compare");
00104     }
00105     {
00106         GList *list;
00107 
00108         list = gncBusinessGetList (book, GNC_ID_VENDOR, TRUE);
00109         do_test (list != NULL, "getList all");
00110         do_test (g_list_length (list) == count, "correct length: all");
00111         g_list_free (list);
00112 
00113         list = gncBusinessGetList (book, GNC_ID_VENDOR, FALSE);
00114         do_test (list != NULL, "getList active");
00115         do_test (g_list_length (list) == 1, "correct length: active");
00116         g_list_free (list);
00117     }
00118     {
00119         const char *str = get_random_string();
00120         const char *res;
00121 
00122         gncVendorSetName (vendor, str);
00123         res = qof_object_printable (GNC_ID_VENDOR, vendor);
00124         do_test (res != NULL, "Printable NULL?");
00125         do_test (safe_strcmp (str, res) == 0, "Printable equals");
00126     }
00127 
00128     qof_book_destroy (book);
00129 }
00130 
00131 static void
00132 test_string_fcn (QofBook *book, const char *message,
00133                  void (*set) (GncVendor *, const char *str),
00134                  const char * (*get)(const GncVendor *))
00135 {
00136     GncVendor *vendor = gncVendorCreate (book);
00137     char const *str = get_random_string ();
00138 
00139     do_test (!gncVendorIsDirty (vendor), "test if start dirty");
00140     gncVendorBeginEdit (vendor);
00141     set (vendor, str);
00142     /* Vendor record should be dirty */
00143     do_test (gncVendorIsDirty (vendor), "test dirty later");
00144     gncVendorCommitEdit (vendor);
00145     /* Vendor record should be not dirty */
00146     /* Skip, because will always fail without a backend.
00147      * It's not possible to load a backend in the engine code
00148      * without having circular dependencies.
00149      */
00150     // do_test (!gncVendorIsDirty (vendor), "test dirty after commit");
00151     do_test (safe_strcmp (get (vendor), str) == 0, message);
00152     gncVendorSetActive (vendor, FALSE);
00153     count++;
00154 }
00155 
00156 #if 0
00157 static void
00158 test_numeric_fcn (QofBook *book, const char *message,
00159                   void (*set) (GncVendor *, gnc_numeric),
00160                   gnc_numeric (*get)(const GncVendor *))
00161 {
00162     GncVendor *vendor = gncVendorCreate (book);
00163     gnc_numeric num = gnc_numeric_create (17, 1);
00164 
00165     do_test (!gncVendorIsDirty (vendor), "test if start dirty");
00166     gncVendoryBeginEdit (vendor);
00167     set (vendor, num);
00168     /* Vendor record should be dirty */
00169     do_test (gncVendorIsDirty (vendor), "test dirty later");
00170     gncVendorCommitEdit (vendor);
00171     /* Vendor record should be not dirty */
00172     /* Skip, because will always fail without a backend.
00173      * It's not possible to load a backend in the engine code
00174      * without having circular dependencies.
00175      */
00176     // do_test (!gncVendorIsDirty (vendor), "test dirty after commit");
00177     do_test (gnc_numeric_equal (get (vendor), num), message);
00178     gncVendorSetActive (vendor, FALSE);
00179     count++;
00180 }
00181 #endif
00182 
00183 static void
00184 test_bool_fcn (QofBook *book, const char *message,
00185                void (*set) (GncVendor *, gboolean),
00186                gboolean (*get) (const GncVendor *))
00187 {
00188     GncVendor *vendor = gncVendorCreate (book);
00189     gboolean num = get_random_boolean ();
00190 
00191     do_test (!gncVendorIsDirty (vendor), "test if start dirty");
00192     gncVendorBeginEdit (vendor);
00193     set (vendor, FALSE);
00194     set (vendor, TRUE);
00195     set (vendor, num);
00196     /* Vendor record should be dirty */
00197     do_test (gncVendorIsDirty (vendor), "test dirty later");
00198     gncVendorCommitEdit (vendor);
00199     /* Vendor record should be not dirty */
00200     /* Skip, because will always fail without a backend.
00201      * It's not possible to load a backend in the engine code
00202      * without having circular dependencies.
00203      */
00204     // do_test (!gncVendorIsDirty (vendor), "test dirty after commit");
00205     do_test (get (vendor) == num, message);
00206     gncVendorSetActive (vendor, FALSE);
00207     count++;
00208 }
00209 
00210 #if 0
00211 static void
00212 test_gint_fcn (QofBook *book, const char *message,
00213                void (*set) (GncVendor *, gint),
00214                gint (*get) (const GncVendor *))
00215 {
00216     GncVendor *vendor = gncVendorCreate (book);
00217     gint num = 17;
00218 
00219     do_test (!gncVendorIsDirty (vendor), "test if start dirty");
00220     gncVendorBeginEdit (vendor);
00221     set (vendor, num);
00222     /* Vendor record should be dirty */
00223     do_test (gncVendorIsDirty (vendor), "test dirty later");
00224     gncVendorCommitEdit (vendor);
00225     /* Vendor record should be not dirty */
00226     /* Skip, because will always fail without a backend.
00227      * It's not possible to load a backend in the engine code
00228      * without having circular dependencies.
00229      */
00230     // do_test (!gncVendorIsDirty (vendor), "test dirty after commit");
00231     do_test (get (vendor) == num, message);
00232     gncVendorSetActive (vendor, FALSE);
00233     count++;
00234 }
00235 #endif
00236 
00237 int
00238 main (int argc, char **argv)
00239 {
00240     qof_init();
00241     do_test (gncInvoiceRegister(), "Cannot register GncInvoice");
00242     do_test (gncJobRegister (),  "Cannot register GncJob");
00243     do_test (gncCustomerRegister(), "Cannot register GncCustomer");
00244     do_test (gncVendorRegister(), "Cannot register GncVendor");
00245     test_vendor();
00246     print_test_results();
00247     qof_close();
00248     return get_rv();
00249 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines