GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
test-dbi-stuff.c
1 /***************************************************************************
2  * test-dbi-stuff.c
3  *
4  * Tests saving and loading to a dbi/sqlite3 db
5  *
6  * Copyright (C) 2009 Phil Longstaff <[email protected]>
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 
26 #include "config.h"
27 #include "qof.h"
28 #include "qofsession-p.h"
29 #include "cashobjects.h"
30 #include "test-dbi-stuff.h"
31 #include <unittest-support.h>
32 
33 #include "Account.h"
34 #include "Split.h"
35 #include "Transaction.h"
36 #include "gnc-commodity.h"
37 #include <SX-book.h>
38 #include <gnc-lot.h>
39 #include "../gnc-backend-dbi-priv.h"
40 
41 G_GNUC_UNUSED static QofLogModule log_module = "test-dbi";
42 
43 /* Placeholder for some old functions that need to be re-written and enabled */
44 static void do_test (G_GNUC_UNUSED gboolean foo, G_GNUC_UNUSED gchar* bar)
45 {
46 }
47 
48 void
49 do_compare( QofBook* book_1, QofBook* book_2, const gchar* id,
50  QofInstanceForeachCB cb, const gchar* msg )
51 {
52  QofCollection* coll;
53  CompareInfoStruct info;
54 
55  coll = qof_book_get_collection( book_1, id );
56  info.book_1 = book_1;
57  info.book_2 = book_2;
58  info.result = TRUE;
59  qof_collection_foreach(coll, cb, &info);
60 }
61 
62 static void
63 compare_account_trees( QofBook* book_1, QofBook* book_2 )
64 {
65  Account* root_1 = gnc_book_get_root_account( book_1 );
66  Account* root_2 = gnc_book_get_root_account( book_2 );
67 
68  xaccAccountSetHidden( root_1, xaccAccountGetHidden( root_1 ) );
69  g_assert (xaccAccountEqual( root_1, root_2, FALSE ));
70 }
71 
72 static void
73 compare_single_tx( QofInstance* inst, gpointer user_data )
74 {
75  CompareInfoStruct* info = (CompareInfoStruct*)user_data;
76  Transaction* tx_1 = GNC_TRANS(inst);
78  info->book_2 );
79 
80  g_assert (xaccTransEqual (tx_1, tx_2, TRUE, TRUE, TRUE, FALSE));
81 }
82 
83 static void
84 compare_txs( QofBook* book_1, QofBook* book_2 )
85 {
86  do_compare( book_1, book_2, GNC_ID_TRANS,
87  compare_single_tx, "Transaction lists match" );
88 }
89 
90 static SchedXaction*
91 get_sx_by_guid (QofBook* book, const GncGUID *guid)
92 {
93  SchedXactions *sxes = gnc_book_get_schedxactions (book);
94  GList *sxitem;
95  for (sxitem = sxes->sx_list; sxitem != NULL; sxitem = sxitem->next)
96  {
97  const GncGUID *sx_guid;
98  sx_guid = qof_instance_get_guid (QOF_INSTANCE(sxitem->data));
99  if (guid_equal (sx_guid, guid))
100  return sxitem->data;
101  }
102  return NULL;
103 }
104 
105 /* Be sure to put the control GDate first, otherwise a date that isn't
106  * properly carried over from the first instance won't assert.
107  */
108 
109 #define TEST_GDATES_EQUAL(gd1, gd2) \
110  if (g_date_valid (gd1)) \
111  { \
112  g_assert (g_date_valid (gd2)); \
113  g_assert (g_date_compare (gd1, gd2) == 0);\
114  }
115 
116 static void
117 compare_recurrences (GList *rl_1, GList *rl_2)
118 {
119  GList *ritem1, *ritem2;
120 
121  if (rl_1 == NULL)
122  return;
123 
124  g_assert (rl_2 != NULL);
125  g_assert_cmpint (g_list_length (rl_1), ==, g_list_length (rl_2));
126  for (ritem1 = rl_1, ritem2 = rl_2; ritem1 != NULL && ritem2 != NULL;
127  ritem1 = g_list_next (ritem1), ritem2 = g_list_next (ritem2))
128  {
129  Recurrence *r1 = ritem1->data, *r2 = ritem2->data;
130 
131  TEST_GDATES_EQUAL (&r1->start, &r2->start);
132  g_assert_cmpint (r1->ptype, ==, r2->ptype);
133  g_assert_cmpint (r1->mult, ==, r2->mult);
134  g_assert_cmpint (r1->wadj, ==, r2->wadj);
135  }
136 }
137 
138 static void
139 compare_single_sx( QofInstance* inst, gpointer user_data )
140 {
141  CompareInfoStruct* info = (CompareInfoStruct*)user_data;
142  SchedXaction* sx_1 = GNC_SCHEDXACTION (inst);
143  SchedXaction* sx_2 = get_sx_by_guid (info->book_2,
144  qof_instance_get_guid (inst));
145 
146  g_assert (sx_2 != NULL);
147  g_assert_cmpstr (sx_1->name, ==, sx_2->name);
148  compare_recurrences (sx_2->schedule, sx_1->schedule);
149  TEST_GDATES_EQUAL(&sx_2->last_date, &sx_1->last_date);
150  TEST_GDATES_EQUAL(&sx_2->start_date, &sx_1->start_date);
151  TEST_GDATES_EQUAL(&sx_2->end_date, &sx_1->end_date);
152  g_assert_cmpint (sx_2->num_occurances_total, ==,
153  sx_1->num_occurances_total);
154  g_assert_cmpint (sx_2->num_occurances_remain, ==,
155  sx_1->num_occurances_remain);
156  g_assert_cmpint (sx_2->instance_num, ==, sx_1->instance_num);
157  g_assert_cmpint (sx_2->enabled, ==, sx_1->enabled);
158  g_assert_cmpint (sx_2->autoCreateOption, ==, sx_1->autoCreateOption);
159  g_assert_cmpint (sx_2->autoCreateNotify, ==, sx_1->autoCreateNotify);
160  g_assert_cmpint (sx_2->advanceCreateDays, ==, sx_1->advanceCreateDays);
161  g_assert_cmpint (sx_2->advanceRemindDays, ==, sx_1->advanceRemindDays);
162 }
163 
164 static void
165 compare_sxs( QofBook* book_1, QofBook* book_2 )
166 {
167  do_compare( book_1, book_2, GNC_ID_SCHEDXACTION,
168  compare_single_sx, "Scheduled transaction lists match" );
169 }
170 
171 /* EFFECTIVE FRIEND FUNCTION */
172 extern KvpFrame *qof_instance_get_slots (const QofInstance *);
173 
174 static void
175 compare_single_lot( QofInstance* inst, gpointer user_data )
176 {
177  CompareInfoStruct* info = (CompareInfoStruct*)user_data;
178  GNCLot *lot_1 = GNC_LOT(inst);
179  GNCLot *lot_2 = gnc_lot_lookup (qof_instance_get_guid(inst),
180  info->book_2 );
181  GList *split1, *splits1, *splits2;
182 
183  g_assert (xaccAccountEqual( gnc_lot_get_account (lot_1),
184  gnc_lot_get_account (lot_2), FALSE ));
185  g_assert_cmpint (gnc_lot_is_closed (lot_1), ==, gnc_lot_is_closed (lot_2));
186 
187  g_assert (kvp_frame_compare (qof_instance_get_slots (QOF_INSTANCE (lot_1)),
188  qof_instance_get_slots (QOF_INSTANCE (lot_2))) == 0);
189  splits1 = gnc_lot_get_split_list (lot_1);
190  splits2 = gnc_lot_get_split_list (lot_2);
191  g_assert_cmpint (g_list_length (splits1), ==, g_list_length (splits2));
192  for (split1 = splits1; split1 != NULL; split1 = g_list_next (split1))
193  {
194  Split *split2;
195  g_assert (GNC_IS_SPLIT (split1->data));
196  split2 = xaccSplitLookup (qof_instance_get_guid (split1->data),
197  info->book_2);
198  g_assert (GNC_IS_SPLIT (split2));
199  g_assert (xaccSplitEqual (split1->data, split2, TRUE, TRUE, TRUE));
200  }
201 }
202 
203 static void
204 compare_lots( QofBook* book_1, QofBook* book_2 )
205 {
206  do_compare( book_1, book_2, GNC_ID_LOT, compare_single_lot, "Lot lists match" );
207 }
208 
209 static void
210 test_conn_index_functions( QofBackend *qbe )
211 {
212  GncDbiBackend *be = (GncDbiBackend*)qbe;
213  GncDbiSqlConnection *conn = (GncDbiSqlConnection*)(be->sql_be.conn);
214  GSList *index_list, *iter;
215 
216  index_list = conn->provider->get_index_list( be->conn );
217  g_test_message ( "Returned from index list\n");
218  g_assert (index_list != NULL);
219  g_assert_cmpint (g_slist_length( index_list ), ==, 4);
220  for ( iter = index_list; iter != NULL; iter = g_slist_next( iter) )
221  {
222  const char *errmsg;
223  conn->provider->drop_index (be->conn, iter->data);
224  g_assert (DBI_ERROR_NONE == dbi_conn_error( conn->conn, &errmsg));
225  }
226 
227  g_slist_free( index_list );
228 
229 
230 }
231 
232 static void
233 compare_pricedbs( QofBook* book_1, QofBook* book_2 )
234 {
235  do_compare( book_1, book_2, GNC_ID_TRANS,
236  compare_single_tx, "Transaction lists match" );
237 }
238 
239 void
240 compare_books( QofBook* book_1, QofBook* book_2 )
241 {
242  QofBackend *be = qof_book_get_backend( book_2 );
243  compare_account_trees( book_1, book_2 );
244  compare_pricedbs( book_1, book_2 );
245  compare_txs( book_1, book_2 );
246  compare_sxs( book_1, book_2 );
247  compare_lots( book_1, book_2 );
248 }
const GncGUID * qof_instance_get_guid(gconstpointer)
gint kvp_frame_compare(const KvpFrame *fa, const KvpFrame *fb)
GncSqlConnection * conn
API for Transactions and Splits (journal entries)
void qof_collection_foreach(const QofCollection *, QofInstanceForeachCB, gpointer user_data)
Definition: guid.h:65
gboolean xaccSplitEqual(const Split *sa, const Split *sb, gboolean check_guids, gboolean check_balances, gboolean check_txn_splits)
Definition: Split.c:815
Transaction * xaccTransLookup(const GncGUID *guid, QofBook *book)
Definition: Transaction.c:1024
Split * xaccSplitLookup(const GncGUID *guid, QofBook *book)
Definition: Split.c:1104
gboolean xaccTransEqual(const Transaction *ta, const Transaction *tb, gboolean check_guids, gboolean check_splits, gboolean check_balances, gboolean assume_ordered)
Definition: Transaction.c:857
Account handling public routines.
gboolean guid_equal(const GncGUID *guid_1, const GncGUID *guid_2)
SplitList * gnc_lot_get_split_list(const GNCLot *lot)
Definition: gnc-lot.c:417
Anchor Scheduled Transaction info in a book. See src/doc/books.txt for design overview.
gboolean xaccAccountEqual(const Account *aa, const Account *ab, gboolean check_guids)
Definition: Account.c:1475
void(* QofInstanceForeachCB)(QofInstance *, gpointer user_data)
Definition: qofid.h:186
gboolean xaccAccountGetHidden(const Account *acc)
Definition: Account.c:3959
gboolean gnc_lot_is_closed(GNCLot *lot)
Definition: gnc-lot.c:376
Definition: SplitP.h:71
void xaccAccountSetHidden(Account *acc, gboolean val)
Definition: Account.c:3970
struct KvpFrameImpl KvpFrame
Definition: kvp_frame.h:76
QofCollection * qof_book_get_collection(const QofBook *, QofIdType)
Account * gnc_lot_get_account(const GNCLot *lot)
Definition: gnc-lot.c:386
QofBackend * qof_book_get_backend(const QofBook *book)
Retrieve the backend used by this book.
API for Transactions and Splits (journal entries)
Commodity handling public routines.
const gchar * QofLogModule
Definition: qofid.h:89