GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
test-xml-pricedb.c
1 /***************************************************************************
2  * test-xml-pricedb.c
3  *
4  * Fri Oct 7 21:24:15 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 "cashobjects.h"
35 #include "gnc-engine.h"
36 #include "gnc-pricedb.h"
37 
38 #include "sixtp-parsers.h"
39 #include "sixtp-dom-parsers.h"
40 
41 #include "test-stuff.h"
42 #include "test-engine-stuff.h"
43 #include "test-file-stuff.h"
44 #include "io-gncxml-v2.h"
45 
46 static QofSession *session;
47 static int iter;
48 
49 static gboolean
50 test_add_pricedb (const char *tag, gpointer globaldata, gpointer data)
51 {
52  sixtp_gdv2 *gdata = globaldata;
53  GNCPriceDB *db = gnc_pricedb_get_db (gdata->book);
54 
55  do_test_args (gnc_pricedb_equal(data, db),
56  "gnc_pricedb_sixtp_parser_create",
57  __FILE__, __LINE__, "%d", iter);
58 
59  return TRUE;
60 }
61 
62 static void
63 test_db (GNCPriceDB *db)
64 {
65  xmlNodePtr test_node;
66  gchar *filename1;
67  int fd;
68  QofBook *book = qof_instance_get_book (QOF_INSTANCE (db));
69 
70  test_node = gnc_pricedb_dom_tree_create (db);
71 
72  if (!test_node && db)
73  {
74  failure_args ("pricedb_xml", __FILE__, __LINE__,
75  "gnc_pricedb_dom_tree_create returned NULL");
76  return;
77  }
78 
79  if (!db)
80  return;
81 
82  filename1 = g_strdup_printf ("test_file_XXXXXX");
83 
84  fd = g_mkstemp (filename1);
85 
86  write_dom_node_to_file (test_node, fd);
87 
88  close (fd);
89 
90  {
91  sixtp *parser;
92  load_counter lc;
93  sixtp_gdv2 data = {book, lc, NULL, NULL, FALSE};
94 
95  parser = sixtp_new ();
96 
97  if (!sixtp_add_some_sub_parsers
98  (parser, TRUE,
99  "gnc:pricedb", gnc_pricedb_sixtp_parser_create(),
100  NULL, NULL))
101  {
102  failure_args ("sixtp_add_some_sub_parsers failed",
103  __FILE__, __LINE__, "%d", iter);
104  }
105  else if (!gnc_xml_parse_file (parser, filename1, test_add_pricedb,
106  (gpointer)&data,
107  qof_session_get_book (session)))
108  {
109  failure_args ("gnc_xml_parse_file returned FALSE",
110  __FILE__, __LINE__, "%d", iter);
111  }
112  }
113 
114  g_unlink (filename1);
115  g_free (filename1);
116  xmlFreeNode (test_node);
117 }
118 
119 static void
120 test_generation (void)
121 {
122  for (iter = 0; iter < 20; iter++)
123  {
124  GNCPriceDB *db;
125  g_message("iter=%d", iter);
126  session = qof_session_new();
127  db = get_random_pricedb (qof_session_get_book (session));
128  if (!db)
129  {
130  failure_args ("gnc_random_price_db returned NULL",
131  __FILE__, __LINE__, "%d", iter);
132  return;
133  }
135  test_db (db);
136 
137  gnc_pricedb_destroy (db);
138  qof_session_end(session);
139  }
140 }
141 
142 int
143 main (int argc, char ** argv)
144 {
145  qof_init();
146  cashobjects_register();
147  //qof_log_init_filename("/tmp/gnctest.trace");
148  //qof_log_set_default(QOF_LOG_DETAIL);
149  //qof_log_set_level(GNC_MOD_PRICE, QOF_LOG_DETAIL);
150  session = qof_session_new ();
151  test_generation ();
152  print_test_results ();
153  qof_close();
154  exit(get_rv());
155 }
Definition: sixtp.h:93
QofBook * qof_instance_get_book(gconstpointer)
a simple price database for gnucash
gboolean gnc_pricedb_equal(GNCPriceDB *db1, GNCPriceDB *db2)
Definition: gnc-pricedb.c:949
GNCPriceDB * gnc_pricedb_get_db(QofBook *book)
Definition: gnc-pricedb.c:872
QofBook * qof_session_get_book(const QofSession *session)
api for GnuCash version 2 XML-based file format
void gnc_pricedb_destroy(GNCPriceDB *db)
Definition: gnc-pricedb.c:828
guint gnc_pricedb_get_num_prices(GNCPriceDB *db)
Definition: gnc-pricedb.c:894
All type declarations for the whole Gnucash engine.
void qof_close(void)
Safely close down the Query Object Framework.
void qof_session_end(QofSession *session)
void qof_init(void)
Initialise the Query Object Framework.