|
Lines 38-48
get '/patrons_to_model' => sub {
Link Here
|
| 38 |
my $c = shift; |
38 |
my $c = shift; |
| 39 |
$c->validation->output($c->req->params->to_hash); |
39 |
$c->validation->output($c->req->params->to_hash); |
| 40 |
my $patrons_set = Koha::Patrons->new; |
40 |
my $patrons_set = Koha::Patrons->new; |
| 41 |
my $patrons = $c->objects->search( $patrons_set, \&_to_model ); |
41 |
my $patrons = $c->objects->search( $patrons_set, \&to_model ); |
| 42 |
$c->render( status => 200, json => $patrons ); |
42 |
$c->render( status => 200, json => $patrons ); |
| 43 |
}; |
43 |
}; |
| 44 |
|
44 |
|
| 45 |
sub _to_model { |
45 |
get '/patrons_to_model_to_api' => sub { |
|
|
46 |
my $c = shift; |
| 47 |
$c->validation->output($c->req->params->to_hash); |
| 48 |
my $patrons_set = Koha::Patrons->new; |
| 49 |
my $patrons = $c->objects->search( $patrons_set, \&to_model, \&to_api ); |
| 50 |
$c->render( status => 200, json => $patrons ); |
| 51 |
}; |
| 52 |
|
| 53 |
sub to_model { |
| 46 |
my $params = shift; |
54 |
my $params = shift; |
| 47 |
|
55 |
|
| 48 |
if ( exists $params->{nombre} ) { |
56 |
if ( exists $params->{nombre} ) { |
|
Lines 52-57
sub _to_model {
Link Here
|
| 52 |
return $params; |
60 |
return $params; |
| 53 |
} |
61 |
} |
| 54 |
|
62 |
|
|
|
63 |
sub to_api { |
| 64 |
my $params = shift; |
| 65 |
|
| 66 |
if ( exists $params->{firstname} ) { |
| 67 |
$params->{nombre} = delete $params->{firstname}; |
| 68 |
} |
| 69 |
|
| 70 |
return $params; |
| 71 |
} |
| 72 |
|
| 55 |
# The tests |
73 |
# The tests |
| 56 |
use Test::More tests => 1; |
74 |
use Test::More tests => 1; |
| 57 |
use Test::Mojo; |
75 |
use Test::Mojo; |
|
Lines 66-72
my $builder = t::lib::TestBuilder->new;
Link Here
|
| 66 |
|
84 |
|
| 67 |
subtest 'objects.search helper' => sub { |
85 |
subtest 'objects.search helper' => sub { |
| 68 |
|
86 |
|
| 69 |
plan tests => 62; |
87 |
plan tests => 90; |
| 70 |
|
88 |
|
| 71 |
my $t = Test::Mojo->new; |
89 |
my $t = Test::Mojo->new; |
| 72 |
|
90 |
|
|
Lines 175-179
subtest 'objects.search helper' => sub {
Link Here
|
| 175 |
->json_is('/1/firstname' => 'Manuela') |
193 |
->json_is('/1/firstname' => 'Manuela') |
| 176 |
->json_is('/2/firstname' => 'Emanuel'); |
194 |
->json_is('/2/firstname' => 'Emanuel'); |
| 177 |
|
195 |
|
|
|
196 |
## _to_model && _to_api tests |
| 197 |
# _match=starts_with |
| 198 |
$t->get_ok('/patrons_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=starts_with') |
| 199 |
->status_is(200) |
| 200 |
->json_has('/0') |
| 201 |
->json_has('/1') |
| 202 |
->json_hasnt('/2') |
| 203 |
->json_is('/0/nombre' => 'Manuel') |
| 204 |
->json_is('/1/nombre' => 'Manuela'); |
| 205 |
|
| 206 |
# _match=ends_with |
| 207 |
$t->get_ok('/patrons_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=ends_with') |
| 208 |
->status_is(200) |
| 209 |
->json_has('/0') |
| 210 |
->json_has('/1') |
| 211 |
->json_hasnt('/2') |
| 212 |
->json_is('/0/nombre' => 'Manuel') |
| 213 |
->json_is('/1/nombre' => 'Emanuel'); |
| 214 |
|
| 215 |
# _match=exact |
| 216 |
$t->get_ok('/patrons_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=exact') |
| 217 |
->status_is(200) |
| 218 |
->json_has('/0') |
| 219 |
->json_hasnt('/1') |
| 220 |
->json_is('/0/nombre' => 'Manuel'); |
| 221 |
|
| 222 |
# _match=contains |
| 223 |
$t->get_ok('/patrons_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=contains') |
| 224 |
->status_is(200) |
| 225 |
->json_has('/0') |
| 226 |
->json_has('/1') |
| 227 |
->json_has('/2') |
| 228 |
->json_hasnt('/3') |
| 229 |
->json_is('/0/nombre' => 'Manuel') |
| 230 |
->json_is('/1/nombre' => 'Manuela') |
| 231 |
->json_is('/2/nombre' => 'Emanuel'); |
| 232 |
|
| 178 |
$schema->storage->txn_rollback; |
233 |
$schema->storage->txn_rollback; |
| 179 |
}; |
234 |
}; |
| 180 |
- |
|
|