diff --git a/src/backend/dbi/test/Makefile.am b/src/backend/dbi/test/Makefile.am index 6863df86c1..33122f3d65 100644 --- a/src/backend/dbi/test/Makefile.am +++ b/src/backend/dbi/test/Makefile.am @@ -61,8 +61,7 @@ endif test_backend_dbi_SOURCES = \ test-backend-dbi.c \ - utest-backend-dbi-basic.c \ - utest-backend-dbi-business.c \ + test-backend-dbi-basic.c \ test-dbi-stuff.c \ test-dbi-business-stuff.c diff --git a/src/backend/dbi/test/test-backend-dbi-basic.c b/src/backend/dbi/test/test-backend-dbi-basic.c new file mode 100644 index 0000000000..eb989d7d0d --- /dev/null +++ b/src/backend/dbi/test/test-backend-dbi-basic.c @@ -0,0 +1,585 @@ +/* + * utest-backend-dbi-basic.c + * + * Created on: 2011-04-23 + * Author: phil + */ + +#include "config.h" + +#include + +#include +#include +#include +#include +#include +/* For cleaning up the database */ +#include +#include +/* For setup_business */ +#include "Account.h" +#include +#include "Transaction.h" +#include "Split.h" +#include "gnc-commodity.h" +#include "gncAddress.h" +#include "gncCustomer.h" +#include "gncInvoice.h" +/* For test_conn_index_functions */ +#include "../gnc-backend-dbi-priv.h" +/* For version_control */ +#include +#include + +static const gchar* suitename = "/backend/dbi"; +void test_suite_gnc_backend_dbi (void); + +typedef struct +{ + QofSession *session; + gchar *filename; + GSList *hdlrs; +} Fixture; + +static void +setup (Fixture *fixture, gconstpointer pData) +{ + gchar *url = (gchar *)pData; + fixture->session = qof_session_new(); + /* When running distcheck the source directory is read-only, which + * prevents creating the lock file. Force the session to get + * around that. + */ + qof_session_begin (fixture->session, DBI_TEST_XML_FILENAME, TRUE, + FALSE, TRUE); + g_assert_cmpint (qof_session_get_error (fixture->session), ==, + ERR_BACKEND_NO_ERR); + qof_session_load (fixture->session, NULL); + + if (g_strcmp0 (url, "sqlite3") == 0) + fixture->filename = tempnam ("/tmp", "test-sqlite3-"); + else + fixture->filename = NULL; +} + +static void +setup_memory (Fixture *fixture, gconstpointer pData) +{ + QofSession* session = qof_session_new(); + gchar *url = (gchar*)pData; + QofBook* book; + Account *root, *acct1, *acct2; + KvpFrame* frame; + Transaction* tx; + Split *spl1, *spl2; + Timespec ts; + struct timeval tv; + gnc_commodity_table* table; + gnc_commodity* currency; + + session = qof_session_new(); + book = qof_session_get_book (session); + root = gnc_book_get_root_account (book); + + table = gnc_commodity_table_get_table (book); + currency = gnc_commodity_table_lookup (table, GNC_COMMODITY_NS_CURRENCY, "CAD"); + + acct1 = xaccMallocAccount (book); + xaccAccountSetType (acct1, ACCT_TYPE_BANK); + xaccAccountSetName (acct1, "Bank 1"); + xaccAccountSetCommodity (acct1, currency); + + frame = qof_instance_get_slots (QOF_INSTANCE(acct1)); + kvp_frame_set_gint64 (frame, "int64-val", 100); + kvp_frame_set_double (frame, "double-val", 3.14159); + kvp_frame_set_numeric (frame, "numeric-val", gnc_numeric_zero()); + + time (&(tv.tv_sec)); + tv.tv_usec = 0; + ts.tv_sec = tv.tv_sec; + ts.tv_nsec = 1000 * tv.tv_usec; + kvp_frame_set_timespec (frame, "timespec-val", ts); + + kvp_frame_set_string (frame, "string-val", "abcdefghijklmnop"); + kvp_frame_set_guid (frame, "guid-val", qof_instance_get_guid (QOF_INSTANCE(acct1))); + + gnc_account_append_child (root, acct1); + + acct2 = xaccMallocAccount (book); + xaccAccountSetType (acct2, ACCT_TYPE_BANK); + xaccAccountSetName (acct2, "Bank 1"); + + tx = xaccMallocTransaction (book); + xaccTransBeginEdit (tx); + xaccTransSetCurrency (tx, currency); + spl1 = xaccMallocSplit (book); + xaccTransAppendSplit (tx, spl1); + spl2 = xaccMallocSplit (book); + xaccTransAppendSplit (tx, spl2); + xaccTransCommitEdit (tx); + + fixture->session = session; + if (g_strcmp0 (url, "sqlite3") == 0) + fixture->filename = tempnam ("/tmp", "test-sqlite3-"); + else + fixture->filename = NULL; +} + +static void +setup_business (Fixture *fixture, gconstpointer pData) +{ + QofSession* session = qof_session_new(); + gchar *url = (gchar*)pData; + QofBook* book = qof_session_get_book (session); + Account* root = gnc_book_get_root_account (book); + Account* acct1; + Account* acct2; + gnc_commodity_table* table; + gnc_commodity* currency; + GncAddress* addr; + GncCustomer* cust; + GncEmployee* emp; + GncTaxTable* tt; + GncTaxTableEntry* tte; + + table = gnc_commodity_table_get_table (book); + currency = gnc_commodity_table_lookup (table, GNC_COMMODITY_NS_CURRENCY, "CAD"); + + acct1 = xaccMallocAccount (book); + xaccAccountSetType (acct1, ACCT_TYPE_BANK); + xaccAccountSetName (acct1, "Bank 1"); + xaccAccountSetCommodity (acct1, currency); + xaccAccountSetHidden (acct1, FALSE); + xaccAccountSetPlaceholder (acct1, FALSE); + gnc_account_append_child (root, acct1); + + acct2 = xaccMallocAccount (book); + xaccAccountSetType (acct2, ACCT_TYPE_BANK); + xaccAccountSetName (acct2, "Bank 2"); + xaccAccountSetCommodity (acct2, currency); + xaccAccountSetHidden (acct2, FALSE); + xaccAccountSetPlaceholder (acct2, FALSE); + gnc_account_append_child (root, acct2); + + tt = gncTaxTableCreate (book); + gncTaxTableSetName (tt, "tt"); + tte = gncTaxTableEntryCreate(); + gncTaxTableEntrySetAccount (tte, acct1); + gncTaxTableEntrySetType (tte, GNC_AMT_TYPE_VALUE); + gncTaxTableEntrySetAmount (tte, gnc_numeric_zero()); + gncTaxTableAddEntry (tt, tte); + tte = gncTaxTableEntryCreate(); + gncTaxTableEntrySetAccount (tte, acct2); + gncTaxTableEntrySetType (tte, GNC_AMT_TYPE_PERCENT); + gncTaxTableEntrySetAmount (tte, gnc_numeric_zero()); + gncTaxTableAddEntry (tt, tte); + + cust = gncCustomerCreate (book); + gncCustomerSetID (cust, "0001"); + gncCustomerSetName (cust, "MyCustomer"); + gncCustomerSetNotes (cust, "Here are some notes"); + gncCustomerSetCurrency (cust, currency); + addr = gncAddressCreate (book, QOF_INSTANCE(cust)); + gncAddressSetName (addr, "theAddress"); + gncAddressSetAddr1 (addr, "Address line #1"); + gncAddressSetAddr2 (addr, "Address line #2"); + gncAddressSetAddr3 (addr, "Address line #3"); + gncAddressSetAddr4 (addr, "Address line #4"); + gncAddressSetPhone (addr, "(123) 555-1212"); + gncAddressSetPhone (addr, "(123) 555-2121"); + gncAddressSetEmail (addr, "cust@mycustomer.com"); + + emp = gncEmployeeCreate (book); + gncEmployeeSetID (emp, "0001"); + gncEmployeeSetUsername (emp, "gnucash"); + gncEmployeeSetLanguage (emp, "english"); + gncEmployeeSetCurrency (emp, currency); + + fixture->session = session; + if (g_strcmp0 (url, "sqlite3") == 0) + fixture->filename = tempnam ("/tmp", "test-sqlite3-"); + else + fixture->filename = NULL; +} + +static void +drop_table (gconstpointer tdata, gconstpointer cdata) +{ + gchar *table = (gchar*)tdata; + dbi_conn conn = (dbi_conn)cdata; + gchar *query = g_strdup_printf ("DROP TABLE %s", table); + dbi_result rslt = dbi_conn_query (conn, query); + g_free (query); +} + +static void +destroy_database (gchar* url) +{ + gchar *protocol = NULL; + gchar *host = NULL; + gchar *dbname = NULL; + gchar *username = NULL; + gchar *password = NULL; + gchar *basename = NULL; + gint portnum = 0; + gchar *port = NULL; + dbi_conn conn = NULL; + gchar *errfmt = "Unable to delete tables in %s: %s"; + gint fail = 0; + dbi_result tables; + GSList *list = NULL; + + gnc_uri_get_components (url, &protocol, &host, &portnum, + &username, &password, &dbname); + conn = dbi_conn_new (protocol); + port = g_strdup_printf ("%d", portnum); + if (conn == NULL) + { + g_printf (errfmt, url, "failed to create connection"); + return; + } + fail = dbi_conn_set_option (conn, "host", host); + if (!fail) + fail = dbi_conn_set_option (conn, "port", port); + if (!fail) + fail = dbi_conn_set_option (conn, "dbname", dbname); + if (!fail) + fail = dbi_conn_set_option (conn, "username", username); + if (!fail) + fail = dbi_conn_set_option (conn, "password", password); + if (!fail) + fail = dbi_conn_set_option (conn, "encoding", "UTF-8"); + g_free (port); + if (fail != 0) + { + g_printf (errfmt, url, "failed to set an option"); + dbi_conn_close (conn); + return; + } + fail = dbi_conn_connect (conn); + if (fail != 0) + { + const gchar *error; + gint errnum = dbi_conn_error (conn, &error); + g_printf (errfmt, url, error); + dbi_conn_close (conn); + return; + } + tables = dbi_conn_get_table_list (conn, dbname, NULL); + while (dbi_result_next_row (tables) != 0) + { + const gchar *table = dbi_result_get_string_idx (tables, 1); + list = g_slist_prepend (list, g_strdup (table)); + } + dbi_result_free (tables); + g_slist_foreach (list, (GFunc)drop_table, (gpointer)conn); + g_slist_free_full (list, (GDestroyNotify)g_free); +} + +static void +teardown (Fixture *fixture, gconstpointer pData) +{ + gchar *lockfile = g_strdup_printf ("%s/test-dbi.xml.LCK", + g_dirname (DBI_TEST_XML_FILENAME)); + gchar *msg = g_strdup_printf ("[xml_session_end()] Error on g_unlink(%s): 2: No such file or directory", lockfile); + gchar *logdomain = "gnc.backend"; + guint loglevel = G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL; + TestErrorStruct *check = test_error_struct_new (logdomain, loglevel, msg); + fixture->hdlrs = test_log_set_fatal_handler (fixture->hdlrs, check, + (GLogFunc)test_checked_handler); + qof_session_end (fixture->session); + qof_session_destroy (fixture->session); + if (fixture->filename) + g_unlink (fixture->filename); + else + destroy_database ((gchar*)pData); + + g_free (msg); + g_free (lockfile); + g_slist_free_full (fixture->hdlrs, test_free_log_handler); + test_clear_error_list(); +} + + +static void +test_conn_index_functions (QofBackend *qbe) +{ + GncDbiBackend *be = (GncDbiBackend*)qbe; + GncDbiSqlConnection *conn = (GncDbiSqlConnection*)(be->sql_be.conn); + GSList *index_list, *iter; + + index_list = conn->provider->get_index_list (be->conn); + g_test_message ("Returned from index list\n"); + g_assert (index_list != NULL); + g_assert_cmpint (g_slist_length (index_list), ==, 4); + for (iter = index_list; iter != NULL; iter = g_slist_next (iter)) + { + const char *errmsg; + conn->provider->drop_index (be->conn, iter->data); + g_assert (DBI_ERROR_NONE == dbi_conn_error (conn->conn, &errmsg)); + } + + g_slist_free (index_list); +} + +/* 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. */ +static void +test_dbi_store_and_reload (Fixture *fixture, gconstpointer pData) +{ + + const gchar* url = (const gchar*)pData; + QofSession* session_2; + QofSession* session_3; + QofBackend *be; + + 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 | G_LOG_FLAG_FATAL; + TestErrorStruct *check = test_error_struct_new (log_domain, loglevel, msg); + fixture->hdlrs = test_log_set_fatal_handler (fixture->hdlrs, check, + (GLogFunc)test_checked_handler); + if (fixture->filename) + url = fixture->filename; + + // Save the session data + session_2 = qof_session_new(); + qof_session_begin (session_2, url, FALSE, TRUE, TRUE); + g_assert (session_2 != NULL); + g_assert_cmpint (qof_session_get_error (session_2), ==, ERR_BACKEND_NO_ERR); + qof_session_swap_data (fixture->session, session_2); + qof_session_save (session_2, NULL); + g_assert (session_2 != NULL); + g_assert_cmpint (qof_session_get_error (session_2), ==, ERR_BACKEND_NO_ERR); + + // Reload the session data + session_3 = qof_session_new(); + g_assert (session_3 != NULL); + qof_session_begin (session_3, url, TRUE, FALSE, FALSE); + g_assert (session_3 != NULL); + g_assert_cmpint (qof_session_get_error (session_3), ==, ERR_BACKEND_NO_ERR); + qof_session_load (session_3, NULL); + g_assert (session_3 != NULL); + g_assert_cmpint (qof_session_get_error (session_3), ==, ERR_BACKEND_NO_ERR); + // Compare with the original data + compare_books (qof_session_get_book (session_2), + qof_session_get_book (session_3)); +/* fixture->session belongs to the fixture and teardown() will clean it up */ + qof_session_end (session_2); + qof_session_destroy (session_2); + qof_session_end (session_3); + qof_session_destroy (session_3); +} + +/** Test the safe_save mechanism. Beware that this test used on its + * own doesn't ensure that the resave is done safely, only that the + * database is intact and unchanged after the save. To observe the + * safety one must run the test in a debugger and break after the + * rename step of gnc_dbi_safe_sync, then examine the database in the + * appropriate shell. + */ +static void +test_dbi_safe_save (Fixture *fixture, gconstpointer pData) +{ + gchar *url = (gchar*)pData; + QofSession *session_1 = NULL, *session_2 = NULL; + QofBackend *be; + + 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 | G_LOG_FLAG_FATAL; + TestErrorStruct *check = test_error_struct_new (log_domain, loglevel, msg); + + if (fixture->filename) + url = fixture->filename; + + // Load the session data + session_1 = qof_session_new (); + qof_session_begin (session_1, url, FALSE, TRUE, TRUE); + 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)); + g_test_message ("DB Session Creation Failed"); + g_test_fail (); + goto cleanup; + } + qof_session_swap_data (fixture->session, session_1); + qof_session_save (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)); + g_test_message ("DB Session Safe Save Failed"); + g_test_fail (); + 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)); + g_test_message ("DB Session re-creation Failed"); + g_test_fail (); + goto cleanup; + } + qof_session_load (session_2, NULL); + compare_books (qof_session_get_book (session_1), + qof_session_get_book (session_2)); + be = qof_book_get_backend (qof_session_get_book (session_2)); + test_conn_index_functions (be); + +cleanup: + fixture->hdlrs = test_log_set_fatal_handler (fixture->hdlrs, check, + (GLogFunc)test_checked_handler); + 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; +} +/* 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. */ +static void +test_dbi_version_control (Fixture *fixture, gconstpointer pData) +{ + gchar *url = (gchar*)pData; + QofSession *sess; + QofBook *book; + QofBackend *qbe; + QofBackendError err; + gint ourversion = gnc_core_prefs_get_long_version(); + + // Load the session data + if (fixture->filename) + url = fixture->filename; + sess = qof_session_new(); + qof_session_begin (sess, url, FALSE, TRUE, TRUE); + 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)); + g_test_message ("DB Session Creation Failed"); + g_test_fail (); + goto cleanup; + } + qof_session_swap_data (fixture->session, sess); + qof_session_save (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); + g_assert_cmpint (err, ==, ERR_SQL_DB_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); + g_assert_cmpint (err, ==, ERR_SQL_DB_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); +} + +static void +test_dbi_business_store_and_reload (Fixture *fixture, gconstpointer pData) +{ + QofSession* session_2; + QofSession* session_3; + const gchar* url = (gchar*)pData; + + 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 | G_LOG_FLAG_FATAL; + TestErrorStruct *check = test_error_struct_new (log_domain, loglevel, msg); + if (fixture->filename) + url = fixture->filename; + // Save the session data + session_2 = qof_session_new(); + qof_session_begin (session_2, url, FALSE, TRUE, TRUE); + qof_session_swap_data (fixture->session, 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_business_books (qof_session_get_book (session_2), qof_session_get_book (session_3)); + qof_session_end (session_2); + qof_session_destroy (session_2); + + fixture->hdlrs = test_log_set_fatal_handler (fixture->hdlrs, check, + (GLogFunc)test_checked_handler); + qof_session_end (session_3); + qof_session_destroy (session_3); +} + +static void +create_dbi_test_suite (gchar *dbm_name, gchar *url) +{ + gchar *subsuite = g_strdup_printf ("%s/%s", suitename, dbm_name); + GNC_TEST_ADD (subsuite, "store_and_reload", Fixture, url, setup, + test_dbi_store_and_reload, teardown); + GNC_TEST_ADD (subsuite, "safe_save", Fixture, url, setup_memory, + test_dbi_safe_save, teardown); + GNC_TEST_ADD (subsuite, "version_control", Fixture, url, setup_memory, + test_dbi_version_control, teardown); + GNC_TEST_ADD (subsuite, "business_store_and_reload", Fixture, url, + setup_business, test_dbi_version_control, teardown); +g_free (subsuite); + +} + +void +test_suite_gnc_backend_dbi (void) +{ + create_dbi_test_suite ("sqlite3", "sqlite3"); + if (strlen (TEST_MYSQL_URL) > 0) + create_dbi_test_suite ("mysql", TEST_MYSQL_URL); + if (strlen (TEST_PGSQL_URL) > 0) + create_dbi_test_suite ("postgres", TEST_PGSQL_URL); + +} diff --git a/src/backend/dbi/test/test-backend-dbi.c b/src/backend/dbi/test/test-backend-dbi.c index 1439b0e9d1..d5cd604df2 100644 --- a/src/backend/dbi/test/test-backend-dbi.c +++ b/src/backend/dbi/test/test-backend-dbi.c @@ -26,8 +26,7 @@ #include "qof.h" #include "cashobjects.h" -extern void test_suite_gnc_backend_dbi_basic(); -extern void test_suite_gnc_backend_dbi_business(); +extern void test_suite_gnc_backend_dbi (); #define GNC_LIB_NAME "gncmod-backend-dbi" @@ -44,13 +43,7 @@ main (int argc, g_assert (qof_load_backend_library ("../../xml/.libs", "gncmod-backend-xml")); - /* Make the missing lock file warning not fatal so that it won't - * crash during teardown. - */ - g_log_set_fatal_mask ("gnc.backend.dbi", G_LOG_FATAL_MASK | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL); - g_log_set_always_fatal (G_LOG_FATAL_MASK); - test_suite_gnc_backend_dbi_basic(); - test_suite_gnc_backend_dbi_business(); + test_suite_gnc_backend_dbi (); return g_test_run( ); } diff --git a/src/backend/dbi/test/test-dbi-basic.c b/src/backend/dbi/test/test-dbi-basic.c deleted file mode 100644 index 4154c5c24d..0000000000 --- a/src/backend/dbi/test/test-dbi-basic.c +++ /dev/null @@ -1,153 +0,0 @@ -/*************************************************************************** - * test-dbi.c - * - * Tests saving and loading to a dbi/sqlite3 db - * - * Copyright (C) 2009 Phil Longstaff - ****************************************************************************/ - -/* - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - * 02110-1301, USA. - */ - -#include "config.h" -#include "qof.h" -#include "cashobjects.h" -#include "test-engine-stuff.h" -#include "test-stuff.h" -#include "test-dbi-stuff.h" -#include - -#include "TransLog.h" -#include "Account.h" -#include "Transaction.h" -#include "Split.h" -#include "gnc-commodity.h" - -#define FILE_NAME "sqlite3:///tmp/test-sqlite3-file" -#define GNC_LIB_NAME "gncmod-backend-dbi" - -static QofSession* -create_session(void) -{ - QofSession* session; - QofBook* book; - Account *root, *acct1, *acct2; - KvpFrame* frame; - Transaction* tx; - Split *spl1, *spl2; - Timespec ts; - struct timeval tv; - gnc_commodity_table* table; - gnc_commodity* currency; - 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 }; - hdlr = g_log_set_handler (log_domain, loglevel, - (GLogFunc)test_checked_handler, &check); - - session = qof_session_new(); - book = qof_session_get_book( session ); - root = gnc_book_get_root_account( book ); - g_log_remove_handler (log_domain, hdlr); - - table = gnc_commodity_table_get_table( book ); - currency = gnc_commodity_table_lookup( table, GNC_COMMODITY_NS_CURRENCY, "CAD" ); - - acct1 = xaccMallocAccount( book ); - xaccAccountSetType( acct1, ACCT_TYPE_BANK ); - xaccAccountSetName( acct1, "Bank 1" ); - xaccAccountSetCommodity( acct1, currency ); - - frame = qof_instance_get_slots( QOF_INSTANCE(acct1) ); - kvp_frame_set_gint64( frame, "int64-val", 100 ); - kvp_frame_set_double( frame, "double-val", 3.14159 ); - kvp_frame_set_numeric( frame, "numeric-val", gnc_numeric_zero() ); - - time( &(tv.tv_sec) ); - tv.tv_usec = 0; - ts.tv_sec = tv.tv_sec; - ts.tv_nsec = 1000 * tv.tv_usec; - kvp_frame_set_timespec( frame, "timespec-val", ts ); - - kvp_frame_set_string( frame, "string-val", "abcdefghijklmnop" ); - kvp_frame_set_guid( frame, "guid-val", qof_instance_get_guid( QOF_INSTANCE(acct1) ) ); - - gnc_account_append_child( root, acct1 ); - - acct2 = xaccMallocAccount( book ); - xaccAccountSetType( acct2, ACCT_TYPE_BANK ); - xaccAccountSetName( acct2, "Bank 1" ); - - tx = xaccMallocTransaction( book ); - xaccTransBeginEdit( tx ); - xaccTransSetCurrency( tx, currency ); - spl1 = xaccMallocSplit( book ); - xaccTransAppendSplit( tx, spl1 ); - spl2 = xaccMallocSplit( book ); - xaccTransAppendSplit( tx, spl2 ); - xaccTransCommitEdit( tx ); - - - return session; -} - -int main (int argc, char ** argv) -{ - gchar* filename; - QofSession* session_1; - - qof_init(); - cashobjects_register(); - xaccLogDisable(); - qof_load_backend_library ("../.libs/", GNC_LIB_NAME); - - // Create a session with data - session_1 = create_session(); - filename = tempnam( "/tmp", "test-sqlite3-" ); - g_test_message ( "Using filename: %s\n", filename ); - test_dbi_store_and_reload( "sqlite3", session_1, filename ); - session_1 = create_session(); - test_dbi_safe_save( "sqlite3", filename ); - test_dbi_version_control( "sqlite3", filename ); -#ifdef TEST_MYSQL_URL - g_test_message ( "TEST_MYSQL_URL='%s'\n", TEST_MYSQL_URL ); - if ( strlen( TEST_MYSQL_URL ) > 0 ) - { - session_1 = create_session(); - test_dbi_store_and_reload( "mysql", session_1, TEST_MYSQL_URL ); - session_1 = create_session(); - test_dbi_safe_save( "mysql", filename ); - test_dbi_version_control( "mysql", filename ); - } -#endif -#ifdef TEST_PGSQL_URL - g_test_message ( "TEST_PGSQL_URL='%s'\n", TEST_PGSQL_URL ); - if ( strlen( TEST_PGSQL_URL ) > 0 ) - { - session_1 = create_session(); - test_dbi_store_and_reload( "pgsql", session_1, TEST_PGSQL_URL ); - session_1 = create_session(); - test_dbi_safe_save( "pgsql", filename ); - test_dbi_version_control( "pgsql", filename ); - } -#endif - print_test_results(); - qof_close(); - exit(get_rv()); -} - diff --git a/src/backend/dbi/test/test-dbi-business-stuff.c b/src/backend/dbi/test/test-dbi-business-stuff.c index 437f4d39a1..de1932d783 100644 --- a/src/backend/dbi/test/test-dbi-business-stuff.c +++ b/src/backend/dbi/test/test-dbi-business-stuff.c @@ -176,8 +176,8 @@ compare_taxtables( QofBook* book_1, QofBook* book_2 ) do_compare( book_1, book_2, GNC_ID_TAXTABLE, compare_single_taxtable, "TaxTable lists match" ); } -static void -compare_books( QofBook* book_1, QofBook* book_2 ) +void +compare_business_books( QofBook* book_1, QofBook* book_2 ) { compare_billterms( book_1, book_2 ); compare_taxtables( book_1, book_2 ); @@ -189,43 +189,3 @@ compare_books( QofBook* book_1, QofBook* book_2 ) compare_vendors( book_1, book_2 ); } -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 | G_LOG_FLAG_FATAL, hdlr; - TestErrorStruct check = { loglevel, log_domain, msg, 0 }; - GLogFunc dhdlr = g_log_set_default_handler ((GLogFunc)test_null_handler, - &check); - g_test_log_set_fatal_handler ((GTestLogFatalFunc)test_checked_handler, - &check); - - 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_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); - g_log_set_default_handler (dhdlr, NULL); - qof_session_destroy( session_3 ); -} diff --git a/src/backend/dbi/test/test-dbi-business-stuff.h b/src/backend/dbi/test/test-dbi-business-stuff.h index 3c9eddd34a..c70165e8d0 100644 --- a/src/backend/dbi/test/test-dbi-business-stuff.h +++ b/src/backend/dbi/test/test-dbi-business-stuff.h @@ -26,14 +26,5 @@ #ifndef _TEST_DBI_BUSINESS_STUFF_H_ #define _TEST_DBI_BUSINESS_STUFF_H_ -/** - * Test storing a session contents to a db, reloading into a new session, then comparing the - * two sessions. - * - * @param driver Driver name - * @param session_1 Session to test - * @param url Database URL - */ -void test_dbi_business_store_and_reload( const gchar* driver, QofSession* session_1, const gchar* url ); - +void compare_business_books( QofBook* book_1, QofBook* book_2 ); #endif diff --git a/src/backend/dbi/test/test-dbi-business.c b/src/backend/dbi/test/test-dbi-business.c deleted file mode 100644 index 4efa9a43c8..0000000000 --- a/src/backend/dbi/test/test-dbi-business.c +++ /dev/null @@ -1,190 +0,0 @@ -/*************************************************************************** - * test-dbi-business.c - * - * Tests saving and loading business objects to a dbi/sqlite3 db - * - * Copyright (C) 2010 Phil Longstaff - ****************************************************************************/ - -/* - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - * 02110-1301, USA. - */ - -#include "config.h" -#include "qof.h" -#include "cashobjects.h" -#include "test-engine-stuff.h" -#include "test-stuff.h" -#include "test-dbi-business-stuff.h" - -#include "Account.h" -#include -#include "Transaction.h" -#include "Split.h" -#include "gnc-commodity.h" -#include "gncAddress.h" -#include "gncCustomer.h" -#include "gncInvoice.h" - -#include "gnc-backend-sql.h" - -#include "gnc-address-sql.h" -#include "gnc-bill-term-sql.h" -#include "gnc-customer-sql.h" -#include "gnc-employee-sql.h" -#include "gnc-entry-sql.h" -#include "gnc-invoice-sql.h" -#include "gnc-job-sql.h" -#include "gnc-order-sql.h" -#include "gnc-owner-sql.h" -#include "gnc-tax-table-sql.h" -#include "gnc-vendor-sql.h" - -#define FILE_NAME "sqlite3:///tmp/test-sqlite3-file" -#define GNC_LIB_NAME "gncmod-backend-dbi" - -static QofSession* -create_session(void) -{ - QofSession* session = qof_session_new(); - QofBook* book = qof_session_get_book( session ); - Account* root = gnc_book_get_root_account( book ); - Account* acct1; - Account* acct2; - gnc_commodity_table* table; - gnc_commodity* currency; - GncAddress* addr; - GncCustomer* cust; - GncEmployee* emp; - GncVendor* v; - GncInvoice* inv; - GncJob* job; - GncTaxTable* tt; - GncTaxTableEntry* tte; - - table = gnc_commodity_table_get_table( book ); - currency = gnc_commodity_table_lookup( table, GNC_COMMODITY_NS_CURRENCY, "CAD" ); - - acct1 = xaccMallocAccount( book ); - xaccAccountSetType( acct1, ACCT_TYPE_BANK ); - xaccAccountSetName( acct1, "Bank 1" ); - xaccAccountSetCommodity( acct1, currency ); - xaccAccountSetHidden( acct1, FALSE ); - xaccAccountSetPlaceholder( acct1, FALSE ); - gnc_account_append_child( root, acct1 ); - - acct2 = xaccMallocAccount( book ); - xaccAccountSetType( acct2, ACCT_TYPE_BANK ); - xaccAccountSetName( acct2, "Bank 2" ); - xaccAccountSetCommodity( acct2, currency ); - xaccAccountSetHidden( acct2, FALSE ); - xaccAccountSetPlaceholder( acct2, FALSE ); - gnc_account_append_child( root, acct2 ); - - tt = gncTaxTableCreate( book ); - gncTaxTableSetName( tt, "tt" ); - tte = gncTaxTableEntryCreate(); - gncTaxTableEntrySetAccount( tte, acct1 ); - gncTaxTableEntrySetType( tte, GNC_AMT_TYPE_VALUE ); - gncTaxTableEntrySetAmount( tte, gnc_numeric_zero() ); - gncTaxTableAddEntry( tt, tte ); - tte = gncTaxTableEntryCreate(); - gncTaxTableEntrySetAccount( tte, acct2 ); - gncTaxTableEntrySetType( tte, GNC_AMT_TYPE_PERCENT ); - gncTaxTableEntrySetAmount( tte, gnc_numeric_zero() ); - gncTaxTableAddEntry( tt, tte ); - - cust = gncCustomerCreate( book ); - gncCustomerSetID( cust, "0001" ); - gncCustomerSetName( cust, "MyCustomer" ); - gncCustomerSetNotes( cust, "Here are some notes" ); - gncCustomerSetCurrency( cust, currency ); - addr = gncAddressCreate( book, QOF_INSTANCE(cust) ); - gncAddressSetName( addr, "theAddress" ); - gncAddressSetAddr1( addr, "Address line #1" ); - gncAddressSetAddr2( addr, "Address line #2" ); - gncAddressSetAddr3( addr, "Address line #3" ); - gncAddressSetAddr4( addr, "Address line #4" ); - gncAddressSetPhone( addr, "(123) 555-1212" ); - gncAddressSetPhone( addr, "(123) 555-2121" ); - gncAddressSetEmail( addr, "cust@mycustomer.com" ); - - emp = gncEmployeeCreate( book ); - gncEmployeeSetID( emp, "0001" ); - gncEmployeeSetUsername( emp, "gnucash" ); - gncEmployeeSetLanguage( emp, "english" ); - gncEmployeeSetCurrency( emp, currency ); - - return session; -} - -/* Order in which business objects need to be loaded */ -static const gchar* fixed_load_order[] = -{ GNC_ID_BILLTERM, GNC_ID_TAXTABLE, NULL }; - -static void -init_business_sql(void) -{ - /* Initialize our pointers into the backend subsystem */ - gnc_address_sql_initialize(); - gnc_billterm_sql_initialize(); - gnc_customer_sql_initialize(); - gnc_employee_sql_initialize(); - gnc_entry_sql_initialize(); - gnc_invoice_sql_initialize(); - gnc_job_sql_initialize(); - gnc_order_sql_initialize(); - gnc_owner_sql_initialize(); - gnc_taxtable_sql_initialize(); - gnc_vendor_sql_initialize(); - - gnc_sql_set_load_order( fixed_load_order ); -} - -int main (int argc, char ** argv) -{ - gchar* filename; - QofSession* session_1; - - qof_init(); - cashobjects_register(); - xaccLogDisable(); - qof_load_backend_library ("../.libs/", GNC_LIB_NAME); - - // Create a session with data - session_1 = create_session(); - filename = tempnam( "/tmp", "test-sqlite3-" ); - g_test_message ( "Using filename: %s\n", filename ); - test_dbi_business_store_and_reload( "sqlite3", session_1, filename ); -#if 0 - g_test_message ( "TEST_MYSQL_URL='%s'\n", TEST_MYSQL_URL ); - if ( strlen( TEST_MYSQL_URL ) > 0 ) - { - session_1 = create_session(); - test_dbi_store_and_reload( "mysql", session_1, TEST_MYSQL_URL ); - } - g_test_message ( "TEST_PGSQL_URL='%s'\n", TEST_PGSQL_URL ); - if ( strlen( TEST_PGSQL_URL ) > 0 ) - { - session_1 = create_session(); - test_dbi_store_and_reload( "pgsql", session_1, TEST_PGSQL_URL ); - } -#endif - print_test_results(); - qof_close(); - exit(get_rv()); -} - diff --git a/src/backend/dbi/test/test-dbi-stuff.c b/src/backend/dbi/test/test-dbi-stuff.c index ae6931f449..1d2b42e856 100644 --- a/src/backend/dbi/test/test-dbi-stuff.c +++ b/src/backend/dbi/test/test-dbi-stuff.c @@ -234,7 +234,7 @@ compare_pricedbs( QofBook* book_1, QofBook* book_2 ) compare_single_tx, "Transaction lists match" ); } -static void +void compare_books( QofBook* book_1, QofBook* book_2 ) { QofBackend *be = qof_book_get_backend( book_2 ); @@ -244,198 +244,3 @@ compare_books( QofBook* book_1, QofBook* book_2 ) compare_sxs( book_1, book_2 ); compare_lots( book_1, book_2 ); } - -/* 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; - QofBackend *be; - - 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 | G_LOG_FLAG_FATAL, hdlr; - TestErrorStruct check = { loglevel, log_domain, msg, 0 }; - GLogFunc dhdlr = g_log_set_default_handler ((GLogFunc)test_null_handler, - &check); - g_test_log_set_fatal_handler ((GTestLogFatalFunc)test_checked_handler, - &check); - - - 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 ); - g_assert (session_2 != NULL); - g_assert_cmpint (qof_session_get_error (session_2), ==, ERR_BACKEND_NO_ERR); - qof_session_swap_data( session_1, session_2 ); - qof_session_save( session_2, NULL ); - g_assert (session_2 != NULL); - g_assert_cmpint (qof_session_get_error (session_2), ==, ERR_BACKEND_NO_ERR); - - // Reload the session data - session_3 = qof_session_new(); - g_assert (session_3 != NULL); - qof_session_begin( session_3, url, TRUE, FALSE, FALSE ); - g_assert (session_3 != NULL); - g_assert_cmpint (qof_session_get_error (session_3), ==, ERR_BACKEND_NO_ERR); - qof_session_load( session_3, NULL ); - g_assert (session_3 != NULL); - g_assert_cmpint (qof_session_get_error (session_3), ==, ERR_BACKEND_NO_ERR); - // Compare with the original data - compare_books (qof_session_get_book( session_2), - qof_session_get_book( session_3)); -/* Session_1 belongs to the fixture and teardown() will clean it up */ - 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); - g_log_set_default_handler (dhdlr, NULL); -} - -/* 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; - QofBackend *be; - - 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 ) ); - be = qof_book_get_backend( qof_session_get_book( session_2 ) ); - test_conn_index_functions( be ); - -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; -} - -/* 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_core_prefs_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 ); -} diff --git a/src/backend/dbi/test/test-dbi-stuff.h b/src/backend/dbi/test/test-dbi-stuff.h index 4babe0b03f..92c23badd7 100644 --- a/src/backend/dbi/test/test-dbi-stuff.h +++ b/src/backend/dbi/test/test-dbi-stuff.h @@ -34,29 +34,8 @@ typedef struct QofBook* book_2; gboolean result; } CompareInfoStruct; +void compare_books( QofBook* book_1, QofBook* book_2 ); void do_compare( QofBook* book_1, QofBook* book_2, const gchar* id, QofInstanceForeachCB cb, const gchar* msg ); -/** - * Test storing a session contents to a db, reloading into a new session, then comparing the - * two sessions. - * - * @param driver Driver name - * @param session_1 Session to test - * @param url Database URL - */ -void test_dbi_store_and_reload( const gchar* driver, QofSession* session_1, const gchar* url ); - -/** Test the safe_save mechanism. Beware that this test used on its - * own doesn't ensure that the resave is done safely, only that the - * database is intact and unchanged after the save. To observe the - * safety one must run the test in a debugger and break after the - * rename step of gnc_dbi_safe_sync, then examine the database in the - * appropriate shell. - */ -void test_dbi_safe_save( const gchar* driver, const gchar* url ); - -/** Test the version control mechanism. - */ -void test_dbi_version_control( const gchar* driver, const gchar* url ); #endif diff --git a/src/backend/dbi/test/test-dbi.c b/src/backend/dbi/test/test-dbi.c deleted file mode 100644 index 54e61f021b..0000000000 --- a/src/backend/dbi/test/test-dbi.c +++ /dev/null @@ -1,82 +0,0 @@ -/*************************************************************************** - * test-dbi.c - * - * Tests saving and loading to a dbi/sqlite3 db. The contents of an XML - * file are read and saved to sqlite3, then the results read back and compared. - * - * Copyright (C) 2009 Phil Longstaff - ****************************************************************************/ - -/* - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - * 02110-1301, USA. - */ - -#include "config.h" -#include "qof.h" -#include "cashobjects.h" -#include "test-engine-stuff.h" -#include "test-stuff.h" -#include "test-dbi-stuff.h" - -#include "TransLog.h" -#include "Account.h" -#include "Split.h" -#include "gnc-commodity.h" - -#define DBI_TEST_XML_FILENAME "test-dbi.xml" -#define FILE_NAME "sqlite3:///tmp/test-sqlite3-file" -#define GNC_LIB_NAME "gncmod-backend-dbi" - -int main (int argc, char ** argv) -{ - gchar* filename; - QofSession* session_1; - - qof_init(); - cashobjects_register(); - xaccLogDisable(); - qof_load_backend_library ("../.libs/", GNC_LIB_NAME); - - // Create a session with data - session_1 = qof_session_new(); - qof_session_begin( session_1, DBI_TEST_XML_FILENAME, FALSE, FALSE, FALSE ); - qof_session_load( session_1, NULL ); - - filename = tempnam( "/tmp", "test-sqlite3-" ); - g_test_message ( "Using filename: %s\n", filename ); - test_dbi_store_and_reload( "sqlite3", session_1, filename ); - - g_test_message ( "TEST_MYSQL_URL='%s'\n", TEST_MYSQL_URL ); - if ( strlen( TEST_MYSQL_URL ) > 0 ) - { - session_1 = qof_session_new(); - qof_session_begin( session_1, DBI_TEST_XML_FILENAME, FALSE, FALSE, FALSE ); - qof_session_load( session_1, NULL ); - test_dbi_store_and_reload( "mysql", session_1, TEST_MYSQL_URL ); - } - - g_test_message ( "TEST_PGSQL_URL='%s'\n", TEST_PGSQL_URL ); - if ( strlen( TEST_PGSQL_URL ) > 0 ) - { - session_1 = qof_session_new(); - qof_session_begin( session_1, DBI_TEST_XML_FILENAME, FALSE, FALSE, FALSE ); - qof_session_load( session_1, NULL ); - test_dbi_store_and_reload( "pgsql", session_1, TEST_PGSQL_URL ); - } - print_test_results(); - qof_close(); - exit(get_rv()); -} diff --git a/src/backend/dbi/test/test-load-backend.c b/src/backend/dbi/test/test-load-backend.c deleted file mode 100644 index 9718e6212f..0000000000 --- a/src/backend/dbi/test/test-load-backend.c +++ /dev/null @@ -1,45 +0,0 @@ -/*************************************************************************** - * test-load-backend.c - * - * Replaces the guile version to test the GModule file backend loading. - * - * Sun Oct 9 18:58:47 2005 - * Copyright 2005 Neil Williams - * linux@codehelp.co.uk - ****************************************************************************/ - -/* - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - * 02110-1301, USA. - */ - -#include "config.h" -#include "qof.h" -#include "cashobjects.h" -#include "test-stuff.h" - -#define GNC_LIB_NAME "gncmod-backend-dbi" - -int main (int argc, char ** argv) -{ - qof_init(); - cashobjects_register(); - do_test( - qof_load_backend_library ("../.libs/", GNC_LIB_NAME), - " loading gnc-backend-dbi GModule failed"); - print_test_results(); - qof_close(); - exit(get_rv()); -} diff --git a/src/backend/dbi/test/utest-backend-dbi-basic.c b/src/backend/dbi/test/utest-backend-dbi-basic.c deleted file mode 100644 index 8256a63e4d..0000000000 --- a/src/backend/dbi/test/utest-backend-dbi-basic.c +++ /dev/null @@ -1,88 +0,0 @@ -/* - * utest-backend-dbi-basic.c - * - * Created on: 2011-04-23 - * Author: phil - */ - -#include "config.h" - -#include - -#include "unittest-support.h" -#include "test-stuff.h" -#include "test-dbi-stuff.h" - -static const gchar* suitename = "/backend/dbi"; -void test_suite_gnc_backend_dbi_basic(void); - -void do_test_sqlite(void); -void do_test_mysql(void); -void do_test_pgsql(void); - -typedef struct -{ - QofSession *session; - gchar *filename; -} Fixture; - -static void -setup (Fixture *fixture, gconstpointer pData) -{ - fixture->session = qof_session_new(); - /* When running distcheck the source directory is read-only, which - * prevents creating the lock file. Force the session to get - * around that. - */ - qof_session_begin( fixture->session, DBI_TEST_XML_FILENAME, TRUE, - FALSE, TRUE ); - g_assert_cmpint (qof_session_get_error (fixture->session), ==, - ERR_BACKEND_NO_ERR); - qof_session_load( fixture->session, NULL ); - - fixture->filename = tempnam( "/tmp", "test-sqlite3-" ); - g_test_message ( "Using filename: %s\n", fixture->filename ); -} - -static void -teardown (Fixture *fixture, gconstpointer pData) -{ - qof_session_end (fixture->session); - qof_session_destroy (fixture->session); - g_unlink (fixture->filename); -} - - -static void -test_sqlite_store_and_reload (Fixture *fixture, gconstpointer pData) -{ - // Create a session with data - test_dbi_store_and_reload( "sqlite3", fixture->session, fixture->filename ); -} - -static void -test_mysql_store_and_reload (Fixture *fixture, gconstpointer pData) -{ - g_assert (strlen (TEST_MYSQL_URL) > 0); - test_dbi_store_and_reload( TEST_MYSQL_URL, fixture->session, - fixture->filename ); -} - -static void -test_pgsql_store_and_reload (Fixture *fixture, gconstpointer pData) -{ - g_assert (strlen (TEST_PGSQL_URL) > 0); - test_dbi_store_and_reload( TEST_PGSQL_URL, fixture->session, - fixture->filename ); -} - -void -test_suite_gnc_backend_dbi_basic(void) -{ - GNC_TEST_ADD (suitename, "store_and_reload/sqlite", Fixture, NULL, setup, test_sqlite_store_and_reload, teardown); - if (strlen (TEST_MYSQL_URL) > 0) - GNC_TEST_ADD (suitename, "store_and_reload/mysql", Fixture, NULL, setup, test_mysql_store_and_reload, teardown); - if (strlen (TEST_PGSQL_URL) > 0) - GNC_TEST_ADD (suitename, "store_and_reload/postgres", Fixture, NULL, setup, test_pgsql_store_and_reload, teardown); - -} diff --git a/src/backend/dbi/test/utest-backend-dbi-business.c b/src/backend/dbi/test/utest-backend-dbi-business.c deleted file mode 100644 index e3814359e5..0000000000 --- a/src/backend/dbi/test/utest-backend-dbi-business.c +++ /dev/null @@ -1,199 +0,0 @@ -/*************************************************************************** - * test-dbi-business.c - * - * Tests saving and loading business objects to a dbi/sqlite3 db - * - * Copyright (C) 2010 Phil Longstaff - ****************************************************************************/ - -/* - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - * 02110-1301, USA. - */ - -/* - * utest-backend-dbi-business.c - * - * Created on: 2011-04-23 - * Author: phil - */ - -#include "config.h" - -#include - -#include "unittest-support.h" -#include "test-stuff.h" -#include "test-dbi-stuff.h" - -static const gchar* suitename = "/backend/dbi"; -void test_suite_gnc_backend_dbi_business(void); - -void do_test_business_sqlite(void); - -#include "config.h" -#include "qof.h" -#include "cashobjects.h" -#include "test-engine-stuff.h" -#include "test-stuff.h" -#include "test-dbi-business-stuff.h" - -#include "Account.h" -#include -#include "Transaction.h" -#include "Split.h" -#include "gnc-commodity.h" -#include "gncAddress.h" -#include "gncCustomer.h" -#include "gncInvoice.h" - -#include "gnc-backend-sql.h" - -#include "gnc-address-sql.h" -#include "gnc-bill-term-sql.h" -#include "gnc-customer-sql.h" -#include "gnc-employee-sql.h" -#include "gnc-entry-sql.h" -#include "gnc-invoice-sql.h" -#include "gnc-job-sql.h" -#include "gnc-order-sql.h" -#include "gnc-owner-sql.h" -#include "gnc-tax-table-sql.h" -#include "gnc-vendor-sql.h" - -#define FILE_NAME "sqlite3:///tmp/test-sqlite3-file" -#define GNC_LIB_NAME "gncmod-backend-dbi" - -static QofSession* -create_business_session(void) -{ - QofSession* session = qof_session_new(); - QofBook* book = qof_session_get_book( session ); - Account* root = gnc_book_get_root_account( book ); - Account* acct1; - Account* acct2; - gnc_commodity_table* table; - gnc_commodity* currency; - GncAddress* addr; - GncCustomer* cust; - GncEmployee* emp; - GncTaxTable* tt; - GncTaxTableEntry* tte; - - table = gnc_commodity_table_get_table( book ); - currency = gnc_commodity_table_lookup( table, GNC_COMMODITY_NS_CURRENCY, "CAD" ); - - acct1 = xaccMallocAccount( book ); - xaccAccountSetType( acct1, ACCT_TYPE_BANK ); - xaccAccountSetName( acct1, "Bank 1" ); - xaccAccountSetCommodity( acct1, currency ); - xaccAccountSetHidden( acct1, FALSE ); - xaccAccountSetPlaceholder( acct1, FALSE ); - gnc_account_append_child( root, acct1 ); - - acct2 = xaccMallocAccount( book ); - xaccAccountSetType( acct2, ACCT_TYPE_BANK ); - xaccAccountSetName( acct2, "Bank 2" ); - xaccAccountSetCommodity( acct2, currency ); - xaccAccountSetHidden( acct2, FALSE ); - xaccAccountSetPlaceholder( acct2, FALSE ); - gnc_account_append_child( root, acct2 ); - - tt = gncTaxTableCreate( book ); - gncTaxTableSetName( tt, "tt" ); - tte = gncTaxTableEntryCreate(); - gncTaxTableEntrySetAccount( tte, acct1 ); - gncTaxTableEntrySetType( tte, GNC_AMT_TYPE_VALUE ); - gncTaxTableEntrySetAmount( tte, gnc_numeric_zero() ); - gncTaxTableAddEntry( tt, tte ); - tte = gncTaxTableEntryCreate(); - gncTaxTableEntrySetAccount( tte, acct2 ); - gncTaxTableEntrySetType( tte, GNC_AMT_TYPE_PERCENT ); - gncTaxTableEntrySetAmount( tte, gnc_numeric_zero() ); - gncTaxTableAddEntry( tt, tte ); - - cust = gncCustomerCreate( book ); - gncCustomerSetID( cust, "0001" ); - gncCustomerSetName( cust, "MyCustomer" ); - gncCustomerSetNotes( cust, "Here are some notes" ); - gncCustomerSetCurrency( cust, currency ); - addr = gncAddressCreate( book, QOF_INSTANCE(cust) ); - gncAddressSetName( addr, "theAddress" ); - gncAddressSetAddr1( addr, "Address line #1" ); - gncAddressSetAddr2( addr, "Address line #2" ); - gncAddressSetAddr3( addr, "Address line #3" ); - gncAddressSetAddr4( addr, "Address line #4" ); - gncAddressSetPhone( addr, "(123) 555-1212" ); - gncAddressSetPhone( addr, "(123) 555-2121" ); - gncAddressSetEmail( addr, "cust@mycustomer.com" ); - - emp = gncEmployeeCreate( book ); - gncEmployeeSetID( emp, "0001" ); - gncEmployeeSetUsername( emp, "gnucash" ); - gncEmployeeSetLanguage( emp, "english" ); - gncEmployeeSetCurrency( emp, currency ); - - return session; -} - -/* Order in which business objects need to be loaded */ -static const gchar* fixed_load_order[] = -{ GNC_ID_BILLTERM, GNC_ID_TAXTABLE, NULL }; - -G_GNUC_UNUSED static void -init_business_sql(void) -{ - /* Initialize our pointers into the backend subsystem */ - gnc_address_sql_initialize(); - gnc_billterm_sql_initialize(); - gnc_customer_sql_initialize(); - gnc_employee_sql_initialize(); - gnc_entry_sql_initialize(); - gnc_invoice_sql_initialize(); - gnc_job_sql_initialize(); - gnc_order_sql_initialize(); - gnc_owner_sql_initialize(); - gnc_taxtable_sql_initialize(); - gnc_vendor_sql_initialize(); - - gnc_sql_set_load_order( fixed_load_order ); -} - -static gboolean handler(const gchar* log_domain, GLogLevelFlags log_level, const gchar* message, gpointer user_data) -{ - printf("domain=%s level=%d message=%s\n", log_domain, log_level, message); - return FALSE; -} - -void -do_test_business_sqlite(void) -{ - gchar* filename; - QofSession* session_1; - - g_test_log_set_fatal_handler(handler, 0); - - // Create a session with data - session_1 = create_business_session(); - filename = tempnam( "/tmp", "test-sqlite3-" ); - g_test_message ( "Using filename: %s\n", filename ); - test_dbi_business_store_and_reload( "sqlite3", session_1, filename ); -} - -void -test_suite_gnc_backend_dbi_business(void) -{ - GNC_TEST_ADD_FUNC(suitename, "gnc dbi test sqlite (business)", do_test_business_sqlite); -}