1 /*
2 * Copyright (C) 2011 Vivien Malerba <malerba@gnome-db.org>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 #include <stdlib.h>
19 #include <string.h>
20 #include <glib.h>
21 #include <libgda/libgda.h>
22
23 #define fail(x) g_warning (x)
24 #define fail_if(x,y) if (x) g_warning (y)
25 #define fail_unless(x,y) if (!(x)) g_warning (y)
26
27 static GdaDataModel *get_source_model (GdaConnection *cnc);
28
29 static gint test_field_formats (GdaDataPivot *pivot);
30 static gint test_column_formats (GdaDataPivot *pivot);
31 static gint test_on_data (GdaConnection *cnc);
32
33 int
main(int argc,char ** argv)34 main (int argc, char **argv)
35 {
36 int number_failed = 0;
37 GdaDataModel *source;
38 GdaDataPivot *pivot;
39 gchar *fname;
40 GdaConnection *cnc;
41
42 gda_init ();
43
44 /* open connection */
45 gchar *cnc_string;
46 fname = g_build_filename (ROOT_DIR, "tests", "data-models", NULL);
47 cnc_string = g_strdup_printf ("DB_DIR=%s;DB_NAME=pivot", fname);
48 g_free (fname);
49 cnc = gda_connection_open_from_string ("SQLite", cnc_string, NULL,
50 GDA_CONNECTION_OPTIONS_READ_ONLY, NULL);
51 if (!cnc) {
52 g_print ("Failed to open connection, cnc_string = %s\n", cnc_string);
53 exit (1);
54 }
55 g_free (cnc_string);
56
57 source = get_source_model (cnc);
58 pivot = GDA_DATA_PIVOT (gda_data_pivot_new (source));
59 g_object_unref (source);
60 number_failed += test_field_formats (pivot);
61 number_failed += test_column_formats (pivot);
62 g_object_unref (pivot);
63
64 number_failed += test_on_data (cnc);
65
66 /* end of tests */
67 g_object_unref (cnc);
68 if (number_failed == 0)
69 g_print ("Ok.\n");
70 else
71 g_print ("%d failed\n", number_failed);
72
73 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
74 }
75
76 static GdaDataModel *
get_source_model(GdaConnection * cnc)77 get_source_model (GdaConnection *cnc)
78 {
79 GdaDataModel *model;
80 GdaStatement *stmt;
81 stmt = gda_connection_parse_sql_string (cnc,
82 "select * from food",
83 NULL, NULL);
84 g_assert (stmt);
85
86 model = gda_connection_statement_execute_select (cnc, stmt, NULL, NULL);
87 g_object_unref (stmt);
88 g_print ("==== Source data:\n");
89 gda_data_model_dump (model, NULL);
90 return model;
91 }
92
93 typedef struct {
94 gchar *field;
95 gchar *alias;
96 gboolean result;
97 } AFieldFormat;
98
99 AFieldFormat fields_test [] = {
100 {"person", "alias", TRUE},
101 {"person", NULL, TRUE},
102 {"name", NULL, FALSE},
103 {"person)", NULL, FALSE},
104 {"person || qty", NULL, TRUE},
105 {"person, qty", NULL, TRUE},
106 };
107
108 static gint
test_field_formats(GdaDataPivot * pivot)109 test_field_formats (GdaDataPivot *pivot)
110 {
111 guint i, nfailed = 0;
112 GError *error = NULL;
113 for (i = 0; i < sizeof (fields_test) / sizeof (AFieldFormat); i++) {
114 AFieldFormat *test = &(fields_test [i]);
115 gboolean res;
116 res = gda_data_pivot_add_field (pivot, GDA_DATA_PIVOT_FIELD_ROW,
117 test->field, test->alias, &error);
118 if (res != test->result) {
119 if (test->result)
120 g_print ("Error: field setting failed for [%s][%s] when "
121 "it should have succedded, error: [%s]\n",
122 test->field, test->alias,
123 error && error->message ? error->message : "no detail");
124 else
125 g_print ("Error: field setting succedded for [%s][%s] when "
126 "it should have failed\n",
127 test->field, test->alias);
128 nfailed ++;
129 }
130 g_clear_error (&error);
131 }
132 return nfailed;
133 }
134
135 static gint
test_column_formats(GdaDataPivot * pivot)136 test_column_formats (GdaDataPivot *pivot)
137 {
138 guint i, nfailed = 0;
139 GError *error = NULL;
140 for (i = 0; i < sizeof (fields_test) / sizeof (AFieldFormat); i++) {
141 AFieldFormat *test = &(fields_test [i]);
142 gboolean res;
143 res = gda_data_pivot_add_field (pivot, GDA_DATA_PIVOT_FIELD_COLUMN,
144 test->field, test->alias, &error);
145 if (res != test->result) {
146 if (test->result)
147 g_print ("Error: field setting failed for [%s][%s] when "
148 "it should have succedded, error: [%s]\n",
149 test->field, test->alias,
150 error && error->message ? error->message : "no detail");
151 else
152 g_print ("Error: field setting succedded for [%s][%s] when "
153 "it should have failed\n",
154 test->field, test->alias);
155 nfailed ++;
156 }
157 g_clear_error (&error);
158 }
159 return nfailed;
160 }
161
162 static gint
test_on_data(GdaConnection * cnc)163 test_on_data (GdaConnection *cnc)
164 {
165 GdaDataModel *source;
166 GdaDataPivot *pivot;
167 GError *error = NULL;
168 gint number_failed = 0;
169
170 source = get_source_model (cnc);
171 pivot = GDA_DATA_PIVOT (gda_data_pivot_new (source));
172 g_object_unref (source);
173
174 gda_data_pivot_add_field (pivot, GDA_DATA_PIVOT_FIELD_ROW,
175 //"person,week",
176 "person",
177 NULL, &error);
178 gda_data_pivot_add_field (pivot, GDA_DATA_PIVOT_FIELD_COLUMN,
179 "food",
180 //"CASE food WHEN 'Car' THEN 'Not food' ELSE 'food' END",
181 NULL, &error);
182
183 gda_data_pivot_add_data (pivot, GDA_DATA_PIVOT_SUM,
184 "qty", "sumqty", &error);
185
186 gda_data_pivot_add_data (pivot, GDA_DATA_PIVOT_COUNT,
187 "food", "thefood", &error);
188
189 if (! gda_data_pivot_populate (pivot, &error)) {
190 g_print ("Failed to populate pivot: %s\n",
191 error && error->message ? error->message : "no detail");
192 number_failed ++;
193 goto out;
194 }
195
196 gda_data_model_dump (GDA_DATA_MODEL (pivot), NULL);
197
198 out:
199 g_object_unref (pivot);
200 return number_failed;
201 }
202