Koha/t/db_dependent/Koha_Elasticsearch_Indexer.t
Tomas Cohen Arazi 3b5529c3ab Bug 18434: (QA followup) Move _convert_marc_to_json tests into Indexer.t
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2017-07-06 14:29:02 -03:00

126 lines
3.5 KiB
Perl

# Copyright 2015 Catalyst IT
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use Test::More tests => 6;
use Test::MockModule;
use t::lib::Mocks;
use MARC::Record;
use Koha::Database;
my $schema = Koha::Database->schema();
use_ok('Koha::SearchEngine::Elasticsearch::Indexer');
my $indexer;
ok(
$indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ 'index' => 'biblio' }),
'Creating new indexer object'
);
my $marc_record = MARC::Record->new();
$marc_record->append_fields(
MARC::Field->new( '001', '1234567' ),
MARC::Field->new( '020', '', '', 'a' => '1234567890123' ),
MARC::Field->new( '245', '', '', 'a' => 'Title' )
);
my $records = [$marc_record];
ok( my $converted = $indexer->_convert_marc_to_json($records),
'Convert some records' );
is( $converted->count, 1, 'One converted record' );
SKIP: {
eval { $indexer->get_elasticsearch_params; };
skip 'ElasticSeatch configuration not available', 1
if $@;
ok( $indexer->update_index(undef,$records), 'Update Index' );
}
subtest '_convert_marc_to_json() tests' => sub {
plan tests => 2;
$schema->storage->txn_begin;
t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
my @mappings = (
{
name => 'author',
type => 'string',
facet => 1,
suggestible => 1,
sort => '~',
marc_type => 'marc21',
marc_field => '100a',
},
{
name => 'author',
type => 'string',
facet => 1,
suggestible => 1,
sort => '~',
marc_type => 'marc21',
marc_field => '110a',
},
);
my $se = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch' );
$se->mock( '_foreach_mapping', sub {
my ($self, $sub ) = @_;
foreach my $map ( @mappings ) {
$sub->(
$map->{name},
$map->{type},
$map->{facet},
$map->{suggestible},
$map->{sort},
$map->{marc_type},
$map->{marc_field}
);
}
});
my $marc_record = MARC::Record->new();
$marc_record->append_fields(
MARC::Field->new( '001', '1234567' ),
MARC::Field->new( '020', '', '', 'a' => '1234567890123' ),
MARC::Field->new( '100', '', '', 'a' => 'Author' ),
MARC::Field->new( '110', '', '', 'a' => 'Corp Author' ),
MARC::Field->new( '245', '', '', 'a' => 'Title' ),
);
my @records = ( $marc_record );
my $importer = Koha::SearchEngine::Elasticsearch::Indexer->new({ index => 'biblios' });
my $conv = $importer->_convert_marc_to_json( \@records )->next();
is( $conv->{author}[0][0], "Author", "First mapped author should be 100a");
is( $conv->{author}[1][0], "Corp Author", "Second mapped author should be 110a");
$schema->storage->txn_rollback;
};
1;