1 /*********************************************************************
2  * test-job.c
3  * Test the job 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 <qof.h>
30 #include <qofinstance-p.h>
31 
32 #include "gncJobP.h"
33 #include "gncInvoiceP.h"
34 #include "gncCustomerP.h"
35 #include "gncOwner.h"
36 #include "test-stuff.h"
37 
38 static int count = 0;
39 
40 static void
41 test_string_fcn (QofBook *book, const char *message,
42                  void (*set) (GncJob *, const char *str),
43                  const char * (*get)(const GncJob *));
44 
45 static void
46 test_numeric_fcn (QofBook *book, const char *message,
47                   void (*set) (GncJob *, gnc_numeric),
48                   gnc_numeric (*get)(const GncJob *));
49 
50 static void
51 test_bool_fcn (QofBook *book, const char *message,
52                void (*set) (GncJob *, gboolean),
53                gboolean (*get) (const GncJob *));
54 
55 #if 0
56 static void
57 test_gint_fcn (QofBook *book, const char *message,
58                void (*set) (GncJob *, gint),
59                gint (*get) (const GncJob *));
60 #endif
61 
62 static void
test_job(void)63 test_job (void)
64 {
65     QofBook *book;
66     GncJob *job;
67 
68     book = qof_book_new();
69 
70     /* Test creation/destruction */
71     {
72         do_test (gncJobCreate (NULL) == NULL, "job create NULL");
73         job = gncJobCreate (book);
74         do_test (job != NULL, "job create");
75         do_test (qof_instance_get_book(QOF_INSTANCE(job)) == book,
76                  "getbook");
77 
78         gncJobBeginEdit (job);
79         gncJobDestroy (job);
80         success ("create/destroy");
81     }
82 
83     /* Test setting/getting routines; does the active flag get set right? */
84     {
85         GncGUID guid;
86 
87         test_string_fcn (book, "Id", gncJobSetID, gncJobGetID);
88         test_string_fcn (book, "Name", gncJobSetName, gncJobGetName);
89         test_string_fcn (book, "Reference", gncJobSetReference, gncJobGetReference);
90         test_numeric_fcn (book, "Rate", gncJobSetRate, gncJobGetRate);
91 
92         test_bool_fcn (book, "Active", gncJobSetActive, gncJobGetActive);
93 
94         guid_replace (&guid);
95         job = gncJobCreate (book);
96         count++;
97         gncJobSetGUID (job, &guid);
98         do_test (guid_equal (&guid, qof_instance_get_guid(QOF_INSTANCE(job))), "guid compare");
99     }
100 #if 0
101     {
102         GList *list;
103 
104         list = gncBusinessGetList (book, GNC_ID_JOB, TRUE);
105         do_test (list != NULL, "getList all");
106         do_test (g_list_length (list) == count, "correct length: all");
107         g_list_free (list);
108 
109         list = gncBusinessGetList (book, GNC_ID_JOB, FALSE);
110         do_test (list != NULL, "getList active");
111         do_test (g_list_length (list) == 1, "correct length: active");
112         g_list_free (list);
113     }
114 #endif
115     {
116         const char *str = get_random_string();
117         const char *res;
118 
119         gncJobSetName (job, str);
120         res = qof_object_printable (GNC_ID_JOB, job);
121         do_test (res != NULL, "Printable NULL?");
122         do_test (g_strcmp0 (str, res) == 0, "Printable equals");
123     }
124     {
125         GList *list;
126         GncOwner owner;
127         GncCustomer *cust = gncCustomerCreate (book);
128 
129         gncOwnerInitCustomer (&owner, cust);
130 
131         do_test (gncCustomerGetJoblist (cust, TRUE) == NULL, "empty list at start");
132         gncJobSetOwner (job, &owner);
133         list = gncCustomerGetJoblist (cust, FALSE);
134         do_test (list != NULL, "added to cust");
135         do_test (g_list_length (list) == 1, "correct joblist length");
136         do_test (list->data == job, "verify job in list");
137         gncJobSetActive (job, FALSE);
138         list = gncCustomerGetJoblist (cust, FALSE);
139         do_test (list == NULL, "no active jobs");
140         list = gncCustomerGetJoblist (cust, TRUE);
141         do_test (list != NULL, "all jobs");
142         gncJobBeginEdit (job);
143         gncJobDestroy (job);
144         list = gncCustomerGetJoblist (cust, TRUE);
145         do_test (list == NULL, "no more jobs");
146     }
147 
148     qof_book_destroy (book);
149 }
150 
151 static void
test_string_fcn(QofBook * book,const char * message,void (* set)(GncJob *,const char * str),const char * (* get)(const GncJob *))152 test_string_fcn (QofBook *book, const char *message,
153                  void (*set) (GncJob *, const char *str),
154                  const char * (*get)(const GncJob *))
155 {
156     GncJob *job = gncJobCreate (book);
157     char const *str = get_random_string ();
158 
159     do_test (!qof_instance_is_dirty (QOF_INSTANCE(job)), "test if start dirty");
160     gncJobBeginEdit (job);
161     set (job, str);
162     /* Job record should be dirty */
163     do_test (qof_instance_is_dirty (QOF_INSTANCE(job)), "test dirty later");
164     gncJobCommitEdit (job);
165     /* Job record should be not dirty */
166     /* Skip, because will always fail without a backend.
167      * It's not possible to load a backend in the engine code
168      * without having circular dependencies.
169      */
170     // do_test (!qof_instance_is_dirty (QOF_INSTANCE(job)), "test dirty after commit");
171     do_test (g_strcmp0 (get (job), str) == 0, message);
172     gncJobSetActive (job, FALSE);
173     count++;
174 }
175 
176 static void
test_numeric_fcn(QofBook * book,const char * message,void (* set)(GncJob *,gnc_numeric),gnc_numeric (* get)(const GncJob *))177 test_numeric_fcn (QofBook *book, const char *message,
178                   void (*set) (GncJob *, gnc_numeric),
179                   gnc_numeric (*get)(const GncJob *))
180 {
181     GncJob *job = gncJobCreate (book);
182     gnc_numeric num = gnc_numeric_create (17, 1);
183 
184     do_test (!qof_instance_is_dirty (QOF_INSTANCE(job)), "test if start dirty");
185     gncJobBeginEdit (job);
186     set (job, num);
187     /* Job record should be dirty */
188     do_test (qof_instance_is_dirty (QOF_INSTANCE(job)), "test dirty later");
189     gncJobCommitEdit (job);
190     /* Job record should be not dirty */
191     /* Skip, because will always fail without a backend.
192      * It's not possible to load a backend in the engine code
193      * without having circular dependencies.
194      */
195     // do_test (!qof_instance_is_dirty (QOF_INSTANCE(job)), "test dirty after commit");
196     do_test (gnc_numeric_equal (get (job), num), message);
197     gncJobSetActive (job, FALSE);
198     count++;
199 }
200 
201 static void
test_bool_fcn(QofBook * book,const char * message,void (* set)(GncJob *,gboolean),gboolean (* get)(const GncJob *))202 test_bool_fcn (QofBook *book, const char *message,
203                void (*set) (GncJob *, gboolean),
204                gboolean (*get) (const GncJob *))
205 {
206     GncJob *job = gncJobCreate (book);
207     gboolean num = get_random_boolean ();
208 
209     do_test (!qof_instance_is_dirty (QOF_INSTANCE(job)), "test if start dirty");
210     gncJobBeginEdit (job);
211     set (job, FALSE);
212     set (job, TRUE);
213     set (job, num);
214     /* Job record should be dirty */
215     do_test (qof_instance_is_dirty (QOF_INSTANCE(job)), "test dirty later");
216     gncJobCommitEdit (job);
217     /* Job record should be not dirty */
218     /* Skip, because will always fail without a backend.
219      * It's not possible to load a backend in the engine code
220      * without having circular dependencies.
221      */
222     // do_test (!qof_instance_is_dirty (QOF_INSTANCE(job)), "test dirty after commit");
223     do_test (get (job) == num, message);
224     gncJobSetActive (job, FALSE);
225     count++;
226 }
227 
228 #if 0
229 static void
230 test_gint_fcn (QofBook *book, const char *message,
231                void (*set) (GncJob *, gint),
232                gint (*get) (const GncJob *))
233 {
234     GncJob *job = gncJobCreate (book);
235     gint num = 17;
236 
237     do_test (!qof_instance_is_dirty (QOF_INSTANCE(job)), "test if start dirty");
238     gncJobBeginEdit (job);
239     set (job, num);
240     /* Job record should be dirty */
241     do_test (qof_instance_is_dirty (QOF_INSTANCE(job)), "test dirty later");
242     gncJobCommitEdit (job);
243     /* Job record should be not dirty */
244     /* Skip, because will always fail without a backend.
245      * It's not possible to load a backend in the engine code
246      * without having circular dependencies.
247      */
248     // do_test (!qof_instance_is_dirty (QOF_INSTANCE(job)), "test dirty after commit");
249     do_test (get (job) == num, message);
250     gncJobSetActive (job, FALSE);
251     count++;
252 }
253 #endif
254 
255 int
main(int argc,char ** argv)256 main (int argc, char **argv)
257 {
258     qof_init();
259     do_test (gncInvoiceRegister(), "Cannot register GncInvoice");
260     do_test (gncJobRegister (),  "Cannot register GncJob");
261     do_test (gncCustomerRegister(), "Cannot register GncCustomer");
262     test_job();
263     print_test_results();
264     qof_close();
265     return get_rv();
266 }
267 
268