Supply Term type as attribute in XML RPN representation.
[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.8 2006-02-02 15:00:58 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 #if HAVE_XML2
49         xmlDocPtr doc = 0;
50
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 #else
75         status = QUERY2XML_FAILED;
76 #endif
77     }
78     odr_destroy(odr);
79     return status;
80 }
81
82 void tst()
83 {
84     YAZ_CHECK_EQ(pqf2xml_text("@attr 1=4 bad query", ""), PQF_FAILED);
85 #if HAVE_XML2
86     YAZ_CHECK_EQ(pqf2xml_text(
87                      "@attr 1=4 computer", 
88                      "<?xml version=\"1.0\"?>\n"
89                      "<query set=\"Bib-1\" type=\"rpn\">"
90                      "<apt><attr type=\"1\" value=\"4\"/>"
91                      "<term type=\"general\">computer</term></apt>"
92                      "</query>\n"), XML_MATCH);
93     
94     YAZ_CHECK_EQ(pqf2xml_text(
95                      "@attr 2=1 @attr 1=title computer",
96                      "<?xml version=\"1.0\"?>\n"
97                      "<query set=\"Bib-1\" type=\"rpn\">"
98                      "<apt><attr type=\"1\" value=\"title\"/>"
99                      "<attr type=\"2\" value=\"1\"/>"
100                      "<term type=\"general\">computer</term></apt>"
101                      "</query>\n"), XML_MATCH);
102
103     YAZ_CHECK_EQ(pqf2xml_text(
104                      "@attr 2=1 @attr exp1 1=1 computer",
105                      "<?xml version=\"1.0\"?>\n"
106                      "<query set=\"Bib-1\" type=\"rpn\">"
107                      "<apt><attr set=\"Exp-1\" type=\"1\" value=\"1\"/>"
108                      "<attr type=\"2\" value=\"1\"/>"
109                      "<term type=\"general\">computer</term></apt>"
110                      "</query>\n"), XML_MATCH);
111     
112     YAZ_CHECK_EQ(pqf2xml_text(
113                      "@and a b", 
114                      "<?xml version=\"1.0\"?>\n"
115                      "<query set=\"Bib-1\" type=\"rpn\">"
116                      "<binary type=\"and\">"
117                      "<apt><term type=\"general\">a</term></apt>"
118                      "<apt><term type=\"general\">b</term></apt>"
119                      "</binary></query>\n"), XML_MATCH);
120     
121     YAZ_CHECK_EQ(pqf2xml_text(
122                      "@or @and a b c", 
123                      "<?xml version=\"1.0\"?>\n"
124                      "<query set=\"Bib-1\" type=\"rpn\">"
125                      "<binary type=\"or\">"
126                      "<binary type=\"and\">"
127                      "<apt><term type=\"general\">a</term></apt>"
128                      "<apt><term type=\"general\">b</term></apt></binary>"
129                      "<apt><term type=\"general\">c</term></apt>"
130                      "</binary></query>\n"), XML_MATCH);
131
132     YAZ_CHECK_EQ(pqf2xml_text(
133                      "@set abe", 
134                      "<?xml version=\"1.0\"?>\n"
135                      "<query set=\"Bib-1\" type=\"rpn\">"
136                      "<rset>abe</rset></query>\n"), XML_MATCH);
137
138     YAZ_CHECK_EQ(pqf2xml_text(
139                      /* exclusion, distance, ordered, relationtype, 
140                         knownunit, proxunit */
141                      "@prox 0 3 1 2 k 2           a b", 
142                      "<?xml version=\"1.0\"?>\n"
143                      "<query set=\"Bib-1\" type=\"rpn\">"
144                      "<binary type=\"prox\" exclusion=\"false\" "
145                      "distance=\"3\" "
146                      "ordered=\"true\" "
147                      "relationType=\"2\" "
148                      "knownProximityUnit=\"2\">"
149                      "<apt><term type=\"general\">a</term></apt>"
150                      "<apt><term type=\"general\">b</term></apt>"
151                      "</binary></query>\n"), XML_MATCH);
152
153     YAZ_CHECK_EQ(pqf2xml_text(
154                      "@term numeric 32", 
155                      "<?xml version=\"1.0\"?>\n"
156                      "<query set=\"Bib-1\" type=\"rpn\">"
157                      "<apt>"
158                      "<term type=\"numeric\">32</term></apt>"
159                      "</query>\n"), XML_MATCH);
160     
161     YAZ_CHECK_EQ(pqf2xml_text(
162                      "@term string computer", 
163                      "<?xml version=\"1.0\"?>\n"
164                      "<query set=\"Bib-1\" type=\"rpn\">"
165                      "<apt>"
166                      "<term type=\"string\">computer</term></apt>"
167                      "</query>\n"), XML_MATCH);
168     
169     YAZ_CHECK_EQ(pqf2xml_text(
170                      "@term null void", 
171                      "<?xml version=\"1.0\"?>\n"
172                      "<query set=\"Bib-1\" type=\"rpn\">"
173                      "<apt>"
174                      "<term type=\"null\"/></apt>"
175                      "</query>\n"), XML_MATCH);
176     
177 #endif
178 }
179
180 int main (int argc, char **argv)
181 {
182     YAZ_CHECK_INIT(argc, argv);
183     tst();
184     YAZ_CHECK_TERM;
185 }
186
187 /*
188  * Local variables:
189  * c-basic-offset: 4
190  * indent-tabs-mode: nil
191  * End:
192  * vim: shiftwidth=4 tabstop=8 expandtab
193  */
194