bugfix - fix 'not' operator in NoZebra
[koha.git] / t / lib / KohaTest / Search / NoZebra.pm
1 package KohaTest::Search::NoZebra;
2 use base qw( KohaTest::Search );
3
4 use strict;
5 use warnings;
6
7 use Test::More;
8
9 use MARC::Record;
10
11 use C4::Search;
12 use C4::Biblio;
13 use C4::Context;
14
15 =head2 STARTUP METHODS
16
17 These get run once, before the main test methods in this module
18
19 =cut
20
21 =head3 startup_50_init_nozebra
22
23 Turn on NoZebra mode, for now, assumes and requires
24 that the test database has started out using Zebra.
25
26 =cut
27
28 sub startup_50_init_nozebra : Test( startup => 3 ) {
29     my $using_nozebra = C4::Context->preference('NoZebra');
30     ok(!$using_nozebra, "starting out using Zebra");
31     my $dbh = C4::Context->dbh;
32     $dbh->do("UPDATE systempreferences SET value=1 WHERE variable='NoZebra'");
33     $dbh->do("UPDATE systempreferences SET value=0 WHERE variable in ('QueryFuzzy','QueryWeightFields','QueryStemming')");
34     $using_nozebra = C4::Context->preference('NoZebra');
35     ok($using_nozebra, "switched to NoZebra");
36
37     my $sth = $dbh->prepare("SELECT COUNT(*) FROM nozebra");
38     $sth->execute;
39     my ($count) = $sth->fetchrow_array;
40     $sth->finish;
41     cmp_ok($count, '==', 0, "NoZebra index starts off empty");
42 }
43
44 sub startup_51_add_bibs : Test( startup => 2 ) {
45     my $self = shift;
46
47     my $bib1 = MARC::Record->new();
48     $bib1->leader('     nam a22     7a 4500');
49     $bib1->append_fields(
50         MARC::Field->new('010', ' ', ' ', a => 'lccn001'), 
51         MARC::Field->new('020', ' ', ' ', a => 'isbn001'), 
52         MARC::Field->new('022', ' ', ' ', a => 'issn001'), 
53         MARC::Field->new('100', ' ', ' ', a => 'Cat, Felix T.'),
54         MARC::Field->new('245', ' ', ' ', a => 'Of mice and men :', b=> 'a history'),
55     );
56     my $bib2 = MARC::Record->new();
57     $bib2->leader('     nam a22     7a 4500');
58     $bib2->append_fields(
59         MARC::Field->new('010', ' ', ' ', a => 'lccn002'), 
60         MARC::Field->new('020', ' ', ' ', a => 'isbn002'), 
61         MARC::Field->new('022', ' ', ' ', a => 'issn002'), 
62         MARC::Field->new('100', ' ', ' ', a => 'Dog, Rover T.'),
63         MARC::Field->new('245', ' ', ' ', a => 'Of mice and men :', b=> 'a digression'),
64     );
65
66     my $dbh = C4::Context->dbh;
67     my $count_sth = $dbh->prepare("SELECT COUNT(*) FROM nozebra");
68     my $count;
69     my ($bib1_bibnum, $bib1_bibitemnum) = AddBiblio($bib1, '');
70     $count_sth->execute;
71     ($count) = $count_sth->fetchrow_array;
72     cmp_ok($count, '==', 14, "correct number of new words indexed"); # tokens + biblionumber + __RAW__
73
74     my ($bib2_bibnum, $bib2_bibitemnum) = AddBiblio($bib2, '');
75     $count_sth->execute;
76     ($count) = $count_sth->fetchrow_array;
77     cmp_ok($count, '==', 22, "correct number of new words indexed"); # tokens + biblionumber + __RAW__
78
79     push @{ $self->{nozebra_test_bibs} }, $bib1_bibnum, $bib2_bibnum;
80 }
81
82 =head2 TEST METHODS
83
84 Standard test methods
85
86 =cut
87
88 sub basic_searches_via_nzanalyze : Test( 28 ) {
89     my $self = shift;
90     my ($bib1_bibnum, $bib2_bibnum) = @{ $self->{nozebra_test_bibs} };
91     
92     my $results = C4::Search::NZanalyse('foobar');
93     ok(!defined($results), "no hits on 'foobar'");
94
95     $results = C4::Search::NZanalyse('dog');
96     my ($hits, @bibnumbers) = parse_nzanalyse($results);
97     cmp_ok($hits, '==', 1, "one hit on 'dog'");
98     is($bib2_bibnum, $bibnumbers[0], "correct hit on 'dog'");
99
100     $results = C4::Search::NZanalyse('au=dog');
101     ($hits, @bibnumbers) = parse_nzanalyse($results);
102     cmp_ok($hits, '==', 1, "one hit on 'au=dog'");
103     is($bib2_bibnum, $bibnumbers[0], "correct hit on 'au=dog'");
104
105     $results = C4::Search::NZanalyse('isbn=dog');
106     ($hits, @bibnumbers) = parse_nzanalyse($results);
107     cmp_ok($hits, '==', 0, "zero hits on 'isbn=dog'");
108
109     $results = C4::Search::NZanalyse('cat');
110     ($hits, @bibnumbers) = parse_nzanalyse($results);
111     cmp_ok($hits, '==', 1, "one hit on 'cat'");
112     is($bib1_bibnum, $bibnumbers[0], "correct hit on 'cat'");
113
114     $results = C4::Search::NZanalyse('cat and dog');
115     ($hits, @bibnumbers) = parse_nzanalyse($results);
116     cmp_ok($hits, '==', 0, "zero hits on 'cat and dog'");
117
118     $results = C4::Search::NZanalyse('cat or dog');
119     ($hits, @bibnumbers) = parse_nzanalyse($results);
120     cmp_ok($hits, '==', 2, "two hits on 'cat or dog'");
121     is_deeply([ sort @bibnumbers ], [ sort($bib1_bibnum, $bib2_bibnum) ], "correct hits on 'cat or dog'");
122
123     $results = C4::Search::NZanalyse('mice and men');
124     ($hits, @bibnumbers) = parse_nzanalyse($results);
125     cmp_ok($hits, '==', 2, "two hits on 'mice and men'");
126     is_deeply([ sort @bibnumbers ], [ sort($bib1_bibnum, $bib2_bibnum) ], "correct hits on 'mice and men'");
127
128     $results = C4::Search::NZanalyse('title=digression or issn=issn001');
129     ($hits, @bibnumbers) = parse_nzanalyse($results);
130     cmp_ok($hits, '==', 2, "two hits on 'title=digression or issn=issn001'");
131     is_deeply([ sort @bibnumbers ], [ sort($bib1_bibnum, $bib2_bibnum) ], "correct hits on 'title=digression or issn=issn001'");
132
133     $results = C4::Search::NZanalyse('title=digression and issn=issn002');
134     ($hits, @bibnumbers) = parse_nzanalyse($results);
135     cmp_ok($hits, '==', 1, "two hits on 'title=digression and issn=issn002'");
136     is($bib2_bibnum, $bibnumbers[0], "correct hit on 'title=digression and issn=issn002'");
137
138     $results = C4::Search::NZanalyse('mice not men');
139     ($hits, @bibnumbers) = parse_nzanalyse($results);
140     cmp_ok($hits, '==', 0, "zero hits on 'mice not men'");
141
142     $results = C4::Search::NZanalyse('mice not dog');
143     ($hits, @bibnumbers) = parse_nzanalyse($results);
144     cmp_ok($hits, '==', 1, "one hit on 'mice not dog'");
145     is($bib1_bibnum, $bibnumbers[0], "correct hit on 'mice not dog'");
146
147     $results = C4::Search::NZanalyse('isbn > a');
148     ($hits, @bibnumbers) = parse_nzanalyse($results);
149     cmp_ok($hits, '==', 2, "two hits on 'isbn > a'");
150     is_deeply([ sort @bibnumbers ], [ sort($bib1_bibnum, $bib2_bibnum) ], "correct hits on 'isbn > a'");
151
152     $results = C4::Search::NZanalyse('isbn < z');
153     ($hits, @bibnumbers) = parse_nzanalyse($results);
154     cmp_ok($hits, '==', 2, "two hits on 'isbn < z'");
155     is_deeply([ sort @bibnumbers ], [ sort($bib1_bibnum, $bib2_bibnum) ], "correct hits on 'isbn < z'");
156
157     $results = C4::Search::NZanalyse('isbn > isbn001');
158     ($hits, @bibnumbers) = parse_nzanalyse($results);
159     cmp_ok($hits, '==', 1, "one hit on 'isbn > isbn001'");
160     is($bib2_bibnum, $bibnumbers[0], "correct hit on 'isbn > isbn001'");
161
162     $results = C4::Search::NZanalyse('isbn>=isbn001');
163     ($hits, @bibnumbers) = parse_nzanalyse($results);
164     cmp_ok($hits, '==', 2, "two hits on 'isbn>=isbn001'");
165     is_deeply([ sort @bibnumbers ], [ sort($bib1_bibnum, $bib2_bibnum) ], "correct hits on 'isbn>=isbn001'");
166 }
167
168 sub parse_nzanalyse {
169     my $results = shift;
170     my @bibnumbers = ();
171     if (defined $results) {
172         # NZanalyze currently has a funky way of returning results -
173         # it does not guarantee that a biblionumber occurs only
174         # once in the results string.  Hence we must remove
175         # duplicates, like NZorder (inefficently) does
176         my %hash;
177         @bibnumbers = grep { ++$hash{$_} == 1 }  map { my @f = split /,/, $_; $f[0]; } split /;/, $results;
178     }
179     return scalar(@bibnumbers), @bibnumbers;
180 }
181
182 =head2 SHUTDOWN METHODS
183
184 These get run once, after all of the main tests methods in this module
185
186 =cut
187
188 sub shutdown_49_remove_bibs : Test( shutdown => 4 ) {
189     my $self = shift;
190     my ($bib1_bibnum, $bib2_bibnum) = @{ $self->{nozebra_test_bibs} };
191
192     my $dbh = C4::Context->dbh;
193     my $count_sth = $dbh->prepare("SELECT COUNT(*) FROM nozebra");
194     my $count;
195
196     my $error = DelBiblio($bib2_bibnum);
197     ok(!defined($error), "deleted bib $bib2_bibnum");
198     $count_sth->execute;
199     ($count) = $count_sth->fetchrow_array;
200     TODO: { local $TODO = 'nothing actually gets deleted from nozebra currently';
201     cmp_ok($count, '==', 14, "correct number of words indexed after bib $bib2_bibnum deleted"); 
202     }
203
204     $error = DelBiblio($bib1_bibnum);
205     ok(!defined($error), "deleted bib $bib1_bibnum");
206     $count_sth->execute;
207     ($count) = $count_sth->fetchrow_array;
208     TODO: { local $TODO = 'nothing actually gets deleted from nozebra currently';
209     cmp_ok($count, '==', 0, "no entries left in nozebra after bib $bib1_bibnum deleted"); 
210     }
211
212     delete $self->{nozebra_test_bibs};
213 }
214
215 sub shutdown_50_init_nozebra : Test( shutdown => 3 ) {
216     my $using_nozebra = C4::Context->preference('NoZebra');
217     ok($using_nozebra, "still in NoZebra mode");
218     my $dbh = C4::Context->dbh;
219     $dbh->do("UPDATE systempreferences SET value=0 WHERE variable='NoZebra'");
220     $dbh->do("UPDATE systempreferences SET value=1 WHERE variable in ('QueryFuzzy','QueryWeightFields','QueryStemming')");
221     $using_nozebra = C4::Context->preference('NoZebra');
222     ok(!$using_nozebra, "switched to Zebra");
223
224     # FIXME
225     $dbh->do("DELETE FROM nozebra");
226     my $sth = $dbh->prepare("SELECT COUNT(*) FROM nozebra");
227     $sth->execute;
228     my ($count) = $sth->fetchrow_array;
229     $sth->finish;
230     cmp_ok($count, '==', 0, "NoZebra index finishes up empty");
231 }
232
233 1;