From 0d3b3d8a2f2bbfc7a39102045604b64b7263a0b4 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 --- .../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 7cf50591ef..0535dbcb03 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; @@ -780,6 +781,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.30.2