Bug 29561: Unit test
[koha.git] / t / db_dependent / Koha / SearchEngine / Elasticsearch / Search.t
1 # Copyright 2015 Catalyst IT
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 => 14;
21 use t::lib::Mocks;
22
23 use Koha::SearchEngine::Elasticsearch::QueryBuilder;
24 use Koha::SearchEngine::Elasticsearch::Indexer;
25
26
27 my $se = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch' );
28 $se->mock( 'get_elasticsearch_mappings', sub {
29     my ($self) = @_;
30
31     my %all_mappings;
32
33     my $mappings = {
34         properties => {
35             title => {
36                 type => 'text'
37             },
38             title__sort => {
39                 type => 'text'
40             },
41             subject => {
42                 type => 'text'
43             },
44             itemnumber => {
45                 type => 'integer'
46             },
47             sortablenumber => {
48                 type => 'integer'
49             },
50             sortablenumber__sort => {
51                 type => 'integer'
52             }
53         }
54     };
55     $all_mappings{$self->index} = $mappings;
56
57     my $sort_fields = {
58         $self->index => {
59             title => 1,
60             subject => 0,
61             itemnumber => 0,
62             sortablenumber => 1
63         }
64     };
65     $self->sort_fields($sort_fields->{$self->index});
66
67     return $all_mappings{$self->index};
68 });
69
70 my $builder = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { index => 'mydb' } );
71
72 use_ok('Koha::SearchEngine::Elasticsearch::Search');
73
74 ok(
75     my $searcher = Koha::SearchEngine::Elasticsearch::Search->new(
76         { 'nodes' => ['localhost:9200'], 'index' => 'mydb' }
77     ),
78     'Creating a Koha::SearchEngine::Elasticsearch::Search object'
79 );
80
81 is( $searcher->index, 'mydb', 'Testing basic accessor' );
82
83 ok( my $query = $builder->build_query('easy'), 'Build a search query');
84
85 SKIP: {
86
87     eval { $builder->get_elasticsearch_params; };
88
89     skip 'Elasticsearch configuration not available', 9
90         if $@;
91
92     Koha::SearchEngine::Elasticsearch::Indexer->new({ index => 'mydb' })->drop_index;
93     Koha::SearchEngine::Elasticsearch::Indexer->new({ index => 'mydb' })->create_index;
94
95     ok( my $results = $searcher->search( $query) , 'Do a search ' );
96
97     is (my $count = $searcher->count( $query ), 0 , 'Get a count of the results, without returning results ');
98
99     ok ($results = $searcher->search_compat( $query ), 'Test search_compat' );
100
101     my ( undef, $scan_query ) = $builder->build_query_compat( undef, ['easy'], [], undef, undef, 1 );
102     ok ((undef, $results) = $searcher->search_compat( $scan_query, undef, [], [], 20, 0, undef, undef, undef, 1 ), 'Test search_compat scan query' );
103     my $expected = {
104         biblioserver => {
105             hits => 0,
106             RECORDS => []
107         }
108     };
109     is_deeply($results, $expected, 'Scan query results ok');
110
111     ok (($results,$count) = $searcher->search_auth_compat ( $query ), 'Test search_auth_compat' );
112
113     is ( $count = $searcher->count_auth_use($searcher,1), 0, 'Testing count_auth_use');
114
115     is ($searcher->max_result_window, 1000000, 'By default, max_result_window is 1000000');
116
117     $searcher->get_elasticsearch()->indices->put_settings(
118         index => $searcher->index_name,
119         body => {
120             'index' => {
121                 'max_result_window' => 12000,
122             },
123         }
124     );
125     is ($searcher->max_result_window, 12000, 'max_result_window returns the correct value');
126
127     subtest "_convert_facets" => sub {
128         plan tests => 2;
129
130         my $es_facets = {
131             'ln' => {
132                     'sum_other_doc_count' => 0,
133                     'buckets' => [
134                         {
135                             'doc_count' => 2,
136                             'key' => 'eng'
137                         },
138                         {
139                             'doc_count' => 12,
140                             'key' => ''
141                         }
142                     ],
143                     'doc_count_error_upper_bound' => 0
144             }
145         };
146
147         my $koha_facets = $searcher->_convert_facets($es_facets);
148         is(@{$koha_facets->[0]->{facets}},1,"We only get one facet, blank is removed");
149
150         $es_facets->{ln}->{buckets}->[1]->{key} = '0';
151         $koha_facets = $searcher->_convert_facets($es_facets);
152         is(@{$koha_facets->[0]->{facets}},2,"We get two facets, '0' is not removed");
153
154     };
155 }