From d7feacfd147185fb6c29a591c32b6e0d1c8ee651 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 19 Sep 2016 12:17:41 -0300 Subject: [PATCH] Bug 17318: Unit tests The current C4::Matcher::_get_match_keys implementation doesn't take normalization rules params into account. This patch makes Matcher.t test the proposed behaviour for new hardcoded values, that match string normalization routines introduced by Bu 17302. Possible values are: - upper_case - lower_case - remove_spaces - legacy_default - none * 'legacy_default' is left (for now) to catch the current behaviour. So tests which did not get a 'norm' param, now are passed 'legacy_default' and they still pass. * 'none' means no normalization, of course Note: on introducing 'none', a bug got highlighted, because the subfields traversal loop was introducing an unneeded space. The tests from 17304 get adjusted to reflect this. To test: - Run: $ sudo koha-shell kohadev ; cd kohaclone $ prove t/Matcher.t => FAIL: The routine doesn't care about the norms param. Sponsored-by: FIT --- t/Matcher.t | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 86 insertions(+), 13 deletions(-) diff --git a/t/Matcher.t b/t/Matcher.t index aac83b0..3d7f6e9 100755 --- a/t/Matcher.t +++ b/t/Matcher.t @@ -19,6 +19,7 @@ use Modern::Perl; use Test::More; use Test::MockModule; +use Test::Warn; use MARC::Record; @@ -86,11 +87,11 @@ is( $testmatcher->description(), 'match on ISSN', 'testing code accessor' ); subtest '_get_match_keys() tests' => sub { - plan tests => 8; + plan tests => 17; my $matchpoint = get_title_matchpoint({ length => 0, - norms => [], + norms => [ 'legacy_default' ], offset => 0 }); @@ -114,7 +115,7 @@ subtest '_get_match_keys() tests' => sub { $matchpoint = get_title_matchpoint({ length => 9, - norms => [], + norms => [ 'legacy_default' ], offset => 0 }); @keys = C4::Matcher::_get_match_keys( $record, $matchpoint ); @@ -123,16 +124,16 @@ subtest '_get_match_keys() tests' => sub { $matchpoint = get_title_matchpoint({ length => 9, - norms => [], + norms => [ 'legacy_default' ], offset => 1 }); @keys = C4::Matcher::_get_match_keys( $record, $matchpoint ); - is( $keys[0], 'THE', + is( $keys[0], 'THE T', 'Match key correctly calculated with length 9 and offset 1'); $matchpoint = get_title_matchpoint({ length => 9, - norms => [], + norms => [ 'legacy_default' ], offset => 2 }); @keys = C4::Matcher::_get_match_keys( $record, $matchpoint ); @@ -141,7 +142,7 @@ subtest '_get_match_keys() tests' => sub { $matchpoint = get_authors_matchpoint({ length => 0, - norms => [], + norms => [ 'legacy_default' ], offset => 0 }); @keys = C4::Matcher::_get_match_keys( $record, $matchpoint ); @@ -150,30 +151,102 @@ subtest '_get_match_keys() tests' => sub { $matchpoint = get_authors_matchpoint({ length => 9, - norms => [], + norms => [ 'legacy_default' ], offset => 0 }); @keys = C4::Matcher::_get_match_keys( $record, $matchpoint ); - is( $keys[0], 'STRAUB KING ST', + is( $keys[0], 'STRAUB P KING STE', 'Match key correctly calculated with multiple components, length 9'); $matchpoint = get_authors_matchpoint({ length => 10, - norms => [], + norms => [ 'legacy_default' ], offset => 0 }); @keys = C4::Matcher::_get_match_keys( $record, $matchpoint ); - is( $keys[0], 'STRAUB P KING STE', + is( $keys[0], 'STRAUB PE KING STEP', 'Match key correctly calculated with multiple components, length 10'); $matchpoint = get_authors_matchpoint({ length => 10, - norms => [], + norms => [ 'legacy_default' ], offset => 2 }); @keys = C4::Matcher::_get_match_keys( $record, $matchpoint ); - is( $keys[0], 'TRAUB PET ING STEPH', + is( $keys[0], 'RAUB PETE NG STEPHE', 'Match key correctly calculated with multiple components, length 10, offset 1'); + + $matchpoint = get_title_matchpoint({ + length => 0, + norms => [ 'none', 'none' ], + offset => 0 + }); + @keys = C4::Matcher::_get_match_keys( $record, $matchpoint ); + is( $keys[0], ' .; thE t[]:,aliS(m)/An\'" Stephen King, Peter Straub.', + 'Match key intact if \'none\' specified, length 0 and offset 0' ); + + $matchpoint = get_authors_matchpoint({ + length => 0, + norms => [ 'upper_case' ], + offset => 0 + }); + @keys = C4::Matcher::_get_match_keys( $record, $matchpoint ); + is( $keys[0], 'STRAUB, PETER KING, STEPHEN', + 'Match key correctly calculated with multiple components, \'upper_case\' norm'); + + $matchpoint = get_authors_matchpoint({ + length => 0, + norms => [ 'lower_case' ], + offset => 0 + }); + @keys = C4::Matcher::_get_match_keys( $record, $matchpoint ); + is( $keys[0], 'straub, peter king, stephen', + 'Match key correctly calculated with multiple components, \'lower_case\' norm'); + + $matchpoint = get_authors_matchpoint({ + length => 0, + norms => [ 'remove_spaces' ], + offset => 0 + }); + @keys = C4::Matcher::_get_match_keys( $record, $matchpoint ); + is( $keys[0], 'Straub,Peter King,Stephen', + 'Match key correctly calculated with multiple components, \'remove_spaces\' norm'); + + $matchpoint = get_authors_matchpoint({ + length => 0, + norms => [ 'remove_spaces', 'lower_case' ], + offset => 0 + }); + @keys = C4::Matcher::_get_match_keys( $record, $matchpoint ); + is( $keys[0], 'straub,peter king,stephen', + 'Match key correctly calculated with multiple components, \'remove_spaces\' and \'lower_case\' norm'); + + my $norm = 'unknown_norm'; + $matchpoint = get_title_matchpoint({ + length => 0, + norms => [ $norm ], + offset => 0 + }); + warning_is + { @keys = C4::Matcher::_get_match_keys( $record, $matchpoint ) } + qq{Invalid normalization routine required ($norm)}, + 'Passing an invalid normalization routine name raises a warning'; + + is( $keys[0], ' .; thE t[]:,aliS(m)/An\'" Stephen King, Peter Straub.', + 'Match key intact if invalid normalization routine specified' ); + + $matchpoint = get_title_matchpoint({ + length => 0, + norms => [ $norm, 'upper_case' ], + offset => 0 + }); + warning_is + { @keys = C4::Matcher::_get_match_keys( $record, $matchpoint ) } + qq{Invalid normalization routine required ($norm)}, + 'Passing an invalid normalization routine name raises a warning'; + + is( $keys[0], ' .; THE T[]:,ALIS(M)/AN\'" STEPHEN KING, PETER STRAUB.', + 'Match key correctly normalized if invalid normalization routine specified' ); }; sub get_title_matchpoint { -- 2.7.4