/* Given a synthetic session, use the same logic as * QofSession::save_as to save it to a specified sql url, then load it * back and compare. */ void test_dbi_store_and_reload( const gchar* driver, QofSession* session_1, const gchar* url ) { QofSession* session_2; QofSession* session_3; gchar *msg = "[gnc_dbi_unlock()] There was no lock entry in the Lock table"; gchar *log_domain = "gnc.backend.dbi"; guint loglevel = G_LOG_LEVEL_WARNING, hdlr; TestErrorStruct check = { loglevel, log_domain, msg }; g_test_message ( "Testing %s\n", driver ); // Save the session data session_2 = qof_session_new(); hdlr = g_log_set_handler (log_domain, loglevel, (GLogFunc)test_checked_handler, &check); qof_session_begin( session_2, url, FALSE, TRUE, TRUE ); if (session_2 && qof_session_get_error(session_2) != ERR_BACKEND_NO_ERR) { g_warning("Session Error: %d, %s", qof_session_get_error(session_2), qof_session_get_error_message(session_2)); do_test( FALSE, "First DB Session Creation Failed"); return; } qof_session_swap_data( session_1, session_2 ); qof_session_save( session_2, NULL ); if (session_2 && qof_session_get_error(session_2) != ERR_BACKEND_NO_ERR) { g_warning("Session Error: %s", qof_session_get_error_message(session_2)); do_test( FALSE, "First DB Session Save Failed"); return; } // Reload the session data session_3 = qof_session_new(); qof_session_begin( session_3, url, TRUE, FALSE, FALSE ); if (session_3 && qof_session_get_error(session_3) != ERR_BACKEND_NO_ERR) { g_warning("Session Error: %s", qof_session_get_error_message(session_3)); do_test( FALSE, "Second DB Session Creation Failed"); return; } qof_session_load( session_3, NULL ); if (session_3 && qof_session_get_error(session_3) != ERR_BACKEND_NO_ERR) { g_warning("Session Error: %s", qof_session_get_error_message(session_3)); do_test( FALSE, "Second DBI Session Load Failed"); return; } // 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 ); g_log_remove_handler (log_domain, hdlr); }
/* Given an already-created url (yeah, bad testing practice: Should * start fresh from a synthetic session) load and safe-save it, then * load it again into a new session and compare the two. Since * safe-save is a more-or-less atomic function call, there's no way to * be sure that it's actually doing what it's supposed to without * running this test in a debugger and stopping in the middle of the * safe-save and inspecting the database. */ void test_dbi_safe_save( const gchar* driver, const gchar* url ) { QofSession *session_1 = NULL, *session_2 = NULL; printf( "Testing safe save %s\n", driver ); // Load the session data session_1 = qof_session_new(); qof_session_begin( session_1, url, TRUE, FALSE, FALSE ); if (session_1 && qof_session_get_error(session_1) != ERR_BACKEND_NO_ERR) { g_warning("Session Error: %d, %s", qof_session_get_error(session_1), qof_session_get_error_message(session_1)); do_test( FALSE, "DB Session Creation Failed"); goto cleanup; } qof_session_load( session_1, NULL ); /* Do a safe save */ qof_session_safe_save( session_1, NULL ); if (session_1 && qof_session_get_error(session_1) != ERR_BACKEND_NO_ERR) { g_warning("Session Error: %s", qof_session_get_error_message(session_1)); do_test( FALSE, "DB Session Safe Save Failed"); goto cleanup; } /* Destroy the session and reload it */ session_2 = qof_session_new(); qof_session_begin( session_2, url, TRUE, FALSE, FALSE ); if (session_2 && qof_session_get_error(session_2) != ERR_BACKEND_NO_ERR) { g_warning("Session Error: %d, %s", qof_session_get_error(session_2), qof_session_get_error_message(session_2)); do_test( FALSE, "DB Session re-creation Failed"); goto cleanup; } qof_session_load( session_2, NULL ); compare_books( qof_session_get_book( session_1 ), qof_session_get_book( session_2 ) ); cleanup: if (session_2 != NULL) { qof_session_end( session_2 ); qof_session_destroy( session_2 ); } if (session_1 != NULL) { qof_session_end( session_1 ); qof_session_destroy( session_1 ); } return; }
/* Given a synthetic session, use the same logic as * QofSession::save_as to save it to a specified sql url, then load it * back and compare. */ void test_dbi_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, FALSE, TRUE, TRUE ); if (session_2 && qof_session_get_error(session_2) != ERR_BACKEND_NO_ERR) { g_warning("Session Error: %d, %s", qof_session_get_error(session_2), qof_session_get_error_message(session_2)); do_test( FALSE, "First DB Session Creation Failed"); return; } qof_session_swap_data( session_1, session_2 ); qof_session_save( session_2, NULL ); if (session_2 && qof_session_get_error(session_2) != ERR_BACKEND_NO_ERR) { g_warning("Session Error: %s", qof_session_get_error_message(session_2)); do_test( FALSE, "First DB Session Save Failed"); return; } // Reload the session data session_3 = qof_session_new(); qof_session_begin( session_3, url, TRUE, FALSE, FALSE ); if (session_3 && qof_session_get_error(session_3) != ERR_BACKEND_NO_ERR) { g_warning("Session Error: %s", qof_session_get_error_message(session_3)); do_test( FALSE, "Second DB Session Creation Failed"); return; } qof_session_load( session_3, NULL ); if (session_3 && qof_session_get_error(session_3) != ERR_BACKEND_NO_ERR) { g_warning("Session Error: %s", qof_session_get_error_message(session_3)); do_test( FALSE, "Second DBI Session Load Failed"); return; } // 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 ); g_print(" You may ignore the warning about the lock file having no entries: We had to ignore locking to run two sessions on the same database\n"); qof_session_end( session_3 ); qof_session_destroy( session_3 ); }
static void test_load_file(const char *filename) { QofSession *session; QofBook *book; Account *root; gboolean ignore_lock; gchar *logdomain = "GConf"; guint loglevel = G_LOG_LEVEL_WARNING; TestErrorStruct check = { loglevel, logdomain, NULL }; g_log_set_handler (logdomain, loglevel, (GLogFunc)test_checked_handler, &check); session = qof_session_new(); remove_locks(filename); ignore_lock = (safe_strcmp(g_getenv("SRCDIR"), ".") != 0); qof_session_begin(session, filename, ignore_lock, FALSE, TRUE); qof_session_load(session, NULL); book = qof_session_get_book (session); root = gnc_book_get_root_account(book); do_test (gnc_account_get_book (root) == book, "book and root account don't match"); do_test_args(qof_session_get_error(session) == ERR_BACKEND_NO_ERR, "session load xml2", __FILE__, __LINE__, "qof error=%d for file [%s]", qof_session_get_error(session), filename); /* Uncomment the line below to generate corrected files */ qof_session_save( session, NULL ); qof_session_end(session); }
void qof_session_destroy (QofSession *session) { GList *node; if (!session) return; ENTER ("sess=%p book_id=%s", session, session->book_id ? session->book_id : "(null)"); qof_session_end (session); /* destroy the backend */ qof_session_destroy_backend(session); for (node = session->books; node; node = node->next) { QofBook *book = node->data; qof_book_set_backend (book, NULL); qof_book_destroy (book); } session->books = NULL; g_free (session); LEAVE ("sess=%p", session); }
int main (int argc, const char *argv[]) { QofSession *original; guint counter; qof_init (); mygrandRegister(); myparentRegister(); mychildRegister(); for (counter = 0; counter < 35; counter++) { original = qof_session_new(); if (debug) { /* FIXME XML backend can't handle STDOUT * qof_session_begin(original, QOF_STDOUT, TRUE, FALSE); */ } create_data(original, (counter % 5)); test_recursion(original, (counter % 5)); qof_session_end(original); } print_test_results(); qof_close(); return EXIT_SUCCESS; }
static void run_test (void) { QofSession *sess; QofBook *book; Account *root; /* --------------------------------------------------------- */ /* In the first test, we will merely try to see if we can run * without crashing. We don't check to see if data is good. */ sess = get_random_session (); book = qof_session_get_book (sess); do_test ((NULL != book), "create random data"); add_random_transactions_to_book (book, transaction_num); root = gnc_book_get_root_account (book); xaccAccountTreeScrubLots (root); /* --------------------------------------------------------- */ /* In the second test, we create an account with unrealized gains, * and see if that gets fixed correctly, with the correct balances, * and etc. * XXX not implemented */ success ("automatic lot scrubbing lightly tested and seem to work"); qof_session_end (sess); }
static void gnc_plugin_business_cmd_export_employee (GtkAction *action, GncMainWindowActionData *mw) { QofSession *current_session, *chart_session; QofBook *book; QofCollection *coll; gchar *filename; gboolean success; current_session = gnc_get_current_session(); book = qof_session_get_book(current_session); chart_session = qof_session_new(); success = FALSE; filename = gnc_file_dialog(_("Export Employees to XML"), NULL, NULL, GNC_FILE_DIALOG_EXPORT); if (filename) { gchar* url = g_strdup_printf( "qsf:%s", filename ); qof_session_begin(chart_session, url, TRUE, TRUE); coll = qof_book_get_collection(book, GNC_ID_EMPLOYEE); success = qof_instance_copy_coll_r(chart_session, coll); if (success) { qof_session_save(chart_session, NULL); } g_free(url); } show_session_error(qof_session_get_error(chart_session), filename, GNC_FILE_DIALOG_EXPORT); qof_session_end(chart_session); g_free(filename); gnc_set_current_session(current_session); }
void test_dbi_business_store_and_reload( const gchar* driver, QofSession* session_1, const gchar* url ) { QofSession* session_2; QofSession* session_3; gchar *msg = "[gnc_dbi_unlock()] There was no lock entry in the Lock table"; gchar *log_domain = "gnc.backend.dbi"; guint loglevel = G_LOG_LEVEL_WARNING, hdlr; TestErrorStruct check = { loglevel, log_domain, msg }; g_test_message ( "Testing %s\n", driver ); // Save the session data session_2 = qof_session_new(); qof_session_begin( session_2, url, FALSE, 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, TRUE, 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 ); hdlr = g_log_set_handler (log_domain, loglevel, (GLogFunc)test_checked_handler, &check); qof_session_end( session_3 ); g_log_remove_handler (log_domain, hdlr); qof_session_destroy( session_3 ); }
int main() { qof_log_init(); qof_init(); gnc_module_system_init(); char * no_args[1] = { NULL }; gnc_engine_init(0, no_args); QofSession * s = qof_session_new(); qof_session_begin(s, "sqlite3:///tmp/blah.gnucash", 0, 1, 0); qof_session_load(s, NULL); qof_session_save(s, NULL); qof_session_end(s); return 0; }
static void test_recursion (QofSession *original, guint counter) { QofSession *copy; QofCollection *grand_coll; struct tally c; QofBook *book; guint d, e, f; c.nulls = 0; c.total = 0; c.collect = 0; c.book = NULL; book = qof_session_get_book(original); grand_coll = qof_book_get_collection(book, GRAND_MODULE_NAME); copy = qof_session_new(); if (debug) { /* FIXME XML backend can't handle STDOUT * qof_session_begin(copy, QOF_STDOUT, TRUE, FALSE); */ } /* TODO: implement QOF_TYPE_CHOICE testing. */ qof_instance_copy_coll_r(copy, grand_coll); /* test the original */ qof_object_foreach(GRAND_MODULE_NAME, book, check_cb, &c); book = qof_session_get_book(copy); /* test the copy */ d = c.nulls; e = c.total; f = c.collect; c.nulls = 0; c.total = 0; c.collect = 0; c.book = book; qof_object_foreach(GRAND_MODULE_NAME, book, check_cb, &c); do_test((d == c.nulls), "Null parents do not match"); do_test((e == c.total), "Total parents do not match"); do_test((f == c.collect), "Number of children in descendents does not match"); if (counter == 4 && debug == TRUE) { /* FIXME XML backend can't handle STDOUT * qof_session_save(copy, NULL); qof_session_save(original, NULL); */ } qof_session_end(copy); copy = NULL; }
int main() { const char* testurl = "sqlite3://" TESTFILE; qof_log_init(); qof_init(); gnc_module_system_init(); char * no_args[1] = { NULL }; gnc_engine_init(0, no_args); QofSession * s = qof_session_new(); qof_session_begin(s, testurl, 0, 1, 0); qof_session_load(s, NULL); qof_session_save(s, NULL); qof_session_end(s); unlink(TESTFILE); return 0; }
static void run_test (void) { QofSession *session; Account *root; QofBook *book; session = get_random_session (); book = qof_session_get_book (session); root = gnc_book_get_root_account (book); add_random_transactions_to_book (book, 20); xaccAccountTreeForEachTransaction (root, test_trans_query, book); qof_session_end (session); }
void qof_session_destroy (QofSession *session) { if (!session) return; ENTER ("sess=%p book_id=%s", session, session->book_id ? session->book_id : "(null)"); qof_session_end (session); /* destroy the backend */ qof_session_destroy_backend(session); qof_book_set_backend (session->book, NULL); qof_book_destroy (session->book); session->book = NULL; g_free (session); LEAVE ("sess=%p", session); }
static void test_generation (void) { for (iter = 0; iter < 20; iter++) { GNCPriceDB* db; g_message ("iter=%d", iter); session = qof_session_new (); db = get_random_pricedb (qof_session_get_book (session)); if (!db) { failure_args ("gnc_random_price_db returned NULL", __FILE__, __LINE__, "%d", iter); return; } if (gnc_pricedb_get_num_prices (db)) test_db (db); gnc_pricedb_destroy (db); qof_session_end (session); } }
/* Test the gnc_dbi_load logic that forces a newer database to be * opened read-only and an older one to be safe-saved. Again, it would * be better to do this starting from a fresh file, but instead we're * being lazy and using an existing one. */ void test_dbi_version_control( const gchar* driver, const gchar* url ) { QofSession *sess; QofBook *book; QofBackend *qbe; QofBackendError err; gint ourversion = gnc_get_long_version(); g_test_message ( "Testing safe save %s\n", driver ); // Load the session data sess = qof_session_new(); qof_session_begin( sess, url, TRUE, FALSE, FALSE ); if (sess && qof_session_get_error(sess) != ERR_BACKEND_NO_ERR) { g_warning("Session Error: %d, %s", qof_session_get_error(sess), qof_session_get_error_message(sess)); do_test( FALSE, "DB Session Creation Failed"); goto cleanup; } qof_session_load( sess, NULL ); qbe = qof_session_get_backend( sess ); book = qof_session_get_book( sess ); qof_book_begin_edit( book ); gnc_sql_set_table_version( (GncSqlBackend*)qbe, "Gnucash", GNUCASH_RESAVE_VERSION - 1 ); qof_book_commit_edit( book ); qof_session_end( sess ); qof_session_destroy( sess ); sess = qof_session_new(); qof_session_begin( sess, url, TRUE, FALSE, FALSE ); qof_session_load( sess, NULL ); err = qof_session_pop_error( sess ); do_test( err == ERR_SQL_DB_TOO_OLD, "DB Failed to flag too old" ); qbe = qof_session_get_backend( sess ); book = qof_session_get_book( sess ); qof_book_begin_edit( book ); gnc_sql_set_table_version( (GncSqlBackend*)qbe, "Gnucash", ourversion ); gnc_sql_set_table_version( (GncSqlBackend*)qbe, "Gnucash-Resave", ourversion + 1 ); qof_book_commit_edit( book ); qof_session_end( sess ); qof_session_destroy( sess ); sess = qof_session_new(); qof_session_begin( sess, url, TRUE, FALSE, FALSE ); qof_session_load( sess, NULL ); qof_session_ensure_all_data_loaded( sess ); err = qof_session_pop_error( sess ); do_test( err == ERR_SQL_DB_TOO_NEW, "DB Failed to flag too new" ); cleanup: qbe = qof_session_get_backend( sess ); book = qof_session_get_book( sess ); qof_book_begin_edit( book ); gnc_sql_set_table_version( (GncSqlBackend*)qbe, "Gnucash-Resave", GNUCASH_RESAVE_VERSION ); qof_book_commit_edit( book ); qof_session_end( sess ); qof_session_destroy( sess ); }
/* Given an already-created url (yeah, bad testing practice: Should * start fresh from a synthetic session) load and safe-save it, then * load it again into a new session and compare the two. Since * safe-save is a more-or-less atomic function call, there's no way to * be sure that it's actually doing what it's supposed to without * running this test in a debugger and stopping in the middle of the * safe-save and inspecting the database. */ void test_dbi_safe_save( const gchar* driver, const gchar* url ) { QofSession *session_1 = NULL, *session_2 = NULL; gchar *msg = "[gnc_dbi_unlock()] There was no lock entry in the Lock table"; gchar *log_domain = "gnc.backend.dbi"; guint loglevel = G_LOG_LEVEL_WARNING, hdlr; TestErrorStruct check = { loglevel, log_domain, msg }; g_test_message ( "Testing safe save %s\n", driver ); // Load the session data session_1 = qof_session_new(); qof_session_begin( session_1, url, TRUE, FALSE, FALSE ); if (session_1 && qof_session_get_error(session_1) != ERR_BACKEND_NO_ERR) { g_warning("Session Error: %d, %s", qof_session_get_error(session_1), qof_session_get_error_message(session_1)); do_test( FALSE, "DB Session Creation Failed"); goto cleanup; } qof_session_load( session_1, NULL ); /* Do a safe save */ qof_session_safe_save( session_1, NULL ); if (session_1 && qof_session_get_error(session_1) != ERR_BACKEND_NO_ERR) { g_warning("Session Error: %s", qof_session_get_error_message(session_1)); do_test( FALSE, "DB Session Safe Save Failed"); goto cleanup; } /* Destroy the session and reload it */ session_2 = qof_session_new(); qof_session_begin( session_2, url, TRUE, FALSE, FALSE ); if (session_2 && qof_session_get_error(session_2) != ERR_BACKEND_NO_ERR) { g_warning("Session Error: %d, %s", qof_session_get_error(session_2), qof_session_get_error_message(session_2)); do_test( FALSE, "DB Session re-creation Failed"); goto cleanup; } qof_session_load( session_2, NULL ); compare_books( qof_session_get_book( session_1 ), qof_session_get_book( session_2 ) ); cleanup: hdlr = g_log_set_handler (log_domain, loglevel, (GLogFunc)test_checked_handler, &check); if (session_2 != NULL) { qof_session_end( session_2 ); qof_session_destroy( session_2 ); } if (session_1 != NULL) { qof_session_end( session_1 ); qof_session_destroy( session_1 ); } g_log_remove_handler (log_domain, hdlr); return; }