GnuCash 2.4.99
test-qofbook.c
00001 /********************************************************************
00002  * test_qofbook.c: GLib g_test test suite for qofbook.              *
00003  * Copyright 2011 John Ralls <jralls@ceridwen.us>                   *
00004  *                                                                  *
00005  * This program is free software; you can redistribute it and/or    *
00006  * modify it under the terms of the GNU General Public License as   *
00007  * published by the Free Software Foundation; either version 2 of   *
00008  * the License, or (at your option) any later version.              *
00009  *                                                                  *
00010  * This program is distributed in the hope that it will be useful,  *
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
00013  * GNU General Public License for more details.                     *
00014  *                                                                  *
00015  * You should have received a copy of the GNU General Public License*
00016  * along with this program; if not, contact:                        *
00017  *                                                                  *
00018  * Free Software Foundation           Voice:  +1-617-542-5942       *
00019  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
00020  * Boston, MA  02110-1301,  USA       gnu@gnu.org                   *
00021 \********************************************************************/
00022 #include "config.h"
00023 #include <string.h>
00024 #include <glib.h>
00025 #include <unittest-support.h>
00026 #include "../qof.h"
00027 #include "../qofbook-p.h"
00028 #include "../qofbookslots.h"
00029 
00030 static const gchar *suitename = "/qof/qofbook";
00031 void test_suite_qofbook ( void );
00032 
00033 typedef struct
00034 {
00035     QofBook *book;
00036 } Fixture;
00037 
00038 static struct
00039 {
00040     guint param;
00041     gpointer data;
00042     gboolean called;
00043     gchar* msg;
00044 } test_struct;
00045 
00046 static struct
00047 {
00048     gboolean col1_called;
00049     gboolean col2_called;
00050     gpointer data;
00051 } col_struct;
00052 
00053 static void
00054 setup( Fixture *fixture, gconstpointer pData )
00055 {
00056     fixture->book = qof_book_new();
00057 }
00058 
00059 static void
00060 teardown( Fixture *fixture, gconstpointer pData )
00061 {
00062     qof_book_destroy( fixture->book );
00063 }
00064 
00065 /* use g_free on test_struct.msg after this function been called */
00066 static gboolean
00067 handle_faults ( const char * log_domain, GLogLevelFlags log_level, const gchar *msg, gpointer user_data)
00068 {
00069     test_struct.msg = (gchar *) g_strdup( msg );
00070     return FALSE;
00071 }
00072 
00073 /* mock dirty callback function */
00074 static void
00075 mock_dirty_cb (QofBook *book, gboolean dirty, gpointer user_data)
00076 {
00077     test_struct.called = TRUE;
00078     g_test_message( "Checking if book is valid" );
00079     g_assert( book );
00080     g_assert( QOF_IS_BOOK( book ) );
00081     g_test_message( "Checking parameters" );
00082     g_assert( dirty );
00083     g_assert( user_data == test_struct.data );
00084 }
00085 
00086 /* mock callback for qof_book_foreach_collection testing */
00087 static void
00088 mock_foreach_collection (QofCollection *col, gpointer user_data)
00089 {
00090     g_test_message( "Checking if collection and data passed correctly" );
00091     g_assert( col );
00092     g_assert( user_data == col_struct.data );
00093     if ( g_strcmp0( qof_collection_get_type(col), "my_type" ) == 0 )
00094         col_struct.col1_called = TRUE;
00095     else if ( g_strcmp0( qof_collection_get_type(col), "my_type2" ) == 0 )
00096         col_struct.col2_called = TRUE;
00097 }
00098 
00099 /* mock final callback function */
00100 static void
00101 mock_final_cb (QofBook *book, gpointer key, gpointer user_data)
00102 {
00103     test_struct.called = TRUE;
00104     g_assert( book );
00105     g_assert( QOF_IS_BOOK( book ) );
00106     g_test_message( "Checking parameters" );
00107     g_assert_cmpstr( (gchar*)key, == , "key" );
00108     g_assert_cmpstr( (gchar*)user_data, == , "data" );
00109 }
00110 
00111 static void
00112 test_book_readonly( Fixture *fixture, gconstpointer pData )
00113 {
00114     g_assert( fixture->book != NULL );
00115     g_assert( !qof_book_is_readonly( fixture->book ) );
00116     qof_book_mark_readonly( fixture->book );
00117     g_assert( qof_book_is_readonly( fixture->book ) );
00118 }
00119 static void
00120 test_book_validate_counter( void )
00121 {
00122     gchar *r;
00123     g_test_bug("644036");
00124 
00125     /* Test for detection of missing format conversion */
00126     r = qof_book_validate_counter_format("This string is missing the conversion specifier");
00127     g_assert(r);
00128     if (r && g_test_verbose())
00129     {
00130         g_test_message("Counter format validation correctly failed: %s", r);
00131     }
00132     g_free(r);
00133 
00134     /* Test the usual Linux/Unix G_GINT64_FORMAT */
00135     r = qof_book_validate_counter_format_internal("Test - %li", "li");
00136     if (r && g_test_verbose())
00137     {
00138         g_test_message("Counter format validation erroneously failed: %s", r);
00139     }
00140     g_assert(r == NULL);
00141     g_free(r);
00142 
00143     /* Test the Windows G_GINT64_FORMAT */
00144     r = qof_book_validate_counter_format_internal("Test - %I64i", "I64i");
00145     if (r && g_test_verbose())
00146     {
00147         g_test_message("Counter format validation erroneously failed: %s", r);
00148     }
00149     g_assert(r == NULL);
00150     g_free(r);
00151 
00152     /* Test the system's GINT64_FORMAT */
00153     r = qof_book_validate_counter_format("Test - %" G_GINT64_FORMAT);
00154     if (r && g_test_verbose())
00155     {
00156         g_test_message("Counter format validation erroneously failed: %s", r);
00157     }
00158     g_assert(r == NULL);
00159     g_free(r);
00160 
00161     /* Test an erroneous Windows G_GINT64_FORMAT */
00162     r = qof_book_validate_counter_format_internal("Test - %li", "I64i");
00163     if (r && g_test_verbose())
00164     {
00165         g_test_message("Counter format validation correctly failed: %s", r);
00166     }
00167     g_assert(r);
00168     g_free(r);
00169 
00170     /* Test an erroneous Linux G_GINT64_FORMAT */
00171     r = qof_book_validate_counter_format_internal("Test - %I64i", "li");
00172     if (r && g_test_verbose())
00173     {
00174         g_test_message("Counter format validation correctly failed: %s", r);
00175     }
00176     g_assert(r);
00177     g_free(r);
00178 }
00179 
00180 static void
00181 test_book_get_string_option( Fixture *fixture, gconstpointer pData )
00182 {
00183     const char *opt_name = "Option Name";
00184     const char *opt_value = "Option Value";
00185     const char *opt_name_notset = "Not Set";
00186     g_assert( fixture->book != NULL );
00187     qof_book_set_string_option( fixture->book, opt_name, opt_value);
00188     g_assert_cmpstr( qof_book_get_string_option( fixture->book, opt_name ), == , opt_value);
00189     g_assert_cmpstr( qof_book_get_string_option( fixture->book, opt_name_notset ), == , NULL );
00190 }
00191 
00192 static void
00193 test_book_set_string_option( Fixture *fixture, gconstpointer pData )
00194 {
00195     const char *opt_name = "Option Name";
00196     const char *opt_value = "Option Value";
00197     g_assert( fixture->book != NULL );
00198     qof_book_set_string_option( fixture->book, opt_name, opt_value);
00199     g_assert( qof_instance_is_dirty (QOF_INSTANCE (fixture->book)) );
00200 }
00201 
00202 static void
00203 test_book_session_not_saved( Fixture *fixture, gconstpointer pData )
00204 {
00205     g_assert( fixture->book != NULL );
00206     g_assert( !qof_book_session_not_saved( fixture->book ) );
00207     qof_book_mark_session_saved( fixture->book );
00208     g_assert( !qof_book_session_not_saved( fixture->book ) );
00209     qof_book_mark_session_dirty( fixture-> book );
00210     g_assert( qof_book_session_not_saved( fixture->book ) );
00211 }
00212 
00213 static void
00214 test_book_mark_session_saved( Fixture *fixture, gconstpointer pData )
00215 {
00216     time_t dirty_time, clean_time;
00217 
00218     qof_book_mark_session_dirty( fixture-> book );
00219     g_assert( qof_book_session_not_saved( fixture->book ) );
00220     dirty_time = qof_book_get_session_dirty_time( fixture->book );
00221     qof_book_mark_session_saved( fixture->book );
00222     clean_time = qof_book_get_session_dirty_time( fixture->book );
00223     g_assert( !qof_book_session_not_saved( fixture->book ) );
00224     g_assert( dirty_time != clean_time );
00225     g_assert( clean_time == 0);
00226 }
00227 
00228 static void
00229 test_book_get_counter( Fixture *fixture, gconstpointer pData )
00230 {
00231     const char *counter_name = "Counter name";
00232     const char *err_no_book = "No book";
00233     const char *err_invalid_cnt = "Invalid counter name";
00234     gint64 counter;
00235 
00236     /* need this as long as we have fatal warnings enabled */
00237     g_test_log_set_fatal_handler ( ( GTestLogFatalFunc )handle_faults, NULL );
00238 
00239     counter = qof_book_get_counter( NULL, counter_name );
00240     g_assert_cmpint( counter, == , -1 );
00241     g_assert( g_strrstr( test_struct.msg, err_no_book ) != NULL );
00242     g_free( test_struct.msg );
00243 
00244     counter = qof_book_get_counter( fixture->book, NULL );
00245     g_assert_cmpint( counter, == , -1 );
00246     g_assert( g_strrstr( test_struct.msg, err_invalid_cnt ) != NULL );
00247     g_free( test_struct.msg );
00248 
00249     counter = qof_book_get_counter( fixture->book, '\0' );
00250     g_assert_cmpint( counter, == , -1 );
00251     g_assert( g_strrstr( test_struct.msg, err_invalid_cnt ) != NULL );
00252     g_free( test_struct.msg );
00253 
00254     counter = qof_book_get_counter( fixture->book, counter_name );
00255     g_assert_cmpint( counter, == , 0 );
00256 
00257     qof_book_increment_and_format_counter( fixture->book, counter_name );
00258     counter = qof_book_get_counter( fixture->book, counter_name );
00259     g_assert_cmpint( counter, == , 1 );
00260 }
00261 
00262 static void
00263 test_book_get_counter_format ( Fixture *fixture, gconstpointer pData )
00264 {
00265     const char *counter_name = "Counter name";
00266     const char *counter_name_not_set = "Counter name not set";
00267     const char *err_no_book = "No book";
00268     const char *err_invalid_cnt = "Invalid counter name";
00269     gchar *r;
00270     gint64 counter;
00271 
00272     /* need this as long as we have fatal warnings enabled */
00273     g_test_log_set_fatal_handler ( ( GTestLogFatalFunc )handle_faults, NULL );
00274 
00275     g_test_message( "Testing counter format when book is null" );
00276     r = qof_book_get_counter_format( NULL, counter_name );
00277     g_assert_cmpstr( r, == , NULL );
00278     g_assert( g_strrstr( test_struct.msg, err_no_book ) != NULL );
00279     g_free( test_struct.msg );
00280 
00281     g_test_message( "Testing counter format when counter name is null" );
00282     r = qof_book_get_counter_format( fixture->book, NULL );
00283     g_assert_cmpstr( r, == , NULL );
00284     g_assert( g_strrstr( test_struct.msg, err_invalid_cnt ) != NULL );
00285     g_free( test_struct.msg );
00286 
00287     g_test_message( "Testing counter format when counter name is empty string" );
00288     r = qof_book_get_counter_format( fixture->book, '\0' );
00289     g_assert_cmpstr( r, == , NULL );
00290     g_assert( g_strrstr( test_struct.msg, err_invalid_cnt ) != NULL );
00291     g_free( test_struct.msg );
00292 
00293     g_test_message( "Testing counter format with existing counter" );
00294     counter = qof_book_get_counter( fixture->book, counter_name );
00295     r = qof_book_get_counter_format( fixture->book, counter_name );
00296     g_assert_cmpstr( r, == , "%.6" G_GINT64_FORMAT);
00297 
00298     g_test_message( "Testing counter format for default value" );
00299     r = qof_book_get_counter_format( fixture->book, counter_name );
00300     g_assert_cmpstr( r, == , "%.6" G_GINT64_FORMAT);
00301 }
00302 
00303 static void
00304 test_book_increment_and_format_counter ( Fixture *fixture, gconstpointer pData )
00305 {
00306     const char *counter_name = "Counter name";
00307     const char *err_no_book = "No book";
00308     const char *err_invalid_cnt = "Invalid counter name";
00309     gchar *format;
00310     gchar *r;
00311     gint64 counter;
00312 
00313     /* need this as long as we have fatal warnings enabled */
00314     g_test_log_set_fatal_handler ( ( GTestLogFatalFunc )handle_faults, NULL );
00315 
00316     g_test_message( "Testing increment and format when book is null" );
00317     r = qof_book_increment_and_format_counter( NULL, counter_name );
00318     g_assert_cmpstr( r, == , NULL );
00319     g_free( r );
00320     g_assert( g_strrstr( test_struct.msg, err_no_book ) != NULL );
00321     g_free( test_struct.msg );
00322 
00323     g_test_message( "Testing increment and format when counter name is null" );
00324     r = qof_book_increment_and_format_counter( fixture->book, NULL );
00325     g_assert_cmpstr( r, == , NULL );
00326     g_free( r );
00327     g_assert( g_strrstr( test_struct.msg, err_invalid_cnt ) != NULL );
00328     g_free( test_struct.msg );
00329 
00330     g_test_message( "Testing increment and format when counter name is empty string" );
00331     r = qof_book_increment_and_format_counter( fixture->book, '\0' );
00332     g_assert_cmpstr( r, == , NULL );
00333     g_free( r );
00334     g_assert( g_strrstr( test_struct.msg, err_invalid_cnt ) != NULL );
00335     g_free( test_struct.msg );
00336 
00337     g_test_message( "Testing increment and format with new counter" );
00338     r = qof_book_increment_and_format_counter( fixture->book, counter_name );
00339     counter = qof_book_get_counter( fixture->book, counter_name );
00340     format = qof_book_get_counter_format( fixture->book, counter_name );
00341     g_assert_cmpint( counter, == , 1 );
00342     g_assert( qof_instance_is_dirty (QOF_INSTANCE (fixture->book)) );
00343     g_assert_cmpstr( r, == , g_strdup_printf( format, counter ));
00344     g_free( r );
00345 
00346     g_test_message( "Testing increment and format with existing counter" );
00347     r = qof_book_increment_and_format_counter( fixture->book, counter_name );
00348     counter = qof_book_get_counter( fixture->book, counter_name );
00349     format = qof_book_get_counter_format( fixture->book, counter_name );
00350     g_assert_cmpint( counter, == , 2 );
00351     g_assert_cmpstr( r, == , g_strdup_printf( format, counter ));
00352     g_free( r );
00353 }
00354 
00355 static void
00356 test_book_kvp_changed( Fixture *fixture, gconstpointer pData )
00357 {
00358     g_test_message( "Testing book is marked dirty after kvp_changed" );
00359     g_assert( !qof_instance_is_dirty (QOF_INSTANCE (fixture->book)) );
00360     qof_book_kvp_changed( fixture->book );
00361     g_assert( qof_instance_is_dirty (QOF_INSTANCE (fixture->book)) );
00362 }
00363 
00364 static void
00365 test_book_use_trading_accounts( Fixture *fixture, gconstpointer pData )
00366 {
00367     const char *slot_path;
00368 
00369     /* create correct slot path */
00370     slot_path = (const char *) g_strconcat( KVP_OPTION_PATH, "/", OPTION_SECTION_ACCOUNTS, "/", OPTION_NAME_TRADING_ACCOUNTS, NULL );
00371     g_assert( slot_path != NULL );
00372 
00373     g_test_message( "Testing when no trading accounts are used" );
00374     g_assert( qof_book_use_trading_accounts( fixture-> book ) == FALSE );
00375 
00376     g_test_message( "Testing with incorrect slot path and correct value - t" );
00377     qof_book_set_string_option( fixture->book, OPTION_NAME_TRADING_ACCOUNTS, "t" );
00378     g_assert( qof_book_use_trading_accounts( fixture-> book ) == FALSE );
00379 
00380     g_test_message( "Testing with existing trading accounts set to true - t" );
00381     qof_book_set_string_option( fixture->book, slot_path, "t" );
00382     g_assert( qof_book_use_trading_accounts( fixture-> book ) == TRUE );
00383 
00384     g_test_message( "Testing with existing trading accounts and incorrect value - tt" );
00385     qof_book_set_string_option( fixture->book, slot_path, "tt" );
00386     g_assert( qof_book_use_trading_accounts( fixture-> book ) == FALSE );
00387 
00388 }
00389 
00390 static void
00391 test_book_get_num_days_autofreeze( Fixture *fixture, gconstpointer pData )
00392 {
00393     const char *slot_path;
00394 
00395     /* create correct slot path */
00396     slot_path = (const char *) g_strconcat( KVP_OPTION_PATH, "/", OPTION_SECTION_ACCOUNTS, "/", OPTION_NAME_AUTO_READONLY_DAYS, NULL );
00397     g_assert( slot_path != NULL );
00398 
00399     g_test_message( "Testing default: No auto-freeze days are set" );
00400     g_assert( qof_book_uses_autoreadonly( fixture-> book ) == FALSE );
00401     g_assert( qof_book_get_num_days_autoreadonly( fixture-> book ) == 0 );
00402 
00403     g_test_message( "Testing with incorrect slot path and some correct value - 17" );
00404     kvp_frame_set_double(qof_book_get_slots(fixture->book), OPTION_NAME_AUTO_READONLY_DAYS, 17);
00405     g_assert( qof_book_uses_autoreadonly( fixture-> book ) == FALSE );
00406     g_assert( qof_book_get_num_days_autoreadonly( fixture-> book ) == 0 );
00407 
00408     g_test_message( "Testing when setting this correctly with some correct value - 17" );
00409     kvp_frame_set_double(qof_book_get_slots(fixture->book), slot_path, 17);
00410     g_assert( qof_book_uses_autoreadonly( fixture-> book ) == TRUE );
00411     g_assert( qof_book_get_num_days_autoreadonly( fixture-> book ) == 17 );
00412 
00413     g_test_message( "Testing when setting this correctly to zero again" );
00414     kvp_frame_set_double(qof_book_get_slots(fixture->book), slot_path, 0);
00415     g_assert( qof_book_uses_autoreadonly( fixture-> book ) == FALSE );
00416     g_assert( qof_book_get_num_days_autoreadonly( fixture-> book ) == 0 );
00417 
00418     g_test_message( "Testing when setting this correctly with some correct value - 32" );
00419     kvp_frame_set_double(qof_book_get_slots(fixture->book), slot_path, 32);
00420     g_assert( qof_book_uses_autoreadonly( fixture-> book ) == TRUE );
00421     g_assert( qof_book_get_num_days_autoreadonly( fixture-> book ) == 32 );
00422 
00423 }
00424 
00425 static void
00426 test_book_mark_session_dirty( Fixture *fixture, gconstpointer pData )
00427 {
00428     QofBook *_empty = NULL;
00429     time_t before, after;
00430     guint param = (guint) g_test_rand_int();
00431 
00432     g_test_message( "Testing when book is NULL" );
00433     qof_book_mark_session_dirty( _empty );
00434     g_assert( _empty == NULL );
00435 
00436     g_test_message( "Testing when book is not dirty and dirty_cb is null" );
00437     g_assert_cmpint( qof_book_get_session_dirty_time( fixture->book ), == , 0);
00438     g_assert( fixture->book->dirty_cb == NULL );
00439     g_assert( qof_book_session_not_saved( fixture->book ) == FALSE );
00440     before = time( NULL );
00441     qof_book_mark_session_dirty( fixture->book );
00442     after = time( NULL );
00443     g_assert_cmpint( qof_book_get_session_dirty_time( fixture->book ), >= , before);
00444     g_assert_cmpint( qof_book_get_session_dirty_time( fixture->book ), <= , after);
00445     g_assert( qof_book_session_not_saved( fixture->book ) == TRUE );
00446 
00447     g_test_message( "Testing when book is not dirty and dirty_cb is not null" );
00448     /* prepare conditions */
00449     qof_book_mark_session_saved( fixture->book );
00450     qof_book_set_dirty_cb( fixture->book, mock_dirty_cb, (gpointer) (&param) );
00451     test_struct.data = (gpointer) (&param);
00452     test_struct.called = FALSE;
00453     g_assert( fixture->book->dirty_cb != NULL );
00454     g_assert_cmpint( qof_book_get_session_dirty_time( fixture->book ), == , 0);
00455     g_assert( qof_book_session_not_saved( fixture->book ) == FALSE );
00456     /* run FUT */
00457     before = time( NULL );
00458     qof_book_mark_session_dirty( fixture->book );
00459     after = time( NULL );
00460     /* test output */
00461     g_assert_cmpint( qof_book_get_session_dirty_time( fixture->book ), >= , before);
00462     g_assert_cmpint( qof_book_get_session_dirty_time( fixture->book ), <= , after);
00463     g_assert( qof_book_session_not_saved( fixture->book ) == TRUE );
00464     g_assert( test_struct.called );
00465 
00466     g_test_message( "Testing when book is dirty" );
00467     g_assert( qof_book_session_not_saved( fixture->book ) == TRUE );
00468     before = qof_book_get_session_dirty_time( fixture->book );
00469     qof_book_mark_session_dirty( fixture->book );
00470     g_assert( qof_book_session_not_saved( fixture->book ) == TRUE );
00471     after = qof_book_get_session_dirty_time( fixture->book );
00472     g_assert_cmpint( before, == , after );
00473 }
00474 
00475 static void
00476 test_book_get_session_dirty_time( Fixture *fixture, gconstpointer pData )
00477 {
00478     time_t before, after;
00479 
00480     g_test_message( "Testing time on saved book = 0" );
00481     g_assert( qof_book_session_not_saved( fixture->book ) == FALSE );
00482     g_assert_cmpint( qof_book_get_session_dirty_time( fixture->book ), == , 0);
00483 
00484     g_test_message( "Testing time on dirty book is correct" );
00485     before = time( NULL );
00486     qof_book_mark_session_dirty( fixture->book );
00487     after = time( NULL );
00488     g_assert_cmpint( qof_book_get_session_dirty_time( fixture->book ), >= , before);
00489     g_assert_cmpint( qof_book_get_session_dirty_time( fixture->book ), <= , after);
00490 
00491 }
00492 
00493 static void
00494 test_book_set_dirty_cb( Fixture *fixture, gconstpointer pData )
00495 {
00496     const char * error_msg = "Already existing callback";
00497 
00498     g_test_message( "Testing when callback is previously not set" );
00499     g_assert( fixture->book->dirty_cb == NULL );
00500     qof_book_set_dirty_cb( fixture->book, mock_dirty_cb, (gpointer) (&test_struct) );
00501     g_assert( fixture->book->dirty_cb == mock_dirty_cb );
00502     g_assert( fixture->book->dirty_data == &test_struct );
00503 
00504     /* need this as long as we have fatal warnings enabled */
00505     g_test_log_set_fatal_handler ( ( GTestLogFatalFunc )handle_faults, NULL );
00506 
00507     g_test_message( "Testing when callback was previously set" );
00508     g_assert( fixture->book->dirty_cb != NULL );
00509     qof_book_set_dirty_cb( fixture->book, NULL, NULL );
00510     g_assert( g_strrstr( test_struct.msg, error_msg ) != NULL );
00511     g_assert( fixture->book->dirty_cb == NULL );
00512     g_assert( fixture->book->dirty_data == NULL );
00513     g_free( test_struct.msg );
00514 }
00515 
00516 static void
00517 test_book_shutting_down( Fixture *fixture, gconstpointer pData )
00518 {
00519     g_test_message( "Testing when book is null" );
00520     g_assert( qof_book_shutting_down( NULL ) == FALSE );
00521     g_test_message( "Testing when shutting down is true" );
00522     fixture->book->shutting_down = TRUE;
00523     g_assert( qof_book_shutting_down( fixture->book ) == TRUE );
00524     g_test_message( "Testing when shutting down is false" );
00525     fixture->book->shutting_down = FALSE;
00526     g_assert( qof_book_shutting_down( fixture->book ) == FALSE );
00527 }
00528 
00529 static void
00530 test_book_set_get_data( Fixture *fixture, gconstpointer pData )
00531 {
00532     const char *key = "key";
00533     const char *data = "data";
00534 
00535     g_assert( fixture->book->data_tables != NULL );
00536     g_test_message( "Testing when book is null" );
00537     qof_book_set_data( NULL, key, (gpointer) data );
00538     g_assert( qof_book_get_data( NULL, key ) == NULL );
00539 
00540     g_test_message( "Testing when key is null" );
00541     qof_book_set_data( fixture->book, NULL, (gpointer) data );
00542     g_assert( qof_book_get_data( fixture->book, NULL) == NULL );
00543 
00544     g_test_message( "Testing with book key not null, data null" );
00545     qof_book_set_data( fixture->book, key, NULL );
00546     g_assert( qof_book_get_data( fixture->book, key ) == NULL );
00547 
00548     g_test_message( "Testing with book key data not null" );
00549     qof_book_set_data( fixture->book, key, (gpointer) data );
00550     g_assert_cmpstr( (const char *)qof_book_get_data( fixture->book, key ), == , data );
00551 }
00552 
00553 static void
00554 test_book_get_collection( Fixture *fixture, gconstpointer pData )
00555 {
00556     QofIdType my_type = "my type";
00557     QofCollection *m_col, *m_col2;
00558 
00559     g_test_message( "Testing when book is null" );
00560     g_assert( qof_book_get_collection( NULL, my_type ) == NULL );
00561 
00562     g_test_message( "Testing when entity type is null" );
00563     g_assert( qof_book_get_collection( fixture->book, NULL ) == NULL );
00564 
00565     g_test_message( "Testing when collection does not exist" );
00566     g_assert( fixture->book->hash_of_collections != NULL );
00567     g_assert( g_hash_table_lookup ( fixture->book->hash_of_collections, my_type ) == NULL );
00568     m_col = qof_book_get_collection( fixture->book, my_type );
00569     g_assert( m_col != NULL );
00570 
00571     g_test_message( "Testing with existing collection" );
00572     g_assert( g_hash_table_lookup ( fixture->book->hash_of_collections, my_type ) != NULL );
00573     m_col2 = qof_book_get_collection( fixture->book, my_type );
00574     g_assert( m_col2 != NULL );
00575     g_assert( m_col == m_col2 );
00576 }
00577 
00578 static void
00579 test_book_foreach_collection( Fixture *fixture, gconstpointer pData )
00580 {
00581     QofCollection *m_col, *m_col2;
00582     QofIdType my_type = "my_type", my_type2 = "my_type2";
00583     guint param = (guint) g_test_rand_int();
00584     gchar *msg1 = "qof_book_foreach_collection: assertion `book' failed";
00585     gchar *msg2 = "qof_book_foreach_collection: assertion `cb' failed";
00586     gchar *log_domain = "qof";
00587     guint loglevel = G_LOG_LEVEL_CRITICAL | G_LOG_FLAG_FATAL, hdlr;
00588     TestErrorStruct check1 = { loglevel, log_domain, msg1 };
00589     TestErrorStruct check2 = { loglevel, log_domain, msg2 };
00590 
00591     /* need this as long as we have fatal warnings enabled */
00592     g_test_log_set_fatal_handler ( ( GTestLogFatalFunc )handle_faults, NULL );
00593     test_add_error (&check1);
00594     test_add_error (&check2);
00595     hdlr = g_log_set_handler (log_domain, loglevel,
00596                               (GLogFunc)test_list_handler, NULL);
00597 
00598     g_test_message( "Testing when book is null" );
00599     m_col = qof_book_get_collection( fixture->book, my_type );
00600     m_col2 = qof_book_get_collection( fixture->book, my_type2 );
00601     col_struct.col1_called = FALSE;
00602     col_struct.col2_called = FALSE;
00603     col_struct.data = (gpointer) (&param);
00604     /* launch foreach make sure callback was not called and check warning msg */
00605     qof_book_foreach_collection( NULL, mock_foreach_collection, (gpointer)(&param) );
00606     g_assert( !col_struct.col1_called );
00607     g_assert( !col_struct.col2_called );
00608     g_assert_cmpstr( test_struct.msg, == , "qof_book_foreach_collection: assertion `book' failed" );
00609     g_free( test_struct.msg );
00610 
00611     g_test_message( "Testing when cb is null" );
00612     /* launching with empty cb should still fail and produce warning */
00613     qof_book_foreach_collection( fixture->book, NULL, (gpointer)(&param) );
00614     g_assert( !col_struct.col1_called );
00615     g_assert( !col_struct.col2_called );
00616     g_assert_cmpstr( test_struct.msg, == , "qof_book_foreach_collection: assertion `cb' failed" );
00617     g_free( test_struct.msg );
00618     g_log_remove_handler (log_domain, hdlr);
00619     test_clear_error_list ();
00620 
00621     g_test_message( "Testing when book and cb not null, user_data provided" );
00622     /* both cols have to be called */
00623     qof_book_foreach_collection( fixture->book, mock_foreach_collection, (gpointer)(&param) );
00624     g_assert( col_struct.col1_called );
00625     g_assert( col_struct.col2_called );
00626 }
00627 
00628 static void
00629 test_book_set_data_fin( void )
00630 {
00631     QofBook *book;
00632     const char *key = "key";
00633     const char *data = "data";
00634 
00635     /* init */
00636     book = qof_book_new();
00637     g_assert_cmpint( g_hash_table_size( book->data_tables ), == , 0 );
00638     g_assert_cmpint( g_hash_table_size( book->data_table_finalizers ), == , 0 );
00639 
00640     g_test_message( "Testing when book is null" );
00641     qof_book_set_data_fin( NULL, key, (gpointer) data, mock_final_cb );
00642     /* assert nothing was set */
00643     g_assert_cmpint( g_hash_table_size( book->data_tables ), == , 0 );
00644     g_assert_cmpint( g_hash_table_size( book->data_table_finalizers ), == , 0 );
00645 
00646     g_test_message( "Testing when key is null" );
00647     qof_book_set_data_fin( book, NULL, (gpointer) data, mock_final_cb );
00648     /* nothing set as well */
00649     g_assert_cmpint( g_hash_table_size( book->data_tables ), == , 0 );
00650     g_assert_cmpint( g_hash_table_size( book->data_table_finalizers ), == , 0 );
00651 
00652     g_test_message( "Testing with book key not null, cb null" );
00653     qof_book_set_data_fin( book, key, (gpointer) data, NULL );
00654     /* now data is set cb not set */
00655     g_assert_cmpint( g_hash_table_size( book->data_tables ), == , 1 );
00656     g_assert_cmpint( g_hash_table_size( book->data_table_finalizers ), == , 0 );
00657     g_assert_cmpstr( (const char *)qof_book_get_data( book, key ), == , data );
00658 
00659     g_test_message( "Testing with all data set" );
00660     qof_book_set_data_fin( book, key, (gpointer) data, mock_final_cb );
00661     /* now we have all set */
00662     g_assert_cmpint( g_hash_table_size( book->data_tables ), == , 1 );
00663     g_assert_cmpint( g_hash_table_size( book->data_table_finalizers ), == , 1 );
00664     g_assert_cmpstr( (const char *)qof_book_get_data( book, key ), == , data );
00665     g_assert( g_hash_table_lookup ( book->data_table_finalizers, (gpointer)key ) == mock_final_cb );
00666 
00667     /* get rid of book make sure final cb is called */
00668     test_struct.called = FALSE;
00669     qof_book_destroy( book );
00670     g_assert( test_struct.called );
00671 }
00672 
00673 static void
00674 test_book_mark_closed( Fixture *fixture, gconstpointer pData )
00675 {
00676     g_test_message( "Testing when book is null" );
00677     g_assert_cmpstr( &fixture->book->book_open, == , "y" );
00678     qof_book_mark_closed( NULL );
00679     g_assert_cmpstr( &fixture->book->book_open, == , "y" );
00680 
00681     g_test_message( "Testing when book is not null" );
00682     qof_book_mark_closed( fixture->book );
00683     g_assert_cmpstr( &fixture->book->book_open, == , "n" );
00684 }
00685 
00686 static void
00687 test_book_new_destroy( void )
00688 {
00689     QofBook *book;
00690     const char *key = "key";
00691     const char *data = "data";
00692 
00693     g_test_message( "Testing book creation and initial setup" );
00694     book = qof_book_new();
00695     g_assert( book );
00696     g_assert( QOF_IS_BOOK( book ) );
00697 
00698     g_test_message( "Testing book initial setup" );
00699     g_assert( book->hash_of_collections );
00700     g_assert( book->data_tables );
00701     g_assert( book->data_table_finalizers );
00702     g_assert_cmpint( g_hash_table_size( book->hash_of_collections ), == , 1 );
00703     g_assert( g_hash_table_lookup ( book->hash_of_collections, QOF_ID_BOOK ) != NULL );
00704     g_assert_cmpint( g_hash_table_size( book->data_tables ), == , 0 );
00705     g_assert_cmpint( g_hash_table_size( book->data_table_finalizers ), == , 0 );
00706     g_assert_cmpstr( &book->book_open, == , "y");
00707     g_assert( !book->read_only );
00708     g_assert_cmpint( book->version, == , 0 );
00709 
00710     /* set finalizer */
00711     qof_book_set_data_fin( book, key, (gpointer) data, mock_final_cb );
00712     test_struct.called = FALSE;
00713 
00714     g_test_message( "Testing book destroy" );
00715     qof_book_destroy( book );
00716     g_assert( qof_book_shutting_down( book ) );
00717     g_assert( test_struct.called );
00718 }
00719 
00720 void
00721 test_suite_qofbook ( void )
00722 {
00723     GNC_TEST_ADD( suitename, "readonly", Fixture, NULL, setup, test_book_readonly, teardown );
00724     GNC_TEST_ADD_FUNC( suitename, "validate counter", test_book_validate_counter );
00725     GNC_TEST_ADD( suitename, "get string option", Fixture, NULL, setup, test_book_get_string_option, teardown );
00726     GNC_TEST_ADD( suitename, "set string option", Fixture, NULL, setup, test_book_set_string_option, teardown );
00727     GNC_TEST_ADD( suitename, "session not saved", Fixture, NULL, setup, test_book_session_not_saved, teardown );
00728     GNC_TEST_ADD( suitename, "session mark saved", Fixture, NULL, setup, test_book_mark_session_saved, teardown );
00729     GNC_TEST_ADD( suitename, "get counter", Fixture, NULL, setup, test_book_get_counter, teardown );
00730     GNC_TEST_ADD( suitename, "get counter format", Fixture, NULL, setup, test_book_get_counter_format, teardown );
00731     GNC_TEST_ADD( suitename, "increment and format counter", Fixture, NULL, setup, test_book_increment_and_format_counter, teardown );
00732     GNC_TEST_ADD( suitename, "kvp changed", Fixture, NULL, setup, test_book_kvp_changed, teardown );
00733     GNC_TEST_ADD( suitename, "use trading accounts", Fixture, NULL, setup, test_book_use_trading_accounts, teardown );
00734     GNC_TEST_ADD( suitename, "get autofreeze days", Fixture, NULL, setup, test_book_get_num_days_autofreeze, teardown );
00735     GNC_TEST_ADD( suitename, "mark session dirty", Fixture, NULL, setup, test_book_mark_session_dirty, teardown );
00736     GNC_TEST_ADD( suitename, "session dirty time", Fixture, NULL, setup, test_book_get_session_dirty_time, teardown );
00737     GNC_TEST_ADD( suitename, "set dirty callback", Fixture, NULL, setup, test_book_set_dirty_cb, teardown );
00738     GNC_TEST_ADD( suitename, "shutting down", Fixture, NULL, setup, test_book_shutting_down, teardown );
00739     GNC_TEST_ADD( suitename, "set get data", Fixture, NULL, setup, test_book_set_get_data, teardown );
00740     GNC_TEST_ADD( suitename, "get collection", Fixture, NULL, setup, test_book_get_collection, teardown );
00741     GNC_TEST_ADD( suitename, "foreach collection", Fixture, NULL, setup, test_book_foreach_collection, teardown );
00742     GNC_TEST_ADD_FUNC( suitename, "set data finalizers", test_book_set_data_fin );
00743     GNC_TEST_ADD( suitename, "mark closed", Fixture, NULL, setup, test_book_mark_closed, teardown );
00744     GNC_TEST_ADD_FUNC( suitename, "book new and destroy", test_book_new_destroy );
00745 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines