Bug 24757: Leap day failing tests - Fix the tests!
[koha.git] / t / db_dependent / Koha / Objects.t
1 #!/usr/bin/perl
2
3 # Copyright 2015 Koha Development team
4 #
5 # This file is part of Koha
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 21;
23 use Test::Exception;
24 use Test::Warn;
25
26 use Koha::Authority::Types;
27 use Koha::Cities;
28 use Koha::Biblios;
29 use Koha::Patron::Category;
30 use Koha::Patron::Categories;
31 use Koha::Patrons;
32 use Koha::Database;
33
34 use t::lib::TestBuilder;
35
36 use Try::Tiny;
37
38 my $schema = Koha::Database->new->schema;
39 $schema->storage->txn_begin;
40 my $builder = t::lib::TestBuilder->new;
41
42 is( ref(Koha::Authority::Types->find('')), 'Koha::Authority::Type', 'Koha::Objects->find should work if the primary key is an empty string' );
43
44 my @columns = Koha::Patrons->columns;
45 my $borrowernumber_exists = grep { /^borrowernumber$/ } @columns;
46 is( $borrowernumber_exists, 1, 'Koha::Objects->columns should return the table columns' );
47
48 subtest 'find' => sub {
49     plan tests => 6;
50     my $patron = $builder->build({source => 'Borrower'});
51     my $patron_object = Koha::Patrons->find( $patron->{borrowernumber} );
52     is( $patron_object->borrowernumber, $patron->{borrowernumber}, '->find should return the correct object' );
53
54     my @patrons = Koha::Patrons->find( $patron->{borrowernumber} );
55     is(scalar @patrons, 1, '->find in list context returns a value');
56     is($patrons[0]->borrowernumber, $patron->{borrowernumber}, '->find in list context returns the same value as in scalar context');
57
58     my $patrons = {
59         foo => Koha::Patrons->find('foo'),
60         bar => 'baz',
61     };
62     is ($patrons->{foo}, undef, '->find in list context returns undef when no record is found');
63
64     # Test sending undef to find; should not generate a warning
65     warning_is { $patron = Koha::Patrons->find( undef ); }
66         "", "Sending undef does not trigger a DBIx warning";
67     warning_is { $patron = Koha::Patrons->find( undef, undef ); }
68         "", "Sending two undefs does not trigger a DBIx warning too";
69 };
70
71 subtest 'update' => sub {
72     plan tests => 2;
73
74     $builder->build( { source => 'City', value => { city_country => 'UK' } } );
75     $builder->build( { source => 'City', value => { city_country => 'UK' } } );
76     $builder->build( { source => 'City', value => { city_country => 'UK' } } );
77     $builder->build( { source => 'City', value => { city_country => 'France' } } );
78     $builder->build( { source => 'City', value => { city_country => 'France' } } );
79     $builder->build( { source => 'City', value => { city_country => 'Germany' } } );
80     Koha::Cities->search( { city_country => 'UK' } )->update( { city_country => 'EU' } );
81     is( Koha::Cities->search( { city_country => 'EU' } )->count, 3, 'Koha::Objects->update should have updated the 3 rows' );
82     is( Koha::Cities->search( { city_country => 'UK' } )->count, 0, 'Koha::Objects->update should have updated the 3 rows' );
83 };
84
85 subtest 'reset' => sub {
86     plan tests => 3;
87
88     my $patrons = Koha::Patrons->search;
89     my $first_borrowernumber = $patrons->next->borrowernumber;
90     my $second_borrowernumber = $patrons->next->borrowernumber;
91     is( ref( $patrons->reset ), 'Koha::Patrons', 'Koha::Objects->reset should allow chaining' );
92     is( ref( $patrons->reset->next ), 'Koha::Patron', 'Koha::Objects->reset should allow chaining' );
93     is( $patrons->reset->next->borrowernumber, $first_borrowernumber, 'Koha::Objects->reset should work as expected');
94 };
95
96 subtest 'delete' => sub {
97     plan tests => 2;
98
99     my $patron_1 = $builder->build({source => 'Borrower'});
100     my $patron_2 = $builder->build({source => 'Borrower'});
101     is( Koha::Patrons->search({ -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}]}})->delete, 2, '');
102     is( Koha::Patrons->search({ -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}]}})->count, 0, '');
103 };
104
105 subtest 'new' => sub {
106     plan tests => 2;
107     my $a_cat_code = 'A_CAT_CODE';
108     my $patron_category = Koha::Patron::Category->new( { categorycode => $a_cat_code } )->store;
109     is( Koha::Patron::Categories->find($a_cat_code)->category_type, 'A', 'Koha::Object->new should set the default value' );
110     Koha::Patron::Categories->find($a_cat_code)->delete;
111     $patron_category = Koha::Patron::Category->new( { categorycode => $a_cat_code, category_type => undef } )->store;
112     is( Koha::Patron::Categories->find($a_cat_code)->category_type, 'A', 'Koha::Object->new should set the default value even if the argument exists but is not defined' );
113     Koha::Patron::Categories->find($a_cat_code)->delete;
114 };
115
116 subtest 'find' => sub {
117     plan tests => 4;
118
119     # check find on a single PK
120     my $patron = $builder->build({ source => 'Borrower' });
121     is( Koha::Patrons->find($patron->{borrowernumber})->surname,
122         $patron->{surname}, "Checking an arbitrary patron column after find"
123     );
124     # check find with unique column
125     my $obj = Koha::Patrons->find($patron->{cardnumber}, { key => 'cardnumber' });
126     is( $obj->borrowernumber, $patron->{borrowernumber},
127         'Find with unique column and key specified' );
128     # check find with an additional where clause in the attrs hash
129     # we do not expect to find something now
130     is( Koha::Patrons->find(
131         $patron->{borrowernumber},
132         { where => { surname => { '!=', $patron->{surname} }}},
133     ), undef, 'Additional where clause in find call' );
134
135     is( Koha::Patrons->find(), undef, 'Find returns undef if no params passed' );
136 };
137
138 subtest 'search_related' => sub {
139     plan tests => 8;
140     my $builder   = t::lib::TestBuilder->new;
141     my $patron_1  = $builder->build( { source => 'Borrower' } );
142     my $patron_2  = $builder->build( { source => 'Borrower' } );
143     my $libraries = Koha::Patrons->search( { -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } } )->search_related('branchcode');
144     is( ref( $libraries ), 'Koha::Libraries', 'Koha::Objects->search_related should return an instanciated Koha::Objects-based object' );
145     is( $libraries->count,            2,                       'Koha::Objects->search_related should work as expected' );
146     is( $libraries->next->branchcode, $patron_1->{branchcode}, 'Koha::Objects->search_related should work as expected' );
147     is( $libraries->next->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
148
149     my @libraries = Koha::Patrons->search( { -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } } )->search_related('branchcode');
150     is( ref( $libraries[0] ),      'Koha::Library',         'Koha::Objects->search_related should return a list of Koha::Object-based objects' );
151     is( scalar(@libraries),        2,                       'Koha::Objects->search_related should work as expected' );
152     is( $libraries[0]->branchcode, $patron_1->{branchcode}, 'Koha::Objects->search_related should work as expected' );
153     is( $libraries[1]->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
154 };
155
156 subtest 'single' => sub {
157     plan tests => 2;
158     my $builder   = t::lib::TestBuilder->new;
159     my $patron_1  = $builder->build( { source => 'Borrower' } );
160     my $patron_2  = $builder->build( { source => 'Borrower' } );
161     my $patron = Koha::Patrons->search({}, { rows => 1 })->single;
162     is(ref($patron), 'Koha::Patron', 'Koha::Objects->single returns a single Koha::Patron object.');
163     warning_like { Koha::Patrons->search->single } qr/SQL that returns multiple rows/,
164     "Warning is presented if single is used for a result with multiple rows.";
165 };
166
167 subtest 'last' => sub {
168     plan tests => 3;
169     my $builder = t::lib::TestBuilder->new;
170     my $patron_1  = $builder->build( { source => 'Borrower' } );
171     my $patron_2  = $builder->build( { source => 'Borrower' } );
172     my $last_patron = Koha::Patrons->search->last;
173     is( $last_patron->borrowernumber, $patron_2->{borrowernumber}, '->last should return the last inserted patron' );
174     $last_patron = Koha::Patrons->search({ borrowernumber => $patron_1->{borrowernumber} })->last;
175     is( $last_patron->borrowernumber, $patron_1->{borrowernumber}, '->last should work even if there is only 1 result' );
176     $last_patron = Koha::Patrons->search({ surname => 'should_not_exist' })->last;
177     is( $last_patron, undef, '->last should return undef if search does not return any results' );
178 };
179
180 subtest 'get_column' => sub {
181     plan tests => 1;
182     my @cities = Koha::Cities->search;
183     my @city_names = map { $_->city_name } @cities;
184     is_deeply( [ Koha::Cities->search->get_column('city_name') ], \@city_names, 'Koha::Objects->get_column should be allowed' );
185 };
186
187 subtest 'Exceptions' => sub {
188     plan tests => 7;
189
190     my $patron_borrowernumber = $builder->build({ source => 'Borrower' })->{ borrowernumber };
191     my $patron = Koha::Patrons->find( $patron_borrowernumber );
192
193     # Koha::Object
194     try {
195         $patron->blah('blah');
196     } catch {
197         ok( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
198             'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
199         is( $_->message, 'The method Koha::Patron->blah is not covered by tests!', 'The message raised should contain the package and the method' );
200     };
201
202     try {
203         $patron->set({ blah => 'blah' });
204     } catch {
205         ok( $_->isa('Koha::Exceptions::Object::PropertyNotFound'),
206             'Setting a non-existent property should raise a Koha::Exceptions::Object::PropertyNotFound exception' );
207     };
208
209     # Koha::Objects
210     try {
211         Koha::Patrons->search->not_covered_yet;
212     } catch {
213         ok( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
214             'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
215         is( $_->message, 'The method Koha::Patrons->not_covered_yet is not covered by tests!', 'The message raised should contain the package and the method' );
216     };
217
218     try {
219         Koha::Patrons->not_covered_yet;
220     } catch {
221         ok( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
222             'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
223         is( $_->message, 'The method Koha::Patrons->not_covered_yet is not covered by tests!', 'The message raised should contain the package and the method' );
224     };
225 };
226
227 $schema->storage->txn_rollback;
228
229 subtest '->is_paged and ->pager tests' => sub {
230
231     plan tests => 5;
232
233     $schema->storage->txn_begin;
234
235     # Delete existing patrons
236     Koha::Checkouts->delete;
237     Koha::Patrons->delete;
238     # Create 10 patrons
239     foreach (1..10) {
240         $builder->build_object({ class => 'Koha::Patrons' });
241     }
242
243     # Non-paginated search
244     my $patrons = Koha::Patrons->search();
245     is( $patrons->count, 10, 'Search returns all patrons' );
246     ok( !$patrons->is_paged, 'Search is not paged' );
247
248     # Paginated search
249     $patrons = Koha::Patrons->search( undef, { 'page' => 1, 'rows' => 3 } );
250     is( $patrons->count, 3, 'Search returns only one page, 3 patrons' );
251     ok( $patrons->is_paged, 'Search is paged' );
252     my $pager = $patrons->pager;
253     is( ref($patrons->pager), 'DBIx::Class::ResultSet::Pager',
254        'Koha::Objects->pager returns a valid DBIx::Class object' );
255
256     $schema->storage->txn_rollback;
257 };
258
259 subtest '->search() tests' => sub {
260
261     plan tests => 12;
262
263     $schema->storage->txn_begin;
264
265     my $count = Koha::Patrons->search->count;
266
267     # Create 10 patrons
268     foreach (1..10) {
269         $builder->build_object({ class => 'Koha::Patrons' });
270     }
271
272     my $patrons = Koha::Patrons->search();
273     is( ref($patrons), 'Koha::Patrons', 'search in scalar context returns the Koha::Object-based type' );
274     my @patrons = Koha::Patrons->search();
275     is( scalar @patrons, $count + 10, 'search in list context returns a list of objects' );
276     my $i = 0;
277     foreach (1..10) {
278         is( ref($patrons[$i]), 'Koha::Patron', 'Objects in the list have the singular type' );
279         $i++;
280     }
281
282     $schema->storage->txn_rollback;
283 };
284
285 subtest "to_api() tests" => sub {
286
287     plan tests => 18;
288
289     $schema->storage->txn_begin;
290
291     my $city_1 = $builder->build_object( { class => 'Koha::Cities' } );
292     my $city_2 = $builder->build_object( { class => 'Koha::Cities' } );
293
294     my $cities = Koha::Cities->search(
295         {
296             cityid => [ $city_1->cityid, $city_2->cityid ]
297         },
298         { -orderby => { -desc => 'cityid' } }
299     );
300
301     is( $cities->count, 2, 'Count is correct' );
302     my $cities_api = $cities->to_api;
303     is( ref( $cities_api ), 'ARRAY', 'to_api returns an array' );
304     is_deeply( $cities_api->[0], $city_1->to_api, 'to_api returns the individual objects with ->to_api' );
305     is_deeply( $cities_api->[1], $city_2->to_api, 'to_api returns the individual objects with ->to_api' );
306
307     my $biblio_1 = $builder->build_sample_biblio();
308     my $item_1   = $builder->build_sample_item({ biblionumber => $biblio_1->biblionumber });
309     my $hold_1   = $builder->build_object(
310         {
311             class => 'Koha::Holds',
312             value => { itemnumber => $item_1->itemnumber }
313         }
314     );
315
316     my $biblio_2 = $builder->build_sample_biblio();
317     my $item_2   = $builder->build_sample_item({ biblionumber => $biblio_2->biblionumber });
318     my $hold_2   = $builder->build_object(
319         {
320             class => 'Koha::Holds',
321             value => { itemnumber => $item_2->itemnumber }
322         }
323     );
324
325     my $embed = { 'items' => {} };
326
327     my $i = 0;
328     my @items = ( $item_1, $item_2 );
329     my @holds = ( $hold_1, $hold_2 );
330
331     my $biblios_api = Koha::Biblios->search(
332         {
333             biblionumber => [ $biblio_1->biblionumber, $biblio_2->biblionumber ]
334         }
335     )->to_api( { embed => $embed } );
336
337     foreach my $biblio_api ( @{ $biblios_api } ) {
338         ok(exists $biblio_api->{items}, 'Items where embedded in biblio results');
339         is($biblio_api->{items}->[0]->{item_id}, $items[$i]->itemnumber, 'Item matches');
340         ok(!exists $biblio_api->{items}->[0]->{holds}, 'No holds info should be embedded yet');
341
342         $i++;
343     }
344
345     # One more level
346     $embed = {
347         'items' => {
348             children => { 'holds' => {} }
349         }
350     };
351
352     $i = 0;
353
354     $biblios_api = Koha::Biblios->search(
355         {
356             biblionumber => [ $biblio_1->biblionumber, $biblio_2->biblionumber ]
357         }
358     )->to_api( { embed => $embed } );
359
360     foreach my $biblio_api ( @{ $biblios_api } ) {
361
362         ok(exists $biblio_api->{items}, 'Items where embedded in biblio results');
363         is($biblio_api->{items}->[0]->{item_id}, $items[$i]->itemnumber, 'Item still matches');
364         ok(exists $biblio_api->{items}->[0]->{holds}, 'Holds info should be embedded');
365         is($biblio_api->{items}->[0]->{holds}->[0]->{hold_id}, $holds[$i]->reserve_id, 'Hold matches');
366
367         $i++;
368     }
369
370     $schema->storage->txn_rollback;
371 };
372
373 subtest "TO_JSON() tests" => sub {
374
375     plan tests => 4;
376
377     $schema->storage->txn_begin;
378
379     my $city_1 = $builder->build_object( { class => 'Koha::Cities' } );
380     my $city_2 = $builder->build_object( { class => 'Koha::Cities' } );
381
382     my $cities = Koha::Cities->search(
383         {
384             cityid => [ $city_1->cityid, $city_2->cityid ]
385         },
386         { -orderby => { -desc => 'cityid' } }
387     );
388
389     is( $cities->count, 2, 'Count is correct' );
390     my $cities_json = $cities->TO_JSON;
391     is( ref($cities_json), 'ARRAY', 'to_api returns an array' );
392     is_deeply( $cities_json->[0], $city_1->TO_JSON, 'TO_JSON returns the individual objects with ->TO_JSON' );
393     is_deeply( $cities_json->[1], $city_2->TO_JSON,'TO_JSON returns the individual objects with ->TO_JSON' );
394
395     $schema->storage->txn_rollback;
396 };
397
398 # Koha::Object[s] must behave the same as DBIx::Class
399 subtest 'Return same values as DBIx::Class' => sub {
400     plan tests => 1;
401
402     subtest 'Delete' => sub {
403         plan tests => 2;
404
405         $schema->storage->txn_begin;
406
407         subtest 'Simple Koha::Objects - Koha::Cities' => sub {
408             plan tests => 2;
409
410             subtest 'Koha::Object->delete' => sub {
411
412                 plan tests => 5;
413
414                 my ( $r_us, $e_us, $r_them, $e_them );
415
416                 # CASE 1 - Delete an existing object
417                 my $c = Koha::City->new( { city_name => 'city4test' } )->store;
418                 try { $r_us = $c->delete; } catch { $e_us = $_ };
419                 $c = $schema->resultset('City')->new( { city_name => 'city4test_2' } )->update_or_insert;
420                 try { $r_them = $c->delete; } catch { $e_them = $_ };
421                 ok( ref($r_us) && ref($r_them),
422                     'Successful delete should return the object ' );
423                 ok( !defined $e_us && !defined $e_them,
424                     'Successful delete should not raise an exception' );
425                 is( ref($r_us), 'Koha::City', 'Successful delete should return our Koha::Obect based object' );
426
427                 # CASE 2 - Delete an object that is not in storage
428                 try { $r_us   = $r_us->delete;   } catch { $e_us   = $_ };
429                 try { $r_them = $r_them->delete; } catch { $e_them = $_ };
430                 ok(
431                     defined $e_us && defined $e_them,
432                     'Delete an object that is not in storage should raise an exception'
433                 );
434                 is( ref($e_us), 'DBIx::Class::Exception' )
435                   ; # FIXME This needs adjustement, we want to throw a Koha::Exception
436
437             };
438
439             subtest 'Koha::Objects->delete' => sub {
440
441                 plan tests => 4;
442
443                 my ( $r_us, $e_us, $r_them, $e_them );
444
445                 # CASE 1 - Delete existing objects
446                 my $city_1 = $builder->build_object({ class => 'Koha::Cities' });
447                 my $city_2 = $builder->build_object({ class => 'Koha::Cities' });
448                 my $city_3 = $builder->build_object({ class => 'Koha::Cities' });
449                 my $cities = Koha::Cities->search(
450                     {
451                         cityid => {
452                             -in => [
453                                 $city_1->cityid,
454                                 $city_2->cityid,
455                                 $city_3->cityid,
456                             ]
457                         }
458                     }
459                 );
460
461                 try { $r_us = $cities->delete; } catch { $e_us = $_ };
462
463                 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
464                 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
465                 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
466                 $cities = $schema->resultset('City')->search(
467                     {
468                         cityid => {
469                             -in => [
470                                 $city_1->cityid,
471                                 $city_2->cityid,
472                                 $city_3->cityid,
473                             ]
474                         }
475                     }
476                 );
477
478                 try { $r_them = $cities->delete; } catch { $e_them = $_ };
479
480                 ok( $r_us == 3 && $r_them == 3 );
481                 ok (!defined($e_us) && !defined($e_them));
482
483                 # CASE 2 - One of the object is not in storage
484                 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
485                 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
486                 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
487                 $cities = Koha::Cities->search(
488                     {
489                         cityid => {
490                             -in => [
491                                 $city_1->cityid,
492                                 $city_2->cityid,
493                                 $city_3->cityid,
494                             ]
495                         }
496                     }
497                 );
498
499                 $city_2->delete; # We delete one of the object
500                 try { $r_us = $cities->delete; } catch { $e_us = $_ };
501
502                 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
503                 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
504                 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
505                 $cities = $schema->resultset('City')->search(
506                     {
507                         cityid => {
508                             -in => [
509                                 $city_1->cityid,
510                                 $city_2->cityid,
511                                 $city_3->cityid,
512                             ]
513                         }
514                     }
515                 );
516
517                 $city_2->delete; # We delete one of the object
518                 try { $r_them = $cities->delete; } catch { $e_them = $_ };
519
520                 ok( $r_us == 2 && $r_them == 2 );
521                 ok (!defined($e_us) && !defined($e_them));
522             };
523         };
524
525         subtest 'Overwritten Koha::Objects->delete - Koha::Patrons' => sub {
526
527             plan tests => 2;
528
529             subtest 'Koha::Object->delete' => sub {
530
531                 plan tests => 7;
532
533                 my ( $r_us, $e_us, $r_them, $e_them );
534
535                 # CASE 1 - Delete an existing patron
536                 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
537                 my $patron_data = $patron->unblessed;
538                 $patron->delete;
539
540                 $patron = Koha::Patron->new( $patron_data )->store;
541                 try {$r_us = $patron->delete;} catch { $e_us = $_ };
542                 $patron = $schema->resultset('Borrower')->new( $patron_data )->update_or_insert;
543                 try {$r_them = $patron->delete;} catch { $e_them = $_ };
544                 ok( ref($r_us) && ref($r_them),
545                     'Successful delete should return the patron object' );
546                 ok( !defined $e_us && !defined $e_them,
547                     'Successful delete should not raise an exception' );
548                 is( ref($r_us), 'Koha::Patron',
549                     'Successful delete should return our Koha::Obect based object' );
550
551                 # CASE 2 - Delete a patron that is not in storage
552                 try { $r_us   = $r_us->delete;   } catch { $e_us   = $_ };
553                 try { $r_them = $r_them->delete; } catch { $e_them = $_ };
554                 ok(
555                     defined $e_us && defined $e_them,
556                     'Delete a patron that is not in storage should raise an exception'
557                 );
558                 is( ref($e_us), 'DBIx::Class::Exception' )
559                   ; # FIXME This needs adjustement, we want to throw a Koha::Exception
560
561                 # CASE 3 - Delete a patron that cannot be deleted (as a checkout)
562                 $patron = Koha::Patron->new($patron_data)->store;
563                 $builder->build_object(
564                     {
565                         class => 'Koha::Checkouts',
566                         value => { borrowernumber => $patron->borrowernumber }
567                     }
568                 );
569                 try { $r_us = $r_us->delete; } catch { $e_us = $_ };
570                 $patron = $schema->resultset('Borrower')->find( $patron->borrowernumber );
571                 try { $r_them = $r_them->delete; } catch { $e_them = $_ };
572                 ok(
573                     defined $e_us && defined $e_them,
574                     'Delete a patron that cannot be deleted should raise an exception'
575                 );
576                 is( ref($e_us), 'DBIx::Class::Exception' )
577                   ; # FIXME This needs adjustement, we want to throw a Koha::Exception
578             };
579
580             subtest 'Koha::Objects->delete' => sub {
581
582                 plan tests => 9;
583
584                 my ( $r_us, $e_us, $r_them, $e_them );
585
586                 # CASE 1 - Delete existing objects
587                 my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
588                 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
589                 my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
590                 my $patrons = Koha::Patrons->search(
591                     {
592                         borrowernumber => {
593                             -in => [
594                                 $patron_1->borrowernumber,
595                                 $patron_2->borrowernumber,
596                                 $patron_3->borrowernumber
597                             ]
598                         }
599                     }
600                 );
601
602                 try { $r_us = $patrons->delete; } catch { $e_us = $_ };
603
604                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
605                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
606                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
607                 $patrons = $schema->resultset('Borrower')->search(
608                     {
609                         borrowernumber => {
610                             -in => [
611                                 $patron_1->borrowernumber,
612                                 $patron_2->borrowernumber,
613                                 $patron_3->borrowernumber
614                             ]
615                         }
616                     }
617                 );
618
619                 try { $r_them = $patrons->delete; } catch { $e_them = $_ };
620
621                 ok( $r_us == 3 && $r_them == 3, '->delete should return the number of deleted patrons' );
622                 ok (!defined($e_us) && !defined($e_them), '->delete should not raise exception if everything went well');
623
624                 # CASE 2 - One of the patrons is not in storage
625                 undef $_ for $r_us, $e_us, $r_them, $e_them;
626                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
627                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
628                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
629                 $patrons = Koha::Patrons->search(
630                     {
631                         borrowernumber => {
632                             -in => [
633                                 $patron_1->borrowernumber,
634                                 $patron_2->borrowernumber,
635                                 $patron_3->borrowernumber
636                             ]
637                         }
638                     }
639                 );
640
641                 $patron_2->delete; # We delete one of the patron
642                 try { $r_us = $patrons->delete; } catch { $e_us = $_ };
643
644                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
645                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
646                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
647                 $patrons = $schema->resultset('Borrower')->search(
648                     {
649                         borrowernumber => {
650                             -in => [
651                                 $patron_1->borrowernumber,
652                                 $patron_2->borrowernumber,
653                                 $patron_3->borrowernumber
654                             ]
655                         }
656                     }
657                 );
658
659                 $patron_2->delete; # We delete one of the patron
660                 try { $r_them = $patrons->delete; } catch { $e_them = $_ };
661
662                 ok( $r_us == 2 && $r_them == 2, 'Delete patrons with one that was not in storage should delete the patrons' );
663                 ok (!defined($e_us) && !defined($e_them), 'no exception should be raised if at least one patron was not in storage');
664
665                 # CASE 3 - Delete a set of patrons with one that that cannot be deleted (as a checkout)
666                 undef $_ for $r_us, $e_us, $r_them, $e_them;
667                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
668                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
669                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
670                 $patrons = Koha::Patrons->search(
671                     {
672                         borrowernumber => {
673                             -in => [
674                                 $patron_1->borrowernumber,
675                                 $patron_2->borrowernumber,
676                                 $patron_3->borrowernumber
677                             ]
678                         }
679                     }
680                 );
681
682                 # Adding a checkout to patron_2
683                 $builder->build_object(
684                     {
685                         class => 'Koha::Checkouts',
686                         value => { borrowernumber => $patron_2->borrowernumber }
687                     }
688                 );
689
690                 warning_like {
691                     try { $r_us = $patrons->delete; } catch { $e_us = $_ };
692                 }
693                 qr{DBD::mysql::st execute failed: Cannot delete or update a parent row: a foreign key constraint fails},
694                   "Foreign key constraint DBI error should be logged";
695                 my $not_deleted_us = $patron_1->in_storage + $patron_2->in_storage + $patron_3->in_storage;
696
697                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
698                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
699                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
700                 $patrons = $schema->resultset('Borrower')->search(
701                     {
702                         borrowernumber => {
703                             -in => [
704                                 $patron_1->borrowernumber,
705                                 $patron_2->borrowernumber,
706                                 $patron_3->borrowernumber
707                             ]
708                         }
709                     }
710                 );
711
712                 # Adding a checkout to patron_2
713                 $builder->build_object(
714                     {
715                         class => 'Koha::Checkouts',
716                         value => { borrowernumber => $patron_2->borrowernumber }
717                     }
718                 );
719
720                 warning_like {
721                     try { $r_them = $patrons->delete; } catch { $e_them = $_ };
722                 }
723                 qr{DBD::mysql::st execute failed: Cannot delete or update a parent row: a foreign key constraint fails},
724                   "Foreign key constraint DBI error should be logged";
725
726                 my $not_deleted_them = $patron_1->in_storage + $patron_2->in_storage + $patron_3->in_storage;
727                 ok(
728                     defined $e_us && defined $e_them,
729                     'Delete patrons with one that cannot be deleted should raise an exception'
730                 );
731                 is( ref($e_us), 'DBIx::Class::Exception' )
732                   ; # FIXME This needs adjustement, we want to throw a Koha::Exception
733
734                 ok($not_deleted_us == 3 && $not_deleted_them == 3, 'If one patron cannot be deleted, none should have been deleted');
735             };
736         };
737
738         $schema->storage->txn_rollback;
739
740     };
741 };
742
743 subtest "attributes_from_api() tests" => sub {
744
745     plan tests => 1;
746
747     $schema->storage->txn_begin;
748
749     my $cities_rs = Koha::Cities->new;
750     my $city      = Koha::City->new;
751
752     my $api_attributes = {
753         name        => 'Cordoba',
754         postal_code => 5000
755     };
756
757     is_deeply(
758         $cities_rs->attributes_from_api($api_attributes),
759         $city->attributes_from_api($api_attributes)
760     );
761
762     $schema->storage->txn_rollback;
763 };
764
765 subtest "from_api_mapping() tests" => sub {
766
767     plan tests => 1;
768
769     $schema->storage->txn_begin;
770
771     my $cities_rs = Koha::Cities->new;
772     my $city      = Koha::City->new;
773
774     is_deeply(
775         $cities_rs->from_api_mapping,
776         $city->from_api_mapping
777     );
778
779     $schema->storage->txn_rollback;
780 };
781
782 subtest 'prefetch_whitelist() tests' => sub {
783
784     plan tests => 2;
785
786     $schema->storage->txn_begin;
787
788     my $biblios = Koha::Biblios->new;
789
790     my $prefetch_whitelist = $biblios->prefetch_whitelist;
791
792     ok(
793         exists $prefetch_whitelist->{orders},
794         'Relationship matching method name is listed'
795     );
796     is(
797         $prefetch_whitelist->{orders},
798         'Koha::Acquisition::Order',
799         'Guessed the object class correctly'
800     );
801
802     $schema->storage->txn_rollback;
803 };