GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
test-sx.c
1 #include "config.h"
2 #include <stdlib.h>
3 #include <glib.h>
4 #include "SX-book.h"
6 #include "gnc-ui-util.h"
7 #include <gnc-gdate-utils.h>
8 
9 #include "test-stuff.h"
10 #include "test-engine-stuff.h"
11 
12 static void
13 test_basic()
14 {
15  GncSxInstanceModel *model;
16  GDate yesterday, today, tomorrow, before_yesterday, after_tomorrow;
17  SchedXaction *one_sx;
18 
19  g_date_clear(&today, 1);
20  gnc_gdate_set_today (&today);
21 
22  yesterday = today;
23  g_date_subtract_days(&yesterday, 1);
24  before_yesterday = yesterday;
25  g_date_subtract_days(&before_yesterday, 1);
26  tomorrow = today;
27  g_date_add_days(&tomorrow, 1);
28  after_tomorrow = tomorrow;
29  g_date_add_days(&after_tomorrow, 1);
30 
31  one_sx = add_daily_sx("foo", &yesterday, NULL, NULL);
32 
33  model = gnc_sx_get_instances(&tomorrow, TRUE);
34 
35  {
36  GncSxInstances *insts;
37  GList *iter;
38 
39  do_test(g_list_length(model->sx_instance_list) == 1, "1 GncSxInstances");
40  insts = (GncSxInstances*)model->sx_instance_list->data;
41  do_test(g_list_length(insts->instance_list) == 3, "yesterday, today and tomorrow");
42  for (iter = insts->instance_list; iter != NULL; iter = iter->next)
43  {
44  GncSxInstance *inst = (GncSxInstance*)iter->data;
45  do_test(inst->state == SX_INSTANCE_STATE_TO_CREATE, "to-create");
46  }
47  }
48 
49  xaccSchedXactionSetEndDate(one_sx, &tomorrow);
50 
51  do_test(gnc_sx_get_num_occur_daterange(one_sx, &before_yesterday, &before_yesterday) == 0, "0 occurrences before start");
52  do_test(gnc_sx_get_num_occur_daterange(one_sx, &today, &today) == 1, "1 occurrence today");
53  do_test(gnc_sx_get_num_occur_daterange(one_sx, &today, &tomorrow) == 2, "2 occurrence today and tomorrow");
54  do_test(gnc_sx_get_num_occur_daterange(one_sx, &yesterday, &tomorrow) == 3, "3 occurrences from yesterday to tomorrow");
55  do_test(gnc_sx_get_num_occur_daterange(one_sx, &tomorrow, &tomorrow) == 1, "1 occurrence tomorrow");
56  do_test(gnc_sx_get_num_occur_daterange(one_sx, &after_tomorrow, &after_tomorrow) == 0, "0 occurrence after the SX has ended");
57 
58  g_object_unref(G_OBJECT(model));
59  remove_sx(one_sx);
60 }
61 
62 static void
63 test_empty()
64 {
65  // no sxes should exist at this point.
66  int way_in_the_future_year = 2038;
67  GDate *end;
68  GncSxInstanceModel *model;
69 
70  end = g_date_new_dmy(31, 12, way_in_the_future_year);
71  model = gnc_sx_get_instances(end, TRUE);
72  do_test(g_list_length(model->sx_instance_list) == 0, "no instances");
73  g_object_unref(G_OBJECT(model));
74  g_date_free(end);
75  success("empty");
76 }
77 
78 static void
79 test_once()
80 {
81  SchedXaction *lonely;
82  GDate *when, *end;
83  int random_offset_within_one_year = 0;
84  GncSxInstanceModel *model;
85  GncSxInstances *instances;
86  GncSxInstance *instance;
87 
88  when = g_date_new();
89  g_date_clear(when, 1);
90  gnc_gdate_set_today (when);
91  while (random_offset_within_one_year == 0)
92  random_offset_within_one_year = get_random_int_in_range(-365, 365);
93  g_date_add_days(when, random_offset_within_one_year);
94 
95  end = g_date_new();
96  g_date_clear(end, 1);
97  gnc_gdate_set_today (end);
98  g_date_add_years(end, 1);
99 
100  lonely = add_once_sx("once", when);
101 
102  model = gnc_sx_get_instances(end, TRUE);
103 
104  do_test(g_list_length(model->sx_instance_list) == 1, "1 instances");
105  instances = (GncSxInstances*)model->sx_instance_list->data;
106  do_test(g_list_length(instances->instance_list) == 1, "1 instance");
107  instance = (GncSxInstance*)instances->instance_list->data;
108  do_test(g_date_compare(when, &instances->next_instance_date) == 0, "next instance is expected");
109  do_test(g_date_compare(when, &instance->date) == 0, "instance date is expected");
110 
111  g_object_unref(model);
112  success("model unref");
113  remove_sx(lonely);
114 }
115 
116 static GncSxInstance*
117 _nth_instance(GncSxInstances *instances, int i)
118 {
119  return (GncSxInstance*)g_list_nth_data(instances->instance_list, i);
120 }
121 
122 static void
123 test_state_changes()
124 {
125  SchedXaction *foo;
126  GDate *start, *end;
127  GncSxInstanceModel *model;
128  GncSxInstances *insts;
129  GncSxInstance *inst;
130 
131  start = g_date_new();
132  gnc_gdate_set_today (start);
133 
134  end = g_date_new();
135  gnc_gdate_set_today (end);
136  g_date_add_days(end, 3);
137 
138  foo = add_daily_sx("foo", start, NULL, NULL);
139  model = gnc_sx_get_instances(end, TRUE);
140 
141  do_test(g_list_length(model->sx_instance_list) == 1, "one sx");
142  insts = (GncSxInstances*)g_list_nth_data(model->sx_instance_list, 0);
143  do_test(g_list_length(insts->instance_list) == 4, "4 instances");
144 
145  inst = _nth_instance(insts, 2);
146  gnc_sx_instance_model_change_instance_state(model, inst, SX_INSTANCE_STATE_TO_CREATE);
147  {
148  int i;
149  for (i = 0; i < 4; i++)
150  {
151  inst = _nth_instance(insts, i);
152  do_test(inst->state == SX_INSTANCE_STATE_TO_CREATE, "0 didn't change");
153  }
154  success("nothing else changed");
155  }
156 
157  inst = _nth_instance(insts, 1);
158  gnc_sx_instance_model_change_instance_state(model, inst, SX_INSTANCE_STATE_POSTPONED);
159  {
160  int i;
161  inst = _nth_instance(insts, 1);
162  do_test(inst->state == SX_INSTANCE_STATE_POSTPONED, "as we said");
163  for (i = 0; i < 4; i++)
164  {
165  if (i == 1)
166  continue; // skip
167  inst = _nth_instance(insts, i);
168  do_test(inst->state == SX_INSTANCE_STATE_TO_CREATE, "still to create");
169  }
170  }
171  success("postponed changed what it needed to");
172 
173  inst = _nth_instance(insts, 1);
174  gnc_sx_instance_model_change_instance_state(model, inst, SX_INSTANCE_STATE_REMINDER);
175  success("changed to reminder");
176  {
177  int i;
178  inst = _nth_instance(insts, 0);
179  do_test(inst->state == SX_INSTANCE_STATE_TO_CREATE, "left alone");
180  inst = _nth_instance(insts, 1);
181  do_test(inst->state == SX_INSTANCE_STATE_REMINDER, "as we asked for");
182  for (i = 2; i < 4; i++)
183  {
184  inst = _nth_instance(insts, i);
185  do_test(inst->state == SX_INSTANCE_STATE_REMINDER, "changed as well");
186  }
187  }
188 
189  g_object_unref(model);
190  remove_sx(foo);
191 }
192 
193 int
194 main(int argc, char **argv)
195 {
196  g_setenv ("GNC_UNINSTALLED", "1", TRUE);
197  qof_init();
198  gnc_engine_init(0, NULL);
199 
200  test_empty();
201  {
202  int i;
203  for (i = 0; i < 10; i++)
204  test_once();
205  }
206  test_basic();
207  test_state_changes();
208 
209  print_test_results();
210  exit(get_rv());
211 }
void gnc_gdate_set_today(GDate *gd)
void gnc_sx_instance_model_change_instance_state(GncSxInstanceModel *model, GncSxInstance *instance, GncSxInstanceState new_state)
utility functions for the GnuCash UI
Anchor Scheduled Transaction info in a book. See src/doc/books.txt for design overview.
void gnc_engine_init(int argc, char **argv)
Definition: gnc-engine.c:139
gint gnc_sx_get_num_occur_daterange(const SchedXaction *sx, const GDate *start_date, const GDate *end_date)
Definition: SchedXaction.c:726
GDate helper routines.
GncSxInstanceModel * gnc_sx_get_instances(const GDate *range_end, gboolean include_disabled)
GncSxInstanceState state
void qof_init(void)
Initialise the Query Object Framework.
void xaccSchedXactionSetEndDate(SchedXaction *sx, const GDate *newEnd)
Definition: SchedXaction.c:635