From 5bf50e2283a6aafea3c9a327cd0fa4780a4fa136 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Fri, 6 Oct 2017 12:49:27 -0300 Subject: [PATCH] Bug 19410: Unit tests Signed-off-by: Tomas Cohen Arazi --- t/db_dependent/Koha/Objects.t | 68 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/t/db_dependent/Koha/Objects.t b/t/db_dependent/Koha/Objects.t index 71b7abb28e..cf261b2e2d 100644 --- a/t/db_dependent/Koha/Objects.t +++ b/t/db_dependent/Koha/Objects.t @@ -19,7 +19,8 @@ use Modern::Perl; -use Test::More tests => 15; +use Test::More tests => 16; +use Test::Exception; use Test::Warn; use Koha::Authority::Types; @@ -237,4 +238,67 @@ subtest '->is_paged and ->pager tests' => sub { 'Koha::Objects->pager returns a valid DBIx::Class object' ); $schema->storage->txn_rollback; -} +}; + +subtest '_build_query_params_from_api' => sub { + + plan tests => 5; + + my $filtered_params = { + title => "Ender", + author => "Orson" + }; + + subtest '_match => contains' => sub { + plan tests => 2; + my $reserved_params = { _match => 'contains' }; + my $params = Koha::Objects::_build_query_params_from_api( $filtered_params, $reserved_params ); + + is_deeply( $params->{author}, { like => '%Orson%' } ); + is_deeply( $params->{title}, { like => '%Ender%' } ); + }; + + subtest '_match => starts_with' => sub { + plan tests => 2; + my $reserved_params = { _match => 'starts_with' }; + my $params = Koha::Objects::_build_query_params_from_api( $filtered_params, $reserved_params ); + + is_deeply( $params->{author}, { like => 'Orson%' } ); + is_deeply( $params->{title}, { like => 'Ender%' } ); + }; + + subtest '_match => ends_with' => sub { + plan tests => 2; + my $reserved_params = { _match => 'ends_with' }; + my $params = Koha::Objects::_build_query_params_from_api( $filtered_params, $reserved_params ); + + is_deeply( $params->{author}, { like => '%Orson' } ); + is_deeply( $params->{title}, { like => '%Ender' } ); + }; + + subtest '_match => exact' => sub { + plan tests => 2; + my $reserved_params = { _match => 'exact' }; + my $params = Koha::Objects::_build_query_params_from_api( $filtered_params, $reserved_params ); + + is( $params->{author}, 'Orson' ); + is( $params->{title}, 'Ender' ); + }; + + subtest '_match => blah' => sub { + plan tests => 2; + + my $reserved_params = { _match => 'blah' }; + throws_ok { + Koha::Objects::_build_query_params_from_api( $filtered_params, + $reserved_params ); + } + 'Koha::Exceptions::WrongParameter', + 'Exception thrown on invalid _match parameter'; + + is( $@, + 'Invalid value for _match param (blah)', + 'Exception carries the right message' + ); + }; +}; -- 2.14.1