GnuCash 2.4.99
test-dbi-business.c
00001 /***************************************************************************
00002  *            test-dbi-business.c
00003  *
00004  *  Tests saving and loading business objects to a dbi/sqlite3 db
00005  *
00006  *  Copyright (C) 2010  Phil Longstaff <plongstaff@rogers.com>
00007  ****************************************************************************/
00008 
00009 /*
00010  *  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU General Public License for more details.
00019  *
00020  *  You should have received a copy of the GNU General Public License
00021  *  along with this program; if not, write to the Free Software
00022  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00023  *  02110-1301, USA.
00024  */
00025 
00026 #include "config.h"
00027 #include "qof.h"
00028 #include "cashobjects.h"
00029 #include "test-engine-stuff.h"
00030 #include "test-stuff.h"
00031 #include "test-dbi-business-stuff.h"
00032 
00033 #include "Account.h"
00034 #include <TransLog.h>
00035 #include "Transaction.h"
00036 #include "Split.h"
00037 #include "gnc-commodity.h"
00038 #include "gncAddress.h"
00039 #include "gncCustomer.h"
00040 #include "gncInvoice.h"
00041 
00042 #include "gnc-backend-sql.h"
00043 
00044 #include "gnc-address-sql.h"
00045 #include "gnc-bill-term-sql.h"
00046 #include "gnc-customer-sql.h"
00047 #include "gnc-employee-sql.h"
00048 #include "gnc-entry-sql.h"
00049 #include "gnc-invoice-sql.h"
00050 #include "gnc-job-sql.h"
00051 #include "gnc-order-sql.h"
00052 #include "gnc-owner-sql.h"
00053 #include "gnc-tax-table-sql.h"
00054 #include "gnc-vendor-sql.h"
00055 
00056 #define FILE_NAME "sqlite3:///tmp/test-sqlite3-file"
00057 #define GNC_LIB_NAME "gncmod-backend-dbi"
00058 
00059 static QofSession*
00060 create_session(void)
00061 {
00062     QofSession* session = qof_session_new();
00063     QofBook* book = qof_session_get_book( session );
00064     Account* root = gnc_book_get_root_account( book );
00065     Account* acct1;
00066     Account* acct2;
00067     gnc_commodity_table* table;
00068     gnc_commodity* currency;
00069     GncAddress* addr;
00070     GncCustomer* cust;
00071     GncEmployee* emp;
00072     GncVendor* v;
00073     GncInvoice* inv;
00074     GncJob* job;
00075     GncTaxTable* tt;
00076     GncTaxTableEntry* tte;
00077 
00078     table = gnc_commodity_table_get_table( book );
00079     currency = gnc_commodity_table_lookup( table, GNC_COMMODITY_NS_CURRENCY, "CAD" );
00080 
00081     acct1 = xaccMallocAccount( book );
00082     xaccAccountSetType( acct1, ACCT_TYPE_BANK );
00083     xaccAccountSetName( acct1, "Bank 1" );
00084     xaccAccountSetCommodity( acct1, currency );
00085     xaccAccountSetHidden( acct1, FALSE );
00086     xaccAccountSetPlaceholder( acct1, FALSE );
00087     gnc_account_append_child( root, acct1 );
00088 
00089     acct2 = xaccMallocAccount( book );
00090     xaccAccountSetType( acct2, ACCT_TYPE_BANK );
00091     xaccAccountSetName( acct2, "Bank 2" );
00092     xaccAccountSetCommodity( acct2, currency );
00093     xaccAccountSetHidden( acct2, FALSE );
00094     xaccAccountSetPlaceholder( acct2, FALSE );
00095     gnc_account_append_child( root, acct2 );
00096 
00097     tt = gncTaxTableCreate( book );
00098     gncTaxTableSetName( tt, "tt" );
00099     tte = gncTaxTableEntryCreate();
00100     gncTaxTableEntrySetAccount( tte, acct1 );
00101     gncTaxTableEntrySetType( tte, GNC_AMT_TYPE_VALUE );
00102     gncTaxTableEntrySetAmount( tte, gnc_numeric_zero() );
00103     gncTaxTableAddEntry( tt, tte );
00104     tte = gncTaxTableEntryCreate();
00105     gncTaxTableEntrySetAccount( tte, acct2 );
00106     gncTaxTableEntrySetType( tte, GNC_AMT_TYPE_PERCENT );
00107     gncTaxTableEntrySetAmount( tte, gnc_numeric_zero() );
00108     gncTaxTableAddEntry( tt, tte );
00109 
00110     cust = gncCustomerCreate( book );
00111     gncCustomerSetID( cust, "0001" );
00112     gncCustomerSetName( cust, "MyCustomer" );
00113     gncCustomerSetNotes( cust, "Here are some notes" );
00114     gncCustomerSetCurrency( cust, currency );
00115     addr = gncAddressCreate( book, QOF_INSTANCE(cust) );
00116     gncAddressSetName( addr, "theAddress" );
00117     gncAddressSetAddr1( addr, "Address line #1" );
00118     gncAddressSetAddr2( addr, "Address line #2" );
00119     gncAddressSetAddr3( addr, "Address line #3" );
00120     gncAddressSetAddr4( addr, "Address line #4" );
00121     gncAddressSetPhone( addr, "(123) 555-1212" );
00122     gncAddressSetPhone( addr, "(123) 555-2121" );
00123     gncAddressSetEmail( addr, "cust@mycustomer.com" );
00124 
00125     emp = gncEmployeeCreate( book );
00126     gncEmployeeSetID( emp, "0001" );
00127     gncEmployeeSetUsername( emp, "gnucash" );
00128     gncEmployeeSetLanguage( emp, "english" );
00129     gncEmployeeSetCurrency( emp, currency );
00130 
00131     return session;
00132 }
00133 
00134 /* Order in which business objects need to be loaded */
00135 static const gchar* fixed_load_order[] =
00136 { GNC_ID_BILLTERM, GNC_ID_TAXTABLE, NULL };
00137 
00138 static void
00139 init_business_sql(void)
00140 {
00141     /* Initialize our pointers into the backend subsystem */
00142     gnc_address_sql_initialize();
00143     gnc_billterm_sql_initialize();
00144     gnc_customer_sql_initialize();
00145     gnc_employee_sql_initialize();
00146     gnc_entry_sql_initialize();
00147     gnc_invoice_sql_initialize();
00148     gnc_job_sql_initialize();
00149     gnc_order_sql_initialize();
00150     gnc_owner_sql_initialize();
00151     gnc_taxtable_sql_initialize();
00152     gnc_vendor_sql_initialize();
00153 
00154     gnc_sql_set_load_order( fixed_load_order );
00155 }
00156 
00157 int main (int argc, char ** argv)
00158 {
00159     gchar* filename;
00160     QofSession* session_1;
00161 
00162     qof_init();
00163     cashobjects_register();
00164     xaccLogDisable();
00165     qof_load_backend_library ("../.libs/", GNC_LIB_NAME);
00166 
00167     // Create a session with data
00168     session_1 = create_session();
00169     filename = tempnam( "/tmp", "test-sqlite3-" );
00170     g_test_message ( "Using filename: %s\n", filename );
00171     test_dbi_business_store_and_reload( "sqlite3", session_1, filename );
00172 #if 0
00173     g_test_message ( "TEST_MYSQL_URL='%s'\n", TEST_MYSQL_URL );
00174     if ( strlen( TEST_MYSQL_URL ) > 0 )
00175     {
00176         session_1 = create_session();
00177         test_dbi_store_and_reload( "mysql", session_1, TEST_MYSQL_URL );
00178     }
00179     g_test_message ( "TEST_PGSQL_URL='%s'\n", TEST_PGSQL_URL );
00180     if ( strlen( TEST_PGSQL_URL ) > 0 )
00181     {
00182         session_1 = create_session();
00183         test_dbi_store_and_reload( "pgsql", session_1, TEST_PGSQL_URL );
00184     }
00185 #endif
00186     print_test_results();
00187     qof_close();
00188     exit(get_rv());
00189 }
00190 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines