Lines 215-221
subtest 'TO_JSON tests' => sub {
Link Here
|
215 |
|
215 |
|
216 |
subtest "to_api() tests" => sub { |
216 |
subtest "to_api() tests" => sub { |
217 |
|
217 |
|
218 |
plan tests => 18; |
218 |
plan tests => 20; |
219 |
|
219 |
|
220 |
$schema->storage->txn_begin; |
220 |
$schema->storage->txn_begin; |
221 |
|
221 |
|
Lines 262-305
subtest "to_api() tests" => sub {
Link Here
|
262 |
my $illrequest = $builder->build_object({ class => 'Koha::Illrequests' }); |
262 |
my $illrequest = $builder->build_object({ class => 'Koha::Illrequests' }); |
263 |
is_deeply( $illrequest->to_api, $illrequest->TO_JSON, 'If no to_api_method present, return TO_JSON' ); |
263 |
is_deeply( $illrequest->to_api, $illrequest->TO_JSON, 'If no to_api_method present, return TO_JSON' ); |
264 |
|
264 |
|
265 |
my $item_class = Test::MockModule->new('Koha::Item'); |
|
|
266 |
$item_class->mock( 'to_api_mapping', |
267 |
sub { |
268 |
return { |
269 |
itemnumber => 'item_id' |
270 |
}; |
271 |
} |
272 |
); |
273 |
|
274 |
my $hold_class = Test::MockModule->new('Koha::Hold'); |
275 |
$hold_class->mock( 'to_api_mapping', |
276 |
sub { |
277 |
return { |
278 |
reserve_id => 'hold_id' |
279 |
}; |
280 |
} |
281 |
); |
282 |
|
283 |
my $biblio = $builder->build_sample_biblio(); |
265 |
my $biblio = $builder->build_sample_biblio(); |
284 |
my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber }); |
266 |
my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber }); |
285 |
my $hold = $builder->build_object({ class => 'Koha::Holds', value => { itemnumber => $item->itemnumber } }); |
267 |
my $hold = $builder->build_object({ class => 'Koha::Holds', value => { itemnumber => $item->itemnumber } }); |
286 |
|
268 |
|
287 |
my @embeds = ('items'); |
269 |
my @embeds = ( |
|
|
270 |
{ |
271 |
accessor => 'items' |
272 |
} |
273 |
); |
288 |
|
274 |
|
289 |
my $biblio_api = $biblio->to_api(\@embeds); |
275 |
my $biblio_api = $biblio->to_api({ embed => \@embeds }); |
290 |
|
276 |
|
291 |
ok(exists $biblio_api->{items}, 'Items where embedded in biblio results'); |
277 |
ok(exists $biblio_api->{items}, 'Items where embedded in biblio results'); |
292 |
is($biblio_api->{items}->[0]->{item_id}, $item->itemnumber, 'Item matches'); |
278 |
is($biblio_api->{items}->[0]->{item_id}, $item->itemnumber, 'Item matches'); |
293 |
ok(!exists $biblio_api->{items}->[0]->{holds}, 'No holds info should be embedded yet'); |
279 |
ok(!exists $biblio_api->{items}->[0]->{holds}, 'No holds info should be embedded yet'); |
294 |
|
280 |
|
295 |
@embeds = ('items.holds'); |
281 |
@embeds = ( |
296 |
$biblio_api = $biblio->to_api(\@embeds); |
282 |
{ |
|
|
283 |
accessor => 'items', |
284 |
children => [ |
285 |
{ |
286 |
accessor => 'holds' |
287 |
} |
288 |
] |
289 |
} |
290 |
); |
291 |
$biblio_api = $biblio->to_api({ embed => \@embeds }); |
297 |
|
292 |
|
298 |
ok(exists $biblio_api->{items}, 'Items where embedded in biblio results'); |
293 |
ok(exists $biblio_api->{items}, 'Items where embedded in biblio results'); |
299 |
is($biblio_api->{items}->[0]->{item_id}, $item->itemnumber, 'Item still matches'); |
294 |
is($biblio_api->{items}->[0]->{item_id}, $item->itemnumber, 'Item still matches'); |
300 |
ok(exists $biblio_api->{items}->[0]->{holds}, 'Holds info should be embedded'); |
295 |
ok(exists $biblio_api->{items}->[0]->{holds}, 'Holds info should be embedded'); |
301 |
is($biblio_api->{items}->[0]->{holds}->[0]->{hold_id}, $hold->reserve_id, 'Hold matches'); |
296 |
is($biblio_api->{items}->[0]->{holds}->[0]->{hold_id}, $hold->reserve_id, 'Hold matches'); |
302 |
|
297 |
|
|
|
298 |
my $hold_api = $hold->to_api( |
299 |
{ |
300 |
embed => [ |
301 |
{ |
302 |
accessor => 'item' |
303 |
} |
304 |
] |
305 |
} |
306 |
); |
307 |
|
308 |
is( ref($hold_api->{item}), 'HASH', 'Single nested object works correctly' ); |
309 |
is( $hold_api->{item}->{item_id}, $item->itemnumber, 'Object embedded correctly' ); |
310 |
|
303 |
$schema->storage->txn_rollback; |
311 |
$schema->storage->txn_rollback; |
304 |
}; |
312 |
}; |
305 |
|
313 |
|