Bug 20144: [sql_modes] Set default value for biblio.datecreated in tests
[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 => 49;
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 => undef,
66             marc_type => 'marc21',
67             marc_field => '100a',
68         },
69         {
70             name => 'author',
71             type => 'string',
72             facet => 1,
73             suggestible => 1,
74             sort => 1,
75             marc_type => 'marc21',
76             marc_field => '110a',
77         },
78     );
79
80     $see->get_elasticsearch_mappings(); #sort_fields will call this and use the actual db values unless we call it first
81     my $result = $see->get_fixer_rules();
82     is( $result->[0], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{.$append', -split => 1)});
83     is( $result->[1], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__facet.$append', -split => 1)});
84     is( $result->[2], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__suggestion.input.$append')});
85     is( $result->[3], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__sort.$append', -split => 1)});
86     is( $result->[4], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{.$append', -split => 1)});
87     is( $result->[5], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__facet.$append', -split => 1)});
88     is( $result->[6], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__suggestion.input.$append')});
89     is( $result->[7], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__sort.$append', -split => 1)});
90     is( $result->[8], q{move_field(_id,es_id)});
91
92     $mappings[0]->{type}  = 'boolean';
93     $mappings[1]->{type}  = 'boolean';
94     $result = $see->get_fixer_rules();
95     is( $result->[0], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{.$append', -split => 1)});
96     is( $result->[1], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__facet.$append', -split => 1)});
97     is( $result->[2], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__suggestion.input.$append')});
98     is( $result->[3], q{unless exists('} . $mappings[0]->{name} . q{') add_field('} . $mappings[0]->{name} . q{', 0) end});
99     is( $result->[4], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__sort.$append', -split => 1)});
100     is( $result->[5], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{.$append', -split => 1)});
101     is( $result->[6], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__facet.$append', -split => 1)});
102     is( $result->[7], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__suggestion.input.$append')});
103     is( $result->[8], q{unless exists('} . $mappings[1]->{name} . q{') add_field('} . $mappings[1]->{name} . q{', 0) end});
104     is( $result->[9], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__sort.$append', -split => 1)});
105     is( $result->[10], q{move_field(_id,es_id)});
106
107     $mappings[0]->{type}  = 'sum';
108     $mappings[1]->{type}  = 'sum';
109     $result = $see->get_fixer_rules();
110     is( $result->[0], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{.$append', )});
111     is( $result->[1], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__facet.$append', )});
112     is( $result->[2], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__suggestion.input.$append')});
113     is( $result->[3], q{sum('} . $mappings[0]->{name} . q{')});
114     is( $result->[4], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__sort.$append', )});
115     is( $result->[5], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{.$append', )});
116     is( $result->[6], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__facet.$append', )});
117     is( $result->[7], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__suggestion.input.$append')});
118     is( $result->[8], q{sum('} . $mappings[1]->{name} . q{')});
119     is( $result->[9], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__sort.$append', )});
120     is( $result->[10], q{move_field(_id,es_id)});
121
122     $mappings[0]->{type}  = 'string';
123     $mappings[0]->{facet} = 0;
124     $mappings[1]->{type}  = 'string';
125     $mappings[1]->{facet} = 0;
126
127     $result = $see->get_fixer_rules();
128     is( $result->[0], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{.$append', -split => 1)});
129     is( $result->[1], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__suggestion.input.$append')});
130     is( $result->[2], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__sort.$append', -split => 1)});
131     is( $result->[3], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{.$append', -split => 1)});
132     is( $result->[4], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__suggestion.input.$append')});
133     is( $result->[5], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__sort.$append', -split => 1)});
134     is( $result->[6], q{move_field(_id,es_id)});
135
136     $mappings[0]->{suggestible}  = 0;
137     $mappings[1]->{suggestible}  = 0;
138
139     $result = $see->get_fixer_rules();
140     is( $result->[0], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{.$append', -split => 1)});
141     is( $result->[1], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__sort.$append', -split => 1)});
142     is( $result->[2], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{.$append', -split => 1)});
143     is( $result->[3], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__sort.$append', -split => 1)});
144     is( $result->[4], q{move_field(_id,es_id)});
145
146     $mappings[0]->{sort}  = 0;
147     $mappings[1]->{sort}  = undef;
148
149     $see->get_elasticsearch_mappings(); #sort_fields will call this and use the actual db values unless we call it first
150     $result = $see->get_fixer_rules();
151     is( $result->[0], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{.$append', -split => 1)});
152     is( $result->[1], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{.$append', -split => 1)});
153     is( $result->[2], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__sort.$append', -split => 1)});
154     is( $result->[3], q{move_field(_id,es_id)});
155
156     t::lib::Mocks::mock_preference( 'marcflavour', 'UNIMARC' );
157
158     $result = $see->get_fixer_rules();
159     is( $result->[0], q{move_field(_id,es_id)});
160     is( $result->[1], undef, q{No mapping when marc_type doesn't match marchflavour} );
161
162     $schema->storage->txn_rollback;
163
164 };