GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
test-engine-kvp-properties.c
1 /********************************************************************
2  * test-engine-kvp-properties.c: GLib g_test test suite for *
3  * KVP-based properties in several engine classes. *
4  * Copyright 2013 John Ralls <[email protected]> *
5  * *
6  * This program is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU General Public License as *
8  * published by the Free Software Foundation; either version 2 of *
9  * the License, or (at your option) any later version. *
10  * *
11  * This program is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License*
17  * along with this program; if not, contact: *
18  * *
19  * Free Software Foundation Voice: +1-617-542-5942 *
20  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
21  * Boston, MA 02110-1301, USA [email protected] *
22 \********************************************************************/
23 
31 #include <config.h>
32 #include <glib.h>
33 #include <qof.h>
34 #include <unittest-support.h>
35 #include "../Transaction.h"
36 #include "../Split.h"
37 #include "../Account.h"
38 #include "../SchedXaction.h"
39 #include "../gncCustomer.h"
40 #include "../gncEmployee.h"
41 #include "../gncJob.h"
42 #include "../gncVendor.h"
43 
44 typedef struct
45 {
46  union
47  {
48  Account *acct;
49  Transaction *trans;
50  Split *split;
51  GNCLot *lot;
52  GncCustomer *cust;
53  GncEmployee *emp;
54  GncJob *job;
55  GncVendor *vend;
56  };
57  GSList *hdlrs;
58 } Fixture;
59 
60 /* Prototype to shut clang up */
61 void test_suite_engine_kvp_properties (void);
62 
63 /* Private QofInstance functions needed for testing */
64 extern void qof_instance_mark_clean (QofInstance*);
65 
66 const gchar *suitename = "/engine/kvp-properties";
67 
68 static void
69 setup_account (Fixture *fixture, gconstpointer pData)
70 {
71  QofBook *book = qof_book_new ();
72  fixture->acct = xaccMallocAccount (book);
73 }
74 
75 static void
76 setup_trans (Fixture *fixture, gconstpointer pData)
77 {
78  QofBook *book = qof_book_new ();
79  fixture->trans = xaccMallocTransaction (book);
80 }
81 
82 static void
83 setup_split (Fixture *fixture, gconstpointer pData)
84 {
85  QofBook *book = qof_book_new ();
86  fixture->split = xaccMallocSplit (book);
87 }
88 
89 static void
90 setup_lot (Fixture *fixture, gconstpointer pData)
91 {
92  QofBook *book = qof_book_new ();
93  fixture->lot = gnc_lot_new (book);
94 }
95 
96 static void
97 setup_customer (Fixture *fixture, gconstpointer pData)
98 {
99  QofBook *book = qof_book_new ();
100  fixture->cust = gncCustomerCreate (book);
101 }
102 
103 static void
104 setup_employee (Fixture *fixture, gconstpointer pData)
105 {
106  QofBook *book = qof_book_new ();
107  fixture->emp = gncEmployeeCreate (book);
108 }
109 
110 static void
111 setup_job (Fixture *fixture, gconstpointer pData)
112 {
113  QofBook *book = qof_book_new ();
114  fixture->job = gncJobCreate (book);
115 }
116 
117 static void
118 setup_vendor (Fixture *fixture, gconstpointer pData)
119 {
120  QofBook *book = qof_book_new ();
121  fixture->vend = gncVendorCreate (book);
122 }
123 
124 static void
125 teardown (Fixture *fixture, gconstpointer pData)
126 {
127 /* It doesn't actually matter which union member we use here, they're
128  * all QofInstances, so this will work for any of them.
129  */
130  QofBook *book = qof_instance_get_book (QOF_INSTANCE (fixture->acct));
131  test_destroy (fixture->acct);
132  test_destroy (book);
133 }
134 
135 static void
136 test_account_kvp_properties (Fixture *fixture, gconstpointer pData)
137 {
138  gint64 next_id = 12345678909876;
139  gint64 ab_acct_uid = 67890987654321;
140  gint64 next_id_r, ab_acct_uid_r;
141  gchar *online_id = "my online id";
142  gchar *ab_acct_id = "1234-5678-9087";
143  gchar *ab_bank_code = "0032340";
144  gchar *online_id_r, *ab_acct_id_r, *ab_bank_code_r;
145  GncGUID *ofx_income_acct = guid_malloc ();
146  GncGUID *ofx_income_acct_r;
147  Timespec trans_retr = timespec_now ();
148  Timespec *trans_retr_r;
149 
150  xaccAccountBeginEdit (fixture->acct);
151  qof_instance_set (QOF_INSTANCE (fixture->acct),
152  "lot-next-id", next_id,
153  "online-id", online_id,
154  "ofx-income-account", ofx_income_acct,
155  "ab-account-id", ab_acct_id,
156  "ab-bank-code", ab_bank_code,
157  "ab-account-uid", ab_acct_uid,
158  "ab-trans-retrieval", &trans_retr,
159  NULL);
160 
161  g_assert (qof_instance_is_dirty (QOF_INSTANCE (fixture->acct)));
162  qof_instance_mark_clean (QOF_INSTANCE (fixture->acct));
163 
164  qof_instance_get (QOF_INSTANCE (fixture->acct),
165  "lot-next-id", &next_id_r,
166  "online-id", &online_id_r,
167  "ofx-income-account", &ofx_income_acct_r,
168  "ab-account-id", &ab_acct_id_r,
169  "ab-bank-code", &ab_bank_code_r,
170  "ab-account-uid", &ab_acct_uid_r,
171  "ab-trans-retrieval", &trans_retr_r,
172  NULL);
173  g_assert_cmpint (next_id, ==, next_id_r);
174  g_assert_cmpstr (online_id, ==, online_id_r);
175  g_assert (guid_equal (ofx_income_acct, ofx_income_acct_r));
176  g_assert_cmpstr (ab_acct_id, ==, ab_acct_id_r);
177  g_assert_cmpstr (ab_bank_code, ==, ab_bank_code_r);
178  g_assert_cmpint (ab_acct_uid, ==, ab_acct_uid_r);
179  g_assert (timespec_equal (&trans_retr, trans_retr_r));
180  g_assert (!qof_instance_is_dirty (QOF_INSTANCE (fixture->acct)));
181 }
182 
183 static void
184 test_trans_kvp_properties (Fixture *fixture, gconstpointer pData)
185 {
186  GncGUID *invoice = guid_malloc ();
187  GncGUID *from_sx = guid_malloc ();
188  GncGUID *invoice_r, *from_sx_r;
189  gchar *online_id = "my online id";
190  gchar *online_id_r;
191 
192  xaccTransBeginEdit (fixture->trans);
193  qof_instance_set (QOF_INSTANCE (fixture->trans),
194  "invoice", invoice,
195  "from-sched-xaction", from_sx,
196  "online-id", online_id,
197  NULL);
198 
199  g_assert (qof_instance_is_dirty (QOF_INSTANCE (fixture->trans)));
200  qof_instance_mark_clean (QOF_INSTANCE (fixture->trans));
201 
202  qof_instance_get (QOF_INSTANCE (fixture->trans),
203  "invoice", &invoice_r,
204  "from-sched-xaction", &from_sx_r,
205  "online-id", &online_id_r,
206  NULL);
207  g_assert (guid_equal (invoice, invoice_r));
208  g_assert (guid_equal (from_sx, from_sx_r));
209  g_assert_cmpstr (online_id, ==, online_id_r);
210  g_assert (!qof_instance_is_dirty (QOF_INSTANCE (fixture->trans)));
211  guid_free (invoice);
212  guid_free (invoice_r);
213  guid_free (from_sx);
214  guid_free (from_sx_r);
215  g_free (online_id_r);
216 }
217 
218 static void
219 test_split_kvp_properties (Fixture *fixture, gconstpointer pData)
220 {
221  gchar *debit_formula = "e^xdydx";
222  gchar *credit_formula = "seccostansin";
223  gchar *sx_shares = "43";
224  gchar *online_id = "my_online_id";
225  gchar *debit_formula_r, *credit_formula_r, *sx_shares_r;
226  gchar *online_id_r;
227  GncGUID *sx_account = guid_malloc ();
228  GncGUID *sx_account_r;
229  gnc_numeric debit_numeric = gnc_numeric_create (123, 456);
230  gnc_numeric credit_numeric = gnc_numeric_create (789, 456);
231  gnc_numeric *debit_numeric_r, *credit_numeric_r;
232 
233  qof_begin_edit (QOF_INSTANCE (fixture->split));
234  qof_instance_set (QOF_INSTANCE (fixture->split),
235  "sx-debit-formula", debit_formula,
236  "sx-debit-numeric", &debit_numeric,
237  "sx-credit-formula", credit_formula,
238  "sx-credit-numeric", &credit_numeric,
239  "sx-account", sx_account,
240  "sx-shares", sx_shares,
241  "online-id", online_id,
242  NULL);
243 
244  g_assert (qof_instance_is_dirty (QOF_INSTANCE (fixture->split)));
245  qof_instance_mark_clean (QOF_INSTANCE (fixture->split));
246 
247  qof_instance_get (QOF_INSTANCE (fixture->split),
248  "sx-debit-formula", &debit_formula_r,
249  "sx-debit-numeric", &debit_numeric_r,
250  "sx-credit-formula", &credit_formula_r,
251  "sx-credit-numeric", &credit_numeric_r,
252  "sx-account", &sx_account_r,
253  "sx-shares", &sx_shares_r,
254  "online-id", &online_id_r,
255  NULL);
256  g_assert_cmpstr (debit_formula, ==, debit_formula_r);
257  g_assert (gnc_numeric_equal (debit_numeric, *debit_numeric_r));
258  g_assert_cmpstr (credit_formula, ==, credit_formula_r);
259  g_assert (gnc_numeric_equal (credit_numeric, *credit_numeric_r));
260  g_assert (guid_equal (sx_account, sx_account_r));
261  g_assert_cmpstr (sx_shares, ==, sx_shares_r);
262  g_assert_cmpstr (online_id, ==, online_id_r);
263  g_assert (!qof_instance_is_dirty (QOF_INSTANCE (fixture->split)));
264  g_free (debit_formula_r);
265  g_free (debit_numeric_r);
266  g_free (credit_formula_r);
267  g_free (credit_numeric_r);
268  qof_begin_edit (QOF_INSTANCE (fixture->split));
269  qof_instance_set (QOF_INSTANCE (fixture->split),
270  "sx-credit-formula", NULL,
271  NULL);
272  qof_instance_get (QOF_INSTANCE (fixture->split),
273  "sx-credit-formula", &credit_numeric_r,
274  NULL);
275  g_assert (credit_numeric_r == NULL);
276  g_free (sx_shares_r);
277  g_free (online_id_r);
278  guid_free (sx_account);
279  guid_free (sx_account_r);
280 }
281 
282 static void
283 test_lot_kvp_properties (Fixture *fixture, gconstpointer pData)
284 {
285  GncGUID *invoice = guid_malloc ();
286  GncGUID *invoice_r;
287  gint64 owner_type = 47;
288  gint64 owner_type_r;
289  GncGUID *owner = guid_malloc ();
290  GncGUID *owner_r;
291 
292  qof_begin_edit (QOF_INSTANCE (fixture->lot));
293  qof_instance_set (QOF_INSTANCE (fixture->lot),
294  "invoice", invoice,
295  GNC_OWNER_TYPE, owner_type,
296  GNC_OWNER_GUID, owner,
297  NULL);
298 
299  g_assert (qof_instance_is_dirty (QOF_INSTANCE (fixture->lot)));
300  qof_instance_mark_clean (QOF_INSTANCE (fixture->lot));
301 
302  qof_instance_get (QOF_INSTANCE (fixture->lot),
303  "invoice", &invoice_r,
304  GNC_OWNER_TYPE, &owner_type_r,
305  GNC_OWNER_GUID, &owner_r,
306  NULL);
307  g_assert (guid_equal (invoice, invoice_r));
308  g_assert_cmpint (owner_type, ==, owner_type_r);
309  g_assert (guid_equal (owner, owner_r));
310  g_assert (!qof_instance_is_dirty (QOF_INSTANCE (fixture->lot)));
311  guid_free (invoice);
312  guid_free (invoice_r);
313  guid_free (owner);
314  guid_free (owner_r);
315 }
316 
317 static void
318 test_customer_kvp_properties (Fixture *fixture, gconstpointer pData)
319 {
320  gchar *pdf_dir = "/foo/bar/baz";
321  gchar *pdf_dir_r;
322  GncGUID *inv_acct = guid_malloc ();
323  GncGUID *pmt_acct = guid_malloc ();
324  GncGUID *inv_acct_r, *pmt_acct_r;
325 
326  qof_begin_edit (QOF_INSTANCE (fixture->cust));
327  qof_instance_set (QOF_INSTANCE (fixture->cust),
328  "export-pdf-dir", pdf_dir,
329  "invoice-last-posted-account", inv_acct,
330  "payment-last-account", pmt_acct,
331  NULL);
332 
333  g_assert (qof_instance_is_dirty (QOF_INSTANCE (fixture->cust)));
334  qof_instance_mark_clean (QOF_INSTANCE (fixture->cust));
335 
336  qof_instance_get (QOF_INSTANCE (fixture->cust),
337  "export-pdf-dir", &pdf_dir_r,
338  "invoice-last-posted-account", &inv_acct_r,
339  "payment-last-account", &pmt_acct_r,
340  NULL);
341 
342  g_assert_cmpstr (pdf_dir, ==, pdf_dir_r);
343  g_assert (guid_equal (inv_acct, inv_acct_r));
344  g_assert (guid_equal (pmt_acct, pmt_acct_r));
345  guid_free (inv_acct);
346  guid_free (inv_acct_r);
347  guid_free (pmt_acct);
348  guid_free (pmt_acct_r);
349  g_free (pdf_dir_r);
350 
351 }
352 
353 static void
354 test_employee_kvp_properties (Fixture *fixture, gconstpointer pData)
355 {
356  gchar *pdf_dir = "/foo/bar/baz";
357  gchar *pdf_dir_r;
358  GncGUID *inv_acct = guid_malloc ();
359  GncGUID *pmt_acct = guid_malloc ();
360  GncGUID *inv_acct_r, *pmt_acct_r;
361 
362  qof_begin_edit (QOF_INSTANCE (fixture->emp));
363  qof_instance_set (QOF_INSTANCE (fixture->emp),
364  "export-pdf-dir", pdf_dir,
365  "invoice-last-posted-account", inv_acct,
366  "payment-last-account", pmt_acct,
367  NULL);
368 
369  g_assert (qof_instance_is_dirty (QOF_INSTANCE (fixture->emp)));
370  qof_instance_mark_clean (QOF_INSTANCE (fixture->emp));
371 
372  qof_instance_get (QOF_INSTANCE (fixture->emp),
373  "export-pdf-dir", &pdf_dir_r,
374  "invoice-last-posted-account", &inv_acct_r,
375  "payment-last-account", &pmt_acct_r,
376  NULL);
377 
378  g_assert_cmpstr (pdf_dir, ==, pdf_dir_r);
379  g_assert (guid_equal (inv_acct, inv_acct_r));
380  g_assert (guid_equal (pmt_acct, pmt_acct_r));
381  guid_free (inv_acct);
382  guid_free (inv_acct_r);
383  guid_free (pmt_acct);
384  guid_free (pmt_acct_r);
385  g_free (pdf_dir_r);
386 
387 }
388 
389 static void
390 test_job_kvp_properties (Fixture *fixture, gconstpointer pData)
391 {
392  gchar *pdf_dir = "/foo/bar/baz";
393  gchar *pdf_dir_r;
394 
395  qof_begin_edit (QOF_INSTANCE (fixture->job));
396  qof_instance_set (QOF_INSTANCE (fixture->job),
397  "export-pdf-dir", pdf_dir,
398  NULL);
399 
400  g_assert (qof_instance_is_dirty (QOF_INSTANCE (fixture->job)));
401  qof_instance_mark_clean (QOF_INSTANCE (fixture->job));
402 
403  qof_instance_get (QOF_INSTANCE (fixture->job),
404  "export-pdf-dir", &pdf_dir_r,
405  NULL);
406 
407  g_assert_cmpstr (pdf_dir, ==, pdf_dir_r);
408  g_free (pdf_dir_r);
409 
410 }
411 
412 static void
413 test_vendor_kvp_properties (Fixture *fixture, gconstpointer pData)
414 {
415  gchar *pdf_dir = "/foo/bar/baz";
416  gchar *pdf_dir_r;
417  GncGUID *inv_acct = guid_malloc ();
418  GncGUID *pmt_acct = guid_malloc ();
419  GncGUID *inv_acct_r, *pmt_acct_r;
420 
421  qof_begin_edit (QOF_INSTANCE (fixture->vend));
422  qof_instance_set (QOF_INSTANCE (fixture->vend),
423  "export-pdf-dir", pdf_dir,
424  "invoice-last-posted-account", inv_acct,
425  "payment-last-account", pmt_acct,
426  NULL);
427 
428  g_assert (qof_instance_is_dirty (QOF_INSTANCE (fixture->vend)));
429  qof_instance_mark_clean (QOF_INSTANCE (fixture->vend));
430 
431  qof_instance_get (QOF_INSTANCE (fixture->vend),
432  "export-pdf-dir", &pdf_dir_r,
433  "invoice-last-posted-account", &inv_acct_r,
434  "payment-last-account", &pmt_acct_r,
435  NULL);
436 
437  g_assert_cmpstr (pdf_dir, ==, pdf_dir_r);
438  g_assert (guid_equal (inv_acct, inv_acct_r));
439  g_assert (guid_equal (pmt_acct, pmt_acct_r));
440  guid_free (inv_acct);
441  guid_free (inv_acct_r);
442  guid_free (pmt_acct);
443  guid_free (pmt_acct_r);
444  g_free (pdf_dir_r);
445 
446 }
447 
448 void test_suite_engine_kvp_properties (void)
449 {
450  GNC_TEST_ADD (suitename, "Account", Fixture, NULL, setup_account, test_account_kvp_properties, teardown);
451  GNC_TEST_ADD (suitename, "Transaction", Fixture, NULL, setup_trans, test_trans_kvp_properties, teardown);
452  GNC_TEST_ADD (suitename, "Split", Fixture, NULL, setup_split, test_split_kvp_properties, teardown);
453  GNC_TEST_ADD (suitename, "Lot", Fixture, NULL, setup_lot, test_lot_kvp_properties, teardown);
454  GNC_TEST_ADD (suitename, "Customer", Fixture, NULL, setup_customer, test_customer_kvp_properties, teardown);
455  GNC_TEST_ADD (suitename, "Employee", Fixture, NULL, setup_employee, test_employee_kvp_properties, teardown);
456  GNC_TEST_ADD (suitename, "Job", Fixture, NULL, setup_job, test_job_kvp_properties, teardown);
457  GNC_TEST_ADD (suitename, "Vendor", Fixture, NULL, setup_vendor, test_vendor_kvp_properties, teardown);
458 }
Transaction * xaccMallocTransaction(QofBook *book)
Definition: Transaction.c:513
gboolean gnc_numeric_equal(gnc_numeric a, gnc_numeric b)
void qof_instance_get(const QofInstance *inst, const gchar *first_param,...)
Wrapper for g_object_get.
#define qof_instance_is_dirty
Definition: qofinstance.h:165
QofBook * qof_instance_get_book(gconstpointer)
void qof_instance_set(QofInstance *inst, const gchar *first_param,...)
Wrapper for g_object_set Group setting multiple parameters in a single begin/commit/rollback.
QofBook * qof_book_new(void)
gboolean timespec_equal(const Timespec *ta, const Timespec *tb)
Use a 64-bit unsigned int timespec.
Definition: gnc-date.h:299
Definition: guid.h:65
gboolean qof_begin_edit(QofInstance *inst)
GncGUID * guid_malloc(void)
Definition: gncJob.c:41
gboolean guid_equal(const GncGUID *guid_1, const GncGUID *guid_2)
void xaccTransBeginEdit(Transaction *trans)
Definition: Transaction.c:1380
Split * xaccMallocSplit(QofBook *book)
Definition: Split.c:582
Definition: SplitP.h:71
void xaccAccountBeginEdit(Account *acc)
Definition: Account.c:1280
Timespec timespec_now(void)
Account * xaccMallocAccount(QofBook *book)
Definition: Account.c:1083