Bug 20248: Improve Elasticsearch mappings UI and rebuild_elastic_search.pl.
[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 => 7;
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 my $indexer;
33 ok(
34     $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ 'index' => 'biblio' }),
35     'Creating new indexer object'
36 );
37
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' )
43 );
44
45 my $records = [$marc_record];
46 ok( my $converted = $indexer->_convert_marc_to_json($records),
47     'Convert some records' );
48
49 is( $converted->count, 1, 'One converted record' );
50
51 SKIP: {
52
53     eval { $indexer->get_elasticsearch_params; };
54
55     skip 'Elasticsearch configuration not available', 1
56         if $@;
57
58     ok( $indexer->update_index(undef,$records), 'Update Index' );
59 }
60
61 subtest 'create_index() tests' => sub {
62
63     plan tests => 3;
64
65     my $se = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch' );
66     $se->mock( 'get_elasticsearch_params', sub {
67         my ($self, $sub ) = @_;
68
69         my $method = $se->original( 'get_elasticsearch_params' );
70         my $params = $method->( $self );
71         $params->{index_name} .= '__test';
72         return $params;
73     });
74
75     my $indexer;
76     ok(
77         $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ 'index' => 'biblios' }),
78         'Creating a new indexer object'
79     );
80     ok(
81         $indexer->create_index(),
82         'Creating an index'
83     );
84     $indexer->drop_index();
85     ok(
86         $indexer->drop_index(),
87         'Dropping the index'
88     );
89 };
90
91 subtest '_convert_marc_to_json() tests' => sub {
92
93     plan tests => 4;
94
95     $schema->storage->txn_begin;
96
97     t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
98
99     my @mappings = (
100         {
101             name => 'author',
102             type => 'string',
103             facet => 1,
104             suggestible => 1,
105             sort => '~',
106             marc_type => 'marc21',
107             marc_field => '100a',
108         },
109         {
110             name => 'author',
111             type => 'string',
112             facet => 1,
113             suggestible => 1,
114             sort => '~',
115             marc_type => 'marc21',
116             marc_field => '110a',
117         },
118     );
119
120
121     my $se = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch' );
122     $se->mock( '_foreach_mapping', sub {
123         my ($self, $sub ) = @_;
124
125         foreach my $map ( @mappings ) {
126             $sub->(
127                 $map->{name},
128                 $map->{type},
129                 $map->{facet},
130                 $map->{suggestible},
131                 $map->{sort},
132                 $map->{marc_type},
133                 $map->{marc_field}
134             );
135         }
136     });
137
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' ),
145     );
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' ),
152     );
153     my @records = ( $marc_record, $marc_record_2 );
154
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");
159
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");
163
164     $schema->storage->txn_rollback;
165 };