Add some dbi backend tests to test db save/load.

In order to test the mysql backend, the --with-test-mysql-url=URL option must be supplied to
configure where URL is the full url (mysql://host[:port]:db:user:password) to access a mysql db.
The same is true for postgres, with the --with-test-pgsql-url=URL option.


git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@18293 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Phil Longstaff
2009-09-05 00:35:40 +00:00
parent 82fe7a2f57
commit 27965d9484
8 changed files with 650 additions and 4 deletions

View File

@@ -926,6 +926,24 @@ else
AC_MSG_RESULT(yes - $GMSGFMT)
fi
# -------------------------------------------------------------------------
# URLs for MySQL and PostgreSQL testing
AC_ARG_WITH(test-mysql-url,
AS_HELP_STRING([--with-test-mysql-url=URL],
[MySQL database URL for testing [default=none]]),
[],[with_test_mysql_url=""])
TEST_MYSQL_URL=$with_test_mysql_url
AC_SUBST(TEST_MYSQL_URL)
AC_ARG_WITH(test-pgsql-url,
AS_HELP_STRING([--with-test-pgsql-url=URL],
[PgSQL database URL for testing [default=none]]),
[],[with_test_pgsql_url=""])
TEST_PGSQL_URL=$with_test_pgsql_url
AC_SUBST(TEST_PGSQL_URL)
### --------------------------------------------------------------------------
### help files

View File

@@ -1,6 +1,16 @@
SUBDIRS = .
test_dbi_basic_SOURCES = \
test-dbi-basic.c \
test-dbi-stuff.c
test_dbi_SOURCES = \
test-dbi.c \
test-dbi-stuff.c
TESTS = \
test-dbi-basic \
test-dbi \
test-load-backend
GNC_TEST_DEPS = \
@@ -9,8 +19,7 @@ GNC_TEST_DEPS = \
--library-dir ${top_builddir}/lib/libqof/qof \
--library-dir ${top_builddir}/src/core-utils \
--library-dir ${top_builddir}/src/gnc-module \
--library-dir ${top_builddir}/src/engine \
--library-dir ${top_builddir}/src/backend/gda
--library-dir ${top_builddir}/src/engine
TESTS_ENVIRONMENT = \
GNC_ACCOUNT_PATH=${top_srcdir}/accounts/C \
@@ -18,6 +27,8 @@ TESTS_ENVIRONMENT = \
$(shell ${top_srcdir}/src/gnc-test-env --no-exports ${GNC_TEST_DEPS})
check_PROGRAMS = \
test-dbi-basic \
test-dbi \
test-load-backend
#noinst_HEADERS = test-file-stuff.h
@@ -38,9 +49,10 @@ AM_CFLAGS = \
-I${top_srcdir}/src/test-core \
-I${top_srcdir}/src/engine \
-I${top_srcdir}/src/engine/test-core \
-I${top_srcdir}/src/backend/gda \
-I${top_srcdir}/src/backend/qsf \
-I${top_srcdir}/src/libqof/qof \
-DTEST_MYSQL_URL=\"${TEST_MYSQL_URL}\" \
-DTEST_PGSQL_URL=\"${TEST_PGSQL_URL}\" \
${GLIB_CFLAGS} \
${GUILE_INCS} \
${GCONF_CFLAGS}

View File

@@ -0,0 +1,102 @@
/***************************************************************************
* test-dbi.c
*
* Tests saving and loading to a dbi/sqlite3 db
*
* Copyright (C) 2009 Phil Longstaff <plongstaff@rogers.com>
****************************************************************************/
/*
* 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
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "config.h"
#include "qof.h"
#include "cashobjects.h"
#include "test-engine-stuff.h"
#include "test-stuff.h"
#include "test-dbi-stuff.h"
#include "Account.h"
#include "Split.h"
#include "gnc-commodity.h"
#define FILE_NAME "sqlite3:///tmp/test-sqlite3-file"
#define GNC_LIB_NAME "gncmod-backend-dbi"
static QofSession*
create_session(void)
{
QofSession* session = qof_session_new();
QofBook* book = qof_session_get_book( session );
Account* root = gnc_book_get_root_account( book );
Account* a;
KvpFrame* frame;
Timespec ts;
struct timeval tv;
a = xaccMallocAccount( book );
xaccAccountSetType( a, ACCT_TYPE_BANK );
xaccAccountSetName( a, "Bank" );
frame = qof_instance_get_slots( QOF_INSTANCE(a) );
kvp_frame_set_gint64( frame, "int64-val", 100 );
kvp_frame_set_double( frame, "double-val", 3.14159 );
kvp_frame_set_numeric( frame, "numeric-val", gnc_numeric_zero() );
time( &(tv.tv_sec) );
tv.tv_usec = 0;
ts.tv_sec = tv.tv_sec;
ts.tv_nsec = 1000*tv.tv_usec;
kvp_frame_set_timespec( frame, "timespec-val", ts );
kvp_frame_set_string( frame, "string-val", "abcdefghijklmnop" );
kvp_frame_set_guid( frame, "guid-val", qof_instance_get_guid( QOF_INSTANCE(a) ) );
gnc_account_append_child( root, a );
return session;
}
int main (int argc, char ** argv)
{
gchar* filename;
QofSession* session_1;
qof_init();
cashobjects_register();
qof_load_backend_library ("../.libs/", GNC_LIB_NAME);
// Create a session with data
session_1 = create_session();
filename = tempnam( "/tmp", "test-sqlite3-" );
printf( "Using filename: %s\n", filename );
test_dbi_store_and_reload( "sqlite3", session_1, filename );
printf( "TEST_MYSQL_URL='%s'\n", TEST_MYSQL_URL );
if( strlen( TEST_MYSQL_URL ) > 0 ) {
session_1 = create_session();
test_dbi_store_and_reload( "mysql", session_1, TEST_MYSQL_URL );
}
printf( "TEST_PGSQL_URL='%s'\n", TEST_PGSQL_URL );
if( strlen( TEST_PGSQL_URL ) > 0 ) {
session_1 = create_session();
test_dbi_store_and_reload( "pgsql", session_1, TEST_PGSQL_URL );
}
print_test_results();
qof_close();
exit(get_rv());
}

View File

@@ -0,0 +1,401 @@
/***************************************************************************
* test-dbi-stuff.c
*
* Tests saving and loading to a dbi/sqlite3 db
*
* Copyright (C) 2009 Phil Longstaff <plongstaff@rogers.com>
****************************************************************************/
/*
* 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
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "config.h"
#include "qof.h"
#include "cashobjects.h"
#include "test-engine-stuff.h"
#include "test-stuff.h"
#include "test-dbi-stuff.h"
#include "Account.h"
#include "Split.h"
#include "gnc-commodity.h"
static gboolean testAccountEqual(const Account *aa, const Account *ab, gboolean check_guids);
static QofLogModule log_module = "test-dbi";
static gboolean
test_commodity_equal(const gnc_commodity * a, const gnc_commodity * b)
{
if (a == b) return TRUE;
if (!a || !b)
{
DEBUG ("one is NULL");
return FALSE;
}
{
const gchar* ns1 = gnc_commodity_get_namespace(a);
const gchar* ns2 = gnc_commodity_get_namespace(b);
if( ns1 != ns2 && safe_strcmp(ns1, ns2) != 0 )
{
DEBUG ("namespaces differ: %s vs %s", ns1, ns2 );
return FALSE;
}
}
if (safe_strcmp(gnc_commodity_get_mnemonic(a), gnc_commodity_get_mnemonic(b)) != 0)
{
DEBUG ("mnemonics differ: %s vs %s", gnc_commodity_get_mnemonic(a), gnc_commodity_get_mnemonic(b));
return FALSE;
}
if (safe_strcmp(gnc_commodity_get_fullname(a), gnc_commodity_get_fullname(b)) != 0)
{
DEBUG ("fullnames differ: %s vs %s", gnc_commodity_get_fullname(a), gnc_commodity_get_fullname(b));
return FALSE;
}
if (safe_strcmp(gnc_commodity_get_cusip(a), gnc_commodity_get_cusip(b)) != 0)
{
DEBUG ("cusips differ: %s vs %s", gnc_commodity_get_cusip(a), gnc_commodity_get_cusip(b));
return FALSE;
}
if (gnc_commodity_get_fraction(a) != gnc_commodity_get_fraction(b))
{
DEBUG ("fractions differ: %d vs %d", gnc_commodity_get_fraction(a), gnc_commodity_get_fraction(b));
return FALSE;
}
return TRUE;
}
static gboolean
testAcctChildrenEqual(const GList *na,
const GList *nb,
gboolean check_guids)
{
if ((!na && nb) || (na && !nb))
{
PWARN ("only one has accounts");
return(FALSE);
}
while (na && nb)
{
Account *aa = na->data;
Account *ab = nb->data;
if (!testAccountEqual(aa, ab, check_guids))
{
char sa[GUID_ENCODING_LENGTH + 1];
char sb[GUID_ENCODING_LENGTH + 1];
guid_to_string_buff (xaccAccountGetGUID (aa), sa);
guid_to_string_buff (xaccAccountGetGUID (ab), sb);
PWARN ("accounts %s and %s differ", sa, sb);
return(FALSE);
}
na = na->next;
nb = nb->next;
}
if (na || nb)
{
PWARN ("different numbers of accounts");
return(FALSE);
}
return(TRUE);
}
static gboolean
testAccountEqual(const Account *aa, const Account *ab, gboolean check_guids)
{
if(!aa && !ab) return TRUE;
g_return_val_if_fail(GNC_IS_ACCOUNT(aa), FALSE);
g_return_val_if_fail(GNC_IS_ACCOUNT(ab), FALSE);
if (xaccAccountGetType(aa) != xaccAccountGetType(ab))
{
PWARN ("'%s' and '%s': types differ: %d vs %d",
xaccAccountGetName(aa), xaccAccountGetName(ab),
xaccAccountGetType(aa), xaccAccountGetType(ab));
return FALSE;
}
if (safe_strcmp(xaccAccountGetName(aa), xaccAccountGetName(ab)) != 0)
{
PWARN ("names differ: %s vs %s", xaccAccountGetName(aa), xaccAccountGetName(ab));
return FALSE;
}
if (safe_strcmp(xaccAccountGetCode(aa), xaccAccountGetCode(ab)) != 0)
{
PWARN ("codes differ: %s vs %s", xaccAccountGetCode(aa), xaccAccountGetCode(ab));
return FALSE;
}
if (safe_strcmp(xaccAccountGetDescription(aa), xaccAccountGetDescription(ab)) != 0)
{
PWARN ("descriptions differ: %s vs %s", xaccAccountGetDescription(aa), xaccAccountGetDescription(ab));
return FALSE;
}
if (!test_commodity_equal(xaccAccountGetCommodity(aa), xaccAccountGetCommodity(ab)))
{
PWARN ("commodities differ");
return FALSE;
}
if(check_guids) {
if(qof_instance_guid_compare(aa, ab) != 0)
{
gchar guid_a[33];
gchar guid_b[33];
guid_to_string_buff( qof_entity_get_guid( QOF_INSTANCE(aa) ), guid_a );
guid_to_string_buff( qof_entity_get_guid( QOF_INSTANCE(ab) ), guid_b );
PWARN ("'%s' and '%s': GUIDs differ %s vs %s",
xaccAccountGetName(aa), xaccAccountGetName(ab),
guid_a, guid_b);
return FALSE;
}
}
if (kvp_frame_compare(qof_instance_get_slots(QOF_INSTANCE(aa)), qof_instance_get_slots(QOF_INSTANCE(ab))) != 0)
{
char *frame_a;
char *frame_b;
frame_a = kvp_frame_to_string (qof_instance_get_slots(QOF_INSTANCE(aa)));
frame_b = kvp_frame_to_string (qof_instance_get_slots(QOF_INSTANCE(ab)));
PWARN ("kvp frames differ:\n%s\n\nvs\n\n%s", frame_a, frame_b);
g_free (frame_a);
g_free (frame_b);
return FALSE;
}
if (!gnc_numeric_equal(gnc_account_get_start_balance((Account*)aa), gnc_account_get_start_balance((Account*)ab)))
{
char *str_a;
char *str_b;
str_a = gnc_numeric_to_string(gnc_account_get_start_balance((Account*)aa));
str_b = gnc_numeric_to_string(gnc_account_get_start_balance((Account*)ab));
PWARN ("starting balances differ: %s vs %s", str_a, str_b);
g_free (str_a);
g_free (str_b);
return FALSE;
}
if (!gnc_numeric_equal(gnc_account_get_start_cleared_balance((Account*)aa),
gnc_account_get_start_cleared_balance((Account*)ab)))
{
char *str_a;
char *str_b;
str_a = gnc_numeric_to_string(gnc_account_get_start_cleared_balance((Account*)aa));
str_b = gnc_numeric_to_string(gnc_account_get_start_cleared_balance((Account*)ab));
PWARN ("starting cleared balances differ: %s vs %s", str_a, str_b);
g_free (str_a);
g_free (str_b);
return FALSE;
}
if (!gnc_numeric_equal(gnc_account_get_start_reconciled_balance((Account*)aa),
gnc_account_get_start_reconciled_balance((Account*)ab)))
{
char *str_a;
char *str_b;
str_a = gnc_numeric_to_string(gnc_account_get_start_reconciled_balance((Account*)aa));
str_b = gnc_numeric_to_string(gnc_account_get_start_reconciled_balance((Account*)ab));
PWARN ("starting reconciled balances differ: %s vs %s", str_a, str_b);
g_free (str_a);
g_free (str_b);
return FALSE;
}
if (!gnc_numeric_equal(xaccAccountGetBalance(aa), xaccAccountGetBalance(ab)))
{
char *str_a;
char *str_b;
str_a = gnc_numeric_to_string(xaccAccountGetBalance(aa));
str_b = gnc_numeric_to_string(xaccAccountGetBalance(ab));
PWARN ("balances differ: %s vs %s", str_a, str_b);
g_free (str_a);
g_free (str_b);
return FALSE;
}
if (!gnc_numeric_equal(xaccAccountGetClearedBalance(aa), xaccAccountGetClearedBalance(ab)))
{
char *str_a;
char *str_b;
str_a = gnc_numeric_to_string(xaccAccountGetClearedBalance(aa));
str_b = gnc_numeric_to_string(xaccAccountGetClearedBalance(ab));
PWARN ("cleared balances differ: %s vs %s", str_a, str_b);
g_free (str_a);
g_free (str_b);
return FALSE;
}
if (!gnc_numeric_equal(xaccAccountGetReconciledBalance(aa), xaccAccountGetReconciledBalance(ab)))
{
char *str_a;
char *str_b;
str_a = gnc_numeric_to_string(xaccAccountGetReconciledBalance(aa));
str_b = gnc_numeric_to_string(xaccAccountGetReconciledBalance(ab));
PWARN ("reconciled balances differ: %s vs %s", str_a, str_b);
g_free (str_a);
g_free (str_b);
return FALSE;
}
/* no parent; always compare downwards. */
{
GList *la = xaccAccountGetSplitList(aa);
GList *lb = xaccAccountGetSplitList(ab);
if ((la && !lb) || (!la && lb))
{
PWARN ("only one has splits");
return FALSE;
}
if(la && lb)
{
/* presume that the splits are in the same order */
while (la && lb)
{
Split *sa = (Split *) la->data;
Split *sb = (Split *) lb->data;
if (!xaccSplitEqual(sa, sb, check_guids, TRUE, FALSE))
{
PWARN ("splits differ");
return(FALSE);
}
la = la->next;
lb = lb->next;
}
if ((la != NULL) || (lb != NULL))
{
PWARN ("number of splits differs");
return(FALSE);
}
}
}
if (!testAcctChildrenEqual(gnc_account_get_children(aa), gnc_account_get_children(ab), check_guids))
{
PWARN ("children differ");
return FALSE;
}
return(TRUE);
}
static void
compare_accounts( QofBook* book_1, QofBook* book_2 )
{
Account* root_1 = gnc_book_get_root_account( book_1 );
Account* root_2 = gnc_book_get_root_account( book_2 );
xaccAccountSetHidden( root_1, xaccAccountGetHidden( root_1 ) );
do_test( testAccountEqual( root_1, root_2, TRUE ), "Accounts trees match" );
}
static void
compare_pricedbs( QofBook* book_1, QofBook* book_2 )
{
}
static void
compare_txs( QofBook* book_1, QofBook* book_2 )
{
}
static void
compare_sxs( QofBook* book_1, QofBook* book_2 )
{
}
static void
compare_books( QofBook* book_1, QofBook* book_2 )
{
compare_accounts( book_1, book_2 );
compare_pricedbs( book_1, book_2 );
compare_txs( book_1, book_2 );
compare_sxs( book_1, book_2 );
}
void
test_dbi_store_and_reload( const gchar* driver, QofSession* session_1, const gchar* url )
{
QofSession* session_2;
QofSession* session_3;
printf( "Testing %s\n", driver );
// Save the session data
session_2 = qof_session_new();
qof_session_begin( session_2, url, TRUE, TRUE );
qof_session_swap_data( session_1, session_2 );
qof_session_save( session_2, NULL );
// Reload the session data
session_3 = qof_session_new();
qof_session_begin( session_3, url, FALSE, FALSE );
qof_session_load( session_3, NULL );
// Compare with the original data
compare_books( qof_session_get_book( session_2 ), qof_session_get_book( session_3 ) );
}

View File

@@ -0,0 +1,39 @@
/***************************************************************************
* test-dbi-stuff.h
*
* Tests saving and loading to a dbi/sqlite3 db
*
* Copyright (C) 2009 Phil Longstaff <plongstaff@rogers.com>
****************************************************************************/
/*
* 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
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#ifndef _TEST_DBI_STUFF_H_
#define _TEST_DBI_STUFF_H_
/**
* Test storing a session contents to a db, reloading into a new session, then comparing the
* two sessions.
*
* @param driver Driver name
* @param session_1 Session to test
* @param url Database URL
*/
void test_dbi_store_and_reload( const gchar* driver, QofSession* session_1, const gchar* url );
#endif

View File

@@ -0,0 +1,69 @@
/***************************************************************************
* test-dbi.c
*
* Tests saving and loading to a dbi/sqlite3 db
*
* Copyright (C) 2009 Phil Longstaff <plongstaff@rogers.com>
****************************************************************************/
/*
* 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
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "config.h"
#include "qof.h"
#include "cashobjects.h"
#include "test-engine-stuff.h"
#include "test-stuff.h"
#include "test-dbi-stuff.h"
#include "Account.h"
#include "Split.h"
#include "gnc-commodity.h"
#define DBI_TEST_XML_FILENAME "test-dbi.xml"
#define FILE_NAME "sqlite3:///tmp/test-sqlite3-file"
#define GNC_LIB_NAME "gncmod-backend-dbi"
int main (int argc, char ** argv)
{
gchar* filename;
QofSession* session_1;
qof_init();
cashobjects_register();
qof_load_backend_library ("../.libs/", GNC_LIB_NAME);
// Create a session with data
session_1 = qof_session_new();
qof_session_begin( session_1, DBI_TEST_XML_FILENAME, FALSE, FALSE );
qof_session_load( session_1, NULL );
filename = tempnam( "/tmp", "test-sqlite3-" );
printf( "Using filename: %s\n", filename );
test_dbi_store_and_reload( "sqlite3", session_1, filename );
printf( "TEST_MYSQL_URL='%s'\n", TEST_MYSQL_URL );
if( strlen( TEST_MYSQL_URL ) > 0 ) {
test_dbi_store_and_reload( "mysql", session_1, TEST_MYSQL_URL );
}
printf( "TEST_PGSQL_URL='%s'\n", TEST_PGSQL_URL );
if( strlen( TEST_PGSQL_URL ) > 0 ) {
test_dbi_store_and_reload( "pgsql", session_1, TEST_PGSQL_URL );
}
print_test_results();
qof_close();
exit(get_rv());
}

Binary file not shown.

View File

@@ -1363,8 +1363,13 @@ load_double( const GncSqlBackend* be, GncSqlRow* row,
} else {
if( G_VALUE_HOLDS(val, G_TYPE_INT) ) {
d_value = (gdouble)g_value_get_int( val );
} else {
} else if( G_VALUE_HOLDS(val, G_TYPE_FLOAT) ) {
d_value = g_value_get_float( val );
} else if (G_VALUE_HOLDS(val, G_TYPE_DOUBLE) ) {
d_value = g_value_get_double( val );
} else {
PWARN( "Unknown float value type: %s\n", g_type_name( G_VALUE_TYPE(val) ) );
d_value = 0;
}
(*setter)( pObject, (gpointer)&d_value );
}