Bug 18434: (QA followup) Move _convert_marc_to_json tests into Indexer.t
[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 => 6;
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 'ElasticSeatch configuration not available', 1
56         if $@;
57
58     ok( $indexer->update_index(undef,$records), 'Update Index' );
59 }
60
61 subtest '_convert_marc_to_json() tests' => sub {
62
63     plan tests => 2;
64
65     $schema->storage->txn_begin;
66
67     t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
68
69     my @mappings = (
70         {
71             name => 'author',
72             type => 'string',
73             facet => 1,
74             suggestible => 1,
75             sort => '~',
76             marc_type => 'marc21',
77             marc_field => '100a',
78         },
79         {
80             name => 'author',
81             type => 'string',
82             facet => 1,
83             suggestible => 1,
84             sort => '~',
85             marc_type => 'marc21',
86             marc_field => '110a',
87         },
88     );
89
90
91     my $se = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch' );
92     $se->mock( '_foreach_mapping', sub {
93         my ($self, $sub ) = @_;
94
95         foreach my $map ( @mappings ) {
96             $sub->(
97                 $map->{name},
98                 $map->{type},
99                 $map->{facet},
100                 $map->{suggestible},
101                 $map->{sort},
102                 $map->{marc_type},
103                 $map->{marc_field}
104             );
105         }
106     });
107
108     my $marc_record = MARC::Record->new();
109     $marc_record->append_fields(
110         MARC::Field->new( '001', '1234567' ),
111         MARC::Field->new( '020', '', '', 'a' => '1234567890123' ),
112         MARC::Field->new( '100', '', '', 'a' => 'Author' ),
113         MARC::Field->new( '110', '', '', 'a' => 'Corp Author' ),
114         MARC::Field->new( '245', '', '', 'a' => 'Title' ),
115     );
116     my @records = ( $marc_record );
117
118     my $importer = Koha::SearchEngine::Elasticsearch::Indexer->new({ index => 'biblios' });
119     my $conv = $importer->_convert_marc_to_json( \@records )->next();
120     is( $conv->{author}[0][0], "Author", "First mapped author should be 100a");
121     is( $conv->{author}[1][0], "Corp Author", "Second mapped author should be 110a");
122
123     $schema->storage->txn_rollback;
124 };
125
126 1;