r18966 - gnucash/trunk - Add business backend sql test infrastructure.
Phil Longstaff
plongstaff at code.gnucash.org
Fri Mar 26 15:15:56 EDT 2010
Author: plongstaff
Date: 2010-03-26 15:15:56 -0400 (Fri, 26 Mar 2010)
New Revision: 18966
Trac: http://svn.gnucash.org/trac/changeset/18966
Added:
gnucash/trunk/src/business/business-core/sql/test/
gnucash/trunk/src/business/business-core/sql/test/Makefile.am
gnucash/trunk/src/business/business-core/sql/test/test-dbi-business-stuff.c
gnucash/trunk/src/business/business-core/sql/test/test-dbi-business-stuff.h
gnucash/trunk/src/business/business-core/sql/test/test-dbi-business.c
Modified:
gnucash/trunk/configure.in
gnucash/trunk/src/business/business-core/sql/Makefile.am
Log:
Add business backend sql test infrastructure.
At this point, the test creates a customer, saves it to an sqlite3 db, loads it and compares the original and the loaded customer. Will be expanded to test other business objects.
Modified: gnucash/trunk/configure.in
===================================================================
--- gnucash/trunk/configure.in 2010-03-26 19:13:34 UTC (rev 18965)
+++ gnucash/trunk/configure.in 2010-03-26 19:15:56 UTC (rev 18966)
@@ -1485,6 +1485,7 @@
src/business/Makefile
src/business/business-core/Makefile
src/business/business-core/sql/Makefile
+ src/business/business-core/sql/test/Makefile
src/business/business-core/test/Makefile
src/business/business-core/xml/Makefile
src/business/business-utils/Makefile
Modified: gnucash/trunk/src/business/business-core/sql/Makefile.am
===================================================================
--- gnucash/trunk/src/business/business-core/sql/Makefile.am 2010-03-26 19:13:34 UTC (rev 18965)
+++ gnucash/trunk/src/business/business-core/sql/Makefile.am 2010-03-26 19:15:56 UTC (rev 18966)
@@ -1,4 +1,4 @@
-SUBDIRS = .
+SUBDIRS = . test
pkglib_LTLIBRARIES = libgncmod-business-backend-sql.la
Added: gnucash/trunk/src/business/business-core/sql/test/Makefile.am
===================================================================
--- gnucash/trunk/src/business/business-core/sql/test/Makefile.am (rev 0)
+++ gnucash/trunk/src/business/business-core/sql/test/Makefile.am 2010-03-26 19:15:56 UTC (rev 18966)
@@ -0,0 +1,56 @@
+SUBDIRS = .
+
+test_dbi_business_SOURCES = \
+ test-dbi-business.c \
+ test-dbi-business-stuff.c
+
+TESTS = \
+ test-dbi-business
+
+GNC_TEST_DEPS = \
+ --gnc-module-dir ${top_builddir}/src/engine \
+ --guile-load-dir ${top_builddir}/src/engine \
+ --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
+
+TESTS_ENVIRONMENT = \
+ GNC_ACCOUNT_PATH=${top_srcdir}/accounts/C \
+ SRCDIR=${srcdir} \
+ GNC_DBD_DIR=@GNC_DBD_DIR@ \
+ $(shell ${top_srcdir}/src/gnc-test-env --no-exports ${GNC_TEST_DEPS})
+
+check_PROGRAMS = \
+ test-dbi-business
+
+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/business/business-core/libgncmod-business-core.la \
+ ${top_builddir}/src/business/business-core/sql/libgncmod-business-backend-sql.la \
+ ${top_builddir}/lib/libc/libc-missing.la
+
+AM_CFLAGS = \
+ -I${top_srcdir}/lib/libc \
+ -I${top_srcdir}/src \
+ -I${top_srcdir}/src/core-utils \
+ -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/dbi/test \
+ -I${top_srcdir}/src/backend/sql \
+ -I${top_srcdir}/src/business/business-core \
+ -I${top_srcdir}/src/business/business-core/sql \
+ -DTEST_MYSQL_URL=\"${TEST_MYSQL_URL}\" \
+ -DTEST_PGSQL_URL=\"${TEST_PGSQL_URL}\" \
+ ${GLIB_CFLAGS} \
+ ${GUILE_INCS} \
+ ${GCONF_CFLAGS}
+
+INCLUDES = -DG_LOG_DOMAIN=\"gnc.business.sql\"
Added: gnucash/trunk/src/business/business-core/sql/test/test-dbi-business-stuff.c
===================================================================
--- gnucash/trunk/src/business/business-core/sql/test/test-dbi-business-stuff.c (rev 0)
+++ gnucash/trunk/src/business/business-core/sql/test/test-dbi-business-stuff.c 2010-03-26 19:15:56 UTC (rev 18966)
@@ -0,0 +1,99 @@
+/***************************************************************************
+ * test-dbi-business-stuff.c
+ *
+ * Tests saving and loading business objects to a dbi/sqlite3 db
+ *
+ * Copyright (C) 2010 Phil Longstaff <plongstaff at 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 "test-dbi-business-stuff.h"
+
+#include "Account.h"
+#include "Split.h"
+#include "Transaction.h"
+#include "gnc-commodity.h"
+#include "gncCustomer.h"
+
+static QofLogModule log_module = "test-dbi";
+
+static void
+compare_single_customer( QofInstance* inst, gpointer user_data )
+{
+ CompareInfoStruct* info = (CompareInfoStruct*)user_data;
+ GncCustomer* cust_1 = GNC_CUSTOMER(inst);
+ GncCustomer* cust_2 = gncCustomerLookup( info->book_2, qof_instance_get_guid(inst) );
+
+ if (!gncCustomerEqual( cust_1, cust_2 ))
+ {
+ info->result = FALSE;
+ }
+}
+
+static void
+compare_customers( QofBook* book_1, QofBook* book_2 )
+{
+ do_compare( book_1, book_2, GNC_ID_CUSTOMER, compare_single_customer, "Customer lists match" );
+}
+
+static void
+compare_books( QofBook* book_1, QofBook* book_2 )
+{
+ compare_customers( book_1, book_2 );
+// compare_invoices( book_1, book_2 );
+// compare_jobs( book_1, book_2 );
+// compare_vendors( book_1, book_2 );
+// compare_billterms( book_1, book_2 );
+// compare_employees( book_1, book_2 );
+}
+
+void
+test_dbi_business_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 ) );
+ qof_session_end( session_1 );
+ qof_session_destroy( session_1 );
+ qof_session_end( session_2 );
+ qof_session_destroy( session_2 );
+ qof_session_end( session_3 );
+ qof_session_destroy( session_3 );
+}
Added: gnucash/trunk/src/business/business-core/sql/test/test-dbi-business-stuff.h
===================================================================
--- gnucash/trunk/src/business/business-core/sql/test/test-dbi-business-stuff.h (rev 0)
+++ gnucash/trunk/src/business/business-core/sql/test/test-dbi-business-stuff.h 2010-03-26 19:15:56 UTC (rev 18966)
@@ -0,0 +1,39 @@
+/***************************************************************************
+ * test-dbi-stuff.h
+ *
+ * Tests saving and loading to a dbi/sqlite3 db
+ *
+ * Copyright (C) 2009 Phil Longstaff <plongstaff at 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_BUSINESS_STUFF_H_
+#define _TEST_DBI_BUSINESS_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_business_store_and_reload( const gchar* driver, QofSession* session_1, const gchar* url );
+
+#endif
Added: gnucash/trunk/src/business/business-core/sql/test/test-dbi-business.c
===================================================================
--- gnucash/trunk/src/business/business-core/sql/test/test-dbi-business.c (rev 0)
+++ gnucash/trunk/src/business/business-core/sql/test/test-dbi-business.c 2010-03-26 19:15:56 UTC (rev 18966)
@@ -0,0 +1,159 @@
+/***************************************************************************
+ * test-dbi-business.c
+ *
+ * Tests saving and loading business objects to a dbi/sqlite3 db
+ *
+ * Copyright (C) 2010 Phil Longstaff <plongstaff at 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-business-stuff.h"
+
+#include "Account.h"
+#include "Transaction.h"
+#include "Split.h"
+#include "gnc-commodity.h"
+#include "gncAddress.h"
+#include "gncCustomer.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_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;
+
+ 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 );
+
+ gnc_account_append_child( root, acct1 );
+
+ acct2 = xaccMallocAccount( book );
+ xaccAccountSetType( acct2, ACCT_TYPE_BANK );
+ xaccAccountSetName( acct2, "Bank 1" );
+
+ 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 at mycustomer.com" );
+
+ 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 );
+}
+
+int main (int argc, char ** argv)
+{
+ gchar* filename;
+ QofSession* session_1;
+
+ qof_init();
+ cashobjects_register();
+ gnc_module_init_business_core_init();
+ qof_load_backend_library ("../../../../backend/dbi/.libs/", GNC_LIB_NAME);
+ init_business_sql();
+
+ // Create a session with data
+ session_1 = create_session();
+ filename = tempnam( "/tmp", "test-sqlite3-" );
+ printf( "Using filename: %s\n", filename );
+ test_dbi_business_store_and_reload( "sqlite3", session_1, filename );
+ #if 0
+ 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 );
+ }
+ #endif
+ print_test_results();
+ qof_close();
+ exit(get_rv());
+}
+
More information about the gnucash-changes
mailing list