Update for YAZ 3s new OID system
[idzebra-moved-to-github.git] / test / api / t16.c
1 /* $Id: t16.c,v 1.10 2007-04-16 08:44:32 adam Exp $
2    Copyright (C) 1995-2007
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 */
22
23 /** \brief test special element set names zebra:: and friends */
24
25 #include "testlib.h"
26
27 const char *myrec[] = {
28         "<gils>\n"
29         "  <title>My title</title>\n"
30         "</gils>\n",
31         0};
32         
33 #define NUMBER_TO_FETCH_MAX 1000
34
35 static ZEBRA_RES fetch_first(ZebraHandle zh, const char *element_set,
36                              const int * format, ODR odr,
37                              const char **rec_buf, size_t *rec_len)
38 {
39     ZebraRetrievalRecord retrievalRecord[1];
40     Z_RecordComposition *comp;
41     ZEBRA_RES res;
42
43     retrievalRecord[0].position = 1; /* get from this position */
44     
45     yaz_set_esn(&comp, element_set, odr->mem);
46
47     res = zebra_records_retrieve(zh, odr, "default", comp, format, 1, 
48                                  retrievalRecord);
49     if (res != ZEBRA_OK)
50     {
51         int code = zebra_errCode(zh);
52         yaz_log(YLOG_FATAL, "zebra_records_retrieve returned error %d",
53                 code);
54     }
55     else
56     {
57         *rec_buf = retrievalRecord[0].buf;
58         *rec_len = retrievalRecord[0].len;
59     }
60     return res;
61 }
62
63 static ZEBRA_RES fetch_first_compare(ZebraHandle zh, const char *element_set,
64                                      const int *format, const char *cmp_rec)
65 {
66     const char *rec_buf = 0;
67     size_t rec_len = 0;
68     ODR odr = odr_createmem(ODR_ENCODE);
69     ZEBRA_RES res = fetch_first(zh, element_set, format, odr,
70                                 &rec_buf, &rec_len);
71     if (res == ZEBRA_OK)
72     {
73         if (strlen(cmp_rec) != rec_len)
74             res = ZEBRA_FAIL;
75         else if (memcmp(cmp_rec, rec_buf, rec_len))
76             res = ZEBRA_FAIL;
77     }
78     odr_destroy(odr);
79     return res;
80 }
81
82
83
84 static void tst(int argc, char **argv)
85 {
86     zint hits;
87     ZEBRA_RES res;
88     const char * zebra_xml_sysno 
89         = "<record xmlns=\"http://www.indexdata.com/zebra/\" sysno=\"2\"/>\n";
90
91     const char * zebra_xml_meta 
92         = "<record xmlns=\"http://www.indexdata.com/zebra/\" sysno=\"2\" base=\"Default\" type=\"grs.sgml\" rank=\"0\" size=\"41\" set=\"zebra::meta\"/>\n";
93
94     const char * zebra_xml_index_title_p
95         = "<record xmlns=\"http://www.indexdata.com/zebra/\" sysno=\"2\" set=\"zebra::index::title:p/\">\n"
96 "  <index name=\"title\" type=\"p\" seq=\"4\">my title</index>\n"
97 "</record>\n";
98
99     ZebraService zs = tl_start_up(0, argc, argv);
100     ZebraHandle zh = zebra_open(zs, 0);
101
102     YAZ_CHECK(tl_init_data(zh, myrec));
103
104     res = zebra_search_PQF(zh, "@attr 1=4 my", "default", &hits);
105     YAZ_CHECK_EQ(res, ZEBRA_OK);
106     YAZ_CHECK_EQ(hits, 1);
107     
108     YAZ_CHECK_EQ(fetch_first_compare(zh, "zebra::data", yaz_oid_xml(),
109                                      "mismatch"), ZEBRA_FAIL);
110
111     YAZ_CHECK_EQ(fetch_first_compare(zh, "zebra::data", yaz_oid_sutrs(),
112                                      myrec[0]), ZEBRA_OK);
113
114     YAZ_CHECK_EQ(fetch_first_compare(zh, "zebra::data", yaz_oid_xml(),
115                                      myrec[0]), ZEBRA_OK);
116
117     YAZ_CHECK_EQ(fetch_first_compare(zh, "zebra::meta::sysno", yaz_oid_sutrs(),
118                                      "2"), ZEBRA_OK);
119
120     YAZ_CHECK_EQ(fetch_first_compare(zh, "zebra::meta::sysno", yaz_oid_xml(),
121                                      zebra_xml_sysno), ZEBRA_OK);
122     
123     YAZ_CHECK_EQ(fetch_first_compare(zh, "zebra::meta", yaz_oid_xml(),
124                                      zebra_xml_meta), ZEBRA_OK);
125     
126     YAZ_CHECK_EQ(fetch_first_compare(zh, "zebra::index::title:p", 
127                                      yaz_oid_xml(),
128                                      zebra_xml_index_title_p), ZEBRA_OK);
129     
130     YAZ_CHECK_EQ(fetch_first_compare(zh, "zebra::nonexistent", 
131                                      yaz_oid_xml(), ""), ZEBRA_OK);
132     
133     YAZ_CHECK_EQ(fetch_first_compare(zh, "zebra::index::nonexistent", 
134                                      yaz_oid_xml(), ""), ZEBRA_OK);
135     
136     YAZ_CHECK(tl_close_down(zh, zs));
137 }
138
139 TL_MAIN
140
141 /*
142  * Local variables:
143  * c-basic-offset: 4
144  * indent-tabs-mode: nil
145  * End:
146  * vim: shiftwidth=4 tabstop=8 expandtab
147  */
148