80bfe7995628ab7f6b658597b51fb6857722dfe5
[koha.git] / t / db_dependent / Koha_Elasticsearch_Indexer.t
1 # Copyright 2015 Catalyst IT
2 #
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 2;
21 use Test::MockModule;
22 use t::lib::Mocks;
23
24 use MARC::Record;
25
26 use Koha::Database;
27
28 my $schema = Koha::Database->schema();
29
30 use_ok('Koha::SearchEngine::Elasticsearch::Indexer');
31
32 subtest 'create_index() tests' => sub {
33     plan tests => 4;
34     my $se = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch' );
35     $se->mock( 'get_elasticsearch_params', sub {
36             my ($self, $sub ) = @_;
37             my $method = $se->original( 'get_elasticsearch_params' );
38             my $params = $method->( $self );
39             $params->{index_name} .= '__test';
40             return $params;
41         });
42
43     my $indexer;
44     ok(
45         $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ 'index' => 'biblios' }),
46         'Creating a new indexer object'
47     );
48
49     is(
50         $indexer->create_index(),
51         Koha::SearchEngine::Elasticsearch::Indexer::INDEX_STATUS_OK(),
52         'Creating an index'
53     );
54
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')
60     );
61     my $records = [$marc_record];
62     ok($indexer->update_index(undef, $records), 'Update Index');
63
64     is(
65         $indexer->drop_index(),
66         Koha::SearchEngine::Elasticsearch::Indexer::INDEX_STATUS_RECREATE_REQUIRED(),
67         'Dropping the index'
68     );
69 };