From 7807e37b75324b7b627c28d3f672f29a3113ce72 Mon Sep 17 00:00:00 2001 From: Janusz Kaczmarek Date: Fri, 27 Oct 2023 21:06:44 +0000 Subject: [PATCH] Bug 32707: Unit tests NB in t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t, I had to modify four existing test by changing Local-number:123456 to Personal-name:donald -- since Local-number should never be auto truncated according to the submitted patch. --- .../SearchEngine/Elasticsearch/QueryBuilder.t | 36 ++++++++++++++- .../SearchEngine/Elasticsearch/QueryBuilder.t | 45 +++++++++++++++---- 2 files changed, 71 insertions(+), 10 deletions(-) diff --git a/t/Koha/SearchEngine/Elasticsearch/QueryBuilder.t b/t/Koha/SearchEngine/Elasticsearch/QueryBuilder.t index a507b2e065..cae91925d4 100755 --- a/t/Koha/SearchEngine/Elasticsearch/QueryBuilder.t +++ b/t/Koha/SearchEngine/Elasticsearch/QueryBuilder.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 6; +use Test::More tests => 7; use t::lib::Mocks; use_ok('Koha::SearchEngine::Elasticsearch::QueryBuilder'); @@ -152,6 +152,40 @@ subtest '_truncate_terms() tests' => sub { is_deeply($res, '"donald duck"', 'quoted search terms surrounded by spaces correctly'); }; +subtest '_is_safe_to_auto_truncate() tests' => sub { + plan tests => 9; + + my $qb; + ok( + $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => $Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX }), + 'Creating a new QueryBuilder object' + ); + + my $res = $qb->_is_safe_to_auto_truncate(undef, 'local-number:1'); + is($res, '0', 'no truncation for biblionumber - OK'); + + $res = $qb->_is_safe_to_auto_truncate(undef, 'koha-auth-number:1'); + is($res, '0', 'no truncation for authid - OK'); + + $res = $qb->_is_safe_to_auto_truncate(undef, 'copydate:1997'); + is($res, '0', 'no truncation for non non test fields (date) - OK'); + + $res = $qb->_is_safe_to_auto_truncate(undef, 'title:some title'); + is($res, '1', 'do truncate titles - OK'); + + $res = $qb->_is_safe_to_auto_truncate('local-number', undef); + is($res, '0', 'no truncation for biblionumber - OK'); + + $res = $qb->_is_safe_to_auto_truncate('koha-auth-number', undef); + is($res, '0', 'no truncation for authid - OK'); + + $res = $qb->_is_safe_to_auto_truncate('copydate', undef); + is($res, '0', 'no truncation for non non test fields (date) - OK'); + + $res = $qb->_is_safe_to_auto_truncate('title', undef); + is($res, '1', 'do truncate titles - OK'); +}; + subtest '_split_query() tests' => sub { plan tests => 7; diff --git a/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t b/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t index 277b784280..e834187ba6 100755 --- a/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t +++ b/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t @@ -219,7 +219,7 @@ subtest 'build_authorities_query_compat() tests' => sub { }; subtest 'build_query tests' => sub { - plan tests => 60; + plan tests => 63; my $qb; @@ -406,6 +406,9 @@ subtest 'build_query tests' => sub { "all quoted strings are unaltered if more than one in query" ); + # Reset ESPreventAutoTruncate syspref + t::lib::Mocks::mock_preference( 'ESPreventAutoTruncate', '' ); + ( undef, $query ) = $qb->build_query_compat( undef, ['barcode:123456'] ); is( $query->{query}{query_string}{query}, @@ -413,34 +416,58 @@ subtest 'build_query tests' => sub { "query of specific field is truncated" ); - ( undef, $query ) = $qb->build_query_compat( undef, ['Local-number:"123456"'] ); + ( undef, $query ) = $qb->build_query_compat( undef, ['Personal-name:"donald"'] ); is( $query->{query}{query_string}{query}, - '(local-number:"123456")', + '(personal-name:"donald")', "query of specific field including hyphen and quoted is not truncated, field name is converted to lower case" ); - ( undef, $query ) = $qb->build_query_compat( undef, ['Local-number:123456'] ); + ( undef, $query ) = $qb->build_query_compat( undef, ['Personal-name:donald'] ); is( $query->{query}{query_string}{query}, - '(local-number:123456*)', + '(personal-name:donald*)', "query of specific field including hyphen and not quoted is truncated, field name is converted to lower case" ); - ( undef, $query ) = $qb->build_query_compat( undef, ['Local-number.raw:123456'] ); + ( undef, $query ) = $qb->build_query_compat( undef, ['Personal-name.raw:donald'] ); is( $query->{query}{query_string}{query}, - '(local-number.raw:123456*)', + '(personal-name.raw:donald*)', "query of specific field including period and not quoted is truncated, field name is converted to lower case" ); - ( undef, $query ) = $qb->build_query_compat( undef, ['Local-number.raw:"123456"'] ); + ( undef, $query ) = $qb->build_query_compat( undef, ['Personal-name.raw:"donald"'] ); is( $query->{query}{query_string}{query}, - '(local-number.raw:"123456")', + '(personal-name.raw:"donald")', "query of specific field including period and quoted is not truncated, field name is converted to lower case" ); + # Set ESPreventAutoTruncate syspref + t::lib::Mocks::mock_preference( 'ESPreventAutoTruncate', 'barcode' ); + + ( undef, $query ) = $qb->build_query_compat( undef, ['barcode:123456'] ); + is( + $query->{query}{query_string}{query}, + '(barcode:123456)', + "query of specific field excluded by ESPreventAutoTruncate is not truncated" + ); + + ( undef, $query ) = $qb->build_query_compat( undef, ['Local-number:123456'] ); + is( + $query->{query}{query_string}{query}, + '(local-number:123456)', + "query of identifier is not truncated even if QueryAutoTruncate is set" + ); + + ( undef, $query ) = $qb->build_query_compat( undef, ['onloan:true'] ); + is( + $query->{query}{query_string}{query}, + '(onloan:true)', + "query of boolean type field is not truncated even if QueryAutoTruncate is set" + ); + ( undef, $query ) = $qb->build_query_compat( undef, ['J.R.R'] ); is( $query->{query}{query_string}{query}, -- 2.30.2