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 => 7;
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 'Elasticsearch configuration not available', 1
58 ok( $indexer->update_index(undef,$records), 'Update Index' );
61 subtest 'create_index() tests' => sub {
65 my $se = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch' );
66 $se->mock( 'get_elasticsearch_params', sub {
67 my ($self, $sub ) = @_;
69 my $method = $se->original( 'get_elasticsearch_params' );
70 my $params = $method->( $self );
71 $params->{index_name} .= '__test';
77 $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ 'index' => 'biblios' }),
78 'Creating a new indexer object'
81 $indexer->create_index(),
84 $indexer->drop_index();
86 $indexer->drop_index(),
91 subtest '_convert_marc_to_json() tests' => sub {
95 $schema->storage->txn_begin;
97 t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
106 marc_type => 'marc21',
107 marc_field => '100a',
115 marc_type => 'marc21',
116 marc_field => '110a',
121 my $se = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch' );
122 $se->mock( '_foreach_mapping', sub {
123 my ($self, $sub ) = @_;
125 foreach my $map ( @mappings ) {
138 my $marc_record = MARC::Record->new();
139 $marc_record->append_fields(
140 MARC::Field->new( '001', '1234567' ),
141 MARC::Field->new( '020', '', '', 'a' => '1234567890123' ),
142 MARC::Field->new( '100', '', '', 'a' => 'Author' ),
143 MARC::Field->new( '110', '', '', 'a' => 'Corp Author' ),
144 MARC::Field->new( '245', '', '', 'a' => 'Title' ),
146 my $marc_record_2 = MARC::Record->new();
147 $marc_record_2->append_fields(
148 MARC::Field->new( '001', '1234567' ),
149 MARC::Field->new( '020', '', '', 'a' => '1234567890123' ),
150 MARC::Field->new( '100', '', '', 'a' => 'Author' ),
151 MARC::Field->new( '245', '', '', 'a' => 'Title' ),
153 my @records = ( $marc_record, $marc_record_2 );
155 my $importer = Koha::SearchEngine::Elasticsearch::Indexer->new({ index => 'biblios' })->_convert_marc_to_json( \@records );
156 my $conv = $importer->next();
157 is( $conv->{author}[0], "Author", "First mapped author should be 100a");
158 is( $conv->{author}[1], "Corp Author", "Second mapped author should be 110a");
160 $conv = $importer->next();
161 is( $conv->{author}[0], "Author", "First mapped author should be 100a");
162 is( scalar @{$conv->{author}} , 1, "We should map field only if exists, shouldn't add extra nulls");
164 $schema->storage->txn_rollback;