Bug 19116: (followup) Add tests to highlight the problem in CheckReserves
[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
26 my $schema = Koha::Database->schema;
27
28 use_ok('Koha::SearchEngine::Elasticsearch');
29
30 subtest 'get_fixer_rules() tests' => sub {
31
32     plan tests => 45;
33
34     $schema->storage->txn_begin;
35
36     t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
37
38     my @mappings;
39
40     my $se = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch' );
41     $se->mock( '_foreach_mapping', sub {
42         my ($self, $sub ) = @_;
43
44         foreach my $map ( @mappings ) {
45             $sub->(
46                 $map->{name},
47                 $map->{type},
48                 $map->{facet},
49                 $map->{suggestible},
50                 $map->{sort},
51                 $map->{marc_type},
52                 $map->{marc_field}
53             );
54         }
55     });
56
57     my $see = Koha::SearchEngine::Elasticsearch->new({ index => 'biblios' });
58
59     @mappings = (
60         {
61             name => 'author',
62             type => 'string',
63             facet => 1,
64             suggestible => 1,
65             sort => '~',
66             marc_type => 'marc21',
67             marc_field => '100a',
68         },
69         {
70             name => 'author',
71             type => 'string',
72             facet => 1,
73             suggestible => 1,
74             sort => '~',
75             marc_type => 'marc21',
76             marc_field => '110a',
77         },
78     );
79
80     my $result = $see->get_fixer_rules();
81     is( $result->[0], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{.$append', -split => 1)});
82     is( $result->[1], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__facet.$append', -split => 1)});
83     is( $result->[2], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__suggestion.input.$append')});
84     is( $result->[3], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__sort.$append', -split => 1)});
85     is( $result->[4], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{.$append', -split => 1)});
86     is( $result->[5], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__facet.$append', -split => 1)});
87     is( $result->[6], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__suggestion.input.$append')});
88     is( $result->[7], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__sort.$append', -split => 1)});
89     is( $result->[8], q{move_field(_id,es_id)});
90
91     $mappings[0]->{type}  = 'boolean';
92     $mappings[1]->{type}  = 'boolean';
93     $result = $see->get_fixer_rules();
94     is( $result->[0], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{.$append', -split => 1)});
95     is( $result->[1], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__facet.$append', -split => 1)});
96     is( $result->[2], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__suggestion.input.$append')});
97     is( $result->[3], q{unless exists('} . $mappings[0]->{name} . q{') add_field('} . $mappings[0]->{name} . q{', 0) end});
98     is( $result->[4], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__sort.$append', -split => 1)});
99     is( $result->[5], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{.$append', -split => 1)});
100     is( $result->[6], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__facet.$append', -split => 1)});
101     is( $result->[7], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__suggestion.input.$append')});
102     is( $result->[8], q{unless exists('} . $mappings[1]->{name} . q{') add_field('} . $mappings[1]->{name} . q{', 0) end});
103     is( $result->[9], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__sort.$append', -split => 1)});
104     is( $result->[10], q{move_field(_id,es_id)});
105
106     $mappings[0]->{type}  = 'sum';
107     $mappings[1]->{type}  = 'sum';
108     $result = $see->get_fixer_rules();
109     is( $result->[0], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{.$append', )});
110     is( $result->[1], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__facet.$append', )});
111     is( $result->[2], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__suggestion.input.$append')});
112     is( $result->[3], q{sum('} . $mappings[0]->{name} . q{')});
113     is( $result->[4], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__sort.$append', )});
114     is( $result->[5], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{.$append', )});
115     is( $result->[6], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__facet.$append', )});
116     is( $result->[7], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__suggestion.input.$append')});
117     is( $result->[8], q{sum('} . $mappings[1]->{name} . q{')});
118     is( $result->[9], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__sort.$append', )});
119     is( $result->[10], q{move_field(_id,es_id)});
120
121     $mappings[0]->{type}  = 'string';
122     $mappings[0]->{facet} = 0;
123     $mappings[1]->{type}  = 'string';
124     $mappings[1]->{facet} = 0;
125
126     $result = $see->get_fixer_rules();
127     is( $result->[0], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{.$append', -split => 1)});
128     is( $result->[1], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__suggestion.input.$append')});
129     is( $result->[2], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__sort.$append', -split => 1)});
130     is( $result->[3], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{.$append', -split => 1)});
131     is( $result->[4], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__suggestion.input.$append')});
132     is( $result->[5], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__sort.$append', -split => 1)});
133     is( $result->[6], q{move_field(_id,es_id)});
134
135     $mappings[0]->{suggestible}  = 0;
136     $mappings[1]->{suggestible}  = 0;
137
138     $result = $see->get_fixer_rules();
139     is( $result->[0], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{.$append', -split => 1)});
140     is( $result->[1], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__sort.$append', -split => 1)});
141     is( $result->[2], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{.$append', -split => 1)});
142     is( $result->[3], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__sort.$append', -split => 1)});
143     is( $result->[4], q{move_field(_id,es_id)});
144
145     t::lib::Mocks::mock_preference( 'marcflavour', 'UNIMARC' );
146
147     $result = $see->get_fixer_rules();
148     is( $result->[0], q{move_field(_id,es_id)});
149     is( $result->[1], undef, q{No mapping when marc_type doesn't match marchflavour} );
150
151     $schema->storage->txn_rollback;
152
153 };