View | Details | Raw Unified | Return to bug 12430
Collapse All | Expand All

(-)a/t/Search/buildQuery.t (-1 / +330 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More;
21
use t::lib::Mocks;
22
23
use Module::Load::Conditional qw/check_install/;
24
25
BEGIN {
26
    if ( check_install( module => 'Test::DBIx::Class' ) ) {
27
        plan tests => 10;
28
    } else {
29
        plan skip_all => "Need Test::DBIx::Class"
30
    }
31
}
32
33
# Mock the DB connection and C4::Context
34
use Test::DBIx::Class;
35
36
use_ok('C4::Search');
37
can_ok('C4::Search',
38
    qw/buildQuery/);
39
use_ok("Net::Z3950::ZOOM");
40
41
#FIXME: would it be better to use our real ccl.properties file?
42
sub _get_ccl_properties {
43
    my $config = q(
44
        kw 1=1016
45
        wrdl 4=6
46
        rtrn 5=1
47
        right-Truncation rtrn
48
        rk 2=102
49
        ti 1=4
50
        title ti
51
        ext 4=1 6=3
52
        Title-cover 1=36
53
        r1 9=32
54
        r2 9=28
55
        r3 9=26
56
        r4 9=24
57
        r5 9=22
58
        r6 9=20
59
        r7 9=18
60
        r8 9=16
61
        r9 9=14
62
        phr 4=1
63
        fuzzy 5=103
64
        ccode 1=8009
65
    );
66
    return $config;
67
}
68
69
#FIXME: We should add QueryFuzzy and QueryStemming preferences to expand test permutations
70
71
subtest "test weighted autotruncated" => sub {
72
    plan tests => 13;
73
74
    t::lib::Mocks::mock_preference('QueryWeightFields', '1');
75
    t::lib::Mocks::mock_preference('QueryAutoTruncate', '1');
76
77
    my $config = _get_ccl_properties();
78
    my $operators = [""];
79
    my $operands = ["test"];
80
    my $indexes = [""];
81
    my $limits = [""];
82
    my $sort_by = [""];
83
    my ($scan,$lang);
84
85
    my ($error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$query_type) =
86
        C4::Search::buildQuery($operators,$operands,$indexes,$limits,$sort_by,$scan,$lang);
87
    is($error,undef,"Error is correct");
88
    is($query,'(rk=(kw,wrdl,rtrn=test )) ','Query is correct with auto truncation');
89
    is($simple_query,'test',"Simple query is correct");
90
    is($query_cgi,'idx=kw&q=test','Query cgi is correct');
91
    is($query_desc,'kw,wrdl: test','Query desc is correct');
92
    is($limit,'',"Limit is correct");
93
    is($limit_cgi,undef,"Limit cgi is correct");
94
    is($limit_desc,'',"Limit desc is correct");
95
    is($query_type,undef,"Query type is correct");
96
    my $q = Net::Z3950::ZOOM::query_create();
97
    my ($ccl_errcode, $ccl_errstr, $ccl_errpos) = (0,"",0);
98
    my $res = Net::Z3950::ZOOM::query_ccl2rpn($q, $query, $config,
99
        $ccl_errcode, $ccl_errstr, $ccl_errpos
100
    );
101
    is($res,0,"created CCL2RPN query");
102
    is($ccl_errcode,0);
103
    is($ccl_errstr,"");
104
    is($ccl_errpos,0);
105
    Net::Z3950::ZOOM::query_destroy($q);
106
107
};
108
109
subtest "test* weighted autotruncated" => sub {
110
    plan tests => 13;
111
112
    t::lib::Mocks::mock_preference('QueryWeightFields', '1');
113
    t::lib::Mocks::mock_preference('QueryAutoTruncate', '1');
114
115
    my $config = _get_ccl_properties();
116
    my $operators = [""];
117
    my $operands = ["test*"];
118
    my $indexes = [""];
119
    my $limits = [""];
120
    my $sort_by = [""];
121
    my ($scan,$lang);
122
123
    my ($error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$query_type) =
124
        C4::Search::buildQuery($operators,$operands,$indexes,$limits,$sort_by,$scan,$lang);
125
    is($error,undef,"Error is correct");
126
    is($query,'(rk=(kw,wrdl,rtrn=test )) ','Query is correct with manual truncation');
127
    is($simple_query,'test*',"Simple query is correct");
128
    is($query_cgi,'idx=kw&q=test%2A','Query cgi is correct');
129
    is($query_desc,'kw,wrdl: test*','Query desc is correct');
130
    is($limit,'',"Limit is correct");
131
    is($limit_cgi,undef,"Limit cgi is correct");
132
    is($limit_desc,'',"Limit desc is correct");
133
    is($query_type,undef,"Query type is correct");
134
    my $q = Net::Z3950::ZOOM::query_create();
135
    my ($ccl_errcode, $ccl_errstr, $ccl_errpos) = (0,"",0);
136
    my $res = Net::Z3950::ZOOM::query_ccl2rpn($q, $query, $config,
137
        $ccl_errcode, $ccl_errstr, $ccl_errpos
138
    );
139
    is($res,0,"created CCL2RPN query");
140
    is($ccl_errcode,0);
141
    is($ccl_errstr,"");
142
    is($ccl_errpos,0);
143
    Net::Z3950::ZOOM::query_destroy($q);
144
};
145
146
subtest "test weighted not-autotruncated" => sub {
147
    plan tests => 13;
148
149
    t::lib::Mocks::mock_preference('QueryWeightFields', '1');
150
    t::lib::Mocks::mock_preference('QueryAutoTruncate', '0');
151
152
    my $config = _get_ccl_properties();
153
    my $operators = [""];
154
    my $operands = ["test"];
155
    my $indexes = [""];
156
    my $limits = [""];
157
    my $sort_by = [""];
158
    my ($scan,$lang);
159
160
    my ($error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$query_type) =
161
        C4::Search::buildQuery($operators,$operands,$indexes,$limits,$sort_by,$scan,$lang);
162
    is($error,undef,"Error is correct");
163
    is($query,'(rk=(Title-cover,ext,r1="test" or ti,ext,r2="test" or Title-cover,phr,r3="test" or ti,wrdl,r4="test" or wrdl,fuzzy,r8="test" or wrdl,right-Truncation,r9="test" or wrdl,r9="test")) ','Query is correct with weighted fields');
164
    is($simple_query,'test',"Simple query is correct");
165
    is($query_cgi,'idx=kw&q=test','Query cgi is correct');
166
    is($query_desc,'kw,wrdl: test','Query desc is correct');
167
    is($limit,'',"Limit is correct");
168
    is($limit_cgi,undef,"Limit cgi is correct");
169
    is($limit_desc,'',"Limit desc is correct");
170
    is($query_type,undef,"Query type is correct");
171
    my $q = Net::Z3950::ZOOM::query_create();
172
    my ($ccl_errcode, $ccl_errstr, $ccl_errpos) = (0,"",0);
173
    my $res = Net::Z3950::ZOOM::query_ccl2rpn($q, $query, $config,
174
        $ccl_errcode, $ccl_errstr, $ccl_errpos
175
    );
176
    is($res,0,"created CCL2RPN query");
177
    is($ccl_errcode,0);
178
    is($ccl_errstr,"");
179
    is($ccl_errpos,0);
180
    Net::Z3950::ZOOM::query_destroy($q);
181
};
182
183
184
subtest "test ccode:REF weighted autotruncated" => sub {
185
    plan tests => 13;
186
187
    t::lib::Mocks::mock_preference('QueryWeightFields', '1');
188
    t::lib::Mocks::mock_preference('QueryAutoTruncate', '1');
189
190
    my $config = _get_ccl_properties();
191
    my $operators = [""];
192
    my $operands = ["test"];
193
    my $indexes = [""];
194
    my $limits = ["ccode:REF"];
195
    my $sort_by = [""];
196
    my ($scan,$lang);
197
198
    my ($error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$query_type) =
199
        C4::Search::buildQuery($operators,$operands,$indexes,$limits,$sort_by,$scan,$lang);
200
    is($error,undef,"Error is correct");
201
    is($query,'(rk=(kw,wrdl,rtrn=test )) and ccode=REF','Query is correct with auto truncation and limits');
202
    is($simple_query,'test',"Simple query is correct");
203
    is($query_cgi,'idx=kw&q=test','Query cgi is correct');
204
    is($query_desc,'kw,wrdl: test','Query desc is correct');
205
    is($limit,'and ccode=REF',"Limit is correct");
206
    is($limit_cgi,'&limit=ccode%3AREF',"Limit cgi is correct");
207
    is($limit_desc,'ccode:REF',"Limit desc is correct");
208
    is($query_type,undef,"Query type is correct");
209
    my $q = Net::Z3950::ZOOM::query_create();
210
    my ($ccl_errcode, $ccl_errstr, $ccl_errpos) = (0,"",0);
211
    my $res = Net::Z3950::ZOOM::query_ccl2rpn($q, $query, $config,
212
        $ccl_errcode, $ccl_errstr, $ccl_errpos
213
    );
214
    is($res,0,"created CCL2RPN query");
215
    is($ccl_errcode,0);
216
    is($ccl_errstr,"");
217
    is($ccl_errpos,0);
218
    Net::Z3950::ZOOM::query_destroy($q);
219
};
220
221
subtest "test ccode:REF weighted not-autotruncated" => sub {
222
    plan tests => 13;
223
224
    t::lib::Mocks::mock_preference('QueryWeightFields', '1');
225
    t::lib::Mocks::mock_preference('QueryAutoTruncate', '0');
226
227
    my $config = _get_ccl_properties();
228
    my $operators = [""];
229
    my $operands = ["test"];
230
    my $indexes = [""];
231
    my $limits = ["ccode:REF"];
232
    my $sort_by = [""];
233
    my ($scan,$lang);
234
235
    my ($error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$query_type) =
236
        C4::Search::buildQuery($operators,$operands,$indexes,$limits,$sort_by,$scan,$lang);
237
    is($error,undef,"Error is correct");
238
    is($query,'(rk=(Title-cover,ext,r1="test" or ti,ext,r2="test" or Title-cover,phr,r3="test" or ti,wrdl,r4="test" or wrdl,fuzzy,r8="test" or wrdl,right-Truncation,r9="test" or wrdl,r9="test")) and ccode=REF','Query is correct with weighted fields and limits');
239
    is($simple_query,'test',"Simple query is correct");
240
    is($query_cgi,'idx=kw&q=test','Query cgi is correct');
241
    is($query_desc,'kw,wrdl: test','Query desc is correct');
242
    is($limit,'and ccode=REF',"Limit is correct");
243
    is($limit_cgi,'&limit=ccode%3AREF',"Limit cgi is correct");
244
    is($limit_desc,'ccode:REF',"Limit desc is correct");
245
    is($query_type,undef,"Query type is correct");
246
    my $q = Net::Z3950::ZOOM::query_create();
247
    my ($ccl_errcode, $ccl_errstr, $ccl_errpos) = (0,"",0);
248
    my $res = Net::Z3950::ZOOM::query_ccl2rpn($q, $query, $config,
249
        $ccl_errcode, $ccl_errstr, $ccl_errpos
250
    );
251
    is($res,0,"created CCL2RPN query");
252
    is($ccl_errcode,0);
253
    is($ccl_errstr,"");
254
    is($ccl_errpos,0);
255
    Net::Z3950::ZOOM::query_destroy($q);
256
};
257
258
subtest "kw:one and title:two ccode:REF weighted autotruncated" => sub {
259
    plan tests => 13;
260
261
    t::lib::Mocks::mock_preference('QueryWeightFields', '1');
262
    t::lib::Mocks::mock_preference('QueryAutoTruncate', '1');
263
264
    my $config = _get_ccl_properties();
265
    my $operators = ["and"];
266
    my $operands = ["one","two"];
267
    my $indexes = ["kw","title"];
268
    my $limits = ["ccode:REF"];
269
    my $sort_by = [""];
270
    my ($scan,$lang);
271
272
    my ($error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$query_type) =
273
        C4::Search::buildQuery($operators,$operands,$indexes,$limits,$sort_by,$scan,$lang);
274
    is($error,undef,"Error is correct");
275
    is($query,'(rk=(kw,wrdl,rtrn=one )) and (rk=(title,wrdl,rtrn=two )) and ccode=REF','Query is correct with auto truncation, limits, and using indexes and operators');
276
    is($simple_query,'one',"Simple query is correct?");
277
    is($query_cgi,'idx=kw&q=one&op=%20and%20&idx=title&q=two','Query cgi is correct');
278
    is($query_desc,'kw,wrdl: one and title,wrdl: two','Query desc is correct');
279
    is($limit,'and ccode=REF',"Limit is correct");
280
    is($limit_cgi,'&limit=ccode%3AREF',"Limit cgi is correct");
281
    is($limit_desc,'ccode:REF',"Limit desc is correct");
282
    is($query_type,undef,"Query type is correct");
283
    my $q = Net::Z3950::ZOOM::query_create();
284
    my ($ccl_errcode, $ccl_errstr, $ccl_errpos) = (0,"",0);
285
    my $res = Net::Z3950::ZOOM::query_ccl2rpn($q, $query, $config,
286
        $ccl_errcode, $ccl_errstr, $ccl_errpos
287
    );
288
    is($res,0,"created CCL2RPN query");
289
    is($ccl_errcode,0);
290
    is($ccl_errstr,"");
291
    is($ccl_errpos,0);
292
    Net::Z3950::ZOOM::query_destroy($q);
293
};
294
295
subtest "one and two weighted autotruncated" => sub {
296
    plan tests => 13;
297
298
    t::lib::Mocks::mock_preference('QueryWeightFields', '1');
299
    t::lib::Mocks::mock_preference('QueryAutoTruncate', '1');
300
301
    my $config = _get_ccl_properties();
302
    my $operators = [""];
303
    my $operands = ["one and two"];
304
    my $indexes = [""];
305
    my $limits = [""];
306
    my $sort_by = [""];
307
    my ($scan,$lang);
308
309
    my ($error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$query_type) =
310
        C4::Search::buildQuery($operators,$operands,$indexes,$limits,$sort_by,$scan,$lang);
311
    is($error,undef,"Error is correct");
312
    is($query,'(rk=(kw,wrdl,rtrn=one and two )) ','Query is correct with auto truncation and unstructured query');
313
    is($simple_query,'one and two',"Simple query is correct");
314
    is($query_cgi,'idx=kw&q=one%20and%20two','Query cgi is correct');
315
    is($query_desc,'kw,wrdl: one and two','Query desc is correct');
316
    is($limit,'',"Limit is correct");
317
    is($limit_cgi,undef,"Limit cgi is correct");
318
    is($limit_desc,'',"Limit desc is correct");
319
    is($query_type,undef,"Query type is correct");
320
    my $q = Net::Z3950::ZOOM::query_create();
321
    my ($ccl_errcode, $ccl_errstr, $ccl_errpos) = (0,"",0);
322
    my $res = Net::Z3950::ZOOM::query_ccl2rpn($q, $query, $config,
323
        $ccl_errcode, $ccl_errstr, $ccl_errpos
324
    );
325
    is($res,0,"created CCL2RPN query");
326
    is($ccl_errcode,0);
327
    is($ccl_errstr,"");
328
    is($ccl_errpos,0);
329
    Net::Z3950::ZOOM::query_destroy($q);
330
};

Return to bug 12430