yaz_query2xml complete and tested
[yaz-moved-to-github.git] / test / tstxmlquery.c
1 /*
2  * Copyright (C) 1995-2005, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: tstxmlquery.c,v 1.6 2006-01-30 14:02:07 adam Exp $
6  */
7
8 #include <stdlib.h>
9 #include <stdio.h>
10
11 #include <yaz/wrbuf.h>
12 #include <yaz/xmlquery.h>
13 #include <yaz/pquery.h>
14 #include <yaz/test.h>
15
16 #if HAVE_XML2
17 #include <libxml/parser.h>
18 #include <libxml/tree.h>
19 #endif
20
21 enum pqf2xml_status {
22     PQF_FAILED,
23     QUERY2XML_FAILED,
24     XML_NO_MATCH,
25     XML_MATCH,
26     XML_NO_ERROR
27 };
28
29 enum pqf2xml_status pqf2xml_text(const char *pqf, const char *expect_xml)
30 {
31     YAZ_PQF_Parser parser = yaz_pqf_create();
32     ODR odr = odr_createmem(ODR_ENCODE);
33     Z_RPNQuery *rpn;
34     enum pqf2xml_status status = XML_NO_ERROR;
35
36     YAZ_CHECK(parser);
37
38     YAZ_CHECK(odr);
39
40     rpn = yaz_pqf_parse(parser, odr, pqf);
41
42     yaz_pqf_destroy(parser);
43
44     if (!rpn)
45         status = PQF_FAILED;
46     else
47     {
48         status = QUERY2XML_FAILED;
49 #if HAVE_XML2
50         xmlDocPtr doc = 0;
51         yaz_rpnquery2xml(rpn, &doc);
52         
53         if (!doc)
54             status = QUERY2XML_FAILED;
55         else
56         {
57             char *buf_out;
58             int len_out;
59
60             xmlDocDumpMemory(doc, (xmlChar **) &buf_out, &len_out);
61             
62             if (len_out == strlen(expect_xml)
63                 && memcmp(buf_out, expect_xml, len_out) == 0)
64             {
65                 status = XML_MATCH;
66             }
67             else
68             {
69                 printf("%.*s\n", len_out, buf_out);
70                 status = XML_NO_MATCH;
71             }
72             xmlFreeDoc(doc);
73         }
74 #endif
75     }
76     odr_destroy(odr);
77     return status;
78 }
79
80 void tst()
81 {
82     YAZ_CHECK_EQ(pqf2xml_text("@attr 1=4 bad query", ""), PQF_FAILED);
83 #if HAVE_XML2
84     YAZ_CHECK_EQ(pqf2xml_text(
85                      "@attr 1=4 computer", 
86                      "<?xml version=\"1.0\"?>\n"
87                      "<query set=\"Bib-1\" type=\"rpn\">"
88                      "<apt><attr type=\"1\" value=\"4\"/>"
89                      "<term>computer</term></apt>"
90                      "</query>\n"), XML_MATCH);
91     
92     YAZ_CHECK_EQ(pqf2xml_text(
93                      "@attr 2=1 @attr 1=title computer",
94                      "<?xml version=\"1.0\"?>\n"
95                      "<query set=\"Bib-1\" type=\"rpn\">"
96                      "<apt><attr type=\"1\" value=\"title\"/>"
97                      "<attr type=\"2\" value=\"1\"/>"
98                      "<term>computer</term></apt>"
99                      "</query>\n"), XML_MATCH);
100
101     YAZ_CHECK_EQ(pqf2xml_text(
102                      "@attr 2=1 @attr exp1 1=1 computer",
103                      "<?xml version=\"1.0\"?>\n"
104                      "<query set=\"Bib-1\" type=\"rpn\">"
105                      "<apt><attr set=\"Exp-1\" type=\"1\" value=\"1\"/>"
106                      "<attr type=\"2\" value=\"1\"/>"
107                      "<term>computer</term></apt>"
108                      "</query>\n"), XML_MATCH);
109     
110     YAZ_CHECK_EQ(pqf2xml_text(
111                      "@and a b", 
112                      "<?xml version=\"1.0\"?>\n"
113                      "<query set=\"Bib-1\" type=\"rpn\">"
114                      "<binary type=\"and\">"
115                      "<apt><term>a</term></apt><apt><term>b</term></apt>"
116                      "</binary></query>\n"), XML_MATCH);
117     
118     YAZ_CHECK_EQ(pqf2xml_text(
119                      "@or @and a b c", 
120                      "<?xml version=\"1.0\"?>\n"
121                      "<query set=\"Bib-1\" type=\"rpn\">"
122                      "<binary type=\"or\">"
123                      "<binary type=\"and\"><apt><term>a</term></apt>"
124                      "<apt><term>b</term></apt></binary>"
125                      "<apt><term>c</term></apt>"
126                      "</binary></query>\n"), XML_MATCH);
127
128     YAZ_CHECK_EQ(pqf2xml_text(
129                      "@set abe", 
130                      "<?xml version=\"1.0\"?>\n"
131                      "<query set=\"Bib-1\" type=\"rpn\">"
132                      "<rset>abe</rset></query>\n"), XML_MATCH);
133
134     YAZ_CHECK_EQ(pqf2xml_text(
135                      /* exclusion, distance, ordered, relationtype, 
136                         knownunit, proxunit */
137                      "@prox 0 3 1 2 k 2           a b", 
138                      "<?xml version=\"1.0\"?>\n"
139                      "<query set=\"Bib-1\" type=\"rpn\">"
140                      "<binary type=\"prox\" exclusion=\"false\" "
141                      "distance=\"3\" "
142                      "ordered=\"true\" "
143                      "relationType=\"2\" "
144                      "knownProximityUnit=\"2\">"
145                      "<apt><term>a</term></apt><apt><term>b</term></apt>"
146                      "</binary></query>\n"), XML_MATCH);
147
148 #endif
149 }
150
151 int main (int argc, char **argv)
152 {
153     YAZ_CHECK_INIT(argc, argv);
154     tst();
155     YAZ_CHECK_TERM;
156 }
157
158 /*
159  * Local variables:
160  * c-basic-offset: 4
161  * indent-tabs-mode: nil
162  * End:
163  * vim: shiftwidth=4 tabstop=8 expandtab
164  */
165