GnuCash 2.4.99
test-xml-pricedb.c
00001 /***************************************************************************
00002  *            test-xml-pricedb.c
00003  *
00004  *  Fri Oct  7 21:24:15 2005
00005  *  Copyright  2005  Neil Williams
00006  *  linux@codehelp.co.uk
00007  ****************************************************************************/
00008 /*
00009  *  This program is free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  This program is distributed in the hope that it will be useful,
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *  GNU General Public License for more details.
00018  *
00019  *  You should have received a copy of the GNU General Public License
00020  *  along with this program; if not, write to the Free Software
00021  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00022  *  02110-1301, USA.
00023  */
00024 
00025 #include "config.h"
00026 
00027 #include <glib.h>
00028 #include <glib/gstdio.h>
00029 #include <stdlib.h>
00030 #include <unistd.h>
00031 
00032 #include "gnc-xml-helper.h"
00033 #include "gnc-xml.h"
00034 #include "cashobjects.h"
00035 #include "gnc-engine.h"
00036 #include "gnc-pricedb.h"
00037 
00038 #include "sixtp-parsers.h"
00039 #include "sixtp-dom-parsers.h"
00040 
00041 #include "test-stuff.h"
00042 #include "test-engine-stuff.h"
00043 #include "test-file-stuff.h"
00044 
00045 #define GNC_V2_STRING "gnc-v2"
00046 const gchar *gnc_v2_xml_version_string = GNC_V2_STRING;
00047 
00048 static QofSession *session;
00049 
00050 struct pricedb_data_struct
00051 {
00052     GNCPriceDB *db;
00053     int value;
00054 };
00055 typedef struct pricedb_data_struct pricedb_data;
00056 
00057 static gboolean
00058 test_add_pricedb (const char *tag, gpointer globaldata, gpointer data)
00059 {
00060     pricedb_data *gdata = globaldata;
00061 
00062     do_test_args (gnc_pricedb_equal(data, gdata->db),
00063                   "gnc_pricedb_sixtp_parser_create",
00064                   __FILE__, __LINE__, "%d", gdata->value);
00065 
00066     return TRUE;
00067 }
00068 
00069 static void
00070 test_db (int i, GNCPriceDB *db)
00071 {
00072     xmlNodePtr test_node;
00073     gchar *filename1;
00074     int fd;
00075 
00076     test_node = gnc_pricedb_dom_tree_create (db);
00077 
00078     if (!test_node && db)
00079     {
00080         failure_args ("pricedb_xml", __FILE__, __LINE__,
00081                       "gnc_pricedb_dom_tree_create returned NULL");
00082         return;
00083     }
00084 
00085     if (!db)
00086         return;
00087 
00088     filename1 = g_strdup_printf ("test_file_XXXXXX");
00089 
00090     fd = g_mkstemp (filename1);
00091 
00092     write_dom_node_to_file (test_node, fd);
00093 
00094     close (fd);
00095 
00096     {
00097         sixtp *parser;
00098         pricedb_data data;
00099 
00100         data.db = db;
00101         data.value = i;
00102 
00103         parser = sixtp_new ();
00104 
00105         if (!sixtp_add_some_sub_parsers
00106                 (parser, TRUE,
00107                  "gnc:pricedb", gnc_pricedb_sixtp_parser_create(),
00108                  NULL, NULL))
00109         {
00110             failure_args ("sixtp_add_some_sub_parsers failed",
00111                           __FILE__, __LINE__, "%d", i);
00112         }
00113         else if (!gnc_xml_parse_file (parser, filename1, test_add_pricedb,
00114                                       (gpointer)&data,
00115                                       qof_session_get_book (session)))
00116         {
00117             failure_args ("gnc_xml_parse_file returned FALSE",
00118                           __FILE__, __LINE__, "%d", i);
00119         }
00120     }
00121 
00122     g_unlink (filename1);
00123     g_free (filename1);
00124     xmlFreeNode (test_node);
00125 }
00126 
00127 static void
00128 test_generation (void)
00129 {
00130     int i;
00131 
00132     for (i = 0; i < 20; i++)
00133     {
00134         GNCPriceDB *db;
00135         g_message("i=%d", i);
00136         session = qof_session_new();
00137         db = get_random_pricedb (qof_session_get_book (session));
00138         if (!db)
00139         {
00140             failure_args ("gnc_random_price_db returned NULL",
00141                           __FILE__, __LINE__, "%d", i);
00142             return;
00143         }
00144         if (gnc_pricedb_get_num_prices (db))
00145             test_db (i, db);
00146 
00147         gnc_pricedb_destroy (db);
00148         qof_session_end(session);
00149     }
00150 }
00151 
00152 int
00153 main (int argc, char ** argv)
00154 {
00155     qof_init();
00156     cashobjects_register();
00157     //qof_log_init_filename("/tmp/gnctest.trace");
00158     //qof_log_set_default(QOF_LOG_DETAIL);
00159     //qof_log_set_level(GNC_MOD_PRICE, QOF_LOG_DETAIL);
00160     session = qof_session_new ();
00161     test_generation ();
00162     print_test_results ();
00163     qof_close();
00164     exit(get_rv());
00165 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines