Add release notes for Koha 16.05.19
[koha.git] / t / Matcher.t
1 #!/usr/bin/perl
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;
21 use Test::MockModule;
22
23 use MARC::Record;
24
25 use Module::Load::Conditional qw/check_install/;
26
27 BEGIN {
28     if ( check_install( module => 'Test::DBIx::Class' ) ) {
29         plan tests => 12;
30     } else {
31         plan skip_all => "Need Test::DBIx::Class"
32     }
33 }
34
35 use_ok('C4::Matcher');
36
37 use Test::DBIx::Class {
38     schema_class => 'Koha::Schema',
39     connect_info => ['dbi:SQLite:dbname=:memory:','',''],
40     connect_opts => { name_sep => '.', quote_char => '`', },
41     fixture_class => '::Populate',
42 }, 'MarcMatcher' ;
43
44 fixtures_ok [
45     MarcMatcher => [
46         [ 'matcher_id', 'code', 'description', 'record_type', 'threshold' ],
47         [ 1,            'ISBN', 'ISBN',        'red',         1 ],
48         [ 2,            'ISSN', 'ISSN',        'blue',        0 ]
49     ],
50 ], 'add fixtures';
51
52 my $db = Test::MockModule->new('Koha::Database');
53 $db->mock( _new_schema => sub { return Schema(); } );
54
55 my @matchers = C4::Matcher::GetMatcherList();
56
57 is( $matchers[0]->{'matcher_id'}, 1, 'First matcher_id value is 1' );
58
59 is( $matchers[1]->{'matcher_id'}, 2, 'Second matcher_id value is 2' );
60
61 my $matcher_id = C4::Matcher::GetMatcherId('ISBN');
62
63 is( $matcher_id, 1, 'testing getmatcherid' );
64
65 my $testmatcher;
66
67 ok( $testmatcher = C4::Matcher->new( 'red', 1 ), 'testing matcher new' );
68
69 ok( $testmatcher = C4::Matcher->new( 'blue', 0 ), 'testing matcher new' );
70
71 $testmatcher->threshold(1000);
72
73 is( $testmatcher->threshold(), 1000, 'testing threshhold accessor method' );
74
75 $testmatcher->_id(53);
76
77 is( $testmatcher->_id(), 53, 'testing _id accessor' );
78
79 $testmatcher->code('match on ISBN');
80
81 is( $testmatcher->code(), 'match on ISBN', 'testing code accessor' );
82
83 $testmatcher->description('match on ISSN');
84
85 is( $testmatcher->description(), 'match on ISSN', 'testing code accessor' );
86
87 subtest '_get_match_keys() tests' => sub {
88
89     plan tests => 8;
90
91     my $matchpoint = get_title_matchpoint({
92         length => 0,
93         norms  => [],
94         offset => 0
95     });
96
97     my $record = MARC::Record->new();
98     $record->append_fields(
99         MARC::Field->new('100', '1', ' ',
100                             a => 'King, Stephen',
101                             d => 'd1947-'),
102         MARC::Field->new('245', ' ', ' ',
103                             a => '  .; thE t[]:,aliS(m)/An\'"',
104                             c => 'Stephen King, Peter Straub.' ),
105         MARC::Field->new('700', ' ', ' ',
106                             a => 'Straub, Peter',
107                             d => '1943-')
108     );
109
110     my @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
111
112     is( $keys[0], 'THE TALISMAN STEPHEN KING PETER STRAUB',
113         'Match key correctly calculated with no $norms');
114
115     $matchpoint = get_title_matchpoint({
116         length => 9,
117         norms  => [],
118         offset => 0
119     });
120     @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
121     is( $keys[0], 'THE',
122         'Match key correctly calculated with length 9');
123
124     $matchpoint = get_title_matchpoint({
125         length => 9,
126         norms  => [],
127         offset => 1
128     });
129     @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
130     is( $keys[0], 'THE',
131         'Match key correctly calculated with length 9 and offset 1');
132
133     $matchpoint = get_title_matchpoint({
134         length => 9,
135         norms  => [],
136         offset => 2
137     });
138     @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
139     is( $keys[0], 'THE T',
140         'Match key correctly calculated with length 9 and offset 2, should not remove space');
141
142     $matchpoint = get_authors_matchpoint({
143         length => 0,
144         norms  => [],
145         offset => 0
146     });
147     @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
148     is( $keys[0], 'STRAUB PETER KING STEPHEN',
149         'Match key correctly calculated with multiple components');
150
151     $matchpoint = get_authors_matchpoint({
152         length => 9,
153         norms  => [],
154         offset => 0
155     });
156     @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
157     is( $keys[0], 'STRAUB KING ST',
158         'Match key correctly calculated with multiple components, length 9');
159
160     $matchpoint = get_authors_matchpoint({
161         length => 10,
162         norms  => [],
163         offset => 0
164     });
165     @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
166     is( $keys[0], 'STRAUB P KING STE',
167         'Match key correctly calculated with multiple components, length 10');
168
169     $matchpoint = get_authors_matchpoint({
170         length => 10,
171         norms  => [],
172         offset => 2
173     });
174     @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
175     is( $keys[0], 'TRAUB PET ING STEPH',
176         'Match key correctly calculated with multiple components, length 10, offset 1');
177 };
178
179 sub get_title_matchpoint {
180
181     my $params = shift;
182
183     my $length = $params->{length} // 0;
184     my $norms  = $params->{norms}  // [];
185     my $offset = $params->{offset} // 0;
186
187     my $matchpoint = {
188         components =>  [
189             {
190                 length    => $length,
191                 norms     => $norms,
192                 offset    => $offset,
193                 subfields =>
194                     {
195                         a => 1,
196                         c => 1
197                     },
198                 tag => '245'
199             }
200         ],
201         index => "title",
202         score => 1000
203     };
204
205     return $matchpoint;
206 }
207
208 sub get_authors_matchpoint {
209
210     my $params = shift;
211
212     my $length = $params->{length} // 0;
213     my $norms  = $params->{norms}  // [];
214     my $offset = $params->{offset} // 0;
215
216     my $matchpoint = {
217         components =>  [
218             {
219                 length    => $length,
220                 norms     => $norms,
221                 offset    => $offset,
222                 subfields =>
223                     {
224                         a => 1
225                     },
226                 tag => '700'
227             },
228             {
229                 length    => $length,
230                 norms     => $norms,
231                 offset    => $offset,
232                 subfields =>
233                     {
234                         a => 1
235                     },
236                 tag => '100'
237             }
238         ],
239         index => "author",
240         score => 1000
241     };
242
243     return $matchpoint;
244 }
245
246 1;