From 7ea5a2bf98f1312944f14f1e69dfbe3760853736 Mon Sep 17 00:00:00 2001 From: Phil Longstaff Date: Sun, 20 May 2012 21:21:09 +0000 Subject: [PATCH] Convert DBI test to utest git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@22182 57a11ea4-9604-0410-9ed3-97b8803252fd --- src/backend/dbi/test/Makefile.am | 120 +++++----- src/backend/dbi/test/test-backend-dbi.c | 50 +++++ src/backend/dbi/test/test-dbi-stuff.h | 2 + .../dbi/test/utest-backend-dbi-basic.c | 52 +++++ .../dbi/test/utest-backend-dbi-business.c | 205 ++++++++++++++++++ 5 files changed, 376 insertions(+), 53 deletions(-) create mode 100644 src/backend/dbi/test/test-backend-dbi.c create mode 100644 src/backend/dbi/test/utest-backend-dbi-basic.c create mode 100644 src/backend/dbi/test/utest-backend-dbi-business.c diff --git a/src/backend/dbi/test/Makefile.am b/src/backend/dbi/test/Makefile.am index a4adeaccb9..a6093e5434 100644 --- a/src/backend/dbi/test/Makefile.am +++ b/src/backend/dbi/test/Makefile.am @@ -1,26 +1,38 @@ -SUBDIRS = . +# Makefile.am for src/backend/dbi/test -check_LTLIBRARIES=libgncmod-test-dbi.la +include $(top_srcdir)/test-templates/Makefile.decl -libgncmod_test_dbi_la_SOURCES = \ - test-dbi-stuff.c -libgncmod_test_dbi_la_LDFLAGS = -module - -test_dbi_business_SOURCES = \ - test-dbi-business.c \ - test-dbi-business-stuff.c - -test_dbi_basic_SOURCES = \ - test-dbi-basic.c +AM_CFLAGS = \ + -I${top_srcdir}/lib/libc \ + -I${top_srcdir}/src \ + -I${top_srcdir}/src/core-utils \ + -I${top_srcdir}/src/gnc-module \ + -I${top_srcdir}/src/test-core \ + -I${top_srcdir}/src/engine \ + -I${top_srcdir}/src/engine/test-core \ + -I${top_srcdir}/src/libqof/qof \ + -I${top_srcdir}/src/backend/sql \ + -DTEST_MYSQL_URL=\"${TEST_MYSQL_URL}\" \ + -DTEST_PGSQL_URL=\"${TEST_PGSQL_URL}\" \ + ${GLIB_CFLAGS} \ + ${GUILE_INCS} \ + ${GCONF_CFLAGS} -test_dbi_SOURCES = \ - test-dbi.c +LDADD = ${top_builddir}/src/test-core/libtest-core.la \ + ${top_builddir}/src/gnc-module/libgnc-module.la \ + ${top_builddir}/src/engine/libgncmod-engine.la \ + ${top_builddir}/src/engine/test-core/libgncmod-test-engine.la \ + ${top_builddir}/src/core-utils/libgnc-core-utils.la \ + ${top_builddir}/src/libqof/qof/libgnc-qof.la \ + ../libgncmod-backend-dbi.la \ + ${top_builddir}/src/backend/sql/libgnc-backend-sql.la \ + ${top_builddir}/lib/libc/libc-missing.la -TESTS = \ - test-dbi-basic \ - test-dbi \ - test-dbi-business \ - test-load-backend +#TESTS = \ +# test-dbi-basic \ +# test-dbi \ +# test-dbi-business \ +# test-load-backend GNC_TEST_DEPS = \ --gnc-module-dir ${top_builddir}/src/engine \ @@ -30,51 +42,53 @@ GNC_TEST_DEPS = \ --library-dir ${top_builddir}/src/gnc-module \ --library-dir ${top_builddir}/src/engine -if CUSTOM_GNC_DBD_DIR -gnc_dbd_dir_override = GNC_DBD_DIR="@GNC_DBD_DIR@" -endif - TESTS_ENVIRONMENT = \ GNC_ACCOUNT_PATH=${top_srcdir}/accounts/C \ SRCDIR=${srcdir} \ ${gnc_dbd_dir_override} \ $(shell ${top_srcdir}/src/gnc-test-env --no-exports ${GNC_TEST_DEPS}) -check_PROGRAMS = \ - test-dbi-basic \ - test-dbi \ - test-dbi-business \ - test-load-backend +#check_PROGRAMS = \ +# test-dbi-basic \ +# test-dbi \ +# test-dbi-business \ +# test-load-backend -EXTRA_DIST = \ +EXTRA_DIST += \ test-dbi-stuff.h \ test-dbi-business-stuff.h -LDADD = ${top_builddir}/src/test-core/libtest-core.la \ - ${top_builddir}/src/gnc-module/libgnc-module.la \ - ${top_builddir}/src/engine/libgncmod-engine.la \ - ${top_builddir}/src/engine/test-core/libgncmod-test-engine.la \ - ${top_builddir}/src/core-utils/libgnc-core-utils.la \ - ${top_builddir}/src/libqof/qof/libgnc-qof.la \ - ${top_builddir}/src/backend/dbi/test/libgncmod-test-dbi.la \ - ${top_builddir}/src/backend/sql/libgnc-backend-sql.la \ - ${top_builddir}/lib/libc/libc-missing.la +TEST_PROGS += \ + test-backend-dbi -AM_CFLAGS = \ - -I${top_srcdir}/lib/libc \ - -I${top_srcdir}/src \ - -I${top_srcdir}/src/core-utils \ - -I${top_srcdir}/src/gnc-module \ - -I${top_srcdir}/src/test-core \ - -I${top_srcdir}/src/engine \ - -I${top_srcdir}/src/engine/test-core \ - -I${top_srcdir}/src/libqof/qof \ - -I${top_srcdir}/src/backend/sql \ - -DTEST_MYSQL_URL=\"${TEST_MYSQL_URL}\" \ - -DTEST_PGSQL_URL=\"${TEST_PGSQL_URL}\" \ - ${GLIB_CFLAGS} \ - ${GUILE_INCS} \ - ${GCONF_CFLAGS} +noinst_PROGRAMS = ${TEST_PROGS} ${CHECK_PROGS} + +#check_LTLIBRARIES=libgncmod-test-dbi.la + +#libgncmod_test_dbi_la_SOURCES = \ +# test-dbi-stuff.c +libgncmod_test_dbi_la_LDFLAGS = -module + +test_dbi_business_SOURCES = \ + test-dbi-business.c \ + test-dbi-business-stuff.c + +test_dbi_basic_SOURCES = \ + test-dbi-basic.c + +test_dbi_SOURCES = \ + test-dbi.c + +if CUSTOM_GNC_DBD_DIR +gnc_dbd_dir_override = GNC_DBD_DIR="@GNC_DBD_DIR@" +endif + +test_backend_dbi_SOURCES = \ + test-backend-dbi.c \ + utest-backend-dbi-basic.c \ + utest-backend-dbi-business.c \ + test-dbi-stuff.c \ + test-dbi-business-stuff.c INCLUDES = -DG_LOG_DOMAIN=\"gnc.backend.dbi\" diff --git a/src/backend/dbi/test/test-backend-dbi.c b/src/backend/dbi/test/test-backend-dbi.c new file mode 100644 index 0000000000..f7c680aa54 --- /dev/null +++ b/src/backend/dbi/test/test-backend-dbi.c @@ -0,0 +1,50 @@ +/******************************************************************** + * test-backend-dbi.c: GLib test execution file for backend/dbi * + * Copyright 2011 John Ralls * + * * + * 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, contact: * + * * + * Free Software Foundation Voice: +1-617-542-5942 * + * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 * + * Boston, MA 02110-1301, USA gnu@gnu.org * +\********************************************************************/ + + +#include "config.h" +#include +#include "qof.h" +#include "cashobjects.h" + +extern void test_suite_gnc_backend_dbi_basic(); +extern void test_suite_gnc_backend_dbi_business(); + +#define GNC_LIB_NAME "gncmod-backend-dbi" + +int +main (int argc, + char *argv[]) +{ + g_type_init(); /* Initialize the GObject system */ + g_test_init ( &argc, &argv, NULL ); /* initialize test program */ + qof_log_init_filename_special("stderr"); /* Init the log system */ + g_test_bug_base("https://bugzilla.gnome.org/show_bug.cgi?id="); /* init the bugzilla URL */ + qof_init(); + cashobjects_register(); + qof_load_backend_library ("../.libs/", GNC_LIB_NAME); + + test_suite_gnc_backend_dbi_basic(); + test_suite_gnc_backend_dbi_business(); + + return g_test_run( ); +} diff --git a/src/backend/dbi/test/test-dbi-stuff.h b/src/backend/dbi/test/test-dbi-stuff.h index fda9bc4f7d..4babe0b03f 100644 --- a/src/backend/dbi/test/test-dbi-stuff.h +++ b/src/backend/dbi/test/test-dbi-stuff.h @@ -26,6 +26,8 @@ #ifndef _TEST_DBI_STUFF_H_ #define _TEST_DBI_STUFF_H_ +#include "qof.h" + typedef struct { QofBook* book_1; diff --git a/src/backend/dbi/test/utest-backend-dbi-basic.c b/src/backend/dbi/test/utest-backend-dbi-basic.c new file mode 100644 index 0000000000..fbebe80df7 --- /dev/null +++ b/src/backend/dbi/test/utest-backend-dbi-basic.c @@ -0,0 +1,52 @@ +/* + * utest-gnc-prefs-gconf.c + * + * Created on: 2011-04-23 + * Author: phil + */ + +#include "config.h" + +#include + +#include "unittest-support.h" +#include "test-stuff.h" +#include "test-dbi-stuff.h" + +static const gchar* suitename = "/backend/dbi"; +void test_suite_gnc_backend_dbi_basic(void); + +void do_test_sqlite(void); + +#define DBI_TEST_XML_FILENAME "test-dbi.xml" +#define FILE_NAME "sqlite3:///tmp/test-sqlite3-file" + +static gboolean handler(const gchar* log_domain, GLogLevelFlags log_level, const gchar* message, gpointer user_data) +{ + printf("domain=%s level=%d message=%s\n", log_domain, log_level, message); + return FALSE; +} + +void +do_test_sqlite(void) +{ + gchar* filename; + QofSession* session_1; + + g_test_log_set_fatal_handler(handler, 0); + + // Create a session with data + session_1 = qof_session_new(); + qof_session_begin( session_1, DBI_TEST_XML_FILENAME, FALSE, FALSE, FALSE ); + qof_session_load( session_1, NULL ); + + filename = tempnam( "/tmp", "test-sqlite3-" ); + g_test_message ( "Using filename: %s\n", filename ); + test_dbi_store_and_reload( "sqlite3", session_1, filename ); +} + +void +test_suite_gnc_backend_dbi_basic(void) +{ + GNC_TEST_ADD_FUNC(suitename, "gnc dbi test sqlite", do_test_sqlite); +} diff --git a/src/backend/dbi/test/utest-backend-dbi-business.c b/src/backend/dbi/test/utest-backend-dbi-business.c new file mode 100644 index 0000000000..4fe251f36d --- /dev/null +++ b/src/backend/dbi/test/utest-backend-dbi-business.c @@ -0,0 +1,205 @@ +/*************************************************************************** + * test-dbi-business.c + * + * Tests saving and loading business objects to a dbi/sqlite3 db + * + * Copyright (C) 2010 Phil Longstaff + ****************************************************************************/ + +/* + * 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. + */ + +/* + * utest-gnc-prefs-gconf.c + * + * Created on: 2011-04-23 + * Author: phil + */ + +#include "config.h" + +#include + +#include "unittest-support.h" +#include "test-stuff.h" +#include "test-dbi-stuff.h" + +static const gchar* suitename = "/backend/dbi"; +void test_suite_gnc_backend_dbi_business(void); + +void do_test_business_sqlite(void); + +#define DBI_TEST_XML_FILENAME "test-dbi.xml" +#define FILE_NAME "sqlite3:///tmp/test-sqlite3-file" +#define GNC_LIB_NAME "gncmod-backend-dbi" +#include "config.h" +#include "qof.h" +#include "cashobjects.h" +#include "test-engine-stuff.h" +#include "test-stuff.h" +#include "test-dbi-business-stuff.h" + +#include "Account.h" +#include +#include "Transaction.h" +#include "Split.h" +#include "gnc-commodity.h" +#include "gncAddress.h" +#include "gncCustomer.h" +#include "gncInvoice.h" + +#include "gnc-backend-sql.h" + +#include "gnc-address-sql.h" +#include "gnc-bill-term-sql.h" +#include "gnc-customer-sql.h" +#include "gnc-employee-sql.h" +#include "gnc-entry-sql.h" +#include "gnc-invoice-sql.h" +#include "gnc-job-sql.h" +#include "gnc-order-sql.h" +#include "gnc-owner-sql.h" +#include "gnc-tax-table-sql.h" +#include "gnc-vendor-sql.h" + +#define FILE_NAME "sqlite3:///tmp/test-sqlite3-file" +#define GNC_LIB_NAME "gncmod-backend-dbi" + +static QofSession* +create_business_session(void) +{ + QofSession* session = qof_session_new(); + QofBook* book = qof_session_get_book( session ); + Account* root = gnc_book_get_root_account( book ); + Account* acct1; + Account* acct2; + gnc_commodity_table* table; + gnc_commodity* currency; + GncAddress* addr; + GncCustomer* cust; + GncEmployee* emp; + GncVendor* v; + GncInvoice* inv; + GncJob* job; + GncTaxTable* tt; + GncTaxTableEntry* tte; + + table = gnc_commodity_table_get_table( book ); + currency = gnc_commodity_table_lookup( table, GNC_COMMODITY_NS_CURRENCY, "CAD" ); + + acct1 = xaccMallocAccount( book ); + xaccAccountSetType( acct1, ACCT_TYPE_BANK ); + xaccAccountSetName( acct1, "Bank 1" ); + xaccAccountSetCommodity( acct1, currency ); + xaccAccountSetHidden( acct1, FALSE ); + xaccAccountSetPlaceholder( acct1, FALSE ); + gnc_account_append_child( root, acct1 ); + + acct2 = xaccMallocAccount( book ); + xaccAccountSetType( acct2, ACCT_TYPE_BANK ); + xaccAccountSetName( acct2, "Bank 2" ); + xaccAccountSetCommodity( acct2, currency ); + xaccAccountSetHidden( acct2, FALSE ); + xaccAccountSetPlaceholder( acct2, FALSE ); + gnc_account_append_child( root, acct2 ); + + tt = gncTaxTableCreate( book ); + gncTaxTableSetName( tt, "tt" ); + tte = gncTaxTableEntryCreate(); + gncTaxTableEntrySetAccount( tte, acct1 ); + gncTaxTableEntrySetType( tte, GNC_AMT_TYPE_VALUE ); + gncTaxTableEntrySetAmount( tte, gnc_numeric_zero() ); + gncTaxTableAddEntry( tt, tte ); + tte = gncTaxTableEntryCreate(); + gncTaxTableEntrySetAccount( tte, acct2 ); + gncTaxTableEntrySetType( tte, GNC_AMT_TYPE_PERCENT ); + gncTaxTableEntrySetAmount( tte, gnc_numeric_zero() ); + gncTaxTableAddEntry( tt, tte ); + + cust = gncCustomerCreate( book ); + gncCustomerSetID( cust, "0001" ); + gncCustomerSetName( cust, "MyCustomer" ); + gncCustomerSetNotes( cust, "Here are some notes" ); + gncCustomerSetCurrency( cust, currency ); + addr = gncAddressCreate( book, QOF_INSTANCE(cust) ); + gncAddressSetName( addr, "theAddress" ); + gncAddressSetAddr1( addr, "Address line #1" ); + gncAddressSetAddr2( addr, "Address line #2" ); + gncAddressSetAddr3( addr, "Address line #3" ); + gncAddressSetAddr4( addr, "Address line #4" ); + gncAddressSetPhone( addr, "(123) 555-1212" ); + gncAddressSetPhone( addr, "(123) 555-2121" ); + gncAddressSetEmail( addr, "cust@mycustomer.com" ); + + emp = gncEmployeeCreate( book ); + gncEmployeeSetID( emp, "0001" ); + gncEmployeeSetUsername( emp, "gnucash" ); + gncEmployeeSetLanguage( emp, "english" ); + gncEmployeeSetCurrency( emp, currency ); + + return session; +} + +/* Order in which business objects need to be loaded */ +static const gchar* fixed_load_order[] = +{ GNC_ID_BILLTERM, GNC_ID_TAXTABLE, NULL }; + +static void +init_business_sql(void) +{ + /* Initialize our pointers into the backend subsystem */ + gnc_address_sql_initialize(); + gnc_billterm_sql_initialize(); + gnc_customer_sql_initialize(); + gnc_employee_sql_initialize(); + gnc_entry_sql_initialize(); + gnc_invoice_sql_initialize(); + gnc_job_sql_initialize(); + gnc_order_sql_initialize(); + gnc_owner_sql_initialize(); + gnc_taxtable_sql_initialize(); + gnc_vendor_sql_initialize(); + + gnc_sql_set_load_order( fixed_load_order ); +} + +static gboolean handler(const gchar* log_domain, GLogLevelFlags log_level, const gchar* message, gpointer user_data) +{ + printf("domain=%s level=%d message=%s\n", log_domain, log_level, message); + return FALSE; +} + +void +do_test_business_sqlite(void) +{ + gchar* filename; + QofSession* session_1; + + g_test_log_set_fatal_handler(handler, 0); + + // Create a session with data + session_1 = create_business_session(); + filename = tempnam( "/tmp", "test-sqlite3-" ); + g_test_message ( "Using filename: %s\n", filename ); + test_dbi_business_store_and_reload( "sqlite3", session_1, filename ); +} + +void +test_suite_gnc_backend_dbi_business(void) +{ + GNC_TEST_ADD_FUNC(suitename, "gnc dbi test sqlite (business)", do_test_business_sqlite); +}