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