Bug 18624: Regression test for 'any' vs 'all'
[koha.git] / t / db_dependent / Koha / SearchEngine / Elasticsearch / QueryBuilder.t
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 tests => 1;
21 use Test::Exception;
22
23 use Koha::Database;
24 use Koha::SearchEngine::Elasticsearch::QueryBuilder;
25
26 subtest 'build_authorities_query_compat() tests' => sub {
27
28     my $qb;
29
30     ok(
31         $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => 'authorities' }),
32         'Creating new query builder object for authorities'
33     );
34
35     my $koha_to_index_name = $Koha::SearchEngine::Elasticsearch::QueryBuilder::koha_to_index_name;
36     my $search_term = 'a';
37
38     foreach my $koha_name ( keys %{ $koha_to_index_name } ) {
39         my $query = $qb->build_authorities_query_compat( [ $koha_name ],  undef, undef, ['contains'], [$search_term], 'AUTH_TYPE', 'asc' );
40         if ( $koha_name eq 'all' ) {
41             is( $query->{query}->{bool}->{should}[0]->{match}->{_all},
42                 $search_term);
43         } else {
44             is( $query->{query}->{bool}->{should}[0]->{match}->{$koha_to_index_name->{$koha_name}},
45                 $search_term);
46         }
47     }
48
49     # Failing case
50     throws_ok {
51         $qb->build_authorities_query_compat( [ 'tomas' ],  undef, undef, ['contains'], [$search_term], 'AUTH_TYPE', 'asc' );
52     }
53     'Koha::Exceptions::WrongParameter',
54         'Exception thrown on invalid value in the marclist param';
55 };
56
57 1;