Happy new year.
[idzebra-moved-to-github.git] / test / api / test_safari.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2011 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 /** \file
21     \brief test safari filter
22  */
23
24 #include "testlib.h"
25
26 const char *myrec[] = 
27 {
28     "1234\n"  /* ID first record */
29     /* chunk owner seq idx term */
30     "00024338 125060 0 1 any the\n"
31     "00024338 125060 0 2 any art\n"
32     "00024338 125060 0 3 any mand\n"
33     "s 00024338 125060 0 3 ti a b c\n"
34     ,
35     "5678\n"  /* other record - same owner id */
36     "00024339 125060 0 1 any den\n"
37     "00024339 125060 0 2 any gamle\n"
38     "00024339 125060 0 3 any mand\n"
39     "s 00024339 125060 0 3 ti d e f\n"
40     ,
41     "5678\n"  /* same record chunk id as before .. */
42     "00024339 125060 0 1 any the\n"
43     "00024339 125060 0 2 any gamle\n"
44     "00024339 125060 0 3 any mand\n"
45     "s 00024339 125060 0 3 ti g h i\n"
46     ,
47     "1000\n"  /* separate record */
48     "00024339 125061 0 1 any the\n"
49     "00024339 125061 0 2 any gamle\n"
50     "00024339 125061 0 3 any mand\n"
51     "w 00024339 125661 0 4 any Hello\n" /* index type given */
52     "s 00024339 125061 0 3 ti j k l\n"
53     ,
54     "1001\n"  /* separate record */
55     "00024340 125062 0 1 any the\n"
56     "00024340 125062 0 1 any the\n" /* DUP KEY, bug #432 */
57     "00024340 125062 0 2 any old\n"
58     "00024340 125062 0 3 any mand\n"
59     "s 00024340 125062 0 3 ti m n o\n"
60     ,
61     "1002\n"  /* segment testing record */
62     "00024341 125062 0 1 title a\n"
63     "00024341 125062 0 2 title b\n"
64
65     "00024341 125062 1 1024 title b\n"
66     "00024341 125062 1 1025 title c\n"
67     "00024341 125062 1 1026 title d\n"
68     "00024341 125062 1 1027 title e\n"
69     "00024341 125062 1 1028 title f\n"
70
71     "00024341 125062 2 2048 title g\n"
72     "00024341 125062 2 2049 title c\n"
73     ,
74
75     0
76 };
77
78 static void tst(int argc, char **argv)
79 {
80     zint ids[3];
81     zint limits[3];
82     ZebraService zs = tl_start_up("test_safari.cfg", argc, argv);
83     
84     ZebraHandle zh = zebra_open(zs, 0);
85
86     YAZ_CHECK(tl_init_data(zh, myrec));
87
88     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=any the", 3));
89     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=any den", 0));
90     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=any @and the art", 1));
91     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=any @and den gamle", 0));
92     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=any @and the gamle", 1));
93     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=any @and the of", 0));
94
95     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=any @prox 0 3 1 2 k 2 the gamle", 1));
96     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=any @prox 0 3 1 2 k 2 den gamle", 0));
97     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=any @prox 0 3 1 2 k 2 the art", 1));
98     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=any @prox 0 3 1 2 k 2 den gamle", 0));
99     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=any @prox 0 3 1 2 k 2 old mand", 1));
100
101     YAZ_CHECK(tl_query(zh, "@attr 1=any hello", 1));
102
103     /* verify that we get these records exactly */
104     ids[0] = 24338;
105     ids[1] = 24339;
106     ids[2] = 24340;
107     YAZ_CHECK(tl_meta_query(zh, "@attr 4=3 @attr 1=any mand", 3, ids));
108
109     YAZ_CHECK_EQ(tl_fetch_compare(zh, 1, "R", yaz_oid_recsyn_sutrs,
110                                   myrec[0]), ZEBRA_OK);
111     YAZ_CHECK_EQ(tl_fetch_compare(zh, 2, "R", yaz_oid_recsyn_sutrs,
112                                   myrec[2]), ZEBRA_OK);
113     YAZ_CHECK_EQ(tl_fetch_compare(zh, 3, "R", yaz_oid_recsyn_sutrs,
114                                   myrec[4]), ZEBRA_OK);
115     YAZ_CHECK_EQ(tl_fetch_compare(zh, 1, "zebra::facet::any:0", 
116                                   yaz_oid_recsyn_xml,
117                                   "<record xmlns=\"http://www.indexdata.com/zebra/\">\n"
118                                   "  <facet type=\"0\" index=\"any\">\n"
119                                   "    <term coccur=\"4\" occur=\"3\">mand</term>\n"
120                                   "    <term coccur=\"4\" occur=\"3\">the</term>\n"
121                                   "    <term coccur=\"1\" occur=\"1\">art</term>\n"
122                                   "    <term coccur=\"2\" occur=\"1\">gamle</term>\n"
123                                   "    <term coccur=\"1\" occur=\"1\">old</term>\n"
124                                   "  </facet>\n"
125                                   "</record>\n"), ZEBRA_OK);
126     YAZ_CHECK_EQ(tl_fetch_compare(zh, 1, "zebra::facet::any:0:2", 
127                                   yaz_oid_recsyn_xml,
128                                   "<record xmlns=\"http://www.indexdata.com/zebra/\">\n"
129                                "  <facet type=\"0\" index=\"any\">\n"
130                                   "    <term coccur=\"4\" occur=\"3\">mand</term>\n"
131                                   "    <term coccur=\"4\" occur=\"3\">the</term>\n"
132                                   "  </facet>\n"
133                                   "</record>\n"), ZEBRA_OK);
134
135
136     YAZ_CHECK_EQ(tl_fetch_compare(zh, 1, "zebra::facet::ti:s", 
137                                   yaz_oid_recsyn_xml,
138                                   "<record xmlns=\"http://www.indexdata.com/zebra/\">\n"
139                                   "  <facet type=\"s\" index=\"ti\">\n"
140                                   "    <term coccur=\"1\" occur=\"1\">a b c</term>\n"
141                                   "    <term coccur=\"1\" occur=\"1\">j k l</term>\n"
142                                   "    <term coccur=\"1\" occur=\"1\">m n o</term>\n"
143                                   "  </facet>\n"
144                                   "</record>\n"), ZEBRA_OK);
145
146     /* limit to 125061 */
147     limits[0] = 125061;
148     limits[1] = 0;
149     zebra_set_limit(zh, 0, limits);
150     ids[0] = 24339;
151     YAZ_CHECK(tl_meta_query(zh, "@attr 4=3 @attr 1=any mand", 1, ids));
152
153     /* limit to 125060, 125061 */
154     limits[0] = 125061;
155     limits[1] = 125060;
156     limits[2] = 0;
157     zebra_set_limit(zh, 0, limits);
158     ids[0] = 24338;
159     ids[1] = 24339;
160     YAZ_CHECK(tl_meta_query(zh, "@attr 4=3 @attr 1=any mand", 2, ids));
161
162     /* all except 125062 */
163     limits[0] = 125062;
164     limits[1] = 0;
165     zebra_set_limit(zh, 1, limits);
166
167     ids[0] = 24338;
168     ids[1] = 24339;
169     YAZ_CHECK(tl_meta_query(zh, "@attr 4=3 @attr 1=any mand", 2, ids));
170
171     /* no limit */
172     zebra_set_limit(zh, 1, 0);
173     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=any mand", 3));
174
175     /* test segments */
176     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title a", 1));
177     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title b", 1));
178     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title c", 1));
179
180     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title @and a b", 1));
181     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title @and a c", 1));
182     
183     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title @and c d", 1));
184     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title @and b f", 1));
185     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title @and f g", 0));
186     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title @and g f", 0));
187     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title @and d g", 0));
188     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title @and g c", 0));
189     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title @and c g", 0));
190     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title @and c c", 1));
191
192     YAZ_CHECK(tl_close_down(zh, zs));
193 }
194
195 TL_MAIN
196 /*
197  * Local variables:
198  * c-basic-offset: 4
199  * c-file-style: "Stroustrup"
200  * indent-tabs-mode: nil
201  * End:
202  * vim: shiftwidth=4 tabstop=8 expandtab
203  */
204