|
GnuCash 2.4.99
|
00001 #include "config.h" 00002 #include <stdlib.h> 00003 #include <glib.h> 00004 #include "SX-book.h" 00005 #include "gnc-sx-instance-model.h" 00006 #include "gnc-ui-util.h" 00007 00008 #include "test-stuff.h" 00009 #include "test-engine-stuff.h" 00010 00011 static void 00012 test_basic() 00013 { 00014 GncSxInstanceModel *model; 00015 GDate yesterday, today, tomorrow, before_yesterday, after_tomorrow; 00016 SchedXaction *one_sx; 00017 00018 g_date_clear(&today, 1); 00019 g_date_set_time_t(&today, time(NULL)); 00020 00021 yesterday = today; 00022 g_date_subtract_days(&yesterday, 1); 00023 before_yesterday = yesterday; 00024 g_date_subtract_days(&before_yesterday, 1); 00025 tomorrow = today; 00026 g_date_add_days(&tomorrow, 1); 00027 after_tomorrow = tomorrow; 00028 g_date_add_days(&after_tomorrow, 1); 00029 00030 one_sx = add_daily_sx("foo", &yesterday, NULL, NULL); 00031 00032 model = gnc_sx_get_instances(&tomorrow, TRUE); 00033 00034 { 00035 GncSxInstances *insts; 00036 GList *iter; 00037 00038 do_test(g_list_length(model->sx_instance_list) == 1, "1 GncSxInstances"); 00039 insts = (GncSxInstances*)model->sx_instance_list->data; 00040 do_test(g_list_length(insts->instance_list) == 3, "yesterday, today and tomorrow"); 00041 for (iter = insts->instance_list; iter != NULL; iter = iter->next) 00042 { 00043 GncSxInstance *inst = (GncSxInstance*)iter->data; 00044 do_test(inst->state == SX_INSTANCE_STATE_TO_CREATE, "to-create"); 00045 } 00046 } 00047 00048 xaccSchedXactionSetEndDate(one_sx, &tomorrow); 00049 00050 do_test(gnc_sx_get_num_occur_daterange(one_sx, &before_yesterday, &before_yesterday) == 0, "0 occurrences before start"); 00051 do_test(gnc_sx_get_num_occur_daterange(one_sx, &today, &today) == 1, "1 occurrence today"); 00052 do_test(gnc_sx_get_num_occur_daterange(one_sx, &today, &tomorrow) == 2, "2 occurrence today and tomorrow"); 00053 do_test(gnc_sx_get_num_occur_daterange(one_sx, &yesterday, &tomorrow) == 3, "3 occurrences from yesterday to tomorrow"); 00054 do_test(gnc_sx_get_num_occur_daterange(one_sx, &tomorrow, &tomorrow) == 1, "1 occurrence tomorrow"); 00055 do_test(gnc_sx_get_num_occur_daterange(one_sx, &after_tomorrow, &after_tomorrow) == 0, "0 occurrence after the SX has ended"); 00056 00057 g_object_unref(G_OBJECT(model)); 00058 remove_sx(one_sx); 00059 } 00060 00061 static void 00062 test_empty() 00063 { 00064 // no sxes should exist at this point. 00065 int way_in_the_future_year = 2038; 00066 GDate *end; 00067 GncSxInstanceModel *model; 00068 00069 end = g_date_new_dmy(31, 12, way_in_the_future_year); 00070 model = gnc_sx_get_instances(end, TRUE); 00071 do_test(g_list_length(model->sx_instance_list) == 0, "no instances"); 00072 g_object_unref(G_OBJECT(model)); 00073 g_date_free(end); 00074 success("empty"); 00075 } 00076 00077 static void 00078 test_once() 00079 { 00080 SchedXaction *lonely; 00081 GDate *when, *end; 00082 int random_offset_within_one_year = 0; 00083 GncSxInstanceModel *model; 00084 GncSxInstances *instances; 00085 GncSxInstance *instance; 00086 00087 when = g_date_new(); 00088 g_date_clear(when, 1); 00089 g_date_set_time_t(when, time(NULL)); 00090 while (random_offset_within_one_year == 0) 00091 random_offset_within_one_year = get_random_int_in_range(-365, 365); 00092 g_date_add_days(when, random_offset_within_one_year); 00093 00094 end = g_date_new(); 00095 g_date_clear(end, 1); 00096 g_date_set_time_t(end, time(NULL)); 00097 g_date_add_years(end, 1); 00098 00099 lonely = add_once_sx("once", when); 00100 00101 model = gnc_sx_get_instances(end, TRUE); 00102 00103 do_test(g_list_length(model->sx_instance_list) == 1, "1 instances"); 00104 instances = (GncSxInstances*)model->sx_instance_list->data; 00105 do_test(g_list_length(instances->instance_list) == 1, "1 instance"); 00106 instance = (GncSxInstance*)instances->instance_list->data; 00107 do_test(g_date_compare(when, &instances->next_instance_date) == 0, "next instance is expected"); 00108 do_test(g_date_compare(when, &instance->date) == 0, "instance date is expected"); 00109 00110 g_object_unref(model); 00111 success("model unref"); 00112 remove_sx(lonely); 00113 } 00114 00115 static GncSxInstance* 00116 _nth_instance(GncSxInstances *instances, int i) 00117 { 00118 return (GncSxInstance*)g_list_nth_data(instances->instance_list, i); 00119 } 00120 00121 static void 00122 test_state_changes() 00123 { 00124 SchedXaction *foo; 00125 GDate *start, *end; 00126 GncSxInstanceModel *model; 00127 GncSxInstances *insts; 00128 GncSxInstance *inst; 00129 00130 start = g_date_new(); 00131 g_date_set_time_t(start, time(NULL)); 00132 00133 end = g_date_new(); 00134 g_date_set_time_t(end, time(NULL)); 00135 g_date_add_days(end, 3); 00136 00137 foo = add_daily_sx("foo", start, NULL, NULL); 00138 model = gnc_sx_get_instances(end, TRUE); 00139 00140 do_test(g_list_length(model->sx_instance_list) == 1, "one sx"); 00141 insts = (GncSxInstances*)g_list_nth_data(model->sx_instance_list, 0); 00142 do_test(g_list_length(insts->instance_list) == 4, "4 instances"); 00143 00144 inst = _nth_instance(insts, 2); 00145 gnc_sx_instance_model_change_instance_state(model, inst, SX_INSTANCE_STATE_TO_CREATE); 00146 { 00147 int i; 00148 for (i = 0; i < 4; i++) 00149 { 00150 inst = _nth_instance(insts, i); 00151 do_test(inst->state == SX_INSTANCE_STATE_TO_CREATE, "0 didn't change"); 00152 } 00153 success("nothing else changed"); 00154 } 00155 00156 inst = _nth_instance(insts, 1); 00157 gnc_sx_instance_model_change_instance_state(model, inst, SX_INSTANCE_STATE_POSTPONED); 00158 { 00159 int i; 00160 inst = _nth_instance(insts, 1); 00161 do_test(inst->state == SX_INSTANCE_STATE_POSTPONED, "as we said"); 00162 for (i = 0; i < 4; i++) 00163 { 00164 if (i == 1) 00165 continue; // skip 00166 inst = _nth_instance(insts, i); 00167 do_test(inst->state == SX_INSTANCE_STATE_TO_CREATE, "still to create"); 00168 } 00169 } 00170 success("postponed changed what it needed to"); 00171 00172 inst = _nth_instance(insts, 1); 00173 gnc_sx_instance_model_change_instance_state(model, inst, SX_INSTANCE_STATE_REMINDER); 00174 success("changed to reminder"); 00175 { 00176 int i; 00177 inst = _nth_instance(insts, 0); 00178 do_test(inst->state == SX_INSTANCE_STATE_TO_CREATE, "left alone"); 00179 inst = _nth_instance(insts, 1); 00180 do_test(inst->state == SX_INSTANCE_STATE_REMINDER, "as we asked for"); 00181 for (i = 2; i < 4; i++) 00182 { 00183 inst = _nth_instance(insts, i); 00184 do_test(inst->state == SX_INSTANCE_STATE_REMINDER, "changed as well"); 00185 } 00186 } 00187 00188 g_object_unref(model); 00189 remove_sx(foo); 00190 } 00191 00192 int 00193 main(int argc, char **argv) 00194 { 00195 g_setenv ("GNC_UNINSTALLED", "1", TRUE); 00196 g_type_init(); 00197 qof_init(); 00198 gnc_engine_init(0, NULL); 00199 00200 test_empty(); 00201 { 00202 int i; 00203 for (i = 0; i < 10; i++) 00204 test_once(); 00205 } 00206 test_basic(); 00207 test_state_changes(); 00208 00209 print_test_results(); 00210 exit(get_rv()); 00211 }
1.7.4