30 #include <unittest-support.h>
36 #include "../qofbook-p.h"
37 #include "../qofbookslots.h"
45 static const gchar *suitename =
"/qof/qofbook";
46 void test_suite_qofbook (
void );
69 setup(
Fixture *fixture, gconstpointer pData )
75 teardown(
Fixture *fixture, gconstpointer pData )
82 handle_faults (
const char * log_domain, GLogLevelFlags log_level,
const gchar *msg, gpointer user_data)
84 test_struct.msg = (gchar *) g_strdup( msg );
90 mock_dirty_cb (
QofBook *book, gboolean dirty, gpointer user_data)
92 test_struct.called = TRUE;
93 g_test_message(
"Checking if book is valid" );
95 g_assert( QOF_IS_BOOK( book ) );
96 g_test_message(
"Checking parameters" );
98 g_assert( user_data == test_struct.data );
103 mock_foreach_collection (QofCollection *col, gpointer user_data)
105 g_test_message(
"Checking if collection and data passed correctly" );
107 g_assert( user_data == col_struct.data );
109 col_struct.col1_called = TRUE;
111 col_struct.col2_called = TRUE;
116 mock_final_cb (
QofBook *book, gpointer key, gpointer user_data)
118 test_struct.called = TRUE;
120 g_assert( QOF_IS_BOOK( book ) );
121 g_test_message(
"Checking parameters" );
122 g_assert_cmpstr( (gchar*)key, == ,
"key" );
123 g_assert_cmpstr( (gchar*)user_data, == ,
"data" );
127 test_book_readonly(
Fixture *fixture, gconstpointer pData )
129 g_assert( fixture->book != NULL );
135 test_book_validate_counter(
void )
138 g_test_bug(
"644036");
143 if (r && g_test_verbose())
145 g_test_message(
"Counter format validation correctly failed: %s", r);
151 if (r && g_test_verbose())
153 g_test_message(
"Counter format validation erroneously failed: %s", r);
160 if (r && g_test_verbose())
162 g_test_message(
"Counter format validation erroneously failed: %s", r);
169 if (r && g_test_verbose())
171 g_test_message(
"Counter format validation erroneously failed: %s", r);
178 if (r && g_test_verbose())
180 g_test_message(
"Counter format validation correctly failed: %s", r);
187 if (r && g_test_verbose())
189 g_test_message(
"Counter format validation correctly failed: %s", r);
196 test_book_get_string_option(
Fixture *fixture, gconstpointer pData )
198 const char *opt_name =
"Option Name";
199 const char *opt_value =
"Option Value";
200 const char *opt_name_notset =
"Not Set";
201 g_assert( fixture->book != NULL );
202 qof_book_set_string_option( fixture->book, opt_name, opt_value);
203 g_assert_cmpstr( qof_book_get_string_option( fixture->book, opt_name ), == , opt_value);
204 g_assert_cmpstr( qof_book_get_string_option( fixture->book, opt_name_notset ), == , NULL );
208 test_book_set_string_option(
Fixture *fixture, gconstpointer pData )
210 const char *opt_name =
"Option Name";
211 const char *opt_value =
"Option Value";
212 g_assert( fixture->book != NULL );
213 qof_book_set_string_option( fixture->book, opt_name, opt_value);
218 test_book_session_not_saved(
Fixture *fixture, gconstpointer pData )
220 g_assert( fixture->book != NULL );
229 test_book_mark_session_saved(
Fixture *fixture, gconstpointer pData )
231 time64 dirty_time, clean_time;
239 g_assert( dirty_time != clean_time );
240 g_assert( clean_time == 0);
244 test_book_get_counter(
Fixture *fixture, gconstpointer pData )
246 const char *counter_name =
"Counter name";
247 const char *err_no_book =
"No book";
248 const char *err_invalid_cnt =
"Invalid counter name";
252 g_test_log_set_fatal_handler ( ( GTestLogFatalFunc )handle_faults, NULL );
255 g_assert_cmpint( counter, == , -1 );
256 g_assert( g_strrstr( test_struct.msg, err_no_book ) != NULL );
257 g_free( test_struct.msg );
260 g_assert_cmpint( counter, == , -1 );
261 g_assert( g_strrstr( test_struct.msg, err_invalid_cnt ) != NULL );
262 g_free( test_struct.msg );
265 g_assert_cmpint( counter, == , -1 );
266 g_assert( g_strrstr( test_struct.msg, err_invalid_cnt ) != NULL );
267 g_free( test_struct.msg );
270 g_assert_cmpint( counter, == , 0 );
274 g_assert_cmpint( counter, == , 1 );
278 test_book_get_counter_format (
Fixture *fixture, gconstpointer pData )
280 const char *counter_name =
"Counter name";
281 const char *err_no_book =
"No book";
282 const char *err_invalid_cnt =
"Invalid counter name";
286 g_test_log_set_fatal_handler ( ( GTestLogFatalFunc )handle_faults, NULL );
288 g_test_message(
"Testing counter format when book is null" );
290 g_assert_cmpstr( r, == , NULL );
291 g_assert( g_strrstr( test_struct.msg, err_no_book ) != NULL );
292 g_free( test_struct.msg );
294 g_test_message(
"Testing counter format when counter name is null" );
296 g_assert_cmpstr( r, == , NULL );
297 g_assert( g_strrstr( test_struct.msg, err_invalid_cnt ) != NULL );
298 g_free( test_struct.msg );
300 g_test_message(
"Testing counter format when counter name is empty string" );
302 g_assert_cmpstr( r, == , NULL );
303 g_assert( g_strrstr( test_struct.msg, err_invalid_cnt ) != NULL );
304 g_free( test_struct.msg );
306 g_test_message(
"Testing counter format with existing counter" );
308 g_assert_cmpstr( r, == ,
"%.6" G_GINT64_FORMAT);
310 g_test_message(
"Testing counter format for default value" );
312 g_assert_cmpstr( r, == ,
"%.6" G_GINT64_FORMAT);
316 test_book_increment_and_format_counter (
Fixture *fixture, gconstpointer pData )
318 const char *counter_name =
"Counter name";
319 const char *err_no_book =
"No book";
320 const char *err_invalid_cnt =
"Invalid counter name";
326 g_test_log_set_fatal_handler ( ( GTestLogFatalFunc )handle_faults, NULL );
328 g_test_message(
"Testing increment and format when book is null" );
330 g_assert_cmpstr( r, == , NULL );
332 g_assert( g_strrstr( test_struct.msg, err_no_book ) != NULL );
333 g_free( test_struct.msg );
335 g_test_message(
"Testing increment and format when counter name is null" );
337 g_assert_cmpstr( r, == , NULL );
339 g_assert( g_strrstr( test_struct.msg, err_invalid_cnt ) != NULL );
340 g_free( test_struct.msg );
342 g_test_message(
"Testing increment and format when counter name is empty string" );
344 g_assert_cmpstr( r, == , NULL );
346 g_assert( g_strrstr( test_struct.msg, err_invalid_cnt ) != NULL );
347 g_free( test_struct.msg );
349 g_test_message(
"Testing increment and format with new counter" );
353 g_assert_cmpint( counter, == , 1 );
355 g_assert_cmpstr( r, == , g_strdup_printf( format, counter ));
358 g_test_message(
"Testing increment and format with existing counter" );
362 g_assert_cmpint( counter, == , 2 );
363 g_assert_cmpstr( r, == , g_strdup_printf( format, counter ));
368 test_book_use_trading_accounts(
Fixture *fixture, gconstpointer pData )
372 g_test_message(
"Testing with existing trading accounts set to true - t" );
373 qof_book_begin_edit (fixture->book);
375 "trading-accts",
"t",
379 g_test_message(
"Testing with existing trading accounts and incorrect value - tt" );
381 "trading-accts",
"tt",
384 qof_book_commit_edit (fixture->book);
389 test_book_get_num_days_autofreeze(
Fixture *fixture, gconstpointer pData )
391 g_test_message(
"Testing default: No auto-freeze days are set" );
398 qof_book_begin_edit (fixture->book);
400 "autoreadonly-days", (gdouble)17,
405 g_test_message(
"Testing when setting this correctly to zero again" );
408 "autoreadonly-days", (gdouble)0,
414 "autoreadonly-days", (gdouble)32,
419 qof_book_commit_edit (fixture->book);
423 test_book_use_split_action_for_num_field(
Fixture *fixture, gconstpointer pData )
425 g_test_message(
"Testing default: No selection has been specified" );
428 g_test_message(
"Testing with existing use split action for num set to true - t" );
430 qof_book_begin_edit (fixture->book);
432 "split-action-num-field",
"t",
436 g_test_message(
"Testing with existing use split action for num and incorrect value - tt" );
438 "split-action-num-field",
"tt",
441 qof_book_commit_edit (fixture->book);
445 test_book_mark_session_dirty(
Fixture *fixture, gconstpointer pData )
449 guint param = (guint) g_test_rand_int();
451 g_test_message(
"Testing when book is NULL" );
453 g_assert( _empty == NULL );
455 g_test_message(
"Testing when book is not dirty and dirty_cb is null" );
457 g_assert( fixture->book->dirty_cb == NULL );
466 g_test_message(
"Testing when book is not dirty and dirty_cb is not null" );
470 test_struct.data = (gpointer) (¶m);
471 test_struct.called = FALSE;
472 g_assert( fixture->book->dirty_cb != NULL );
483 g_assert( test_struct.called );
485 g_test_message(
"Testing when book is dirty" );
491 g_assert_cmpint( before, == , after );
495 test_book_get_session_dirty_time(
Fixture *fixture, gconstpointer pData )
499 g_test_message(
"Testing time on saved book = 0" );
503 g_test_message(
"Testing time on dirty book is correct" );
513 test_book_set_dirty_cb(
Fixture *fixture, gconstpointer pData )
515 const char * error_msg =
"Already existing callback";
517 g_test_message(
"Testing when callback is previously not set" );
518 g_assert( fixture->book->dirty_cb == NULL );
520 g_assert( fixture->book->dirty_cb == mock_dirty_cb );
521 g_assert( fixture->book->dirty_data == &test_struct );
524 g_test_log_set_fatal_handler ( ( GTestLogFatalFunc )handle_faults, NULL );
526 g_test_message(
"Testing when callback was previously set" );
527 g_assert( fixture->book->dirty_cb != NULL );
529 g_assert( g_strrstr( test_struct.msg, error_msg ) != NULL );
530 g_assert( fixture->book->dirty_cb == NULL );
531 g_assert( fixture->book->dirty_data == NULL );
532 g_free( test_struct.msg );
536 test_book_shutting_down(
Fixture *fixture, gconstpointer pData )
538 g_test_message(
"Testing when book is null" );
540 g_test_message(
"Testing when shutting down is true" );
541 fixture->book->shutting_down = TRUE;
543 g_test_message(
"Testing when shutting down is false" );
544 fixture->book->shutting_down = FALSE;
549 test_book_set_get_data(
Fixture *fixture, gconstpointer pData )
551 const char *key =
"key";
552 const char *data =
"data";
554 g_assert( fixture->book->data_tables != NULL );
555 g_test_message(
"Testing when book is null" );
559 g_test_message(
"Testing when key is null" );
563 g_test_message(
"Testing with book key not null, data null" );
567 g_test_message(
"Testing with book key data not null" );
569 g_assert_cmpstr( (
const char *)
qof_book_get_data( fixture->book, key ), == , data );
573 test_book_get_collection(
Fixture *fixture, gconstpointer pData )
576 QofCollection *m_col, *m_col2;
578 g_test_message(
"Testing when book is null" );
581 g_test_message(
"Testing when entity type is null" );
584 g_test_message(
"Testing when collection does not exist" );
585 g_assert( fixture->book->hash_of_collections != NULL );
586 g_assert( g_hash_table_lookup ( fixture->book->hash_of_collections, my_type ) == NULL );
588 g_assert( m_col != NULL );
590 g_test_message(
"Testing with existing collection" );
591 g_assert( g_hash_table_lookup ( fixture->book->hash_of_collections, my_type ) != NULL );
593 g_assert( m_col2 != NULL );
594 g_assert( m_col == m_col2 );
598 test_book_foreach_collection(
Fixture *fixture, gconstpointer pData )
600 G_GNUC_UNUSED QofCollection *m_col, *m_col2;
601 QofIdType my_type =
"my_type", my_type2 =
"my_type2";
602 guint param = (guint) g_test_rand_int();
604 #if defined(__clang__)
605 #define _func "void qof_book_foreach_collection(const QofBook *, QofCollectionForeachCB, gpointer)"
607 #define _func "void qof_book_foreach_collection(const QofBook*, QofCollectionForeachCB, gpointer)"
610 gchar *msg1 = _func
": assertion " _Q
"book' failed";
611 gchar *msg2 = _func
": assertion " _Q
"cb' failed";
613 gchar *log_domain =
"qof";
614 guint loglevel = G_LOG_LEVEL_CRITICAL | G_LOG_FLAG_FATAL, hdlr;
619 g_test_log_set_fatal_handler ( ( GTestLogFatalFunc )handle_faults, NULL );
620 test_add_error (&check1);
621 test_add_error (&check2);
622 hdlr = g_log_set_handler (log_domain, loglevel,
623 (GLogFunc)test_list_handler, NULL);
625 g_test_message(
"Testing when book is null" );
628 col_struct.col1_called = FALSE;
629 col_struct.col2_called = FALSE;
630 col_struct.data = (gpointer) (¶m);
632 qof_book_foreach_collection( NULL, mock_foreach_collection, (gpointer)(¶m) );
633 g_assert( !col_struct.col1_called );
634 g_assert( !col_struct.col2_called );
635 g_assert_cmpstr( test_struct.msg, == , msg1);
636 g_free( test_struct.msg );
638 g_test_message(
"Testing when cb is null" );
640 qof_book_foreach_collection( fixture->book, NULL, (gpointer)(¶m) );
641 g_assert( !col_struct.col1_called );
642 g_assert( !col_struct.col2_called );
643 g_assert_cmpstr( test_struct.msg, == , msg2);
644 g_free( test_struct.msg );
645 g_log_remove_handler (log_domain, hdlr);
646 test_clear_error_list ();
648 g_test_message(
"Testing when book and cb not null, user_data provided" );
650 qof_book_foreach_collection( fixture->book, mock_foreach_collection, (gpointer)(¶m) );
651 g_assert( col_struct.col1_called );
652 g_assert( col_struct.col2_called );
656 test_book_set_data_fin(
void )
659 const char *key =
"key";
660 const char *data =
"data";
664 g_assert_cmpint( g_hash_table_size( book->data_tables ), == , 0 );
665 g_assert_cmpint( g_hash_table_size( book->data_table_finalizers ), == , 0 );
667 g_test_message(
"Testing when book is null" );
670 g_assert_cmpint( g_hash_table_size( book->data_tables ), == , 0 );
671 g_assert_cmpint( g_hash_table_size( book->data_table_finalizers ), == , 0 );
673 g_test_message(
"Testing when key is null" );
676 g_assert_cmpint( g_hash_table_size( book->data_tables ), == , 0 );
677 g_assert_cmpint( g_hash_table_size( book->data_table_finalizers ), == , 0 );
679 g_test_message(
"Testing with book key not null, cb null" );
682 g_assert_cmpint( g_hash_table_size( book->data_tables ), == , 1 );
683 g_assert_cmpint( g_hash_table_size( book->data_table_finalizers ), == , 0 );
686 g_test_message(
"Testing with all data set" );
689 g_assert_cmpint( g_hash_table_size( book->data_tables ), == , 1 );
690 g_assert_cmpint( g_hash_table_size( book->data_table_finalizers ), == , 1 );
692 g_assert( g_hash_table_lookup ( book->data_table_finalizers, (gpointer)key ) == mock_final_cb );
695 test_struct.called = FALSE;
697 g_assert( test_struct.called );
701 test_book_mark_closed(
Fixture *fixture, gconstpointer pData )
703 g_test_message(
"Testing when book is null" );
704 g_assert_cmpstr( &fixture->book->book_open, == ,
"y" );
706 g_assert_cmpstr( &fixture->book->book_open, == ,
"y" );
708 g_test_message(
"Testing when book is not null" );
710 g_assert_cmpstr( &fixture->book->book_open, == ,
"n" );
714 test_book_new_destroy(
void )
717 const char *key =
"key";
718 const char *data =
"data";
720 g_test_message(
"Testing book creation and initial setup" );
723 g_assert( QOF_IS_BOOK( book ) );
725 g_test_message(
"Testing book initial setup" );
726 g_assert( book->hash_of_collections );
727 g_assert( book->data_tables );
728 g_assert( book->data_table_finalizers );
729 g_assert_cmpint( g_hash_table_size( book->hash_of_collections ), == , 1 );
730 g_assert( g_hash_table_lookup ( book->hash_of_collections, QOF_ID_BOOK ) != NULL );
731 g_assert_cmpint( g_hash_table_size( book->data_tables ), == , 0 );
732 g_assert_cmpint( g_hash_table_size( book->data_table_finalizers ), == , 0 );
733 g_assert_cmpstr( &book->book_open, == ,
"y");
734 g_assert( !book->read_only );
735 g_assert_cmpint( book->version, == , 0 );
739 test_struct.called = FALSE;
741 g_test_message(
"Testing book destroy" );
744 g_assert( test_struct.called );
748 test_suite_qofbook (
void )
750 GNC_TEST_ADD( suitename,
"readonly",
Fixture, NULL, setup, test_book_readonly, teardown );
751 GNC_TEST_ADD_FUNC( suitename,
"validate counter", test_book_validate_counter );
752 GNC_TEST_ADD( suitename,
"get string option",
Fixture, NULL, setup, test_book_get_string_option, teardown );
753 GNC_TEST_ADD( suitename,
"set string option",
Fixture, NULL, setup, test_book_set_string_option, teardown );
754 GNC_TEST_ADD( suitename,
"session not saved",
Fixture, NULL, setup, test_book_session_not_saved, teardown );
755 GNC_TEST_ADD( suitename,
"session mark saved",
Fixture, NULL, setup, test_book_mark_session_saved, teardown );
756 GNC_TEST_ADD( suitename,
"get counter",
Fixture, NULL, setup, test_book_get_counter, teardown );
757 GNC_TEST_ADD( suitename,
"get counter format",
Fixture, NULL, setup, test_book_get_counter_format, teardown );
758 GNC_TEST_ADD( suitename,
"increment and format counter",
Fixture, NULL, setup, test_book_increment_and_format_counter, teardown );
759 GNC_TEST_ADD( suitename,
"use trading accounts",
Fixture, NULL, setup, test_book_use_trading_accounts, teardown );
760 GNC_TEST_ADD( suitename,
"get autofreeze days",
Fixture, NULL, setup, test_book_get_num_days_autofreeze, teardown );
761 GNC_TEST_ADD( suitename,
"use split action for num field",
Fixture, NULL, setup, test_book_use_split_action_for_num_field, teardown );
762 GNC_TEST_ADD( suitename,
"mark session dirty",
Fixture, NULL, setup, test_book_mark_session_dirty, teardown );
763 GNC_TEST_ADD( suitename,
"session dirty time",
Fixture, NULL, setup, test_book_get_session_dirty_time, teardown );
764 GNC_TEST_ADD( suitename,
"set dirty callback",
Fixture, NULL, setup, test_book_set_dirty_cb, teardown );
765 GNC_TEST_ADD( suitename,
"shutting down",
Fixture, NULL, setup, test_book_shutting_down, teardown );
766 GNC_TEST_ADD( suitename,
"set get data",
Fixture, NULL, setup, test_book_set_get_data, teardown );
767 GNC_TEST_ADD( suitename,
"get collection",
Fixture, NULL, setup, test_book_get_collection, teardown );
768 GNC_TEST_ADD( suitename,
"foreach collection",
Fixture, NULL, setup, test_book_foreach_collection, teardown );
769 GNC_TEST_ADD_FUNC( suitename,
"set data finalizers", test_book_set_data_fin );
770 GNC_TEST_ADD( suitename,
"mark closed",
Fixture, NULL, setup, test_book_mark_closed, teardown );
771 GNC_TEST_ADD_FUNC( suitename,
"book new and destroy", test_book_new_destroy );
void qof_book_set_dirty_cb(QofBook *book, QofBookDirtyCB cb, gpointer user_data)
#define qof_instance_is_dirty
time64 qof_book_get_session_dirty_time(const QofBook *book)
gchar * qof_book_increment_and_format_counter(QofBook *book, const char *counter_name)
gint qof_book_get_num_days_autoreadonly(const QofBook *book)
void qof_instance_set(QofInstance *inst, const gchar *first_param,...)
Wrapper for g_object_set Group setting multiple parameters in a single begin/commit/rollback.
gboolean qof_book_use_split_action_for_num_field(const QofBook *book)
QofBook * qof_book_new(void)
const char * qof_book_get_counter_format(const QofBook *book, const char *counter_name)
void qof_book_mark_closed(QofBook *book)
void qof_book_mark_readonly(QofBook *book)
gchar * qof_book_validate_counter_format_internal(const gchar *p, const gchar *gint64_format)
gint64 qof_book_get_counter(QofBook *book, const char *counter_name)
void qof_book_mark_session_saved(QofBook *book)
void qof_book_set_data_fin(QofBook *book, const gchar *key, gpointer data, QofBookFinalCB)
void qof_book_set_data(QofBook *book, const gchar *key, gpointer data)
gboolean qof_book_session_not_saved(const QofBook *book)
void qof_book_mark_session_dirty(QofBook *book)
QofIdType qof_collection_get_type(const QofCollection *)
gboolean qof_book_is_readonly(const QofBook *book)
time64 gnc_time(time64 *tbuf)
get the current local time
QofCollection * qof_book_get_collection(const QofBook *, QofIdType)
gboolean qof_book_uses_autoreadonly(const QofBook *book)
gboolean qof_book_shutting_down(const QofBook *book)
gpointer qof_book_get_data(const QofBook *book, const gchar *key)
gboolean qof_book_use_trading_accounts(const QofBook *book)
void qof_book_destroy(QofBook *book)
gchar * qof_book_validate_counter_format(const gchar *format)