Bug 18434: Add tests for K:SE:E::get_fixer_rules
[koha.git] / t / db_dependent / Koha_Elasticsearch.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
23 use t::lib::Mocks;
24 use MARC::Record;
25 use Koha::SearchEngine::Elasticsearch::Indexer;
26 use JSON::XS;
27
28 my $schema = Koha::Database->schema;
29
30 use_ok('Koha::SearchEngine::Elasticsearch');
31
32 subtest 'get_fixer_rules() tests' => sub {
33
34     plan tests => 47;
35
36     $schema->storage->txn_begin;
37
38     t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
39
40     my @mappings;
41
42     my $se = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch' );
43     $se->mock( '_foreach_mapping', sub {
44         my ($self, $sub ) = @_;
45
46         foreach my $map ( @mappings ) {
47             $sub->(
48                 $map->{name},
49                 $map->{type},
50                 $map->{facet},
51                 $map->{suggestible},
52                 $map->{sort},
53                 $map->{marc_type},
54                 $map->{marc_field}
55             );
56         }
57     });
58
59     my $see = Koha::SearchEngine::Elasticsearch->new({ index => 'biblios' });
60
61     @mappings = (
62         {
63             name => 'author',
64             type => 'string',
65             facet => 1,
66             suggestible => 1,
67             sort => '~',
68             marc_type => 'marc21',
69             marc_field => '100a',
70         },
71         {
72             name => 'author',
73             type => 'string',
74             facet => 1,
75             suggestible => 1,
76             sort => '~',
77             marc_type => 'marc21',
78             marc_field => '110a',
79         },
80     );
81
82     my $marc_record = MARC::Record->new();
83     $marc_record->append_fields(
84         MARC::Field->new( '001', '1234567' ),
85         MARC::Field->new( '020', '', '', 'a' => '1234567890123' ),
86         MARC::Field->new( '100', '', '', 'a' => 'Author' ),
87         MARC::Field->new( '110', '', '', 'a' => 'Corp Author' ),
88         MARC::Field->new( '245', '', '', 'a' => 'Title' ),
89     );
90     my @records = ( $marc_record );
91
92     my $importer = Koha::SearchEngine::Elasticsearch::Indexer->new({ index => 'biblios' });
93     my $conv = $importer->_convert_marc_to_json( \@records )->next();
94     is( $conv->{author}[0][0], "Author", "First mapped author should be 100a");
95     is( $conv->{author}[1][0], "Corp Author", "Second mapped author should be 110a");
96
97     my $result = $see->get_fixer_rules();
98     is( $result->[0], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{.$append', -split => 1)});
99     is( $result->[1], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__facet', -split => 1)});
100     is( $result->[2], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__suggestion.input.$append')});
101     is( $result->[3], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__sort', -split => 1)});
102     is( $result->[4], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{.$append', -split => 1)});
103     is( $result->[5], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__facet', -split => 1)});
104     is( $result->[6], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__suggestion.input.$append')});
105     is( $result->[7], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__sort', -split => 1)});
106     is( $result->[8], q{move_field(_id,es_id)});
107
108     $mappings[0]->{type}  = 'boolean';
109     $mappings[1]->{type}  = 'boolean';
110     $result = $see->get_fixer_rules();
111     is( $result->[0], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{.$append', -split => 1)});
112     is( $result->[1], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__facet', -split => 1)});
113     is( $result->[2], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__suggestion.input.$append')});
114     is( $result->[3], q{unless exists('} . $mappings[0]->{name} . q{') add_field('} . $mappings[0]->{name} . q{', 0) end});
115     is( $result->[4], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__sort', -split => 1)});
116     is( $result->[5], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{.$append', -split => 1)});
117     is( $result->[6], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__facet', -split => 1)});
118     is( $result->[7], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__suggestion.input.$append')});
119     is( $result->[8], q{unless exists('} . $mappings[1]->{name} . q{') add_field('} . $mappings[1]->{name} . q{', 0) end});
120     is( $result->[9], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__sort', -split => 1)});
121     is( $result->[10], q{move_field(_id,es_id)});
122
123     $mappings[0]->{type}  = 'sum';
124     $mappings[1]->{type}  = 'sum';
125     $result = $see->get_fixer_rules();
126     is( $result->[0], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{.$append', )});
127     is( $result->[1], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__facet', )});
128     is( $result->[2], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__suggestion.input.$append')});
129     is( $result->[3], q{sum('} . $mappings[0]->{name} . q{')});
130     is( $result->[4], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__sort', )});
131     is( $result->[5], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{.$append', )});
132     is( $result->[6], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__facet', )});
133     is( $result->[7], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__suggestion.input.$append')});
134     is( $result->[8], q{sum('} . $mappings[1]->{name} . q{')});
135     is( $result->[9], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__sort', )});
136     is( $result->[10], q{move_field(_id,es_id)});
137
138     $mappings[0]->{type}  = 'string';
139     $mappings[0]->{facet} = 0;
140     $mappings[1]->{type}  = 'string';
141     $mappings[1]->{facet} = 0;
142
143     $result = $see->get_fixer_rules();
144     is( $result->[0], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{.$append', -split => 1)});
145     is( $result->[1], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__suggestion.input.$append')});
146     is( $result->[2], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__sort', -split => 1)});
147     is( $result->[3], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{.$append', -split => 1)});
148     is( $result->[4], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__suggestion.input.$append')});
149     is( $result->[5], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__sort', -split => 1)});
150     is( $result->[6], q{move_field(_id,es_id)});
151
152     $mappings[0]->{suggestible}  = 0;
153     $mappings[1]->{suggestible}  = 0;
154
155     $result = $see->get_fixer_rules();
156     is( $result->[0], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{.$append', -split => 1)});
157     is( $result->[1], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__sort', -split => 1)});
158     is( $result->[2], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{.$append', -split => 1)});
159     is( $result->[3], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__sort', -split => 1)});
160     is( $result->[4], q{move_field(_id,es_id)});
161
162     t::lib::Mocks::mock_preference( 'marcflavour', 'UNIMARC' );
163
164     $result = $see->get_fixer_rules();
165     is( $result->[0], q{move_field(_id,es_id)});
166     is( $result->[1], undef, q{No mapping when marc_type doesn't match marchflavour} );
167
168     $schema->storage->txn_rollback;
169
170 };
171
172 1;