1 /* -*- Mode: c; c-basic-offset: 2 -*-
2 *
3 * rasqal_graph_test.c - Rasqal RDF Query GRAPH Tests
4 *
5 * Copyright (C) 2006, David Beckett http://purl.org/net/dajobe/
6 *
7 * This package is Free Software and part of Redland http://librdf.org/
8 *
9 * It is licensed under the following three licenses as alternatives:
10 * 1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
11 * 2. GNU General Public License (GPL) V2 or any newer version
12 * 3. Apache License, V2.0 or any newer version
13 *
14 * You may not use this file except in compliance with at least one of
15 * the above three licenses.
16 *
17 * See LICENSE.html or LICENSE.txt at the top of this package for the
18 * complete terms and further detail along with the license texts for
19 * the licenses in COPYING.LIB, COPYING and LICENSE-2.0.txt respectively.
20 *
21 *
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <rasqal_config.h>
26 #endif
27
28 #ifdef WIN32
29 #include <win32_rasqal_config.h>
30 #endif
31
32 #include <stdio.h>
33 #include <string.h>
34 #ifdef HAVE_STDLIB_H
35 #include <stdlib.h>
36 #endif
37 #include <stdarg.h>
38
39 #include "rasqal.h"
40 #include "rasqal_internal.h"
41
42 #ifdef RASQAL_QUERY_SPARQL
43
44
45 #define DATA_GRAPH_COUNT 3
46 static const char* graph_files[DATA_GRAPH_COUNT] = {
47 "graph-a.ttl",
48 "graph-b.ttl",
49 "graph-c.ttl"
50 };
51
52
53 #define QUERY_VARIABLES_MAX_COUNT 3
54
55 struct test
56 {
57 const char *query_language;
58 const char *query_string;
59 /* expected result count */
60 int expected_count;
61 /* data graph offsets (<0 for not used) */
62 int data_graphs[DATA_GRAPH_COUNT];
63 /* expected value answers */
64 const char* value_answers[QUERY_VARIABLES_MAX_COUNT];
65 /* expected graph answers */
66 int graph_answers[QUERY_VARIABLES_MAX_COUNT];
67 const char* value_var;
68 const char* graph_var;
69 };
70
71
72
73 #define QUERY_COUNT 2
74
75
76 static const struct test tests[QUERY_COUNT] = {
77 { /* query_language */ "sparql",
78 /* query_string */ "\
79 PREFIX : <http://example.org/>\
80 SELECT ?graph ?value \
81 WHERE\
82 {\
83 GRAPH ?graph { :x :b ?value } \
84 }\
85 ",
86 /* expected_count */ 2,
87 /* data_graphs */ { 0, 1, -1 },
88 /* value_answers */ { "mercury", "orange" },
89 /* graph_answers */ { 0, 1 },
90 /* value_var */ "value",
91 /* graph_var */ "graph",
92 },
93 { /* query_language */ "sparql",
94 /* query_string */ "\
95 PREFIX : <http://example.org/>\
96 SELECT ?graph ?value \
97 WHERE\
98 {\
99 GRAPH ?graph { ?var :a ?value } \
100 }\
101 ",
102 /* expected_count */ 3,
103 /* data_graphs */ { 0, 1, 2 },
104 /* value_answers */ { "venus", "apple", "red" },
105 /* graph_answers */ { 0, 1, 2 },
106 /* value_var */ "value",
107 /* graph_var */ "graph",
108 }
109 };
110
111
112 #else
113 #define NO_QUERY_LANGUAGE
114 #endif
115
116
117 #ifdef NO_QUERY_LANGUAGE
118 int
main(int argc,char ** argv)119 main(int argc, char **argv) {
120 const char *program=rasqal_basename(argv[0]);
121 fprintf(stderr, "%s: No supported query language available, skipping test\n", program);
122 return(0);
123 }
124 #else
125
126 int
main(int argc,char ** argv)127 main(int argc, char **argv) {
128 const char *program=rasqal_basename(argv[0]);
129 int failures=0;
130 raptor_uri *base_uri;
131 unsigned char *data_dir_string;
132 raptor_uri* data_dir_uri;
133 unsigned char *uri_string;
134 int i;
135 raptor_uri* graph_uris[DATA_GRAPH_COUNT];
136 rasqal_world *world;
137
138 if(argc != 2) {
139 fprintf(stderr, "USAGE: %s <path to data directory>\n", program);
140 return(1);
141 }
142
143 world=rasqal_new_world();
144 if(!world || rasqal_world_open(world)) {
145 fprintf(stderr, "%s: rasqal_world init failed\n", program);
146 return(1);
147 }
148
149 uri_string=raptor_uri_filename_to_uri_string("");
150 base_uri = raptor_new_uri(world->raptor_world_ptr, uri_string);
151 raptor_free_memory(uri_string);
152
153
154 data_dir_string=raptor_uri_filename_to_uri_string(argv[1]);
155 data_dir_uri = raptor_new_uri(world->raptor_world_ptr, data_dir_string);
156
157 for(i=0; i < DATA_GRAPH_COUNT; i++)
158 #ifdef RAPTOR_V2_AVAILABLE
159 graph_uris[i] = raptor_new_uri_relative_to_base(world->raptor_world_ptr,
160 data_dir_uri,
161 (const unsigned char*)graph_files[i]);
162 #else
163 graph_uris[i] = raptor_new_uri_relative_to_base(data_dir_uri,
164 (const unsigned char*)graph_files[i]);
165 #endif
166
167 for(i=0; i < QUERY_COUNT; i++) {
168 rasqal_query *query = NULL;
169 rasqal_query_results *results = NULL;
170 const char *query_language_name=tests[i].query_language;
171 const unsigned char *query_string=(const unsigned char *)tests[i].query_string;
172 int count;
173 int query_failed=0;
174 int j;
175
176 query=rasqal_new_query(world, query_language_name, NULL);
177 if(!query) {
178 fprintf(stderr, "%s: creating query %d in language %s FAILED\n",
179 program, i, query_language_name);
180 query_failed=1;
181 goto tidy_query;
182 }
183
184 printf("%s: preparing %s query %d\n", program, query_language_name, i);
185 if(rasqal_query_prepare(query, query_string, base_uri)) {
186 fprintf(stderr, "%s: %s query prepare %d FAILED\n", program,
187 query_language_name, i);
188 query_failed=1;
189 goto tidy_query;
190 }
191
192 for(j=0; j < DATA_GRAPH_COUNT; j++) {
193 int offset=tests[i].data_graphs[j];
194 if(offset >= 0) {
195 rasqal_data_graph* dg;
196 dg = rasqal_new_data_graph_from_uri(world,
197 /* source URI */ graph_uris[offset],
198 /* name URI */ graph_uris[offset],
199 RASQAL_DATA_GRAPH_NAMED,
200 NULL, NULL, NULL);
201 rasqal_query_add_data_graph(query, dg);
202 }
203 }
204
205
206 printf("%s: executing query %d\n", program, i);
207 results=rasqal_query_execute(query);
208 if(!results) {
209 fprintf(stderr, "%s: query execution %d FAILED\n", program, i);
210 query_failed=1;
211 goto tidy_query;
212 }
213
214 printf("%s: checking query %d results\n", program, i);
215 count=0;
216 query_failed=0;
217 while(results && !rasqal_query_results_finished(results)) {
218 rasqal_literal *value;
219 rasqal_literal *graph_value;
220 raptor_uri* graph_uri;
221 const char *value_answer=tests[i].value_answers[count];
222 raptor_uri* graph_answer=graph_uris[tests[i].graph_answers[count]];
223 const unsigned char* graph_var=(const unsigned char*)tests[i].graph_var;
224 const unsigned char* value_var=(const unsigned char*)tests[i].value_var;
225
226 value=rasqal_query_results_get_binding_value_by_name(results,
227 value_var);
228 if(strcmp((const char*)rasqal_literal_as_string(value), value_answer)) {
229 printf("result %d FAILED: %s=", count, (char*)value_var);
230 rasqal_literal_print(value, stdout);
231 printf(" expected value '%s'\n", value_answer);
232 query_failed=1;
233 break;
234 }
235
236 graph_value=rasqal_query_results_get_binding_value_by_name(results,
237 graph_var);
238 if(!graph_value) {
239 printf("variable '%s' is not in the result\n", graph_var);
240 query_failed=1;
241 break;
242 }
243
244 if(graph_value->type != RASQAL_LITERAL_URI) {
245 printf("variable '%s' is type %d expected %d\n", graph_var,
246 graph_value->type, RASQAL_LITERAL_URI);
247 query_failed=1;
248 break;
249 }
250
251 graph_uri=graph_value->value.uri;
252 if(!raptor_uri_equals(graph_uri, graph_answer)) {
253 printf("result %d FAILED: %s=", count, (char*)graph_var);
254 rasqal_literal_print(graph_value, stdout);
255 printf(" expected URI value <%s>\n",
256 raptor_uri_as_string(graph_answer));
257 query_failed=1;
258 break;
259 }
260
261 rasqal_query_results_next(results);
262 count++;
263 }
264 if(results)
265 rasqal_free_query_results(results);
266
267 printf("%s: query %d results count returned %d results\n", program, i,
268 count);
269 if(count != tests[i].expected_count) {
270 printf("%s: query execution %d FAILED returning %d results, expected %d\n",
271 program, i, count, tests[i].expected_count);
272 query_failed=1;
273 }
274
275 tidy_query:
276
277 rasqal_free_query(query);
278
279 if(!query_failed)
280 printf("%s: query %d OK\n", program, i);
281 else {
282 printf("%s: query %d FAILED\n", program, i);
283 failures++;
284 }
285 }
286
287 for(i=0; i < DATA_GRAPH_COUNT; i++) {
288 if(graph_uris[i])
289 raptor_free_uri(graph_uris[i]);
290 }
291 raptor_free_uri(data_dir_uri);
292 raptor_free_memory(data_dir_string);
293
294 raptor_free_uri(base_uri);
295
296 rasqal_free_world(world);
297
298 return failures;
299 }
300
301 #endif
302