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 => 3;
28 my $schema = Koha::Database->schema();
30 use_ok('Koha::SearchEngine::Elasticsearch::Indexer');
32 subtest 'create_index() tests' => sub {
34 my $se = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch' );
35 $se->mock( '_read_configuration', sub {
36 my ($self, $sub ) = @_;
37 my $method = $se->original( '_read_configuration' );
38 my $conf = $method->( $self );
39 $conf->{index_name} .= '__test';
45 $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ 'index' => 'biblios' }),
46 'Creating a new indexer object'
50 $indexer->create_index(),
51 Koha::SearchEngine::Elasticsearch::Indexer::INDEX_STATUS_OK(),
55 my $marc_record = MARC::Record->new();
56 $marc_record->append_fields(
57 MARC::Field->new('001', '1234567'),
58 MARC::Field->new('020', '', '', 'a' => '1234567890123'),
59 MARC::Field->new('245', '', '', 'a' => 'Title')
61 my $records = [$marc_record];
63 my $response = $indexer->update_index([1], $records);
64 is( $response->{errors}, 0, "no error on update_index" );
65 is( scalar(@{$response->{items}}), 1, "1 item indexed" );
68 $indexer->drop_index(),
69 Koha::SearchEngine::Elasticsearch::Indexer::INDEX_STATUS_RECREATE_REQUIRED(),
75 subtest 'update_index() tests' => sub {
77 my $kse = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch' );
78 $kse->mock( 'marc_records_to_documents', sub {
79 my ($self, $params ) = @_;
85 $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ 'index' => 'biblios' }),
86 'Creating a new indexer object'
89 my $searcher = $indexer->get_elasticsearch();
90 my $se = Test::MockModule->new( ref $searcher );
91 $se->mock( 'bulk', sub {
92 my ($self, %params ) = @_;
96 my $bibnumber_array = $indexer->update_index([13],["faked"]);
97 is( $bibnumber_array->[0]->{index}->{_id},"13", "We should get a string matching the bibnumber");