GnuCash 2.4.99
test-dbi-basic.c
00001 /***************************************************************************
00002  *            test-dbi.c
00003  *
00004  *  Tests saving and loading to a dbi/sqlite3 db
00005  *
00006  *  Copyright (C) 2009  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-stuff.h"
00032 #include <unittest-support.h>
00033 
00034 #include "TransLog.h"
00035 #include "Account.h"
00036 #include "Transaction.h"
00037 #include "Split.h"
00038 #include "gnc-commodity.h"
00039 
00040 #define FILE_NAME "sqlite3:///tmp/test-sqlite3-file"
00041 #define GNC_LIB_NAME "gncmod-backend-dbi"
00042 
00043 static QofSession*
00044 create_session(void)
00045 {
00046     QofSession* session;
00047     QofBook* book;
00048     Account *root, *acct1, *acct2;
00049     KvpFrame* frame;
00050     Transaction* tx;
00051     Split *spl1, *spl2;
00052     Timespec ts;
00053     struct timeval tv;
00054     gnc_commodity_table* table;
00055     gnc_commodity* currency;
00056     gchar *msg = "[gnc_dbi_unlock()] There was no lock entry in the Lock table";
00057     gchar *log_domain = "gnc.backend.dbi";
00058     guint loglevel = G_LOG_LEVEL_WARNING, hdlr;
00059     TestErrorStruct check = { loglevel, log_domain, msg };
00060     hdlr = g_log_set_handler (log_domain, loglevel,
00061                               (GLogFunc)test_checked_handler, &check);
00062 
00063     session = qof_session_new();
00064     book = qof_session_get_book( session );
00065     root = gnc_book_get_root_account( book );
00066     g_log_remove_handler (log_domain, hdlr);
00067 
00068     table = gnc_commodity_table_get_table( book );
00069     currency = gnc_commodity_table_lookup( table, GNC_COMMODITY_NS_CURRENCY, "CAD" );
00070 
00071     acct1 = xaccMallocAccount( book );
00072     xaccAccountSetType( acct1, ACCT_TYPE_BANK );
00073     xaccAccountSetName( acct1, "Bank 1" );
00074     xaccAccountSetCommodity( acct1, currency );
00075 
00076     frame = qof_instance_get_slots( QOF_INSTANCE(acct1) );
00077     kvp_frame_set_gint64( frame, "int64-val", 100 );
00078     kvp_frame_set_double( frame, "double-val", 3.14159 );
00079     kvp_frame_set_numeric( frame, "numeric-val", gnc_numeric_zero() );
00080 
00081     time( &(tv.tv_sec) );
00082     tv.tv_usec = 0;
00083     ts.tv_sec = tv.tv_sec;
00084     ts.tv_nsec = 1000 * tv.tv_usec;
00085     kvp_frame_set_timespec( frame, "timespec-val", ts );
00086 
00087     kvp_frame_set_string( frame, "string-val", "abcdefghijklmnop" );
00088     kvp_frame_set_guid( frame, "guid-val", qof_instance_get_guid( QOF_INSTANCE(acct1) ) );
00089 
00090     gnc_account_append_child( root, acct1 );
00091 
00092     acct2 = xaccMallocAccount( book );
00093     xaccAccountSetType( acct2, ACCT_TYPE_BANK );
00094     xaccAccountSetName( acct2, "Bank 1" );
00095 
00096     tx = xaccMallocTransaction( book );
00097     xaccTransBeginEdit( tx );
00098     xaccTransSetCurrency( tx, currency );
00099     spl1 = xaccMallocSplit( book );
00100     xaccTransAppendSplit( tx, spl1 );
00101     spl2 = xaccMallocSplit( book );
00102     xaccTransAppendSplit( tx, spl2 );
00103     xaccTransCommitEdit( tx );
00104 
00105 
00106     return session;
00107 }
00108 
00109 int main (int argc, char ** argv)
00110 {
00111     gchar* filename;
00112     QofSession* session_1;
00113 
00114     qof_init();
00115     cashobjects_register();
00116     xaccLogDisable();
00117     qof_load_backend_library ("../.libs/", GNC_LIB_NAME);
00118 
00119     // Create a session with data
00120     session_1 = create_session();
00121     filename = tempnam( "/tmp", "test-sqlite3-" );
00122     g_test_message ( "Using filename: %s\n", filename );
00123     test_dbi_store_and_reload( "sqlite3", session_1, filename );
00124     session_1 = create_session();
00125     test_dbi_safe_save( "sqlite3", filename );
00126     test_dbi_version_control( "sqlite3", filename );
00127 #ifdef TEST_MYSQL_URL
00128     g_test_message ( "TEST_MYSQL_URL='%s'\n", TEST_MYSQL_URL );
00129     if ( strlen( TEST_MYSQL_URL ) > 0 )
00130     {
00131         session_1 = create_session();
00132         test_dbi_store_and_reload( "mysql", session_1, TEST_MYSQL_URL );
00133         session_1 = create_session();
00134         test_dbi_safe_save( "mysql", filename );
00135         test_dbi_version_control( "mysql", filename );
00136     }
00137 #endif
00138 #ifdef TEST_PGSQL_URL
00139     g_test_message ( "TEST_PGSQL_URL='%s'\n", TEST_PGSQL_URL );
00140     if ( strlen( TEST_PGSQL_URL ) > 0 )
00141     {
00142         session_1 = create_session();
00143         test_dbi_store_and_reload( "pgsql", session_1, TEST_PGSQL_URL );
00144         session_1 = create_session();
00145         test_dbi_safe_save( "pgsql", filename );
00146         test_dbi_version_control( "pgsql", filename );
00147     }
00148 #endif
00149     print_test_results();
00150     qof_close();
00151     exit(get_rv());
00152 }
00153 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines