]> git.koha-community.org Git - koha.git/blob - t/db_dependent/Koha_Elasticsearch_Indexer.t
Bug 18897: Improve error message
[koha.git] / t / db_dependent / Koha_Elasticsearch_Indexer.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;
21 use Test::MockModule;
22 use t::lib::Mocks;
23
24 use MARC::Record;
25
26 use Koha::Database;
27
28 my $schema = Koha::Database->schema();
29
30 eval { require Koha::SearchEngine::Elasticsearch::Indexer; };
31 if ( $@ ) {
32     my $missing_module;
33     if ( $@ =~ /Can\'t locate (.*?) / ) {
34         $missing_module = $1;
35     }
36     my $es_dep_msg = "Required module $missing_module is not installed";
37     plan( skip_all => $es_dep_msg );
38 }
39 else {
40     plan tests => 6;
41 }
42
43 use_ok('Koha::SearchEngine::Elasticsearch::Indexer');
44
45 my $indexer;
46 ok(
47     $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ 'index' => 'biblio' }),
48     'Creating new indexer object'
49 );
50
51 my $marc_record = MARC::Record->new();
52 $marc_record->append_fields(
53     MARC::Field->new( '001', '1234567' ),
54     MARC::Field->new( '020', '', '', 'a' => '1234567890123' ),
55     MARC::Field->new( '245', '', '', 'a' => 'Title' )
56 );
57
58 my $records = [$marc_record];
59 ok( my $converted = $indexer->_convert_marc_to_json($records),
60     'Convert some records' );
61
62 is( $converted->count, 1, 'One converted record' );
63
64 SKIP: {
65
66     eval { $indexer->get_elasticsearch_params; };
67
68     skip 'ElasticSeatch configuration not available', 1
69         if $@;
70
71     ok( $indexer->update_index(undef,$records), 'Update Index' );
72 }
73
74 subtest '_convert_marc_to_json() tests' => sub {
75
76     plan tests => 2;
77
78     $schema->storage->txn_begin;
79
80     t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
81
82     my @mappings = (
83         {
84             name => 'author',
85             type => 'string',
86             facet => 1,
87             suggestible => 1,
88             sort => '~',
89             marc_type => 'marc21',
90             marc_field => '100a',
91         },
92         {
93             name => 'author',
94             type => 'string',
95             facet => 1,
96             suggestible => 1,
97             sort => '~',
98             marc_type => 'marc21',
99             marc_field => '110a',
100         },
101     );
102
103
104     my $se = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch' );
105     $se->mock( '_foreach_mapping', sub {
106         my ($self, $sub ) = @_;
107
108         foreach my $map ( @mappings ) {
109             $sub->(
110                 $map->{name},
111                 $map->{type},
112                 $map->{facet},
113                 $map->{suggestible},
114                 $map->{sort},
115                 $map->{marc_type},
116                 $map->{marc_field}
117             );
118         }
119     });
120
121     my $marc_record = MARC::Record->new();
122     $marc_record->append_fields(
123         MARC::Field->new( '001', '1234567' ),
124         MARC::Field->new( '020', '', '', 'a' => '1234567890123' ),
125         MARC::Field->new( '100', '', '', 'a' => 'Author' ),
126         MARC::Field->new( '110', '', '', 'a' => 'Corp Author' ),
127         MARC::Field->new( '245', '', '', 'a' => 'Title' ),
128     );
129     my @records = ( $marc_record );
130
131     my $importer = Koha::SearchEngine::Elasticsearch::Indexer->new({ index => 'biblios' });
132     my $conv = $importer->_convert_marc_to_json( \@records )->next();
133     is( $conv->{author}[0][0], "Author", "First mapped author should be 100a");
134     is( $conv->{author}[1][0], "Corp Author", "Second mapped author should be 110a");
135
136     $schema->storage->txn_rollback;
137 };