GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
test-group-vs-book.c
1 /***************************************************************************
2  * test-group-vs-book.c
3  *
4  * Tue Sep 27 19:32:31 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 "Account.h"
29 #include "TransLog.h"
30 #include "gnc-engine.h"
31 #include "test-engine-stuff.h"
32 #include "test-stuff.h"
33 
34 static gboolean
35 account_tree_has_book (Account *parent, QofBook *book)
36 {
37  GList *children, *node;
38 
39  if (!parent)
40  return (book == NULL);
41 
42  if (gnc_account_get_book(parent) != book)
43  return FALSE;
44 
45  children = gnc_account_get_children(parent);
46  for (node = children; node; node = node->next)
47  {
48  if (!account_tree_has_book (node->data, book))
49  return FALSE;
50  }
51  g_list_free(children);
52 
53  return TRUE;
54 }
55 
56 
57 static void
58 run_test (void)
59 {
60  Account *root1;
61  Account *root2;
62  Account *account1;
63  Account *account2;
64  QofBook *book;
65 
66  book = qof_book_new ();
67  if (!book)
68  {
69  failure("book not created");
70  exit(get_rv());
71  }
72 
73  root1 = get_random_account (book);
74  if (!root1)
75  {
76  failure("root1 not created");
77  exit(get_rv());
78  }
79 
80  if (!account_tree_has_book (root1, book))
81  {
82  failure("new root has wrong book");
83  exit(get_rv());
84  }
85 
86  /* This test is testing routines that are private
87  * to the engine. these tests are intended to test
88  * the engine as a whole, not just the public
89  * interface. the maintenance of the correct
90  * book pointers is important for correct
91  * engine operation. */
92  gnc_book_set_root_account (book, root1);
93  if (!account_tree_has_book (root1, book))
94  {
95  failure("gnc_book_set_root_account didn't take");
96  exit(get_rv());
97  }
98 
99  root2 = get_random_account (book);
100  if (!root2)
101  {
102  failure("root2 not created");
103  exit(get_rv());
104  }
105 
106  gnc_book_set_root_account (book, root2);
107 
108 #if 0
109  /* a group cannot have a 'null' book; this test is nonsense. */
110  if (!account_tree_has_book (root1, NULL))
111  {
112  failure("gnc_book_set_root_account didn't clear old");
113  exit(get_rv());
114  }
115 #endif
116 
117  if (!account_tree_has_book (root2, book))
118  {
119  failure("gnc_book_set_root_account didn't take");
120  exit(get_rv());
121  }
122 
123  account1 = get_random_account (book);
124  if (!account1)
125  {
126  failure("account1 not created");
127  exit(get_rv());
128  }
129 
130  gnc_account_append_child (root2, account1);
131  if (root2 != gnc_account_get_parent (account1))
132  {
133  failure("group insert account didn't work");
134  exit(get_rv());
135  }
136 
137  account2 = get_random_account (book);
138  if (!account2)
139  {
140  failure("account2 not created");
141  exit(get_rv());
142  }
143 
144  gnc_account_append_child (account1, account2);
145  if (!account_tree_has_book (gnc_account_get_parent (account2), book))
146  {
147  failure("account2 has wrong book");
148  exit(get_rv());
149  }
150 
151  gnc_account_remove_child (root2, account1);
152  if (gnc_account_get_parent (account1) != NULL)
153  {
154  failure("remove group didn't take");
155  exit(get_rv());
156  }
157 }
158 
159 int
160 main (int argc, char **argv)
161 {
162  gint i;
163  qof_init();
164  if (cashobjects_register())
165  {
166  xaccLogDisable ();
167  for (i = 0; i < 10; i++)
168  {
169  run_test ();
170  }
171  success ("group/book stuff seems to work");
172  print_test_results();
173  }
174  qof_close();
175  return get_rv();
176 }
Account * gnc_account_get_parent(const Account *acc)
Definition: Account.c:2623
void gnc_account_append_child(Account *new_parent, Account *child)
Definition: Account.c:2525
void xaccLogDisable(void)
Definition: TransLog.c:93
QofBook * qof_book_new(void)
void gnc_account_remove_child(Account *parent, Account *child)
Definition: Account.c:2587
Account handling public routines.
All type declarations for the whole Gnucash engine.
API for the transaction logger.
void qof_close(void)
Safely close down the Query Object Framework.
GList * gnc_account_get_children(const Account *account)
Definition: Account.c:2654
void qof_init(void)
Initialise the Query Object Framework.