1f38486ec5b7db76294a4fdb7000ad3eb44279c9
[idzebra-moved-to-github.git] / test / api / test_safari.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1995-2008 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     ,
34     "5678\n"  /* other record - same owner id */
35     "00024339 125060 0 1 any den\n"
36     "00024339 125060 0 2 any gamle\n"
37     "00024339 125060 0 3 any mand\n"
38     ,
39     "5678\n"  /* same record chunk id as before .. */
40     "00024339 125060 0 1 any the\n"
41     "00024339 125060 0 2 any gamle\n"
42     "00024339 125060 0 3 any mand\n"
43     ,
44     "1000\n"  /* separate record */
45     "00024339 125061 0 1 any the\n"
46     "00024339 125061 0 2 any gamle\n"
47     "00024339 125061 0 3 any mand\n"
48     "w 00024339 125661 0 4 any Hello\n" /* index type given */
49     ,
50     "1001\n"  /* separate record */
51     "00024340 125062 0 1 any the\n"
52     "00024340 125062 0 1 any the\n" /* DUP KEY, bug #432 */
53     "00024340 125062 0 2 any old\n"
54     "00024340 125062 0 3 any mand\n"
55     ,
56     "1002\n"  /* segment testing record */
57     "00024341 125062 0 1 title a\n"
58     "00024341 125062 0 2 title b\n"
59
60     "00024341 125062 1 1024 title b\n"
61     "00024341 125062 1 1025 title c\n"
62     "00024341 125062 1 1026 title d\n"
63     "00024341 125062 1 1027 title e\n"
64     "00024341 125062 1 1028 title f\n"
65
66     "00024341 125062 2 2048 title g\n"
67     "00024341 125062 2 2049 title c\n"
68     ,
69
70     0
71 };
72
73 static void tst(int argc, char **argv)
74 {
75     zint ids[3];
76     zint limits[3];
77     ZebraService zs = tl_start_up("test_safari.cfg", argc, argv);
78     
79     ZebraHandle zh = zebra_open(zs, 0);
80
81     YAZ_CHECK(tl_init_data(zh, myrec));
82
83     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=any the", 3));
84     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=any den", 0));
85     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=any @and the art", 1));
86     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=any @and den gamle", 0));
87     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=any @and the gamle", 1));
88     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=any @and the of", 0));
89
90     YAZ_CHECK(tl_query(zh, "@attr 1=any hello", 1));
91
92     /* verify that we get these records exactly */
93     ids[0] = 24338;
94     ids[1] = 24339;
95     ids[2] = 24340;
96     YAZ_CHECK(tl_meta_query(zh, "@attr 4=3 @attr 1=any mand", 3, ids));
97
98     YAZ_CHECK_EQ(tl_fetch_compare(zh, 1, "R", yaz_oid_recsyn_sutrs,
99                                   myrec[0]), ZEBRA_OK);
100     YAZ_CHECK_EQ(tl_fetch_compare(zh, 2, "R", yaz_oid_recsyn_sutrs,
101                                   myrec[2]), ZEBRA_OK);
102     YAZ_CHECK_EQ(tl_fetch_compare(zh, 3, "R", yaz_oid_recsyn_sutrs,
103                                   myrec[4]), ZEBRA_OK);
104     YAZ_CHECK_EQ(tl_fetch_compare(zh, 1, "zebra::facet::any:0", 
105                                   yaz_oid_recsyn_xml,
106                                   "<record xmlns=\"http://www.indexdata.com/zebra/\">\n"
107                                   "  <facet type=\"0\" index=\"any\">\n"
108                                   "    <term coccur=\"4\" occur=\"3\">mand</term>\n"
109                                   "    <term coccur=\"4\" occur=\"3\">the</term>\n"
110                                   "    <term coccur=\"1\" occur=\"1\">art</term>\n"
111                                   "    <term coccur=\"2\" occur=\"1\">gamle</term>\n"
112                                   "    <term coccur=\"1\" occur=\"1\">old</term>\n"
113                                   "  </facet>\n"
114                                   "</record>\n"), ZEBRA_OK);
115     YAZ_CHECK_EQ(tl_fetch_compare(zh, 1, "zebra::facet::any:0:2", 
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                                   "  </facet>\n"
122                                   "</record>\n"), ZEBRA_OK);
123
124     /* limit to 125061 */
125     limits[0] = 125061;
126     limits[1] = 0;
127     zebra_set_limit(zh, 0, limits);
128     ids[0] = 24339;
129     YAZ_CHECK(tl_meta_query(zh, "@attr 4=3 @attr 1=any mand", 1, ids));
130
131     /* limit to 125060, 125061 */
132     limits[0] = 125061;
133     limits[1] = 125060;
134     limits[2] = 0;
135     zebra_set_limit(zh, 0, limits);
136     ids[0] = 24338;
137     ids[1] = 24339;
138     YAZ_CHECK(tl_meta_query(zh, "@attr 4=3 @attr 1=any mand", 2, ids));
139
140     /* all except 125062 */
141     limits[0] = 125062;
142     limits[1] = 0;
143     zebra_set_limit(zh, 1, limits);
144
145     ids[0] = 24338;
146     ids[1] = 24339;
147     YAZ_CHECK(tl_meta_query(zh, "@attr 4=3 @attr 1=any mand", 2, ids));
148
149     /* no limit */
150     zebra_set_limit(zh, 1, 0);
151     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=any mand", 3));
152
153     /* test segments */
154     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title a", 1));
155     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title b", 1));
156     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title c", 1));
157
158     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title @and a b", 1));
159     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title @and a c", 1));
160     
161     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title @and c d", 1));
162     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title @and b f", 1));
163     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title @and f g", 0));
164     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title @and g f", 0));
165     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title @and d g", 0));
166     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title @and g c", 0));
167     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title @and c g", 0));
168     YAZ_CHECK(tl_query(zh, "@attr 4=3 @attr 1=title @and c c", 1));
169
170     YAZ_CHECK(tl_close_down(zh, zs));
171 }
172
173 TL_MAIN
174 /*
175  * Local variables:
176  * c-basic-offset: 4
177  * indent-tabs-mode: nil
178  * End:
179  * vim: shiftwidth=4 tabstop=8 expandtab
180  */
181