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