From 05ee5d10a878fb7ffc4149b77642428ea6571946 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Wed, 13 Apr 2022 12:17:25 +0000 Subject: [PATCH] Bug 17170: Unit tests Sponsored-by: Sponsored by: Round Rock Public Library [https://www.roundrocktexas.gov/departments/library/] Signed-off-by: Martin Renvoize Signed-off-by: Tomas Cohen Arazi --- .../SearchEngine/Elasticsearch/QueryBuilder.t | 28 ++++++++++++++++++- .../Koha/SearchEngine/Zebra/QueryBuilder.t | 27 +++++++++++++++++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t b/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t index 27d11c04da..8488af5f56 100755 --- a/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t +++ b/t/db_dependent/Koha/SearchEngine/Elasticsearch/QueryBuilder.t @@ -22,12 +22,13 @@ use Test::Exception; use Test::Warn; use t::lib::Mocks; use t::lib::TestBuilder; -use Test::More tests => 7; +use Test::More tests => 8; use List::Util qw( all ); use Koha::Database; use Koha::SearchEngine::Elasticsearch::QueryBuilder; +use Koha::SearchFilters; my $schema = Koha::Database->new->schema; $schema->storage->txn_begin; @@ -783,6 +784,31 @@ subtest 'build_query_compat() SearchLimitLibrary tests' => sub { }; +subtest "Handle search filters" => sub { + plan tests => 4; + + my $qb; + + ok( + $qb = Koha::SearchEngine::Elasticsearch::QueryBuilder->new({ 'index' => 'biblios' }), + 'Creating new query builder object for biblios' + ); + + my $filter = Koha::SearchFilter->new({ + name => "test", + query => q|{"operands":["cat","bat","rat"],"indexes":["kw","ti","au"],"operators":["AND","OR"]}|, + limits => q|{"limits":["mc-itype,phr:BK","available"]}|, + })->store; + my $filter_id = $filter->id; + + my ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc ) = $qb->build_query_compat( undef, undef, undef, ["search_filter:$filter_id"] ); + + is( $limit,q{(itype:("BK")) AND (onloan:false) AND (((cat) AND title:(bat) OR author:(rat)))},"Limit correctly formed"); + is( $limit_cgi,"&limit=search_filter%3A$filter_id","CGI limit is not expanded"); + is( $limit_desc,q{(itype:("BK")) AND (onloan:false) AND (((cat) AND title:(bat) OR author:(rat)))},"Limit description is correctly expanded"); + +}; + subtest "_convert_sort_fields() tests" => sub { plan tests => 3; diff --git a/t/db_dependent/Koha/SearchEngine/Zebra/QueryBuilder.t b/t/db_dependent/Koha/SearchEngine/Zebra/QueryBuilder.t index 02c129aa64..2653e45acd 100755 --- a/t/db_dependent/Koha/SearchEngine/Zebra/QueryBuilder.t +++ b/t/db_dependent/Koha/SearchEngine/Zebra/QueryBuilder.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 1; +use Test::More tests => 2; use t::lib::Mocks; use t::lib::TestBuilder; use Test::MockModule; @@ -96,3 +96,28 @@ subtest 'build_query_compat() SearchLimitLibrary tests' => sub { is( $limit_cgi, "&limit=multibranchlimit%3A$groupid", "Limit cgi does not get expanded"); }; + +subtest "Handle search filters" => sub { + plan tests => 4; + + my $qb; + + ok( + $qb = Koha::SearchEngine::Zebra::QueryBuilder->new({ 'index' => 'biblios' }), + 'Creating new query builder object for biblios' + ); + + my $filter = Koha::SearchFilter->new({ + name => "test", + query => q|{"operands":["cat","bat","rat"],"indexes":["kw","ti","au"],"operators":["AND","OR"]}|, + limits => q|{"limits":["mc-itype,phr:BK","available"]}|, + })->store; + my $filter_id = $filter->id; + + my ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc ) = $qb->build_query_compat( undef, undef, undef, ["search_filter:$filter_id"] ); + + is( $limit,q{(kw=(cat) AND ti=(bat) OR au=(rat)) and (mc-itype,phr=BK) and (( (allrecords,AlwaysMatches='') and (not-onloan-count,st-numeric >= 1) and (lost,st-numeric=0) ))},"Limit correctly formed"); + is( $limit_cgi,"&limit=search_filter%3A$filter_id","CGI limit is not expanded"); + is( $limit_desc,q{(kw=(cat) AND ti=(bat) OR au=(rat)) and (mc-itype,phr=BK) and (( (allrecords,AlwaysMatches='') and (not-onloan-count,st-numeric >= 1) and (lost,st-numeric=0) ))},"Limit description is correctly expanded"); + +}; -- 2.39.5