GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
test-customer.c
1 /*********************************************************************
2  * test-customer.c
3  * Test the customer object (without Guile/Scheme)
4  *
5  * Copyright (c) 2001 Derek Atkins <[email protected]>
6  * Copyright (c) 2005 Neil Williams <[email protected]>
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 [email protected]
24  *
25  *********************************************************************/
26 
27 #include <config.h>
28 #include <glib.h>
29 #include <qof.h>
30 #include <qofinstance-p.h>
31 
32 #include "cashobjects.h"
33 #include "gncCustomerP.h"
34 #include "gncInvoiceP.h"
35 #include "gncJobP.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) (GncCustomer *, const char *str),
43  const char * (*get)(const GncCustomer *));
44 
45 static void
46 test_numeric_fcn (QofBook *book, const char *message,
47  void (*set) (GncCustomer *, gnc_numeric),
48  gnc_numeric (*get)(const GncCustomer *));
49 
50 static void
51 test_bool_fcn (QofBook *book, const char *message,
52  void (*set) (GncCustomer *, gboolean),
53  gboolean (*get) (const GncCustomer *));
54 
55 static void
56 test_customer (void)
57 {
58  QofBook *book;
59  GncCustomer *customer;
60 
61  book = qof_book_new ();
62 
63  /* Test creation/destruction */
64  {
65  do_test (gncCustomerCreate (NULL) == NULL, "customer create NULL");
66  customer = gncCustomerCreate (book);
67  do_test (customer != NULL, "customer create");
68  do_test (gncCustomerGetBook (customer) == book, "getbook");
69 
70  gncCustomerBeginEdit (customer);
71  gncCustomerDestroy (customer);
72  success ("create/destroy");
73  }
74 
75  /* Test setting/getting routines; does the active flag get set right? */
76  {
77  GncGUID guid;
78 
79  test_string_fcn (book, "Id", gncCustomerSetID, gncCustomerGetID);
80  test_string_fcn (book, "Name", gncCustomerSetName, gncCustomerGetName);
81  test_string_fcn (book, "Notes", gncCustomerSetNotes, gncCustomerGetNotes);
82 
83  //test_string_fcn (book, "Terms", gncCustomerSetTerms, gncCustomerGetTerms);
84 
85  test_numeric_fcn (book, "Discount", gncCustomerSetDiscount, gncCustomerGetDiscount);
86  test_numeric_fcn (book, "Credit", gncCustomerSetCredit, gncCustomerGetCredit);
87 
88  test_bool_fcn (book, "Active", gncCustomerSetActive, gncCustomerGetActive);
89 
90  do_test (gncCustomerGetAddr (customer) != NULL, "Addr");
91  do_test (gncCustomerGetShipAddr (customer) != NULL, "ShipAddr");
92 
93  guid_replace (&guid);
94  customer = gncCustomerCreate (book);
95  count++;
96  gncCustomerSetGUID (customer, &guid);
97  do_test (guid_equal (&guid, gncCustomerGetGUID (customer)), "guid compare");
98  }
99  {
100  GList *list;
101 
102  list = gncBusinessGetList (book, GNC_ID_CUSTOMER, TRUE);
103  do_test (list != NULL, "getList all");
104  do_test (g_list_length (list) == count, "correct length: all");
105  g_list_free (list);
106 
107  list = gncBusinessGetList (book, GNC_ID_CUSTOMER, FALSE);
108  do_test (list != NULL, "getList active");
109  do_test (g_list_length (list) == 1, "correct length: active");
110  g_list_free (list);
111  }
112  {
113  const char *str = get_random_string();
114  const char *res;
115 
116  res = NULL;
117  gncCustomerBeginEdit(customer);
118  gncCustomerSetName (customer, str);
119  gncCustomerCommitEdit(customer);
120  res = qof_object_printable (GNC_ID_CUSTOMER, customer);
121  do_test (res != NULL, "Printable NULL?");
122  do_test (g_strcmp0 (str, res) == 0, "Printable equals");
123  }
124 
125  do_test (gncCustomerGetJoblist (customer, TRUE) == NULL, "joblist empty");
126 
127  /* Test the Entity Table */
128  {
129  const GncGUID *guid;
130 
131  guid = gncCustomerGetGUID (customer);
132  do_test (gncCustomerLookup (book, guid) == customer, "Entity Table");
133  }
134 
135  /* Note: JobList is tested from the Job tests */
136  qof_book_destroy (book);
137 }
138 
139 static void
140 test_string_fcn (QofBook *book, const char *message,
141  void (*set) (GncCustomer *, const char *str),
142  const char * (*get)(const GncCustomer *))
143 {
144  GncCustomer *customer = gncCustomerCreate (book);
145  char const *str = get_random_string ();
146 
147  do_test (!gncCustomerIsDirty (customer), "test if start dirty");
148  gncCustomerBeginEdit (customer);
149  set (customer, str);
150  /* Customer record should be dirty */
151  do_test (gncCustomerIsDirty (customer), "test dirty later");
152  gncCustomerCommitEdit (customer);
153  /* Customer record should be not dirty */
154  /* Skip, because will always fail without a backend.
155  * It's not possible to load a backend in the engine code
156  * without having circular dependencies.
157  */
158  // do_test (!gncCustomerIsDirty (customer), "test dirty after commit");
159  do_test (g_strcmp0 (get (customer), str) == 0, message);
160  gncCustomerSetActive (customer, FALSE);
161  count++;
162 }
163 
164 static void
165 test_numeric_fcn (QofBook *book, const char *message,
166  void (*set) (GncCustomer *, gnc_numeric),
167  gnc_numeric (*get)(const GncCustomer *))
168 {
169  GncCustomer *customer = gncCustomerCreate (book);
170  gnc_numeric num = gnc_numeric_create (17, 1);
171 
172  do_test (!gncCustomerIsDirty (customer), "test if start dirty");
173  gncCustomerBeginEdit (customer);
174  set (customer, num);
175  /* Customer record should be dirty */
176  do_test (gncCustomerIsDirty (customer), "test dirty later");
177  gncCustomerCommitEdit (customer);
178  /* Customer record should be not dirty */
179  /* Skip, because will always fail without a backend.
180  * It's not possible to load a backend in the engine code
181  * without having circular dependencies.
182  */
183  // do_test (!gncCustomerIsDirty (customer), "test dirty after commit");
184  do_test (gnc_numeric_equal (get (customer), num), message);
185  gncCustomerSetActive (customer, FALSE);
186  count++;
187 }
188 
189 static void
190 test_bool_fcn (QofBook *book, const char *message,
191  void (*set) (GncCustomer *, gboolean),
192  gboolean (*get) (const GncCustomer *))
193 {
194  GncCustomer *customer = gncCustomerCreate (book);
195  gboolean num = get_random_boolean ();
196 
197  do_test (!gncCustomerIsDirty (customer), "test if start dirty");
198  gncCustomerBeginEdit (customer);
199  set (customer, FALSE);
200  set (customer, TRUE);
201  set (customer, num);
202  /* Customer record should be dirty */
203  do_test (gncCustomerIsDirty (customer), "test dirty later");
204  gncCustomerCommitEdit (customer);
205  /* Customer record should be not dirty */
206  /* Skip, because will always fail without a backend.
207  * It's not possible to load a backend in the engine code
208  * without having circular dependencies.
209  */
210  // do_test (!gncCustomerIsDirty (customer), "test dirty after commit");
211  do_test (get (customer) == num, message);
212  gncCustomerSetActive (customer, FALSE);
213  count++;
214 }
215 
216 int
217 main (int argc, char **argv)
218 {
219  qof_init();
220  do_test (cashobjects_register(), "Cannot register cash objects");
221  /* These three registrations are done during cashobjects_register,
222  so trying to register them again naturally fails. */
223 #if 0
224  do_test (gncInvoiceRegister(), "Cannot register GncInvoice");
225  do_test (gncJobRegister (), "Cannot register GncJob");
226  do_test (gncCustomerRegister(), "Cannot register GncCustomer");
227 #endif
228  test_customer();
229  print_test_results();
230  qof_close ();
231  return get_rv();
232 }
void guid_replace(GncGUID *guid)
gboolean gnc_numeric_equal(gnc_numeric a, gnc_numeric b)
QofBook * qof_book_new(void)
Definition: guid.h:65
gboolean guid_equal(const GncGUID *guid_1, const GncGUID *guid_2)
#define gncCustomerGetGUID(x)
Definition: gncCustomer.h:156
void qof_close(void)
Safely close down the Query Object Framework.
GList * gncBusinessGetList(QofBook *book, QofIdTypeConst type_name, gboolean all_including_inactive)
const char * qof_object_printable(QofIdTypeConst type_name, gpointer instance)
void qof_init(void)
Initialise the Query Object Framework.
void qof_book_destroy(QofBook *book)