1 /***************************************************************************
2  *            test-dbi-business-stuff.c
3  *
4  *  Tests saving and loading business objects to a dbi/sqlite3 db
5  *
6  *  Copyright (C) 2010  Phil Longstaff <plongstaff@rogers.com>
7  ****************************************************************************/
8 
9 /*
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23  *  02110-1301, USA.
24  */
25 extern "C"
26 {
27 #include <config.h>
28 #include "qof.h"
29 #include "cashobjects.h"
30 #include "test-engine-stuff.h"
31 #include "test-stuff.h"
32 #include <unittest-support.h>
33 
34 #include "Account.h"
35 #include "Split.h"
36 #include "Transaction.h"
37 #include "gnc-commodity.h"
38 #include "gncCustomer.h"
39 #include "gncInvoice.h"
40 #include "gncEmployee.h"
41 #include "gncVendor.h"
42 }
43 
44 #include "test-dbi-stuff.h"
45 #include "test-dbi-business-stuff.h"
46 
47 G_GNUC_UNUSED static QofLogModule log_module = "test-dbi";
48 
49 static void
compare_single_customer(QofInstance * inst,gpointer user_data)50 compare_single_customer (QofInstance* inst, gpointer user_data)
51 {
52     CompareInfoStruct* info = (CompareInfoStruct*)user_data;
53     GncCustomer* cust_1 = GNC_CUSTOMER (inst);
54     GncCustomer* cust_2 = gncCustomerLookup (info->book_2,
55                                              qof_instance_get_guid (inst));
56 
57     if (!gncCustomerEqual (cust_1, cust_2))
58     {
59         info->result = FALSE;
60     }
61 }
62 
63 static void
compare_customers(QofBook * book_1,QofBook * book_2)64 compare_customers (QofBook* book_1, QofBook* book_2)
65 {
66     do_compare (book_1, book_2, GNC_ID_CUSTOMER, compare_single_customer,
67                 "Customer lists match");
68 }
69 
70 static void
compare_single_employee(QofInstance * inst,gpointer user_data)71 compare_single_employee (QofInstance* inst, gpointer user_data)
72 {
73     CompareInfoStruct* info = (CompareInfoStruct*)user_data;
74     GncEmployee* emp_1 = GNC_EMPLOYEE (inst);
75     GncEmployee* emp_2 = gncEmployeeLookup (info->book_2,
76                                             qof_instance_get_guid (inst));
77 
78     if (!gncEmployeeEqual (emp_1, emp_2))
79     {
80         info->result = FALSE;
81     }
82 }
83 
84 static void
compare_employees(QofBook * book_1,QofBook * book_2)85 compare_employees (QofBook* book_1, QofBook* book_2)
86 {
87     do_compare (book_1, book_2, GNC_ID_EMPLOYEE, compare_single_employee,
88                 "Employee lists match");
89 }
90 
91 static void
compare_single_invoice(QofInstance * inst,gpointer user_data)92 compare_single_invoice (QofInstance* inst, gpointer user_data)
93 {
94     CompareInfoStruct* info = (CompareInfoStruct*)user_data;
95     GncInvoice* inv_1 = GNC_INVOICE (inst);
96     GncInvoice* inv_2 = gncInvoiceLookup (info->book_2,
97                                           qof_instance_get_guid (inst));
98 
99     if (!gncInvoiceEqual (inv_1, inv_2))
100     {
101         info->result = FALSE;
102     }
103 }
104 
105 static void
compare_invoices(QofBook * book_1,QofBook * book_2)106 compare_invoices (QofBook* book_1, QofBook* book_2)
107 {
108     do_compare (book_1, book_2, GNC_ID_INVOICE, compare_single_invoice,
109                 "Invoice lists match");
110 }
111 
112 static void
compare_single_job(QofInstance * inst,gpointer user_data)113 compare_single_job (QofInstance* inst, gpointer user_data)
114 {
115     CompareInfoStruct* info = (CompareInfoStruct*)user_data;
116     GncJob* job_1 = GNC_JOB (inst);
117     GncJob* job_2 = gncJobLookup (info->book_2, qof_instance_get_guid (inst));
118 
119     if (!gncJobEqual (job_1, job_2))
120     {
121         info->result = FALSE;
122     }
123 }
124 
125 static void
compare_jobs(QofBook * book_1,QofBook * book_2)126 compare_jobs (QofBook* book_1, QofBook* book_2)
127 {
128     do_compare (book_1, book_2, GNC_ID_JOB, compare_single_job, "Job lists match");
129 }
130 
131 static void
compare_single_vendor(QofInstance * inst,gpointer user_data)132 compare_single_vendor (QofInstance* inst, gpointer user_data)
133 {
134     CompareInfoStruct* info = (CompareInfoStruct*)user_data;
135     GncVendor* vendor_1 = GNC_VENDOR (inst);
136     GncVendor* vendor_2 = gncVendorLookup (info->book_2,
137                                            qof_instance_get_guid (inst));
138 
139     if (!gncVendorEqual (vendor_1, vendor_2))
140     {
141         info->result = FALSE;
142     }
143 }
144 
145 static void
compare_vendors(QofBook * book_1,QofBook * book_2)146 compare_vendors (QofBook* book_1, QofBook* book_2)
147 {
148     do_compare (book_1, book_2, GNC_ID_VENDOR, compare_single_vendor,
149                 "Vendor lists match");
150 }
151 
152 static void
compare_single_billterm(QofInstance * inst,gpointer user_data)153 compare_single_billterm (QofInstance* inst, gpointer user_data)
154 {
155     CompareInfoStruct* info = (CompareInfoStruct*)user_data;
156     GncBillTerm* bt_1 = GNC_BILLTERM (inst);
157     GncBillTerm* bt_2 = gncBillTermLookup (info->book_2,
158                                            qof_instance_get_guid (inst));
159 
160     if (!gncBillTermEqual (bt_1, bt_2))
161     {
162         info->result = FALSE;
163     }
164 }
165 
166 static void
compare_billterms(QofBook * book_1,QofBook * book_2)167 compare_billterms (QofBook* book_1, QofBook* book_2)
168 {
169     do_compare (book_1, book_2, GNC_ID_BILLTERM, compare_single_billterm,
170                 "Billterms lists match");
171 }
172 
173 static void
compare_single_taxtable(QofInstance * inst,gpointer user_data)174 compare_single_taxtable (QofInstance* inst, gpointer user_data)
175 {
176     CompareInfoStruct* info = (CompareInfoStruct*)user_data;
177     GncTaxTable* tt_1 = GNC_TAXTABLE (inst);
178     GncTaxTable* tt_2 = gncTaxTableLookup (info->book_2,
179                                            qof_instance_get_guid (inst));
180 
181     if (!gncTaxTableEqual (tt_1, tt_2))
182     {
183         info->result = FALSE;
184     }
185 }
186 
187 static void
compare_taxtables(QofBook * book_1,QofBook * book_2)188 compare_taxtables (QofBook* book_1, QofBook* book_2)
189 {
190     do_compare (book_1, book_2, GNC_ID_TAXTABLE, compare_single_taxtable,
191                 "TaxTable lists match");
192 }
193 
194 void
compare_business_books(QofBook * book_1,QofBook * book_2)195 compare_business_books (QofBook* book_1, QofBook* book_2)
196 {
197     compare_billterms (book_1, book_2);
198     compare_taxtables (book_1, book_2);
199 
200     compare_customers (book_1, book_2);
201     compare_employees (book_1, book_2);
202     compare_invoices (book_1, book_2);
203     compare_jobs (book_1, book_2);
204     compare_vendors (book_1, book_2);
205 }
206