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-data/tracker-data.h>
29 #include <libtracker-sparql/tracker-sparql.h>
30 
31 typedef struct _TestInfo TestInfo;
32 
33 struct _TestInfo {
34 	const gchar *test_name;
35 	gint number_of_queries;
36 };
37 
38 const TestInfo tests[] = {
39 	{ "fts3aa", 3 },
40 	{ "fts3ae", 1 },
41 	{ "prefix/fts3prefix", 3 },
42 	{ "limits/fts3limits", 4 },
43 	{ "input/fts3input", 3 },
44 	{ NULL }
45 };
46 
47 static void
test_sparql_query(gconstpointer test_data)48 test_sparql_query (gconstpointer test_data)
49 {
50 	TrackerSparqlCursor *cursor;
51 	const TestInfo *test_info;
52 	GError *error;
53 	GString *test_results;
54 	gchar *update, *update_filename;
55 	gchar *query, *query_filename;
56 	gchar *results, *results_filename;
57 	gchar *prefix, *test_prefix;
58 	GFile *ontology, *data_location;
59 	TrackerSparqlConnection *conn;
60 	gchar *rm_command, *path;
61 	const gchar *datadir;
62 	gint i;
63 
64 	error = NULL;
65 	test_info = test_data;
66 
67 	/* initialization */
68 	prefix = g_build_path (G_DIR_SEPARATOR_S, TOP_SRCDIR, "tests", "libtracker-fts", NULL);
69 	test_prefix = g_build_filename (prefix, test_info->test_name, NULL);
70 	ontology = g_file_new_for_path (prefix);
71 	g_free (prefix);
72 
73 	path = g_build_filename (g_get_tmp_dir (), "tracker-fts-test-XXXXXX", NULL);
74 	datadir = g_mkdtemp_full (path, 0700);
75 
76 	data_location = g_file_new_for_path (datadir);
77 
78 	conn = tracker_sparql_connection_new (TRACKER_SPARQL_CONNECTION_FLAGS_NONE,
79 	                                      data_location, ontology,
80 	                                      NULL, &error);
81 	g_assert_no_error (error);
82 
83 	g_object_unref (ontology);
84 	g_object_unref (data_location);
85 
86 	/* load data / perform updates */
87 
88 	update_filename = g_strconcat (test_prefix, "-data.rq", NULL);
89 	g_file_get_contents (update_filename, &update, NULL, &error);
90 	g_assert_no_error (error);
91 
92 	tracker_sparql_connection_update (conn, update, NULL, &error);
93 	g_assert_no_error (error);
94 
95 	g_free (update_filename);
96 	g_free (update);
97 
98 	/* perform queries */
99 
100 	for (i = 1; i <= test_info->number_of_queries; i++) {
101 		query_filename = g_strdup_printf ("%s-%d.rq", test_prefix, i);
102 		g_file_get_contents (query_filename, &query, NULL, &error);
103 		g_assert_no_error (error);
104 
105 		results_filename = g_strdup_printf ("%s-%d.out", test_prefix, i);
106 		g_file_get_contents (results_filename, &results, NULL, &error);
107 		g_assert_no_error (error);
108 
109 		cursor = tracker_sparql_connection_query (conn, query, NULL, &error);
110 		g_assert_no_error (error);
111 
112 		/* compare results with reference output */
113 
114 		test_results = g_string_new ("");
115 
116 		if (cursor) {
117 			gint col;
118 
119 			while (tracker_sparql_cursor_next (cursor, NULL, &error)) {
120 				for (col = 0; col < tracker_sparql_cursor_get_n_columns (cursor); col++) {
121 					const gchar *str;
122 
123 					if (col > 0) {
124 						g_string_append (test_results, "\t");
125 					}
126 
127 					str = tracker_sparql_cursor_get_string (cursor, col, NULL);
128 					if (str != NULL) {
129 						/* bound variable */
130 						g_string_append_printf (test_results, "\"%s\"", str);
131 					}
132 				}
133 
134 				g_string_append (test_results, "\n");
135 			}
136 
137 			g_object_unref (cursor);
138 		}
139 
140 		if (strcmp (results, test_results->str)) {
141 			/* print result difference */
142 			gchar *quoted_results;
143 			gchar *command_line;
144 			gchar *quoted_command_line;
145 			gchar *shell;
146 			gchar *diff;
147 
148 			quoted_results = g_shell_quote (test_results->str);
149 			command_line = g_strdup_printf ("echo -n %s | diff -u %s -", quoted_results, results_filename);
150 			quoted_command_line = g_shell_quote (command_line);
151 			shell = g_strdup_printf ("sh -c %s", quoted_command_line);
152 			g_spawn_command_line_sync (shell, &diff, NULL, NULL, &error);
153 			g_assert_no_error (error);
154 
155 			g_error ("%s", diff);
156 
157 			g_free (quoted_results);
158 			g_free (command_line);
159 			g_free (quoted_command_line);
160 			g_free (shell);
161 			g_free (diff);
162 		}
163 
164 		/* cleanup */
165 
166 		g_free (query_filename);
167 		g_free (query);
168 		g_free (results_filename);
169 		g_free (results);
170 		g_string_free (test_results, TRUE);
171 	}
172 
173 	g_free (test_prefix);
174 	g_object_unref (conn);
175 
176 	/* clean up */
177 	rm_command = g_strdup_printf ("rm -R %s", datadir);
178 	g_spawn_command_line_sync (rm_command, NULL, NULL, NULL, NULL);
179 	g_free (rm_command);
180 	g_free (path);
181 }
182 
183 int
main(int argc,char ** argv)184 main (int argc, char **argv)
185 {
186 	gint result;
187 	gint i;
188 	gchar *path;
189 
190 	g_test_init (&argc, &argv, NULL);
191 
192 	/* add test cases */
193 	for (i = 0; tests[i].test_name; i++) {
194 		gchar *testpath;
195 
196 		testpath = g_strconcat ("/libtracker-fts/", tests[i].test_name, NULL);
197 		g_test_add_data_func (testpath, &tests[i], test_sparql_query);
198 		g_free (testpath);
199 	}
200 
201 	/* run tests */
202 	result = g_test_run ();
203 
204 	path = g_build_filename (TOP_BUILDDIR, "tests", "libtracker-fts", "dconf", "user", NULL);
205 	g_unlink (path);
206 	g_free (path);
207 
208 	return result;
209 }
210