GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
test-xml-account.c
1 /***************************************************************************
2  * test-xml-account.c
3  *
4  * Sun Oct 9 15:37:26 2005
5  * Copyright 2005 Neil Williams
7  ****************************************************************************/
8 /*
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301, USA.
23  */
24 
25 #include "config.h"
26 
27 #include <glib.h>
28 #include <glib/gstdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 
32 #include "../gnc-xml-helper.h"
33 #include "../gnc-xml.h"
34 #include <gnc-engine.h>
35 #include <cashobjects.h>
36 #include "../sixtp-parsers.h"
37 #include "../sixtp-dom-parsers.h"
38 
39 #include <test-stuff.h>
40 #include <test-engine-stuff.h>
41 #include <test-file-stuff.h>
42 #include <unittest-support.h>
43 
44 #include "Account.h"
45 #include "Scrub.h"
46 
47 static QofBook *sixbook;
48 
49 static gchar*
50 node_and_account_equal(xmlNodePtr node, Account *act)
51 {
52  xmlNodePtr mark;
53 
54  while (g_strcmp0 ((char*)node->name, "text") == 0)
55  {
56  node = node->next;
57  }
58 
59  if (!check_dom_tree_version(node, "2.0.0"))
60  {
61  return g_strdup("version wrong. Not 2.0.0 or not there");
62  }
63 
64  if (!node->name || g_strcmp0((char*)node->name, "gnc:account"))
65  {
66  return g_strdup("Name of toplevel node is bad");
67  }
68 
69  for (mark = node->xmlChildrenNode; mark; mark = mark->next)
70  {
71  if (g_strcmp0((char*)mark->name, "text") == 0)
72  {
73  }
74  else if (g_strcmp0((char*)mark->name, "act:name") == 0)
75  {
76  if (!equals_node_val_vs_string(mark, xaccAccountGetName(act)))
77  {
78  return g_strdup("names differ");
79  }
80  }
81  else if (g_strcmp0((char*)mark->name, "act:id") == 0)
82  {
83  if (!equals_node_val_vs_guid(mark, xaccAccountGetGUID(act)))
84  {
85  return g_strdup("ids differ");
86  }
87  }
88  else if (g_strcmp0((char*)mark->name, "act:type") == 0)
89  {
90  gchar *txt;
91  int type;
92 
93  txt = dom_tree_to_text(mark);
94 
95  if (!txt)
96  {
97  return g_strdup("couldn't get type string");
98  }
99  else if (!xaccAccountStringToType(txt, &type))
100  {
101  g_free(txt);
102  return g_strdup("couldn't convert type string to int");
103  }
104  else if (type != xaccAccountGetType(act))
105  {
106  g_free(txt);
107  return g_strdup("types differ");
108  }
109  else
110  {
111  g_free(txt);
112  }
113  }
114  else if (g_strcmp0((char*)mark->name, "act:commodity") == 0)
115  {
116  /* This is somewhat BS, because if the commodity isn't a
117  currency (and therefore built in) there isn't a
118  corresponding currency in the XML, skip the test. jralls
119  2010-11-02 */
120  if (xaccAccountGetCommodity(act) == NULL) continue;
121  if (!equals_node_val_vs_commodity(
122  mark, xaccAccountGetCommodity(act),
123  gnc_account_get_book(act)))
124  {
125  return g_strdup("commodities differ");
126  }
127  }
128  else if (g_strcmp0((char*)mark->name, "act:code") == 0)
129  {
130  if (!equals_node_val_vs_string(mark, xaccAccountGetCode(act)))
131  {
132  return g_strdup("codes differ");
133  }
134  }
135  else if (g_strcmp0((char*)mark->name, "act:description") == 0)
136  {
137  if (!equals_node_val_vs_string(
138  mark, xaccAccountGetDescription(act)))
139  {
140  return g_strdup("descriptions differ");
141  }
142  }
143  else if (g_strcmp0((char*)mark->name, "act:slots") == 0)
144  {
145  /* xaccAccountDeleteOldData (act); */
146 
147  if (!equals_node_val_vs_kvp_frame(mark, qof_instance_get_slots(QOF_INSTANCE (act))))
148  {
149  return g_strdup("slots differ");
150  }
151  }
152  else if (g_strcmp0((char*)mark->name, "act:parent") == 0)
153  {
154  if (!equals_node_val_vs_guid(
156  {
157  return g_strdup("parent ids differ");
158  }
159  }
160  else if (g_strcmp0((char*)mark->name, "act:commodity-scu") == 0)
161  {
162  if (!equals_node_val_vs_int(mark, xaccAccountGetCommoditySCU(act)))
163  {
164  return g_strdup("commodity scus differ");
165  }
166  }
167  else if (g_strcmp0((char*)mark->name, "act:hidden") == 0)
168  {
169  if (!equals_node_val_vs_boolean(mark, xaccAccountGetHidden(act)))
170  {
171  return g_strdup("Hidden flags differ");
172  }
173  }
174  else if (g_strcmp0((char*)mark->name, "act:placeholder") == 0)
175  {
176  if (!equals_node_val_vs_boolean(mark, xaccAccountGetPlaceholder(act)))
177  {
178  return g_strdup("Placeholder flags differ");
179  }
180  }
181  else if (g_strcmp0((char*)mark->name, "act:security") == 0)
182  {
183  return NULL; // This tag is ignored.
184  }
185  else
186  {
187  return g_strdup_printf("unknown node in dom tree: %s", mark->name);
188  }
189  }
190 
191  return NULL;
192 }
193 
194 static void
195 delete_random_account(Account *act)
196 {
198  xaccAccountDestroy(act);
199 }
200 
202 {
203  Account *act;
204  int value;
205 };
206 typedef struct act_data_struct act_data;
207 
208 static gboolean
209 test_add_account(const char *tag, gpointer globaldata, gpointer data)
210 {
211  Account *account = data;
212  act_data *gdata = (act_data*)globaldata;
213  gnc_commodity * com;
214  gnc_commodity * new_com;
216 
217  com = xaccAccountGetCommodity (account);
218 
219  t = gnc_commodity_table_get_table (sixbook);
220 
221  new_com = gnc_commodity_table_lookup (t,
224 
225  if (new_com)
226  {
227  xaccAccountSetCommodity (account, new_com);
228  }
229 
230  do_test_args(xaccAccountEqual((Account*)account, (Account*)(gdata->act),
231  TRUE),
232  "gnc_account_sixtp_parser_create",
233  __FILE__, __LINE__, "%d", gdata->value );
234 
235  return TRUE;
236 }
237 
238 static void
239 test_account(int i, Account *test_act)
240 {
241  xmlNodePtr test_node;
242  gchar *filename1;
243  gchar *compare_msg;
244  int fd;
245 
246  test_node = gnc_account_dom_tree_create(test_act, FALSE, TRUE);
247 
248  if (!test_node)
249  {
250  failure_args("account_xml", __FILE__, __LINE__,
251  "gnc_account_dom_tree_create returned NULL");
252  return;
253  }
254 
255  if ((compare_msg = node_and_account_equal(test_node, test_act)) != NULL)
256  {
257  failure_args("account_xml", __FILE__, __LINE__,
258  "node and account were not equal: %s", compare_msg);
259  xmlElemDump(stdout, NULL, test_node);
260  fprintf(stdout, "\n");
261  xmlFreeNode(test_node);
262  g_free(compare_msg);
263  return;
264  }
265  else
266  {
267  success("account_xml");
268  }
269 
270  filename1 = g_strdup_printf("test_file_XXXXXX");
271 
272  fd = g_mkstemp(filename1);
273 
274  write_dom_node_to_file(test_node, fd);
275 
276  close(fd);
277 
278  {
279  sixtp *parser;
280  act_data data;
281 
282  data.act = test_act;
283  data.value = i;
284 
285  parser = gnc_account_sixtp_parser_create();
286 
287  if (!gnc_xml_parse_file(parser, filename1, test_add_account,
288  &data, sixbook))
289  {
290  failure_args("gnc_xml_parse_file returned FALSE",
291  __FILE__, __LINE__, "%d", i);
292  }
293 
294  /* no handling of circular data structures. We'll do that later */
295  /* sixtp_destroy(parser); */
296  }
297 
298 
299  g_unlink(filename1);
300  g_free(filename1);
301  xmlFreeNode(test_node);
302 }
303 
304 static void
305 test_generation()
306 {
307  int i;
308 
309  for (i = 0; i < 20; i++)
310  {
311  Account *ran_act;
312 
313  ran_act = get_random_account(sixbook);
314 
315  test_account(i, ran_act);
316 
317  delete_random_account(ran_act);
318  }
319 
320  {
321  /* empty some things. */
322  Account *act;
323  gchar *msg = "xaccAccountSetCommodity: assertion `GNC_IS_COMMODITY(com)' failed";
324  gchar *logdomain = "gnc.engine";
325  guint loglevel = G_LOG_LEVEL_CRITICAL;
326  TestErrorStruct check = { loglevel, logdomain, msg };
327  g_log_set_handler (logdomain, loglevel,
328  (GLogFunc)test_checked_handler, &check);
329 
330  act = get_random_account(sixbook);
331 
332  xaccAccountSetCode(act, "");
333  xaccAccountSetDescription(act, "");
334 
335  xaccAccountSetCommodity(act, NULL);
336 
337  test_account(-1, act);
338 
339  delete_random_account(act);
340  }
341 
342  /* { */
343  /* Account *act1; */
344  /* Account *act2; */
345 
346  /* act1 = get_random_account(); */
347  /* act2 = get_random_account(); */
348 
349  /* gnc_account_append_child(act1, act2); */
350 
351  /* test_account(-1, act2); */
352  /* test_account(-1, act1); */
353 
354  /* delete_random_account(act2); */
355  /* delete_random_account(act1); */
356  /* } */
357 
358 }
359 
360 static gboolean
361 test_real_account(const char *tag, gpointer global_data, gpointer data)
362 {
363  char *msg;
364  Account *act = (Account*)data;
365 
366  if (!gnc_account_get_parent(act))
367  {
368  gnc_account_append_child(gnc_book_get_root_account(sixbook), act);
369  }
370 
371  msg = node_and_account_equal((xmlNodePtr)global_data, act);
372  do_test_args(msg == NULL, "test_real_account",
373  __FILE__, __LINE__, msg);
374 
375  g_free(msg);
376  return TRUE;
377 }
378 
379 int
380 main (int argc, char ** argv)
381 {
382  QofSession *session;
383 
384  qof_init();
385  cashobjects_register();
386  session = qof_session_new();
387  sixbook = qof_session_get_book (session);
388  if (argc > 1)
389  {
390  test_files_in_dir(argc, argv, test_real_account,
391  gnc_account_sixtp_parser_create(),
392  "gnc:account", sixbook);
393  }
394  else
395  {
396  test_generation();
397  }
398 
399  qof_session_destroy(session);
400  print_test_results();
401  qof_close();
402  exit(get_rv());
403 }
Account * gnc_account_get_parent(const Account *acc)
Definition: Account.c:2623
gnc_commodity_table * gnc_commodity_table_get_table(QofBook *book)
Definition: sixtp.h:93
void gnc_account_append_child(Account *new_parent, Account *child)
Definition: Account.c:2525
const char * gnc_commodity_get_mnemonic(const gnc_commodity *cm)
GNCAccountType xaccAccountGetType(const Account *acc)
Definition: Account.c:3009
int xaccAccountGetCommoditySCU(const Account *acc)
Definition: Account.c:2458
const char * xaccAccountGetCode(const Account *acc)
Definition: Account.c:3086
void xaccAccountSetCode(Account *acc, const char *str)
Definition: Account.c:2249
const char * gnc_commodity_get_namespace(const gnc_commodity *cm)
void xaccAccountDestroy(Account *acc)
Definition: Account.c:1400
#define xaccAccountGetGUID(X)
Definition: Account.h:239
convert single-entry accounts to clean double-entry
QofBook * qof_session_get_book(const QofSession *session)
Account handling public routines.
const char * xaccAccountGetDescription(const Account *acc)
Definition: Account.c:3093
gboolean xaccAccountStringToType(const char *str, GNCAccountType *type)
Definition: Account.c:4064
gboolean xaccAccountEqual(const Account *aa, const Account *ab, gboolean check_guids)
Definition: Account.c:1475
All type declarations for the whole Gnucash engine.
gboolean xaccAccountGetHidden(const Account *acc)
Definition: Account.c:3959
void qof_close(void)
Safely close down the Query Object Framework.
void xaccAccountBeginEdit(Account *acc)
Definition: Account.c:1280
gnc_commodity * xaccAccountGetCommodity(const Account *acc)
Definition: Account.c:3148
gboolean xaccAccountGetPlaceholder(const Account *acc)
Definition: Account.c:3912
void xaccAccountSetDescription(Account *acc, const char *str)
Definition: Account.c:2268
const char * xaccAccountGetName(const Account *acc)
Definition: Account.c:3031
void qof_init(void)
Initialise the Query Object Framework.
void xaccAccountSetCommodity(Account *acc, gnc_commodity *com)
Definition: Account.c:2389