1 # Copyright 2015 Catalyst IT
3 # This file is part of Koha.
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.
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.
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>.
20 use Module::Load::Conditional qw[can_load check_install requires];
29 my $schema = Koha::Database->schema();
32 modules => { 'Koha::SearchEngine::Elasticsearch::Indexer' => undef, } )
35 if ( $Module::Load::Conditional::ERROR =~ /Can\'t locate (.*?) / ) {
38 my $es_dep_msg = "Required module $missing_module is not installed";
39 plan( skip_all => $es_dep_msg );
45 use_ok('Koha::SearchEngine::Elasticsearch::Indexer');
49 $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ 'index' => 'biblio' }),
50 'Creating new indexer object'
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' )
60 my $records = [$marc_record];
61 ok( my $converted = $indexer->_convert_marc_to_json($records),
62 'Convert some records' );
64 is( $converted->count, 1, 'One converted record' );
68 eval { $indexer->get_elasticsearch_params; };
70 skip 'ElasticSeatch configuration not available', 1
73 ok( $indexer->update_index(undef,$records), 'Update Index' );
76 subtest '_convert_marc_to_json() tests' => sub {
80 $schema->storage->txn_begin;
82 t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
91 marc_type => 'marc21',
100 marc_type => 'marc21',
101 marc_field => '110a',
106 my $se = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch' );
107 $se->mock( '_foreach_mapping', sub {
108 my ($self, $sub ) = @_;
110 foreach my $map ( @mappings ) {
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' ),
131 my @records = ( $marc_record );
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");
138 $schema->storage->txn_rollback;