2005-11-02 03:32:36 +00:00
|
|
|
/***************************************************************************
|
|
|
|
|
* test-xml-account.c
|
|
|
|
|
*
|
|
|
|
|
* Sun Oct 9 15:37:26 2005
|
|
|
|
|
* Copyright 2005 Neil Williams
|
|
|
|
|
* linux@codehelp.co.uk
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
/*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
2005-11-17 05:35:02 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
|
* 02110-1301, USA.
|
2005-11-02 03:32:36 +00:00
|
|
|
*/
|
|
|
|
|
|
2001-08-07 23:29:04 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#include "gnc-xml-helper.h"
|
|
|
|
|
#include "gnc-xml.h"
|
|
|
|
|
#include "gnc-engine.h"
|
2005-11-02 03:32:36 +00:00
|
|
|
#include "cashobjects.h"
|
2001-08-07 23:29:04 +00:00
|
|
|
#include "sixtp-parsers.h"
|
|
|
|
|
#include "sixtp-dom-parsers.h"
|
|
|
|
|
|
2001-08-18 20:33:51 +00:00
|
|
|
#include "test-stuff.h"
|
|
|
|
|
#include "test-engine-stuff.h"
|
|
|
|
|
#include "test-file-stuff.h"
|
2001-08-07 23:29:04 +00:00
|
|
|
|
|
|
|
|
#include "Account.h"
|
|
|
|
|
#include "Group.h"
|
|
|
|
|
#include "Scrub.h"
|
|
|
|
|
|
2005-11-02 03:32:36 +00:00
|
|
|
static QofBook *sixbook;
|
2001-10-03 10:07:45 +00:00
|
|
|
|
2001-08-07 23:29:04 +00:00
|
|
|
static gchar*
|
|
|
|
|
node_and_account_equal(xmlNodePtr node, Account *act)
|
|
|
|
|
{
|
|
|
|
|
xmlNodePtr mark;
|
|
|
|
|
|
2005-11-02 03:32:36 +00:00
|
|
|
while (safe_strcmp ((char*)node->name, "text") == 0)
|
|
|
|
|
{
|
2001-10-11 11:35:50 +00:00
|
|
|
node = node->next;
|
2005-11-02 03:32:36 +00:00
|
|
|
}
|
2001-10-11 11:35:50 +00:00
|
|
|
|
2001-08-07 23:29:04 +00:00
|
|
|
if(!check_dom_tree_version(node, "2.0.0"))
|
|
|
|
|
{
|
|
|
|
|
return g_strdup("version wrong. Not 2.0.0 or not there");
|
|
|
|
|
}
|
|
|
|
|
|
2005-11-02 03:32:36 +00:00
|
|
|
if(!node->name || safe_strcmp((char*)node->name, "gnc:account"))
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
|
|
|
|
return g_strdup("Name of toplevel node is bad");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(mark = node->xmlChildrenNode; mark; mark = mark->next)
|
|
|
|
|
{
|
2005-11-02 03:32:36 +00:00
|
|
|
if(safe_strcmp((char*)mark->name, "text") == 0)
|
2001-10-11 11:35:50 +00:00
|
|
|
{
|
|
|
|
|
}
|
2005-11-02 03:32:36 +00:00
|
|
|
else if(safe_strcmp((char*)mark->name, "act:name") == 0)
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
|
|
|
|
if(!equals_node_val_vs_string(mark, xaccAccountGetName(act)))
|
|
|
|
|
{
|
|
|
|
|
return g_strdup("names differ");
|
|
|
|
|
}
|
|
|
|
|
}
|
2005-11-02 03:32:36 +00:00
|
|
|
else if(safe_strcmp((char*)mark->name, "act:id") == 0)
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
|
|
|
|
if(!equals_node_val_vs_guid(mark, xaccAccountGetGUID(act)))
|
|
|
|
|
{
|
|
|
|
|
return g_strdup("ids differ");
|
|
|
|
|
}
|
|
|
|
|
}
|
2005-11-02 03:32:36 +00:00
|
|
|
else if(safe_strcmp((char*)mark->name, "act:type") == 0)
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
|
|
|
|
gchar *txt;
|
|
|
|
|
int type;
|
|
|
|
|
|
|
|
|
|
txt = dom_tree_to_text(mark);
|
|
|
|
|
|
|
|
|
|
if(!txt)
|
|
|
|
|
{
|
|
|
|
|
return g_strdup("couldn't get type string");
|
|
|
|
|
}
|
|
|
|
|
else if(!xaccAccountStringToType(txt, &type))
|
|
|
|
|
{
|
|
|
|
|
g_free(txt);
|
|
|
|
|
return g_strdup("couldn't convert type string to int");
|
|
|
|
|
}
|
|
|
|
|
else if(type != xaccAccountGetType(act))
|
|
|
|
|
{
|
|
|
|
|
g_free(txt);
|
|
|
|
|
return g_strdup("types differ");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
g_free(txt);
|
|
|
|
|
}
|
|
|
|
|
}
|
2005-11-02 03:32:36 +00:00
|
|
|
else if(safe_strcmp((char*)mark->name, "act:commodity") == 0)
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
|
|
|
|
if(!equals_node_val_vs_commodity(
|
2002-01-02 04:28:32 +00:00
|
|
|
mark, xaccAccountGetCommodity(act), xaccAccountGetBook(act)))
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
|
|
|
|
return g_strdup("commodities differ");
|
|
|
|
|
}
|
|
|
|
|
}
|
2005-11-02 03:32:36 +00:00
|
|
|
else if(safe_strcmp((char*)mark->name, "act:code") == 0)
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
|
|
|
|
if(!equals_node_val_vs_string(mark, xaccAccountGetCode(act)))
|
|
|
|
|
{
|
|
|
|
|
return g_strdup("codes differ");
|
|
|
|
|
}
|
|
|
|
|
}
|
2005-11-02 03:32:36 +00:00
|
|
|
else if(safe_strcmp((char*)mark->name, "act:description") == 0)
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
|
|
|
|
if(!equals_node_val_vs_string(
|
|
|
|
|
mark, xaccAccountGetDescription(act)))
|
|
|
|
|
{
|
|
|
|
|
return g_strdup("descriptions differ");
|
|
|
|
|
}
|
|
|
|
|
}
|
2005-11-02 03:32:36 +00:00
|
|
|
else if(safe_strcmp((char*)mark->name, "act:slots") == 0)
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
2003-09-09 15:05:53 +00:00
|
|
|
/* xaccAccountDeleteOldData (act); */
|
2001-08-07 23:29:04 +00:00
|
|
|
|
|
|
|
|
if(!equals_node_val_vs_kvp_frame(mark, xaccAccountGetSlots(act)))
|
|
|
|
|
{
|
|
|
|
|
return g_strdup("slots differ");
|
|
|
|
|
}
|
|
|
|
|
}
|
2005-11-02 03:32:36 +00:00
|
|
|
else if(safe_strcmp((char*)mark->name, "act:parent") == 0)
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
|
|
|
|
if(!equals_node_val_vs_guid(
|
|
|
|
|
mark, xaccAccountGetGUID(xaccGroupGetParentAccount(
|
|
|
|
|
xaccAccountGetParent(act)))))
|
|
|
|
|
{
|
|
|
|
|
return g_strdup("parent ids differ");
|
|
|
|
|
}
|
|
|
|
|
}
|
2005-11-02 03:32:36 +00:00
|
|
|
else if(safe_strcmp((char*)mark->name, "act:commodity-scu") == 0)
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
|
|
|
|
if(!equals_node_val_vs_int(mark, xaccAccountGetCommoditySCU(act)))
|
|
|
|
|
{
|
|
|
|
|
return g_strdup("commodity scus differ");
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-01-12 21:26:01 +00:00
|
|
|
else if (safe_strcmp((char*)mark->name, "act:security") == 0)
|
|
|
|
|
{
|
|
|
|
|
return NULL; // This tag is ignored.
|
|
|
|
|
}
|
2001-08-07 23:29:04 +00:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return g_strdup_printf("unknown node in dom tree: %s", mark->name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
delete_random_account(Account *act)
|
|
|
|
|
{
|
|
|
|
|
xaccAccountBeginEdit(act);
|
|
|
|
|
xaccAccountDestroy(act);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct act_data_struct
|
|
|
|
|
{
|
|
|
|
|
Account *act;
|
|
|
|
|
int value;
|
|
|
|
|
};
|
|
|
|
|
typedef struct act_data_struct act_data;
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
test_add_account(const char *tag, gpointer globaldata, gpointer data)
|
|
|
|
|
{
|
2001-10-12 10:50:35 +00:00
|
|
|
Account *account = data;
|
2001-08-07 23:29:04 +00:00
|
|
|
act_data *gdata = (act_data*)globaldata;
|
2001-10-12 10:50:35 +00:00
|
|
|
gnc_commodity * com;
|
|
|
|
|
gnc_commodity * new_com;
|
|
|
|
|
gnc_commodity_table *t;
|
|
|
|
|
|
|
|
|
|
com = xaccAccountGetCommodity (account);
|
|
|
|
|
|
2005-11-02 03:32:36 +00:00
|
|
|
t = gnc_book_get_commodity_table (sixbook);
|
2001-10-12 10:50:35 +00:00
|
|
|
|
|
|
|
|
new_com = gnc_commodity_table_lookup (t,
|
|
|
|
|
gnc_commodity_get_namespace (com),
|
|
|
|
|
gnc_commodity_get_mnemonic (com));
|
|
|
|
|
|
2005-11-02 03:32:36 +00:00
|
|
|
if (new_com) { xaccAccountSetCommodity (account, new_com); }
|
2001-10-03 10:07:45 +00:00
|
|
|
|
2005-11-02 03:32:36 +00:00
|
|
|
do_test_args(xaccAccountEqual((Account*)account, (Account*)(gdata->act),
|
2001-08-07 23:29:04 +00:00
|
|
|
TRUE),
|
|
|
|
|
"gnc_account_sixtp_parser_create",
|
2003-09-01 15:08:26 +00:00
|
|
|
__FILE__, __LINE__, "%d", gdata->value );
|
2001-08-07 23:29:04 +00:00
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
test_account(int i, Account *test_act)
|
|
|
|
|
{
|
|
|
|
|
xmlNodePtr test_node;
|
|
|
|
|
gchar *filename1;
|
|
|
|
|
gchar *compare_msg;
|
|
|
|
|
int fd;
|
|
|
|
|
|
2003-01-05 05:49:27 +00:00
|
|
|
test_node = gnc_account_dom_tree_create(test_act, FALSE);
|
2001-08-07 23:29:04 +00:00
|
|
|
|
|
|
|
|
if(!test_node)
|
|
|
|
|
{
|
|
|
|
|
failure_args("account_xml", __FILE__, __LINE__,
|
|
|
|
|
"gnc_account_dom_tree_create returned NULL");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if((compare_msg = node_and_account_equal(test_node, test_act)) != NULL)
|
|
|
|
|
{
|
|
|
|
|
failure_args("account_xml", __FILE__, __LINE__,
|
|
|
|
|
"node and account were not equal: %s", compare_msg);
|
|
|
|
|
xmlElemDump(stdout, NULL, test_node);
|
|
|
|
|
fprintf(stdout, "\n");
|
|
|
|
|
xmlFreeNode(test_node);
|
|
|
|
|
g_free(compare_msg);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
success("account_xml");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
filename1 = g_strdup_printf("test_file_XXXXXX");
|
|
|
|
|
|
2006-11-14 20:58:21 +00:00
|
|
|
fd = g_mkstemp(filename1);
|
2001-08-07 23:29:04 +00:00
|
|
|
|
|
|
|
|
write_dom_node_to_file(test_node, fd);
|
|
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
sixtp *parser;
|
|
|
|
|
act_data data;
|
|
|
|
|
|
|
|
|
|
data.act = test_act;
|
|
|
|
|
data.value = i;
|
2001-10-03 10:07:45 +00:00
|
|
|
|
2001-08-07 23:29:04 +00:00
|
|
|
parser = gnc_account_sixtp_parser_create();
|
|
|
|
|
|
|
|
|
|
if(!gnc_xml_parse_file(parser, filename1, test_add_account,
|
2005-11-02 03:32:36 +00:00
|
|
|
&data, sixbook))
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
|
|
|
|
failure_args("gnc_xml_parse_file returned FALSE",
|
|
|
|
|
__FILE__, __LINE__, "%d", i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* no handling of circular data structures. We'll do that later */
|
|
|
|
|
/* sixtp_destroy(parser); */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unlink(filename1);
|
|
|
|
|
g_free(filename1);
|
|
|
|
|
xmlFreeNode(test_node);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2005-11-02 03:32:36 +00:00
|
|
|
test_generation()
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
|
|
|
|
int i;
|
2001-10-03 10:07:45 +00:00
|
|
|
|
2001-08-07 23:29:04 +00:00
|
|
|
for(i = 0; i < 20; i++)
|
|
|
|
|
{
|
|
|
|
|
Account *ran_act;
|
|
|
|
|
|
2005-11-02 03:32:36 +00:00
|
|
|
ran_act = get_random_account(sixbook);
|
2001-08-07 23:29:04 +00:00
|
|
|
|
|
|
|
|
test_account(i, ran_act);
|
2001-10-03 10:07:45 +00:00
|
|
|
|
2001-08-07 23:29:04 +00:00
|
|
|
delete_random_account(ran_act);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
/* empty some things. */
|
|
|
|
|
Account *act;
|
|
|
|
|
|
2005-11-02 03:32:36 +00:00
|
|
|
act = get_random_account(sixbook);
|
2001-08-07 23:29:04 +00:00
|
|
|
|
|
|
|
|
xaccAccountSetCode(act, "");
|
|
|
|
|
xaccAccountSetDescription(act, "");
|
|
|
|
|
xaccAccountSetCommodity(act, NULL);
|
|
|
|
|
|
|
|
|
|
test_account(-1, act);
|
|
|
|
|
|
|
|
|
|
delete_random_account(act);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* { */
|
|
|
|
|
/* Account *act1; */
|
|
|
|
|
/* Account *act2; */
|
|
|
|
|
|
|
|
|
|
/* act1 = get_random_account(); */
|
|
|
|
|
/* act2 = get_random_account(); */
|
|
|
|
|
|
|
|
|
|
/* xaccAccountInsertSubAccount(act1, act2); */
|
|
|
|
|
|
|
|
|
|
/* test_account(-1, act2); */
|
|
|
|
|
/* test_account(-1, act1); */
|
|
|
|
|
|
|
|
|
|
/* delete_random_account(act2); */
|
|
|
|
|
/* delete_random_account(act1); */
|
|
|
|
|
/* } */
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
test_real_account(const char *tag, gpointer global_data, gpointer data)
|
|
|
|
|
{
|
|
|
|
|
char *msg;
|
|
|
|
|
Account *act = (Account*)data;
|
2001-10-03 10:07:45 +00:00
|
|
|
|
2001-08-07 23:29:04 +00:00
|
|
|
if(!xaccAccountGetParent(act))
|
|
|
|
|
{
|
2005-11-02 03:32:36 +00:00
|
|
|
xaccGroupInsertAccount(xaccGetAccountGroup(sixbook), act);
|
2001-08-07 23:29:04 +00:00
|
|
|
}
|
2001-10-03 10:07:45 +00:00
|
|
|
|
2001-08-07 23:29:04 +00:00
|
|
|
msg = node_and_account_equal((xmlNodePtr)global_data, act);
|
|
|
|
|
do_test_args(msg == NULL, "test_real_account",
|
|
|
|
|
__FILE__, __LINE__, msg);
|
2001-10-03 10:07:45 +00:00
|
|
|
|
2001-08-07 23:29:04 +00:00
|
|
|
g_free(msg);
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2005-11-02 03:32:36 +00:00
|
|
|
int
|
|
|
|
|
main (int argc, char ** argv)
|
2001-08-07 23:29:04 +00:00
|
|
|
{
|
2005-11-02 03:32:36 +00:00
|
|
|
QofSession *session;
|
2001-08-07 23:29:04 +00:00
|
|
|
|
2005-11-02 03:32:36 +00:00
|
|
|
qof_init();
|
|
|
|
|
cashobjects_register();
|
|
|
|
|
session = qof_session_new();
|
|
|
|
|
sixbook = qof_session_get_book (session);
|
2001-08-07 23:29:04 +00:00
|
|
|
if(argc > 1)
|
|
|
|
|
{
|
|
|
|
|
test_files_in_dir(argc, argv, test_real_account,
|
|
|
|
|
gnc_account_sixtp_parser_create(),
|
2005-11-02 03:32:36 +00:00
|
|
|
"gnc:account", sixbook);
|
2001-08-07 23:29:04 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
test_generation();
|
|
|
|
|
}
|
2006-01-12 21:26:01 +00:00
|
|
|
|
|
|
|
|
qof_session_destroy(session);
|
2001-08-07 23:29:04 +00:00
|
|
|
print_test_results();
|
2005-11-02 03:32:36 +00:00
|
|
|
qof_close();
|
2007-01-15 20:28:01 +00:00
|
|
|
exit(get_rv());
|
2001-10-03 10:07:45 +00:00
|
|
|
}
|