1 package KohaTest::Search::NoZebra;
2 use base qw( KohaTest::Search );
15 =head2 STARTUP METHODS
17 These get run once, before the main test methods in this module
21 =head3 startup_50_init_nozebra
23 Turn on NoZebra mode, for now, assumes and requires
24 that the test database has started out using Zebra.
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");
37 my $sth = $dbh->prepare("SELECT COUNT(*) FROM nozebra");
39 my ($count) = $sth->fetchrow_array;
41 cmp_ok($count, '==', 0, "NoZebra index starts off empty");
44 sub startup_51_add_bibs : Test( startup => 2 ) {
47 my $bib1 = MARC::Record->new();
48 $bib1->leader(' nam a22 7a 4500');
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'),
56 my $bib2 = MARC::Record->new();
57 $bib2->leader(' nam a22 7a 4500');
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'),
66 my $dbh = C4::Context->dbh;
67 my $count_sth = $dbh->prepare("SELECT COUNT(*) FROM nozebra");
69 my ($bib1_bibnum, $bib1_bibitemnum) = AddBiblio($bib1, '');
71 ($count) = $count_sth->fetchrow_array;
72 cmp_ok($count, '==', 14, "correct number of new words indexed"); # tokens + biblionumber + __RAW__
74 my ($bib2_bibnum, $bib2_bibitemnum) = AddBiblio($bib2, '');
76 ($count) = $count_sth->fetchrow_array;
77 cmp_ok($count, '==', 22, "correct number of new words indexed"); # tokens + biblionumber + __RAW__
79 push @{ $self->{nozebra_test_bibs} }, $bib1_bibnum, $bib2_bibnum;
88 sub basic_searches_via_nzanalyze : Test( 28 ) {
90 my ($bib1_bibnum, $bib2_bibnum) = @{ $self->{nozebra_test_bibs} };
92 my $results = C4::Search::NZanalyse('foobar');
93 ok(!defined($results), "no hits on 'foobar'");
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'");
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'");
105 $results = C4::Search::NZanalyse('isbn=dog');
106 ($hits, @bibnumbers) = parse_nzanalyse($results);
107 cmp_ok($hits, '==', 0, "zero hits on 'isbn=dog'");
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'");
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'");
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'");
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'");
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'");
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'");
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'");
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'");
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'");
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'");
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'");
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'");
168 sub parse_nzanalyse {
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
177 @bibnumbers = grep { ++$hash{$_} == 1 } map { my @f = split /,/, $_; $f[0]; } split /;/, $results;
179 return scalar(@bibnumbers), @bibnumbers;
182 =head2 SHUTDOWN METHODS
184 These get run once, after all of the main tests methods in this module
188 sub shutdown_49_remove_bibs : Test( shutdown => 4 ) {
190 my ($bib1_bibnum, $bib2_bibnum) = @{ $self->{nozebra_test_bibs} };
192 my $dbh = C4::Context->dbh;
193 my $count_sth = $dbh->prepare("SELECT COUNT(*) FROM nozebra");
196 my $error = DelBiblio($bib2_bibnum);
197 ok(!defined($error), "deleted bib $bib2_bibnum");
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");
204 $error = DelBiblio($bib1_bibnum);
205 ok(!defined($error), "deleted bib $bib1_bibnum");
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");
212 delete $self->{nozebra_test_bibs};
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");
225 $dbh->do("DELETE FROM nozebra");
226 my $sth = $dbh->prepare("SELECT COUNT(*) FROM nozebra");
228 my ($count) = $sth->fetchrow_array;
230 cmp_ok($count, '==', 0, "NoZebra index finishes up empty");