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>.
28 my $schema = Koha::Database->schema();
30 eval { require Koha::SearchEngine::Elasticsearch::Indexer; };
33 if ( $@ =~ /Can\'t locate (.*?) / ) {
36 my $es_dep_msg = "Required module $missing_module is not installed";
37 plan( skip_all => $es_dep_msg );
43 use_ok('Koha::SearchEngine::Elasticsearch::Indexer');
47 $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ 'index' => 'biblio' }),
48 'Creating new indexer object'
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' )
58 my $records = [$marc_record];
59 ok( my $converted = $indexer->_convert_marc_to_json($records),
60 'Convert some records' );
62 is( $converted->count, 1, 'One converted record' );
66 eval { $indexer->get_elasticsearch_params; };
68 skip 'ElasticSeatch configuration not available', 1
71 ok( $indexer->update_index(undef,$records), 'Update Index' );
74 subtest '_convert_marc_to_json() tests' => sub {
78 $schema->storage->txn_begin;
80 t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
89 marc_type => 'marc21',
98 marc_type => 'marc21',
104 my $se = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch' );
105 $se->mock( '_foreach_mapping', sub {
106 my ($self, $sub ) = @_;
108 foreach my $map ( @mappings ) {
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' ),
129 my @records = ( $marc_record );
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");
136 $schema->storage->txn_rollback;