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-2009 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 1=any hello", 1));
96
97     /* verify that we get these records exactly */
98     ids[0] = 24338;
99     ids[1] = 24339;
100     ids[2] = 24340;
101     YAZ_CHECK(tl_meta_query(zh, "@attr 4=3 @attr 1=any mand", 3, ids));
102
103     YAZ_CHECK_EQ(tl_fetch_compare(zh, 1, "R", yaz_oid_recsyn_sutrs,
104                                   myrec[0]), ZEBRA_OK);
105     YAZ_CHECK_EQ(tl_fetch_compare(zh, 2, "R", yaz_oid_recsyn_sutrs,
106                                   myrec[2]), ZEBRA_OK);
107     YAZ_CHECK_EQ(tl_fetch_compare(zh, 3, "R", yaz_oid_recsyn_sutrs,
108                                   myrec[4]), ZEBRA_OK);
109     YAZ_CHECK_EQ(tl_fetch_compare(zh, 1, "zebra::facet::any:0", 
110                                   yaz_oid_recsyn_xml,
111                                   "<record xmlns=\"http://www.indexdata.com/zebra/\">\n"
112                                   "  <facet type=\"0\" index=\"any\">\n"
113                                   "    <term coccur=\"4\" occur=\"3\">mand</term>\n"
114                                   "    <term coccur=\"4\" occur=\"3\">the</term>\n"
115                                   "    <term coccur=\"1\" occur=\"1\">art</term>\n"
116                                   "    <term coccur=\"2\" occur=\"1\">gamle</term>\n"
117                                   "    <term coccur=\"1\" occur=\"1\">old</term>\n"
118                                   "  </facet>\n"
119                                   "</record>\n"), ZEBRA_OK);
120     YAZ_CHECK_EQ(tl_fetch_compare(zh, 1, "zebra::facet::any:0:2", 
121                                   yaz_oid_recsyn_xml,
122                                   "<record xmlns=\"http://www.indexdata.com/zebra/\">\n"
123                                "  <facet type=\"0\" index=\"any\">\n"
124                                   "    <term coccur=\"4\" occur=\"3\">mand</term>\n"
125                                   "    <term coccur=\"4\" occur=\"3\">the</term>\n"
126                                   "  </facet>\n"
127                                   "</record>\n"), ZEBRA_OK);
128
129
130     YAZ_CHECK_EQ(tl_fetch_compare(zh, 1, "zebra::facet::ti:s", 
131                                   yaz_oid_recsyn_xml,
132                                   "<record xmlns=\"http://www.indexdata.com/zebra/\">\n"
133                                   "  <facet type=\"s\" index=\"ti\">\n"
134                                   "    <term coccur=\"1\" occur=\"1\">a b c</term>\n"
135                                   "    <term coccur=\"1\" occur=\"1\">j k l</term>\n"
136                                   "    <term coccur=\"1\" occur=\"1\">m n o</term>\n"
137                                   "  </facet>\n"
138                                   "</record>\n"), ZEBRA_OK);
139
140     /* limit to 125061 */
141     limits[0] = 125061;
142     limits[1] = 0;
143     zebra_set_limit(zh, 0, limits);
144     ids[0] = 24339;
145     YAZ_CHECK(tl_meta_query(zh, "@attr 4=3 @attr 1=any mand", 1, ids));
146
147     /* limit to 125060, 125061 */
148     limits[0] = 125061;
149     limits[1] = 125060;
150     limits[2] = 0;
151     zebra_set_limit(zh, 0, limits);
152     ids[0] = 24338;
153     ids[1] = 24339;
154     YAZ_CHECK(tl_meta_query(zh, "@attr 4=3 @attr 1=any mand", 2, ids));
155
156     /* all except 125062 */
157     limits[0] = 125062;
158     limits[1] = 0;
159     zebra_set_limit(zh, 1, limits);
160
161     ids[0] = 24338;
162     ids[1] = 24339;
163     YAZ_CHECK(tl_meta_query(zh, "@attr 4=3 @attr 1=any mand", 2, ids));
164
165     /* no limit */
166     zebra_set_limit(zh, 1, 0);
167     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=any mand", 3));
168
169     /* test segments */
170     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title a", 1));
171     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title b", 1));
172     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title c", 1));
173
174     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title @and a b", 1));
175     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title @and a c", 1));
176     
177     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title @and c d", 1));
178     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title @and b f", 1));
179     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title @and f g", 0));
180     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title @and g f", 0));
181     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title @and d g", 0));
182     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title @and g c", 0));
183     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title @and c g", 0));
184     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title @and c c", 1));
185
186     YAZ_CHECK(tl_close_down(zh, zs));
187 }
188
189 TL_MAIN
190 /*
191  * Local variables:
192  * c-basic-offset: 4
193  * indent-tabs-mode: nil
194  * End:
195  * vim: shiftwidth=4 tabstop=8 expandtab
196  */
197