GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
test-kvp.c
1 /********************************************************************
2  * test_submodule.c: Example GLib g_test test suite. *
3  * Copyright 2011 John Ralls <[email protected]> *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License*
16  * along with this program; if not, contact: *
17  * *
18  * Free Software Foundation Voice: +1-617-542-5942 *
19  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
20  * Boston, MA 02110-1301, USA [email protected] *
21 \********************************************************************/
22 
23 #include <config.h>
24 #include <glib.h>
25 
26 // for the gnc_ab_get_book_template_list() et al. functions
29 #include "engine/gnc-hooks.h"
30 
31 static char* get_filepath(const char* filename)
32 {
33  char *result;
34 
35  const char *srcdir = g_getenv("SRCDIR");
36  if (!srcdir)
37  {
38  g_test_message("No env variable SRCDIR exists, assuming \".\"\n");
39  srcdir = ".";
40  }
41 
42  result = g_strdup_printf("%s/%s", srcdir, filename);
43 
44  g_test_message("Using file path %s\n", result);
45 
46  // Test whether the file really exists
47  g_assert(g_file_test(result, G_FILE_TEST_EXISTS));
48 
49  return result;
50 }
51 
52 void
53 test_qofsession_aqb_kvp( void )
54 {
55  /* load the accounts from the users datafile */
56  /* but first, check to make sure we've got a session going. */
57  QofBackendError io_err;
58  char *file1 = get_filepath("file-book.gnucash");
59  char *file2 = get_filepath("file-book-hbcislot.gnucash");
60 
61  if (1)
62  {
63  // A file with no content at all, but a valid XML file
64  QofSession *new_session = qof_session_new ();
65  char *newfile = g_strdup_printf("file://%s", file1);
66 
67  qof_session_begin (new_session, newfile, TRUE, FALSE, FALSE);
68  io_err = qof_session_get_error (new_session);
69  //printf("io_err1 = %d\n", io_err);
70  g_assert(io_err != ERR_BACKEND_NO_HANDLER); // Do not have no handler
71 
72  g_assert(io_err != ERR_BACKEND_NO_SUCH_DB); // DB must exist
73  g_assert(io_err != ERR_BACKEND_LOCKED);
74  g_assert(io_err == 0);
75 
76  qof_session_load (new_session, NULL);
77  io_err = qof_session_get_error (new_session);
78  //printf("io_err2 = %d\n", io_err);
79  g_assert(io_err == 0);
80 
81  // No HBCI slot exists, of course
82 
83  {
84  // No HBCI slot exists, of course
85  GList *mylist = gnc_ab_get_book_template_list(qof_session_get_book(new_session));
86  g_assert(mylist == 0);
87  }
88 
89  g_free(newfile);
90  g_free(file1);
91 
92  gnc_hook_run(HOOK_BOOK_CLOSED, new_session);
93  //qof_session_destroy(new_session); // tries to delete the LCK file but it wasn't created in the first place
94  }
95 
96  if (1)
97  {
98  // A file with no content except for the book_template_list kvp
99  // slot
100  QofSession *new_session = qof_session_new ();
101  char *newfile = g_strdup_printf("file://%s", file2);
102 
103  qof_session_begin (new_session, newfile, TRUE, FALSE, FALSE);
104  io_err = qof_session_get_error (new_session);
105  //printf("io_err1 = %d\n", io_err);
106  g_assert(io_err != ERR_BACKEND_NO_HANDLER); // Do not have no handler
107 
108  g_assert(io_err != ERR_BACKEND_NO_SUCH_DB); // DB must exist
109  g_assert(io_err != ERR_BACKEND_LOCKED);
110  g_assert(io_err == 0);
111 
112  qof_session_load (new_session, NULL);
113  io_err = qof_session_get_error (new_session);
114  //printf("io_err2 = %d\n", io_err);
115  g_assert(io_err == 0);
116 
117  {
118  GList *templ_list;
119  GncABTransTempl *templ;
120  QofBook *book = qof_session_get_book(new_session);
121  const char* ORIGINAL_NAME = "Some Name";
122  const char* CHANGED_NAME = "Some Changed Name";
123 
124  GList *kvp_list = gnc_ab_get_book_template_list(book);
125  g_assert(kvp_list != 0); // do we have the slot?!
126  g_assert_cmpint(g_list_length(kvp_list), ==, 1);
127 
128  templ_list = gnc_ab_trans_templ_list_new_from_kvp_list(kvp_list);
129  g_assert_cmpint(g_list_length(templ_list), ==, 1);
130 
131  templ = templ_list->data;
132  g_assert_cmpstr(gnc_ab_trans_templ_get_name(templ), ==, ORIGINAL_NAME); // ok, name from file is here
133 
134  // Now we change the name into something else and verify it can be saved
135  gnc_ab_trans_templ_set_name(templ, CHANGED_NAME);
136  {
137  GList *kvp_list_new = gnc_ab_trans_templ_list_to_kvp_list(templ_list);
138  gnc_ab_trans_templ_list_free(templ_list);
139  g_assert(!qof_instance_get_dirty(QOF_INSTANCE(book))); // not yet dirty
140 
141  // Here we save the changed kvp
142  gnc_ab_set_book_template_list(book, kvp_list_new);
143  g_assert(qof_instance_get_dirty(QOF_INSTANCE(book))); // yup, now dirty
144  }
145 
146  {
147  GList *mylist = gnc_ab_get_book_template_list(book);
148  g_assert(mylist != 0);
149  g_assert_cmpint(g_list_length(mylist), ==, 1);
150 
151  templ_list = gnc_ab_trans_templ_list_new_from_kvp_list(mylist);
152  g_assert_cmpint(g_list_length(templ_list), ==, 1);
153 
154  templ = templ_list->data;
155  g_assert_cmpstr(gnc_ab_trans_templ_get_name(templ), ==, CHANGED_NAME); // ok, the change has been saved!
156  gnc_ab_trans_templ_list_free(templ_list);
157  }
158  }
159 
160  {
161  // Check the kvp slots of a aqbanking-enabled account
162  QofBook *book = qof_session_get_book(new_session);
163  Account* account = gnc_book_get_root_account(book);
164  GDate retrieved_date, original_date;
165  gchar buff[MAX_DATE_LENGTH];
166 
167  g_assert(account);
168 
169  // The interesting test case here: Can we read the correct date
170  // from the xml file?
171  if (1)
172  {
173  Timespec retrieved_ts = gnc_ab_get_account_trans_retrieval(account);
174  g_test_message("retrieved_ts=%s\n", gnc_print_date(retrieved_ts));
175  //printf("Time=%s\n", gnc_print_date(retrieved_ts));
176 
177  retrieved_date = timespec_to_gdate(retrieved_ts);
178  g_date_set_dmy(&original_date, 29, 8, 2014);
179 
180  g_assert_cmpint(g_date_compare(&retrieved_date, &original_date), ==, 0);
181  }
182 
183  // A lower-level test here: Can we write and read again the
184  // trans_retrieval date? This wouldn't need this particular
185  // Account, just a general Account object.
186  if (0)
187  {
188  Timespec original_ts = timespec_now(), retrieved_ts;
189 
190  // Check whether the "ab-trans-retrieval" property of Account
191  // is written and read again correctly.
192  gnc_ab_set_account_trans_retrieval(account, original_ts);
193  retrieved_ts = gnc_ab_get_account_trans_retrieval(account);
194 
195 // printf("original_ts=%s = %d retrieved_ts=%s = %d\n",
196 // gnc_print_date(original_ts), original_ts.tv_sec,
197 // gnc_print_date(retrieved_ts), retrieved_ts.tv_sec);
198 
199  original_date = timespec_to_gdate(original_ts);
200  retrieved_date = timespec_to_gdate(retrieved_ts);
201 
202  qof_print_gdate (buff, sizeof (buff), &original_date);
203  //printf("original_date=%s\n", buff);
204  qof_print_gdate (buff, sizeof (buff), &retrieved_date);
205  //printf("retrieved_date=%s\n", buff);
206 
207  // Is the retrieved date identical to the one written
208  g_assert_cmpint(g_date_compare(&retrieved_date, &original_date), ==, 0);
209  }
210 
211  }
212 
213  g_free(newfile);
214  g_free(file2);
215 
216  gnc_hook_run(HOOK_BOOK_CLOSED, new_session);
217  //qof_session_destroy(new_session); // tries to delete the LCK file but it wasn't created in the first place
218  }
219 
220 
221 }
typedefG_BEGIN_DECLS struct _GncABTransTempl GncABTransTempl
Timespec gnc_ab_get_account_trans_retrieval(const Account *a)
Definition: gnc-ab-kvp.c:99
const char * gnc_print_date(Timespec ts)
Templates for AqBanking transactions.
QofBackendError
The errors that can be reported to the GUI & other front-end users.
Definition: qofbackend.h:59
Use a 64-bit unsigned int timespec.
Definition: gnc-date.h:299
void gnc_ab_trans_templ_list_free(GList *l)
void gnc_ab_trans_templ_set_name(GncABTransTempl *t, const gchar *name)
GList * gnc_ab_trans_templ_list_to_kvp_list(GList *k)
GDate timespec_to_gdate(Timespec ts)
QofBook * qof_session_get_book(const QofSession *session)
const gchar * gnc_ab_trans_templ_get_name(const GncABTransTempl *t)
void gnc_ab_set_account_trans_retrieval(Account *a, Timespec time)
Definition: gnc-ab-kvp.c:109
QofBackendError qof_session_get_error(QofSession *session)
#define MAX_DATE_LENGTH
Definition: gnc-date.h:106
void gnc_ab_set_book_template_list(QofBook *b, GList *template_list)
Definition: gnc-ab-kvp.c:138
void qof_session_begin(QofSession *session, const char *book_id, gboolean ignore_lock, gboolean create, gboolean force)
Timespec timespec_now(void)
GList * gnc_ab_trans_templ_list_new_from_kvp_list(GList *v)
AqBanking KVP handling.
GList * gnc_ab_get_book_template_list(QofBook *b)
Definition: gnc-ab-kvp.c:130
size_t qof_print_gdate(char *buf, size_t bufflen, const GDate *gd)