GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
test-query.c
1 /***************************************************************************
2  * test-query.c
3  *
4  * Tue Sep 27 19:12:41 2005
5  * Copyright 2005 GnuCash team
6  ****************************************************************************/
7 /*
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301, USA.
22  */
23 
24 #include "config.h"
25 #include <glib.h>
26 #include "qof.h"
27 #include "cashobjects.h"
28 #include "Transaction.h"
29 #include "TransLog.h"
30 #include "gnc-engine.h"
31 #include "test-engine-stuff.h"
32 #include "test-stuff.h"
33 
34 static int
35 test_trans_query (Transaction *trans, gpointer data)
36 {
37  QofBook *book = data;
38  GList *list;
39  QofQuery *q;
40 
41  q = make_trans_query (trans, ALL_QT);
42  qof_query_set_book (q, book);
43 
44  list = xaccQueryGetTransactions (q, QUERY_TXN_MATCH_ANY);
45  if (g_list_length (list) != 1)
46  {
47  failure_args ("test number returned", __FILE__, __LINE__,
48  "number of matching transactions %d not 1",
49  g_list_length (list));
50  g_list_free (list);
51  return 13;
52  }
53 
54  if (list->data != trans)
55  {
56  failure ("matching transaction is wrong");
57  g_list_free (list);
58  return 13;
59  }
60 
61  success ("found right transaction");
63  g_list_free (list);
64 
65  return 0;
66 }
67 
68 static void
69 run_test (void)
70 {
71  QofSession *session;
72  Account *root;
73  QofBook *book;
74 
75  session = get_random_session ();
76  book = qof_session_get_book (session);
77  root = gnc_book_get_root_account (book);
78 
79  add_random_transactions_to_book (book, 20);
80 
81  xaccAccountTreeForEachTransaction (root, test_trans_query, book);
82 
83  qof_session_end (session);
84 }
85 
86 int
87 main (int argc, char **argv)
88 {
89  int i;
90 
91  qof_init();
92  g_log_set_always_fatal( G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING );
93 
94  xaccLogDisable ();
95 
96  /* Always start from the same random seed so we fail consistently */
97  srand(0);
98  if (!cashobjects_register())
99  {
100  failure("can't register cashbojects");
101  goto cleanup;
102  }
103 
104  /* Loop the test. */
105  for (i = 0; i < 10; i++)
106  {
107  run_test ();
108  }
109  success("queries seem to work");
110 
111 cleanup:
112  qof_close();
113  return get_rv();
114 }
int xaccAccountTreeForEachTransaction(Account *acc, TransactionCallback proc, void *data)
void xaccLogDisable(void)
Definition: TransLog.c:93
struct _QofQuery QofQuery
Definition: qofquery.h:90
QofBook * qof_session_get_book(const QofSession *session)
void qof_query_destroy(QofQuery *q)
void qof_query_set_book(QofQuery *q, QofBook *book)
All type declarations for the whole Gnucash engine.
API for the transaction logger.
void qof_close(void)
Safely close down the Query Object Framework.
void qof_session_end(QofSession *session)
void qof_init(void)
Initialise the Query Object Framework.
API for Transactions and Splits (journal entries)