Bug 17301 - Add callnumber to label-edit-batch.pl
[koha.git] / t / Matcher.t
blobaac83b0913c5b5dd454a4ddc19a037d462914743
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
20 use Test::More;
21 use Test::MockModule;
23 use MARC::Record;
25 use Module::Load::Conditional qw/check_install/;
27 BEGIN {
28 if ( check_install( module => 'Test::DBIx::Class' ) ) {
29 plan tests => 12;
30 } else {
31 plan skip_all => "Need Test::DBIx::Class"
35 use_ok('C4::Matcher');
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' ;
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 ]
50 ], 'add fixtures';
52 my $db = Test::MockModule->new('Koha::Database');
53 $db->mock( _new_schema => sub { return Schema(); } );
55 my @matchers = C4::Matcher::GetMatcherList();
57 is( $matchers[0]->{'matcher_id'}, 1, 'First matcher_id value is 1' );
59 is( $matchers[1]->{'matcher_id'}, 2, 'Second matcher_id value is 2' );
61 my $matcher_id = C4::Matcher::GetMatcherId('ISBN');
63 is( $matcher_id, 1, 'testing getmatcherid' );
65 my $testmatcher;
67 ok( $testmatcher = C4::Matcher->new( 'red', 1 ), 'testing matcher new' );
69 ok( $testmatcher = C4::Matcher->new( 'blue', 0 ), 'testing matcher new' );
71 $testmatcher->threshold(1000);
73 is( $testmatcher->threshold(), 1000, 'testing threshhold accessor method' );
75 $testmatcher->_id(53);
77 is( $testmatcher->_id(), 53, 'testing _id accessor' );
79 $testmatcher->code('match on ISBN');
81 is( $testmatcher->code(), 'match on ISBN', 'testing code accessor' );
83 $testmatcher->description('match on ISSN');
85 is( $testmatcher->description(), 'match on ISSN', 'testing code accessor' );
87 subtest '_get_match_keys() tests' => sub {
89 plan tests => 8;
91 my $matchpoint = get_title_matchpoint({
92 length => 0,
93 norms => [],
94 offset => 0
95 });
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-')
110 my @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
112 is( $keys[0], 'THE TALISMAN STEPHEN KING PETER STRAUB',
113 'Match key correctly calculated with no $norms');
115 $matchpoint = get_title_matchpoint({
116 length => 9,
117 norms => [],
118 offset => 0
120 @keys = C4::Matcher::_get_match_keys( $record, $matchpoint );
121 is( $keys[0], 'THE',
122 'Match key correctly calculated with length 9');
124 $matchpoint = get_title_matchpoint({
125 length => 9,
126 norms => [],
127 offset => 1
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');
133 $matchpoint = get_title_matchpoint({
134 length => 9,
135 norms => [],
136 offset => 2
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');
142 $matchpoint = get_authors_matchpoint({
143 length => 0,
144 norms => [],
145 offset => 0
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');
151 $matchpoint = get_authors_matchpoint({
152 length => 9,
153 norms => [],
154 offset => 0
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');
160 $matchpoint = get_authors_matchpoint({
161 length => 10,
162 norms => [],
163 offset => 0
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');
169 $matchpoint = get_authors_matchpoint({
170 length => 10,
171 norms => [],
172 offset => 2
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');
179 sub get_title_matchpoint {
181 my $params = shift;
183 my $length = $params->{length} // 0;
184 my $norms = $params->{norms} // [];
185 my $offset = $params->{offset} // 0;
187 my $matchpoint = {
188 components => [
190 length => $length,
191 norms => $norms,
192 offset => $offset,
193 subfields =>
195 a => 1,
196 c => 1
198 tag => '245'
201 index => "title",
202 score => 1000
205 return $matchpoint;
208 sub get_authors_matchpoint {
210 my $params = shift;
212 my $length = $params->{length} // 0;
213 my $norms = $params->{norms} // [];
214 my $offset = $params->{offset} // 0;
216 my $matchpoint = {
217 components => [
219 length => $length,
220 norms => $norms,
221 offset => $offset,
222 subfields =>
224 a => 1
226 tag => '700'
229 length => $length,
230 norms => $norms,
231 offset => $offset,
232 subfields =>
234 a => 1
236 tag => '100'
239 index => "author",
240 score => 1000
243 return $matchpoint;