1 /*********************************************************************
2  * test-vendor.c
3  * Test the vendor object.
4  *
5  * Copyright (c) 2001 Derek Atkins <warlord@MIT.EDU>
6  * Copyright (c) 2005 Neil Williams <linux@codehelp.co.uk>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, contact:
20  *
21  * Free Software Foundation           Voice:  +1-617-542-5942
22  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
23  * Boston, MA  02110-1301,  USA       gnu@gnu.org
24  *
25  *********************************************************************/
26 
27 #include <config.h>
28 #include <glib.h>
29 #include <qofinstance-p.h>
30 
31 #include "gncInvoiceP.h"
32 #include "gncCustomerP.h"
33 #include "gncJobP.h"
34 #include "gncVendorP.h"
35 #include "test-stuff.h"
36 
37 static int count = 0;
38 
39 static void
40 test_string_fcn (QofBook *book, const char *message,
41                  void (*set) (GncVendor *, const char *str),
42                  const char * (*get)(const GncVendor *));
43 
44 #if 0
45 static void
46 test_numeric_fcn (QofBook *book, const char *message,
47                   void (*set) (GncVendor *, gnc_numeric),
48                   gnc_numeric (*get)(const GncVendor *));
49 #endif
50 
51 static void
52 test_bool_fcn (QofBook *book, const char *message,
53                void (*set) (GncVendor *, gboolean),
54                gboolean (*get) (const GncVendor *));
55 
56 #if 0
57 static void
58 test_gint_fcn (QofBook *book, const char *message,
59                void (*set) (GncVendor *, gint),
60                gint (*get) (const GncVendor *));
61 #endif
62 
63 static void
test_vendor(void)64 test_vendor (void)
65 {
66     QofBook *book;
67     GncVendor *vendor;
68 
69     book = qof_book_new();
70 
71     /* Test creation/destruction */
72     {
73         do_test (gncVendorCreate (NULL) == NULL, "vendor create NULL");
74         vendor = gncVendorCreate (book);
75         do_test (vendor != NULL, "vendor create");
76         do_test (qof_instance_get_book (QOF_INSTANCE(vendor)) == book,
77                  "getbook");
78 
79         gncVendorBeginEdit (vendor);
80         gncVendorDestroy (vendor);
81         success ("create/destroy");
82     }
83 
84     /* Test setting/getting routines; does the active flag get set right? */
85     {
86         GncGUID guid;
87 
88         test_string_fcn (book, "Id", gncVendorSetID, gncVendorGetID);
89         test_string_fcn (book, "Name", gncVendorSetName, gncVendorGetName);
90         test_string_fcn (book, "Notes", gncVendorSetNotes, gncVendorGetNotes);
91 
92         //test_string_fcn (book, "Terms", gncVendorSetTerms, gncVendorGetTerms);
93 
94         //test_bool_fcn (book, "TaxIncluded", gncVendorSetTaxIncluded, gncVendorGetTaxIncluded);
95         test_bool_fcn (book, "Active", gncVendorSetActive, gncVendorGetActive);
96 
97         do_test (gncVendorGetAddr (vendor) != NULL, "Addr");
98 
99         guid_replace (&guid);
100         vendor = gncVendorCreate (book);
101         count++;
102         gncVendorSetGUID (vendor, &guid);
103         do_test (guid_equal (&guid, qof_instance_get_guid(QOF_INSTANCE(vendor))), "guid compare");
104     }
105     {
106         GList *list;
107 
108         list = gncBusinessGetList (book, GNC_ID_VENDOR, TRUE);
109         do_test (list != NULL, "getList all");
110         do_test (g_list_length (list) == count, "correct length: all");
111         g_list_free (list);
112 
113         list = gncBusinessGetList (book, GNC_ID_VENDOR, FALSE);
114         do_test (list != NULL, "getList active");
115         do_test (g_list_length (list) == 1, "correct length: active");
116         g_list_free (list);
117     }
118     {
119         const char *str = get_random_string();
120         const char *res;
121 
122         gncVendorSetName (vendor, str);
123         res = qof_object_printable (GNC_ID_VENDOR, vendor);
124         do_test (res != NULL, "Printable NULL?");
125         do_test (g_strcmp0 (str, res) == 0, "Printable equals");
126     }
127 
128     qof_book_destroy (book);
129 }
130 
131 static void
test_string_fcn(QofBook * book,const char * message,void (* set)(GncVendor *,const char * str),const char * (* get)(const GncVendor *))132 test_string_fcn (QofBook *book, const char *message,
133                  void (*set) (GncVendor *, const char *str),
134                  const char * (*get)(const GncVendor *))
135 {
136     GncVendor *vendor = gncVendorCreate (book);
137     char const *str = get_random_string ();
138 
139     do_test (!gncVendorIsDirty (vendor), "test if start dirty");
140     gncVendorBeginEdit (vendor);
141     set (vendor, str);
142     /* Vendor record should be dirty */
143     do_test (gncVendorIsDirty (vendor), "test dirty later");
144     gncVendorCommitEdit (vendor);
145     /* Vendor record should be not dirty */
146     /* Skip, because will always fail without a backend.
147      * It's not possible to load a backend in the engine code
148      * without having circular dependencies.
149      */
150     // do_test (!gncVendorIsDirty (vendor), "test dirty after commit");
151     do_test (g_strcmp0 (get (vendor), str) == 0, message);
152     gncVendorSetActive (vendor, FALSE);
153     count++;
154 }
155 
156 #if 0
157 static void
158 test_numeric_fcn (QofBook *book, const char *message,
159                   void (*set) (GncVendor *, gnc_numeric),
160                   gnc_numeric (*get)(const GncVendor *))
161 {
162     GncVendor *vendor = gncVendorCreate (book);
163     gnc_numeric num = gnc_numeric_create (17, 1);
164 
165     do_test (!gncVendorIsDirty (vendor), "test if start dirty");
166     gncVendoryBeginEdit (vendor);
167     set (vendor, num);
168     /* Vendor record should be dirty */
169     do_test (gncVendorIsDirty (vendor), "test dirty later");
170     gncVendorCommitEdit (vendor);
171     /* Vendor record should be not dirty */
172     /* Skip, because will always fail without a backend.
173      * It's not possible to load a backend in the engine code
174      * without having circular dependencies.
175      */
176     // do_test (!gncVendorIsDirty (vendor), "test dirty after commit");
177     do_test (gnc_numeric_equal (get (vendor), num), message);
178     gncVendorSetActive (vendor, FALSE);
179     count++;
180 }
181 #endif
182 
183 static void
test_bool_fcn(QofBook * book,const char * message,void (* set)(GncVendor *,gboolean),gboolean (* get)(const GncVendor *))184 test_bool_fcn (QofBook *book, const char *message,
185                void (*set) (GncVendor *, gboolean),
186                gboolean (*get) (const GncVendor *))
187 {
188     GncVendor *vendor = gncVendorCreate (book);
189     gboolean num = get_random_boolean ();
190 
191     do_test (!gncVendorIsDirty (vendor), "test if start dirty");
192     gncVendorBeginEdit (vendor);
193     set (vendor, FALSE);
194     set (vendor, TRUE);
195     set (vendor, num);
196     /* Vendor record should be dirty */
197     do_test (gncVendorIsDirty (vendor), "test dirty later");
198     gncVendorCommitEdit (vendor);
199     /* Vendor record should be not dirty */
200     /* Skip, because will always fail without a backend.
201      * It's not possible to load a backend in the engine code
202      * without having circular dependencies.
203      */
204     // do_test (!gncVendorIsDirty (vendor), "test dirty after commit");
205     do_test (get (vendor) == num, message);
206     gncVendorSetActive (vendor, FALSE);
207     count++;
208 }
209 
210 #if 0
211 static void
212 test_gint_fcn (QofBook *book, const char *message,
213                void (*set) (GncVendor *, gint),
214                gint (*get) (const GncVendor *))
215 {
216     GncVendor *vendor = gncVendorCreate (book);
217     gint num = 17;
218 
219     do_test (!gncVendorIsDirty (vendor), "test if start dirty");
220     gncVendorBeginEdit (vendor);
221     set (vendor, num);
222     /* Vendor record should be dirty */
223     do_test (gncVendorIsDirty (vendor), "test dirty later");
224     gncVendorCommitEdit (vendor);
225     /* Vendor record should be not dirty */
226     /* Skip, because will always fail without a backend.
227      * It's not possible to load a backend in the engine code
228      * without having circular dependencies.
229      */
230     // do_test (!gncVendorIsDirty (vendor), "test dirty after commit");
231     do_test (get (vendor) == num, message);
232     gncVendorSetActive (vendor, FALSE);
233     count++;
234 }
235 #endif
236 
237 int
main(int argc,char ** argv)238 main (int argc, char **argv)
239 {
240     qof_init();
241     do_test (gncInvoiceRegister(), "Cannot register GncInvoice");
242     do_test (gncJobRegister (),  "Cannot register GncJob");
243     do_test (gncCustomerRegister(), "Cannot register GncCustomer");
244     do_test (gncVendorRegister(), "Cannot register GncVendor");
245     test_vendor();
246     print_test_results();
247     qof_close();
248     return get_rv();
249 }
250