Work
[yaz-moved-to-github.git] / test / test_sparql.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5 #if HAVE_CONFIG_H
6 #include <config.h>
7 #endif
8
9 #include <string.h>
10 #include <yaz/sparql.h>
11 #include <yaz/log.h>
12 #include <yaz/test.h>
13 #include <yaz/pquery.h>
14
15 static int test_query(yaz_sparql_t s, const char *pqf, const char *expect)
16 {
17     YAZ_PQF_Parser parser = yaz_pqf_create();
18     ODR odr = odr_createmem(ODR_ENCODE);
19     Z_RPNQuery *rpn = yaz_pqf_parse(parser, odr, pqf);
20     int ret = 0;
21     WRBUF addinfo = wrbuf_alloc();
22     WRBUF w = wrbuf_alloc();
23
24     if (rpn)
25     {
26         int r = yaz_sparql_from_rpn_wrbuf(s, addinfo, w, rpn);
27         if (expect)
28         {
29             if (!r)
30             {
31                 if (!strcmp(expect, wrbuf_cstr(w)))
32                     ret = 1;
33                 else
34                 {
35                     yaz_log(YLOG_WARN, "test_sparql: pqf=%s", pqf);
36                     yaz_log(YLOG_WARN, " expect: %s", expect);
37                     yaz_log(YLOG_WARN, " got:    %s", wrbuf_cstr(w));
38                 }
39             }
40             else
41             {
42                 yaz_log(YLOG_WARN, "test_sparql: pqf=%s", pqf);
43                 yaz_log(YLOG_WARN, " expect: %s", expect);
44                 yaz_log(YLOG_WARN, " got error: %d:%s", r, wrbuf_cstr(addinfo));
45             }
46         }
47         else
48         {
49             if (r)
50                 ret = 1;
51             else
52             {
53                 yaz_log(YLOG_WARN, "test_sparql: pqf=%s", pqf);
54                 yaz_log(YLOG_WARN, " expect error");
55                 yaz_log(YLOG_WARN, " got:    %s", wrbuf_cstr(w));
56             }
57         }
58     }
59     wrbuf_destroy(w);
60     wrbuf_destroy(addinfo);
61     odr_destroy(odr);
62     yaz_pqf_destroy(parser);
63     return ret;
64 }
65
66 static void tst1(void)
67 {
68     yaz_sparql_t s = yaz_sparql_create();
69
70     yaz_sparql_add_pattern(s, "prefix",
71                            "rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns");
72     yaz_sparql_add_pattern(s, "prefix",
73                            "bf: <http://bibframe.org/vocab/>");
74     yaz_sparql_add_pattern(s, "field.title", "?title");
75     yaz_sparql_add_pattern(s, "field.author", "?author");
76     yaz_sparql_add_pattern(s, "field.description", "?description");
77     yaz_sparql_add_pattern(s, "field.instanceTitle", "?ititle");
78     yaz_sparql_add_pattern(s, "criteria", "?work a bf:Work");
79     yaz_sparql_add_pattern(s, "criteria", "?work bf:workTitle/bf:titleValue ?title");
80     yaz_sparql_add_pattern(s, "criteria", "?work bf:creator/bf:label ?author");
81     yaz_sparql_add_pattern(s, "criteria", "?work bf:note ?description");
82     yaz_sparql_add_pattern(s, "criteria", "?inst bf:instanceOf ?work");
83     yaz_sparql_add_pattern(s, "criteria", "?inst bf:instanceTitle/bf:titleValue ?ititle");
84     YAZ_CHECK(test_query(
85                   s, "computer",
86                   "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns>\n"
87                   "PREFIX bf: <http://bibframe.org/vocab/>\n"
88                   "\n"
89                   "SELECT ?title ?author ?description ?ititle\n"
90                   "WHERE {\n"
91                   "  ?work a bf:Work .\n"
92                   "  ?work bf:workTitle/bf:titleValue ?title .\n"
93                   "  ?work bf:creator/bf:label ?author .\n"
94                   "  ?work bf:note ?description .\n"
95                   "  ?inst bf:instanceOf ?work .\n"
96                   "  ?inst bf:instanceTitle/bf:titleValue ?ititle .\n"
97                   "}\n"
98 ));
99
100     yaz_sparql_destroy(s);
101 }
102
103 int main(int argc, char **argv)
104 {
105     YAZ_CHECK_INIT(argc, argv);
106     YAZ_CHECK_LOG();
107     tst1();
108     YAZ_CHECK_TERM;
109 }
110 /*
111  * Local variables:
112  * c-basic-offset: 4
113  * c-file-style: "Stroustrup"
114  * indent-tabs-mode: nil
115  * End:
116  * vim: shiftwidth=4 tabstop=8 expandtab
117  */
118