From 37ed1111cee60c519f844c789d4385eacd299fbb Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Thu, 23 Nov 2017 17:10:13 -0300 Subject: [PATCH] Bug 19686: Unit tests Signed-off-by: Tomas Cohen Arazi Signed-off-by: David Bourgault Signed-off-by: Kyle M Hall Signed-off-by: Jonathan Druart --- t/db_dependent/Koha/REST/Plugin/Objects.t | 56 ++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/REST/Plugin/Objects.t b/t/db_dependent/Koha/REST/Plugin/Objects.t index 46eed8fd65..5595061fec 100644 --- a/t/db_dependent/Koha/REST/Plugin/Objects.t +++ b/t/db_dependent/Koha/REST/Plugin/Objects.t @@ -34,6 +34,23 @@ get '/patrons' => sub { $c->render( status => 200, json => $patrons ); }; +get '/patrons_to_model' => sub { + my $c = shift; + $c->validation->output($c->req->params->to_hash); + my $patrons_set = Koha::Patrons->new; + my $patrons = $c->objects->search( $patrons_set, \&_to_model ); + $c->render( status => 200, json => $patrons ); +}; + +sub _to_model { + my $params = shift; + + if ( exists $params->{nombre} ) { + $params->{firstname} = delete $params->{nombre}; + } + + return $params; +} # The tests use Test::More tests => 1; @@ -49,7 +66,7 @@ my $builder = t::lib::TestBuilder->new; subtest 'objects.search helper' => sub { - plan tests => 34; + plan tests => 62; my $t = Test::Mojo->new; @@ -121,5 +138,42 @@ subtest 'objects.search helper' => sub { ->json_is('/1/firstname' => 'Manuela') ->json_is('/2/firstname' => 'Emanuel'); + ## _to_model tests + # _match=starts_with + $t->get_ok('/patrons_to_model?nombre=manuel&_per_page=3&_page=1&_match=starts_with') + ->status_is(200) + ->json_has('/0') + ->json_has('/1') + ->json_hasnt('/2') + ->json_is('/0/firstname' => 'Manuel') + ->json_is('/1/firstname' => 'Manuela'); + + # _match=ends_with + $t->get_ok('/patrons_to_model?nombre=manuel&_per_page=3&_page=1&_match=ends_with') + ->status_is(200) + ->json_has('/0') + ->json_has('/1') + ->json_hasnt('/2') + ->json_is('/0/firstname' => 'Manuel') + ->json_is('/1/firstname' => 'Emanuel'); + + # _match=exact + $t->get_ok('/patrons_to_model?nombre=manuel&_per_page=3&_page=1&_match=exact') + ->status_is(200) + ->json_has('/0') + ->json_hasnt('/1') + ->json_is('/0/firstname' => 'Manuel'); + + # _match=contains + $t->get_ok('/patrons_to_model?nombre=manuel&_per_page=3&_page=1&_match=contains') + ->status_is(200) + ->json_has('/0') + ->json_has('/1') + ->json_has('/2') + ->json_hasnt('/3') + ->json_is('/0/firstname' => 'Manuel') + ->json_is('/1/firstname' => 'Manuela') + ->json_is('/2/firstname' => 'Emanuel'); + $schema->storage->txn_rollback; };