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 => 26; |
219 |
|
219 |
|
220 |
$schema->storage->txn_begin; |
220 |
$schema->storage->txn_begin; |
221 |
|
221 |
|
Lines 262-304
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 overloaded to_api_mapping method, return TO_JSON' ); |
263 |
is_deeply( $illrequest->to_api, $illrequest->TO_JSON, 'If no overloaded to_api_mapping method, 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 = { 'items' => {} }; |
288 |
|
270 |
|
289 |
my $biblio_api = $biblio->to_api(\@embeds); |
271 |
my $biblio_api = $biblio->to_api({ embed => $embeds }); |
290 |
|
272 |
|
291 |
ok(exists $biblio_api->{items}, 'Items where embedded in biblio results'); |
273 |
ok(exists $biblio_api->{items}, 'Items where embedded in biblio results'); |
292 |
is($biblio_api->{items}->[0]->{item_id}, $item->itemnumber, 'Item matches'); |
274 |
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'); |
275 |
ok(!exists $biblio_api->{items}->[0]->{holds}, 'No holds info should be embedded yet'); |
294 |
|
276 |
|
295 |
@embeds = ('items.holds'); |
277 |
$embeds = ( |
296 |
$biblio_api = $biblio->to_api(\@embeds); |
278 |
{ |
|
|
279 |
'items' => { |
280 |
'children' => { |
281 |
'holds' => {} |
282 |
} |
283 |
}, |
284 |
'biblioitem' => {} |
285 |
} |
286 |
); |
287 |
$biblio_api = $biblio->to_api({ embed => $embeds }); |
297 |
|
288 |
|
298 |
ok(exists $biblio_api->{items}, 'Items where embedded in biblio results'); |
289 |
ok(exists $biblio_api->{items}, 'Items where embedded in biblio results'); |
299 |
is($biblio_api->{items}->[0]->{item_id}, $item->itemnumber, 'Item still matches'); |
290 |
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'); |
291 |
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'); |
292 |
is($biblio_api->{items}->[0]->{holds}->[0]->{hold_id}, $hold->reserve_id, 'Hold matches'); |
|
|
293 |
is_deeply($biblio_api->{biblioitem}, $biblio->biblioitem->to_api, 'More than one root'); |
294 |
|
295 |
my $hold_api = $hold->to_api( |
296 |
{ |
297 |
embed => { 'item' => {} } |
298 |
} |
299 |
); |
300 |
|
301 |
is( ref($hold_api->{item}), 'HASH', 'Single nested object works correctly' ); |
302 |
is( $hold_api->{item}->{item_id}, $item->itemnumber, 'Object embedded correctly' ); |
303 |
|
304 |
# biblio with no items |
305 |
my $new_biblio = $builder->build_sample_biblio; |
306 |
my $new_biblio_api = $new_biblio->to_api({ embed => $embeds }); |
307 |
|
308 |
is_deeply( $new_biblio_api->{items}, [], 'Empty list if no items' ); |
309 |
|
310 |
my $biblio_class = Test::MockModule->new('Koha::Biblio'); |
311 |
$biblio_class->mock( 'undef_result', sub { return; } ); |
312 |
|
313 |
$new_biblio_api = $new_biblio->to_api({ embed => ( { 'undef_result' => {} } ) }); |
314 |
ok( exists $new_biblio_api->{undef_result}, 'If a method returns undef, then the attribute is defined' ); |
315 |
is( $new_biblio_api->{undef_result}, undef, 'If a method returns undef, then the attribute is undef' ); |
316 |
|
317 |
$biblio_class->mock( 'items', |
318 |
sub { return [ bless { itemnumber => 1 }, 'Somethings' ]; } ); |
319 |
|
320 |
throws_ok { |
321 |
$new_biblio_api = $new_biblio->to_api( |
322 |
{ embed => { 'items' => { children => { asd => {} } } } } ); |
323 |
} |
324 |
'Koha::Exceptions::Exception', |
325 |
"An exception is thrown if a blessed object to embed doesn't implement to_api"; |
326 |
|
327 |
is( |
328 |
"$@", |
329 |
"Asked to embed items but its return value doesn't implement to_api", |
330 |
"Exception message correct" |
331 |
); |
302 |
|
332 |
|
303 |
$schema->storage->txn_rollback; |
333 |
$schema->storage->txn_rollback; |
304 |
}; |
334 |
}; |