No issues found
1 /*
2 * Copyright (C) 2009, Nokia <ivan.frade@nokia.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library 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 GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include "config.h"
21
22 #include <string.h>
23
24 #include <glib.h>
25 #include <glib/gstdio.h>
26 #include <gio/gio.h>
27
28 #include <libtracker-common/tracker-ontologies.h>
29
30 #include <libtracker-data/tracker-data-manager.h>
31 #include <libtracker-data/tracker-data-query.h>
32 #include <libtracker-data/tracker-data-update.h>
33 #include <libtracker-data/tracker-data.h>
34 #include <libtracker-data/tracker-sparql-query.h>
35
36 typedef struct _TestInfo TestInfo;
37
38 struct _TestInfo {
39 const gchar *test_name;
40 gint number_of_queries;
41 };
42
43 const TestInfo tests[] = {
44 { "fts3aa", 2 },
45 { "fts3ae", 1 },
46 { "prefix/fts3prefix", 3 },
47 { "limits/fts3limits", 4 },
48 { NULL }
49 };
50
51 static void
52 test_sparql_query (gconstpointer test_data)
53 {
54 TrackerDBCursor *cursor;
55 const TestInfo *test_info;
56 GError *error;
57 GString *test_results;
58 gchar *update, *update_filename;
59 gchar *query, *query_filename;
60 gchar *results, *results_filename;
61 gchar *prefix, *data_prefix, *test_prefix;
62 gint i;
63 const gchar *test_schemas[2] = { NULL, NULL };
64
65 error = NULL;
66 test_info = test_data;
67
68 /* initialization */
69 prefix = g_build_path (G_DIR_SEPARATOR_S, TOP_SRCDIR, "tests", "libtracker-fts", NULL);
70 data_prefix = g_build_filename (prefix, "data", NULL);
71 test_prefix = g_build_filename (prefix, test_info->test_name, NULL);
72 g_free (prefix);
73
74 test_schemas[0] = data_prefix;
75 tracker_db_journal_set_rotating (FALSE, G_MAXSIZE, NULL);
76 tracker_data_manager_init (TRACKER_DB_MANAGER_FORCE_REINDEX,
77 test_schemas,
78 NULL, FALSE, FALSE,
79 100, 100, NULL, NULL, NULL, &error);
80
81 g_assert_no_error (error);
82
83 /* load data / perform updates */
84
85 update_filename = g_strconcat (test_prefix, "-data.rq", NULL);
86 g_file_get_contents (update_filename, &update, NULL, &error);
87 g_assert_no_error (error);
88
89 tracker_data_update_sparql (update, &error);
90 g_assert_no_error (error);
91
92 g_free (update_filename);
93 g_free (update);
94
95 /* perform queries */
96
97 for (i = 1; i <= test_info->number_of_queries; i++) {
98 query_filename = g_strdup_printf ("%s-%d.rq", test_prefix, i);
99 g_file_get_contents (query_filename, &query, NULL, &error);
100 g_assert_no_error (error);
101
102 results_filename = g_strdup_printf ("%s-%d.out", test_prefix, i);
103 g_file_get_contents (results_filename, &results, NULL, &error);
104 g_assert_no_error (error);
105
106 cursor = tracker_data_query_sparql_cursor (query, &error);
107 g_assert_no_error (error);
108
109 /* compare results with reference output */
110
111 test_results = g_string_new ("");
112
113 if (cursor) {
114 gint col;
115
116 while (tracker_db_cursor_iter_next (cursor, NULL, &error)) {
117 for (col = 0; col < tracker_db_cursor_get_n_columns (cursor); col++) {
118 const gchar *str;
119
120 if (col > 0) {
121 g_string_append (test_results, "\t");
122 }
123
124 str = tracker_db_cursor_get_string (cursor, col, NULL);
125 if (str != NULL) {
126 /* bound variable */
127 g_string_append_printf (test_results, "\"%s\"", str);
128 }
129 }
130
131 g_string_append (test_results, "\n");
132 }
133
134 g_object_unref (cursor);
135 }
136
137 if (strcmp (results, test_results->str)) {
138 /* print result difference */
139 gchar *quoted_results;
140 gchar *command_line;
141 gchar *quoted_command_line;
142 gchar *shell;
143 gchar *diff;
144
145 quoted_results = g_shell_quote (test_results->str);
146 command_line = g_strdup_printf ("echo -n %s | diff -u %s -", quoted_results, results_filename);
147 quoted_command_line = g_shell_quote (command_line);
148 shell = g_strdup_printf ("sh -c %s", quoted_command_line);
149 g_spawn_command_line_sync (shell, &diff, NULL, NULL, &error);
150 g_assert_no_error (error);
151
152 g_error ("%s", diff);
153
154 g_free (quoted_results);
155 g_free (command_line);
156 g_free (quoted_command_line);
157 g_free (shell);
158 g_free (diff);
159 }
160
161 /* cleanup */
162
163 g_free (query_filename);
164 g_free (query);
165 g_free (results_filename);
166 g_free (results);
167 g_string_free (test_results, TRUE);
168 }
169
170 g_free (data_prefix);
171 g_free (test_prefix);
172
173 tracker_data_manager_shutdown ();
174 }
175
176 int
177 main (int argc, char **argv)
178 {
179 gint result;
180 gint i;
181 gchar *current_dir;
182 gchar *path;
183
184 g_test_init (&argc, &argv, NULL);
185
186 current_dir = g_get_current_dir ();
187
188 g_setenv ("XDG_DATA_HOME", current_dir, TRUE);
189 g_setenv ("XDG_CACHE_HOME", current_dir, TRUE);
190 g_setenv ("TRACKER_DB_ONTOLOGIES_DIR", TOP_SRCDIR "/data/ontologies/", TRUE);
191 g_setenv ("TRACKER_FTS_STOP_WORDS", "0", TRUE);
192
193 g_free (current_dir);
194
195 /* add test cases */
196 for (i = 0; tests[i].test_name; i++) {
197 gchar *testpath;
198
199 testpath = g_strconcat ("/libtracker-fts/", tests[i].test_name, NULL);
200 g_test_add_data_func (testpath, &tests[i], test_sparql_query);
201 g_free (testpath);
202 }
203
204 /* run tests */
205 result = g_test_run ();
206
207 /* clean up */
208 g_print ("Removing temporary data\n");
209 g_spawn_command_line_sync ("rm -R tracker/", NULL, NULL, NULL, NULL);
210
211 path = g_build_filename (TOP_BUILDDIR, "tests", "libtracker-fts", "dconf", "user", NULL);
212 g_unlink (path);
213 g_free (path);
214
215 return result;
216 }