GnuCash 2.4.99
test-transaction-voiding.c
00001 /***************************************************************************
00002  *            test-transaction-voiding.c
00003  *
00004  *  Modified to run without Guile: Mon Aug 22 11:24:44 2005
00005  *  Copyright  2005  Neil Williams
00006  *  linux@codehelp.co.uk
00007  ****************************************************************************/
00008 /*
00009  *  This program is free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  This program is distributed in the hope that it will be useful,
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *  GNU General Public License for more details.
00018  *
00019  *  You should have received a copy of the GNU General Public License
00020  *  along with this program; if not, write to the Free Software
00021  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00022  *  02110-1301, USA.
00023  */
00024 
00025 #include "config.h"
00026 #include <glib.h>
00027 #include <string.h>
00028 #include "cashobjects.h"
00029 #include "Account.h"
00030 #include "TransLog.h"
00031 #include "test-engine-stuff.h"
00032 #include "test-stuff.h"
00033 #include "Transaction.h"
00034 
00035 #define print_gnc_numeric(num) fprintf(stderr, "%s\n", gnc_numeric_to_string(num))
00036 
00037 static void
00038 transaction_set_splits_to_accounts(Transaction *tr, Account *a1, Account *a2)
00039 {
00040 
00041     Split *split;
00042 
00043     split  = xaccTransGetSplit(tr, 0);
00044 
00045     xaccAccountInsertSplit(a1, split);
00046 
00047     split = xaccTransGetSplit(tr, 1);
00048     xaccAccountInsertSplit(a2, split);
00049     return;
00050 }
00051 
00052 static void
00053 run_test (void)
00054 {
00055     Account *acc1, *acc2;
00056     Transaction *transaction;
00057     gnc_numeric old_amt, new_amt, old_val, new_val;
00058     QofBook *book;
00059     Timespec ts;
00060     time_t now;
00061 
00062     char *reason = "because I can";
00063 
00064     book = qof_book_new();
00065 
00066     acc1 = get_random_account(book);
00067     acc2 = get_random_account(book);
00068 
00069     if (!acc1 || !acc2)
00070     {
00071         failure("accounts not created");
00072     }
00073 
00074     do
00075     {
00076         transaction = get_random_transaction (book);
00077         if (xaccTransGetVoidStatus (transaction))
00078         {
00079             xaccTransBeginEdit (transaction);
00080             xaccTransDestroy (transaction);
00081             xaccTransCommitEdit (transaction);
00082             transaction = NULL;
00083         }
00084     }
00085     while (!transaction);
00086 
00087     transaction_set_splits_to_accounts(transaction, acc1, acc2);
00088 
00089     /*  Compromise, check amount on one and value on the other */
00090 
00091     old_amt = xaccSplitGetAmount(xaccTransGetSplit(transaction, 0));
00092     old_val = xaccSplitGetValue(xaccTransGetSplit(transaction, 1));
00093 
00094     now = time (NULL);
00095 
00096     xaccTransVoid(transaction, reason);
00097 
00098     ts = xaccTransGetVoidTime (transaction);
00099 
00100     /* figure at most 2 seconds difference */
00101     if ((ts.tv_sec < now) || ((ts.tv_sec - now) > 2))
00102     {
00103         failure("bad void time");
00104     }
00105 
00106     if (!xaccTransGetVoidStatus(transaction))
00107     {
00108         failure("void status reports false after setting void");
00109     }
00110 
00111     if (strcmp(reason, xaccTransGetVoidReason(transaction)) != 0)
00112     {
00113         failure("Reasons didn't match");
00114     }
00115 
00116     new_amt = xaccSplitGetAmount(xaccTransGetSplit(transaction, 0));
00117     /* print_gnc_numeric(new_amt); */
00118 
00119     if (!gnc_numeric_zero_p( new_amt))
00120     {
00121         failure("Amount of split0 not zero after voiding");
00122     }
00123 
00124     new_val = xaccSplitGetValue(xaccTransGetSplit(transaction, 1));
00125 
00126     if (!(gnc_numeric_zero_p(new_val)))
00127     {
00128         failure("Value of split1 not zero after voiding");
00129     }
00130 
00131 
00132     if (!(gnc_numeric_eq(old_amt, xaccSplitVoidFormerAmount(xaccTransGetSplit(transaction, 0)))))
00133     {
00134         failure("former amount (after voiding) didn't match actual old amount");
00135     }
00136 
00137     if (!(gnc_numeric_eq(old_val, xaccSplitVoidFormerValue(xaccTransGetSplit(transaction, 1)))))
00138     {
00139         failure("former value (after voiding) didn't match actual old value");
00140     }
00141 
00142     /*
00143      * Retore the transaction to its former glory.
00144      */
00145     xaccTransUnvoid(transaction);
00146 
00147     ts = xaccTransGetVoidTime (transaction);
00148 
00149     /* figure at most 2 seconds difference */
00150     if ((ts.tv_sec != 0) || (ts.tv_sec != 0))
00151     {
00152         failure("void time not zero after restore");
00153     }
00154 
00155     if (xaccTransGetVoidStatus(transaction))
00156     {
00157         failure("void status reports trus after restoring transaction");
00158     }
00159 
00160     if (xaccTransGetVoidReason(transaction))
00161     {
00162         failure("void reason exists after restoring transaction");
00163     }
00164 
00165     new_amt = xaccSplitGetAmount(xaccTransGetSplit(transaction, 0));
00166     /* print_gnc_numeric(new_amt); */
00167 
00168     if (!(gnc_numeric_eq(old_amt, new_amt)))
00169     {
00170         failure("Amount of split0 not correct after restoring transaction");
00171     }
00172 
00173     new_val = xaccSplitGetValue(xaccTransGetSplit(transaction, 1));
00174 
00175     if (!(gnc_numeric_eq(old_val, new_val)))
00176     {
00177         failure("Value of split1 not correct after restoring transaction");
00178     }
00179 
00180 
00181     if (!(gnc_numeric_zero_p(xaccSplitVoidFormerAmount(xaccTransGetSplit(transaction, 0)))))
00182     {
00183         failure("former amount (after restore) should be zero");
00184     }
00185 
00186     if (!(gnc_numeric_zero_p(xaccSplitVoidFormerValue(xaccTransGetSplit(transaction, 1)))))
00187     {
00188         failure("former value (after restore) should be zero");
00189     }
00190 
00191     return;
00192 }
00193 
00194 int
00195 main (int argc, char **argv)
00196 {
00197     qof_init();
00198     if (cashobjects_register())
00199     {
00200         xaccLogDisable ();
00201         run_test ();
00202         success("transaction voiding seems OK");
00203         print_test_results();
00204     }
00205     qof_close();
00206     return get_rv();
00207 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines