GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
test-employee.c
1 /*********************************************************************
2  * test-employee.c
3  * Test the employee object (without Guile).
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 "gncEmployeeP.h"
33 #include "gncCustomerP.h"
34 #include "gncJobP.h"
35 #include "gncInvoiceP.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) (GncEmployee *, const char *str),
43  const char * (*get)(const GncEmployee *));
44 
45 static void
46 test_numeric_fcn (QofBook *book, const char *message,
47  void (*set) (GncEmployee *, gnc_numeric),
48  gnc_numeric (*get)(const GncEmployee *));
49 
50 static void
51 test_bool_fcn (QofBook *book, const char *message,
52  void (*set) (GncEmployee *, gboolean),
53  gboolean (*get) (const GncEmployee *));
54 
55 #if 0
56 static void
57 test_gint_fcn (QofBook *book, const char *message,
58  void (*set) (GncEmployee *, gint),
59  gint (*get) (GncEmployee *));
60 #endif
61 
62 static void
63 test_employee (void)
64 {
65  QofBook *book;
66  GncEmployee *employee;
67 
68  book = qof_book_new();
69 
70  /* Test creation/destruction */
71  {
72  do_test (gncEmployeeCreate (NULL) == NULL, "employee create NULL");
73  employee = gncEmployeeCreate (book);
74  do_test (employee != NULL, "employee create");
75  do_test (qof_instance_get_book(QOF_INSTANCE(employee)) == book,
76  "getbook");
77 
78  gncEmployeeBeginEdit (employee);
79  gncEmployeeDestroy (employee);
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", gncEmployeeSetID, gncEmployeeGetID);
88  test_string_fcn (book, "Username", gncEmployeeSetUsername, gncEmployeeGetUsername);
89  test_string_fcn (book, "Language", gncEmployeeSetLanguage, gncEmployeeGetLanguage);
90  test_string_fcn (book, "Acl", gncEmployeeSetAcl, gncEmployeeGetAcl);
91 
92  test_numeric_fcn (book, "Workday", gncEmployeeSetWorkday, gncEmployeeGetWorkday);
93  test_numeric_fcn (book, "Rate", gncEmployeeSetRate, gncEmployeeGetRate);
94 
95  test_bool_fcn (book, "Active", gncEmployeeSetActive, gncEmployeeGetActive);
96 
97  do_test (gncEmployeeGetAddr (employee) != NULL, "Addr");
98 
99  guid_replace (&guid);
100  employee = gncEmployeeCreate (book);
101  count++;
102  gncEmployeeSetGUID (employee, &guid);
103  do_test (guid_equal (&guid, qof_instance_get_guid(QOF_INSTANCE(employee))), "guid compare");
104  }
105  {
106  GList *list;
107 
108  list = gncBusinessGetList (book, GNC_EMPLOYEE_MODULE_NAME, 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_EMPLOYEE_MODULE_NAME, 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  GncAddress *addr;
122 
123  addr = gncEmployeeGetAddr (employee);
124  gncAddressSetName (addr, str);
125  res = qof_object_printable (GNC_ID_EMPLOYEE, employee);
126  do_test (res != NULL, "Printable NULL?");
127  do_test (g_strcmp0 (str, res) == 0, "Printable equals");
128  }
129 
130  qof_book_destroy (book);
131 }
132 
133 static void
134 test_string_fcn (QofBook *book, const char *message,
135  void (*set) (GncEmployee *, const char *str),
136  const char * (*get)(const GncEmployee *))
137 {
138  GncEmployee *employee = gncEmployeeCreate (book);
139  char const *str = get_random_string ();
140 
141  do_test (!gncEmployeeIsDirty (employee), "test if start dirty");
142  gncEmployeeBeginEdit (employee);
143  set (employee, str);
144  /* Employee record should be dirty */
145  do_test (gncEmployeeIsDirty (employee), "test dirty later");
146  gncEmployeeCommitEdit (employee);
147  /* Employee record should be not dirty */
148  /* Skip, because will always fail without a backend.
149  * It's not possible to load a backend in the engine code
150  * without having circular dependencies.
151  */
152  // do_test (!gncEmployeeIsDirty (employee), "test dirty after commit");
153  do_test (g_strcmp0 (get (employee), str) == 0, message);
154  gncEmployeeSetActive (employee, FALSE);
155  count++;
156 }
157 
158 static void
159 test_numeric_fcn (QofBook *book, const char *message,
160  void (*set) (GncEmployee *, gnc_numeric),
161  gnc_numeric (*get)(const GncEmployee *))
162 {
163  GncEmployee *employee = gncEmployeeCreate (book);
164  gnc_numeric num = gnc_numeric_create (17, 1);
165 
166  do_test (!gncEmployeeIsDirty (employee), "test if start dirty");
167  gncEmployeeBeginEdit (employee);
168  set (employee, num);
169  /* Employee record should be dirty */
170  do_test (gncEmployeeIsDirty (employee), "test dirty later");
171  gncEmployeeCommitEdit (employee);
172  /* Employee record should be not dirty */
173  /* Skip, because will always fail without a backend.
174  * It's not possible to load a backend in the engine code
175  * without having circular dependencies.
176  */
177  // do_test (!gncEmployeeIsDirty (employee), "test dirty after commit");
178  do_test (gnc_numeric_equal (get (employee), num), message);
179  gncEmployeeSetActive (employee, FALSE);
180  count++;
181 }
182 
183 static void
184 test_bool_fcn (QofBook *book, const char *message,
185  void (*set) (GncEmployee *, gboolean),
186  gboolean (*get) (const GncEmployee *))
187 {
188  GncEmployee *employee = gncEmployeeCreate (book);
189  gboolean num = get_random_boolean ();
190 
191  do_test (!gncEmployeeIsDirty (employee), "test if start dirty");
192  gncEmployeeBeginEdit (employee);
193  set (employee, FALSE);
194  set (employee, TRUE);
195  set (employee, num);
196  /* Employee record should be dirty */
197  do_test (gncEmployeeIsDirty (employee), "test dirty later");
198  gncEmployeeCommitEdit (employee);
199  /* Employee 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 (!gncEmployeeIsDirty (employee), "test dirty after commit");
205  do_test (get (employee) == num, message);
206  gncEmployeeSetActive (employee, FALSE);
207  count++;
208 }
209 
210 #if 0
211 static void
212 test_gint_fcn (QofBook *book, const char *message,
213  void (*set) (GncEmployee *, gint),
214  gint (*get) (GncEmployee *))
215 {
216  GncEmployee *employee = gncEmployeeCreate (book);
217  gint num = 17;
218 
219  do_test (!gncEmployeeIsDirty (employee), "test if start dirty");
220  gncEmployeeBeginEdit (employee);
221  set (employee, num);
222  /* Employee record should be dirty */
223  do_test (gncEmployeeIsDirty (employee), "test dirty later");
224  gncEmployeeCommitEdit (employee);
225  /* Employee 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 (!gncEmployeeIsDirty (employee), "test dirty after commit");
231  do_test (get (employee) == num, message);
232  gncEmployeeSetActive (employee, FALSE);
233  count++;
234 }
235 #endif
236 
237 int
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 (gncEmployeeRegister(), "Cannot register GncEmployee");
245  test_employee();
246  print_test_results();
247  qof_close();
248  return get_rv();
249 }
void guid_replace(GncGUID *guid)
gboolean gnc_numeric_equal(gnc_numeric a, gnc_numeric b)
const GncGUID * qof_instance_get_guid(gconstpointer)
QofBook * qof_instance_get_book(gconstpointer)
QofBook * qof_book_new(void)
Definition: guid.h:65
gboolean guid_equal(const GncGUID *guid_1, const GncGUID *guid_2)
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)