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 Test::More tests => 6;
28 my $schema = Koha::Database->schema();
30 use_ok('Koha::SearchEngine::Elasticsearch::Indexer');
34 $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ 'index' => 'biblio' }),
35 'Creating new indexer object'
38 my $marc_record = MARC::Record->new();
39 $marc_record->append_fields(
40 MARC::Field->new( '001', '1234567' ),
41 MARC::Field->new( '020', '', '', 'a' => '1234567890123' ),
42 MARC::Field->new( '245', '', '', 'a' => 'Title' )
45 my $records = [$marc_record];
46 ok( my $converted = $indexer->_convert_marc_to_json($records),
47 'Convert some records' );
49 is( $converted->count, 1, 'One converted record' );
53 eval { $indexer->get_elasticsearch_params; };
55 skip 'ElasticSeatch configuration not available', 1
58 ok( $indexer->update_index(undef,$records), 'Update Index' );
61 subtest '_convert_marc_to_json() tests' => sub {
65 $schema->storage->txn_begin;
67 t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
76 marc_type => 'marc21',
85 marc_type => 'marc21',
91 my $se = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch' );
92 $se->mock( '_foreach_mapping', sub {
93 my ($self, $sub ) = @_;
95 foreach my $map ( @mappings ) {
108 my $marc_record = MARC::Record->new();
109 $marc_record->append_fields(
110 MARC::Field->new( '001', '1234567' ),
111 MARC::Field->new( '020', '', '', 'a' => '1234567890123' ),
112 MARC::Field->new( '100', '', '', 'a' => 'Author' ),
113 MARC::Field->new( '110', '', '', 'a' => 'Corp Author' ),
114 MARC::Field->new( '245', '', '', 'a' => 'Title' ),
116 my $marc_record_2 = MARC::Record->new();
117 $marc_record_2->append_fields(
118 MARC::Field->new( '001', '1234567' ),
119 MARC::Field->new( '020', '', '', 'a' => '1234567890123' ),
120 MARC::Field->new( '100', '', '', 'a' => 'Author' ),
121 MARC::Field->new( '245', '', '', 'a' => 'Title' ),
123 my @records = ( $marc_record, $marc_record_2 );
125 my $importer = Koha::SearchEngine::Elasticsearch::Indexer->new({ index => 'biblios' })->_convert_marc_to_json( \@records );
126 my $conv = $importer->next();
127 is( $conv->{author}[0], "Author", "First mapped author should be 100a");
128 is( $conv->{author}[1], "Corp Author", "Second mapped author should be 110a");
130 $conv = $importer->next();
131 is( $conv->{author}[0], "Author", "First mapped author should be 100a");
132 is( scalar @{$conv->{author}} , 1, "We should map field only if exists, shouldn't add extra nulls");
134 $schema->storage->txn_rollback;