GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
test-option-util.c
1 /********************************************************************
2  * test-option-util.c: GLib g_test test suite for Split.c. *
3  * Copyright 2013 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, you can retrieve it from *
17  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html *
18  * or contact: *
19  * *
20  * Free Software Foundation Voice: +1-617-542-5942 *
21  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
22  * Boston, MA 02110-1301, USA [email protected] *
23  ********************************************************************/
24 
25 #include <config.h>
26 #include <glib.h>
27 #include <unittest-support.h>
28 #include <qofbookslots.h>
29 
30 #include "../option-util.h"
31 
32 static const gchar *suitename = "/app-utils/option-util";
33 void test_suite_option_util (void);
34 
35 typedef struct
36 {
37  QofBook *book;
38  GSList *hdlrs;
39 } Fixture;
40 
41 /* Expose a mostly-private QofInstance function to load options into
42  * the Book.
43  */
44 extern KvpFrame *qof_instance_get_slots (const QofInstance*);
45 
46 static void
47 setup (Fixture *fixture, gconstpointer pData)
48 {
49  fixture->book = qof_book_new ();
50  fixture->hdlrs = NULL;
51 }
52 
53 static void
54 setup_kvp (Fixture *fixture, gconstpointer pData)
55 {
56  QofBook *book;
57  KvpFrame *slots;
58  setup (fixture, pData);
59  book = fixture->book;
60  slots = qof_instance_get_slots (QOF_INSTANCE (book));
61  qof_begin_edit (QOF_INSTANCE (book));
62  qof_instance_set (QOF_INSTANCE (book),
63  "trading-accts", "t",
64  "split-action-num-field", "t",
65  "autoreadonly-days", (double)21,
66  NULL);
67 
68  kvp_frame_set_string (slots, "options/Business/Company Name",
69  "Bogus Company");
70  qof_commit_edit (QOF_INSTANCE (book));
71 }
72 
73 static void
74 teardown (Fixture *fixture, gconstpointer pData)
75 {
76  qof_book_destroy (fixture->book);
77  g_slist_free_full (fixture->hdlrs, test_free_log_handler);
78  test_clear_error_list();
79 }
80 
81 static void
82 test_option_load_from_kvp (Fixture *fixture, gconstpointer pData)
83 {
84  QofBook *book = fixture->book;
85  GNCOptionDB *odb = gnc_option_db_new_for_type (QOF_ID_BOOK);
86 
87  qof_book_load_options (book, gnc_option_db_load_from_kvp, odb);
88  g_assert (gnc_option_db_lookup_boolean_option (odb, OPTION_SECTION_ACCOUNTS,
89  OPTION_NAME_TRADING_ACCOUNTS, FALSE));
90  g_assert_cmpstr (gnc_option_db_lookup_string_option (odb, "Business", "Company Name", FALSE), ==, "Bogus Company");
91  g_assert_cmpfloat (gnc_option_db_lookup_number_option (odb, OPTION_SECTION_ACCOUNTS, OPTION_NAME_AUTO_READONLY_DAYS, FALSE), ==, 21);
92 
93  gnc_option_db_destroy (odb);
94 }
95 
96 static void
97 test_option_save_to_kvp (Fixture *fixture, gconstpointer pData)
98 {
99  QofBook *book = fixture->book;
100  GNCOptionDB *odb = gnc_option_db_new_for_type (QOF_ID_BOOK);
101  KvpFrame *slots = qof_instance_get_slots (QOF_INSTANCE (book));
102 
103  g_assert (gnc_option_db_set_boolean_option (odb, OPTION_SECTION_ACCOUNTS,
104  OPTION_NAME_TRADING_ACCOUNTS,
105  TRUE));
106  g_assert (gnc_option_db_set_string_option (odb, "Business", "Company Name",
107  "Bogus Company"));
108  g_assert (gnc_option_db_set_number_option (odb, OPTION_SECTION_ACCOUNTS,
109  OPTION_NAME_AUTO_READONLY_DAYS,
110  17));
111  qof_book_save_options (book, gnc_option_db_save_to_kvp, odb, TRUE);
112  g_assert_cmpstr (kvp_frame_get_string (slots, "options/Accounts/Use Trading Accounts"), == , "t");
113  g_assert_cmpstr (kvp_frame_get_string (slots, "options/Business/Company Name"), ==, "Bogus Company");
114  g_assert_cmpfloat (kvp_frame_get_double (slots, "options/Accounts/Day Threshold for Read-Only Transactions (red line)"), ==, 17);
115 
116  gnc_option_db_destroy (odb);
117 }
118 
119 
120 void
121 test_suite_option_util (void)
122 {
123  GNC_TEST_ADD (suitename, "Option DB Load from KVP", Fixture, NULL, setup_kvp, test_option_load_from_kvp, teardown);
124  GNC_TEST_ADD (suitename, "Option DB Save to KVP", Fixture, NULL, setup, test_option_save_to_kvp, teardown);
125 
126 }
void qof_instance_set(QofInstance *inst, const gchar *first_param,...)
Wrapper for g_object_set Group setting multiple parameters in a single begin/commit/rollback.
QofBook * qof_book_new(void)
gboolean qof_commit_edit(QofInstance *inst)
gboolean qof_begin_edit(QofInstance *inst)
struct KvpFrameImpl KvpFrame
Definition: kvp_frame.h:76
void kvp_frame_set_string(KvpFrame *frame, const gchar *path, const gchar *str)
Store a copy of the string at the indicated path.
void qof_book_destroy(QofBook *book)