Bug 29149: Add the capability to provide more info to the background job detail view
[koha.git] / t / db_dependent / Koha / Objects.t
1 #!/usr/bin/perl
2
3 # Copyright 2019 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 => 24;
23 use Test::Exception;
24 use Test::MockModule;
25 use Test::Warn;
26
27 use C4::Context;
28
29 use Koha::Authority::Types;
30 use Koha::Cities;
31 use Koha::Biblios;
32 use Koha::Patron::Category;
33 use Koha::Patron::Categories;
34 use Koha::Patrons;
35 use Koha::Database;
36 use Koha::DateUtils qw( dt_from_string );
37
38 use t::lib::TestBuilder;
39 use t::lib::Mocks;
40
41 use Try::Tiny;
42
43 my $schema = Koha::Database->new->schema;
44 $schema->storage->txn_begin;
45 my $builder = t::lib::TestBuilder->new;
46
47 is( ref(Koha::Authority::Types->find('')), 'Koha::Authority::Type', 'Koha::Objects->find should work if the primary key is an empty string' );
48
49 my @columns = Koha::Patrons->columns;
50 my $borrowernumber_exists = grep { /^borrowernumber$/ } @columns;
51 is( $borrowernumber_exists, 1, 'Koha::Objects->columns should return the table columns' );
52
53 subtest 'find' => sub {
54     plan tests => 6;
55     my $patron = $builder->build({source => 'Borrower'});
56     my $patron_object = Koha::Patrons->find( $patron->{borrowernumber} );
57     is( $patron_object->borrowernumber, $patron->{borrowernumber}, '->find should return the correct object' );
58
59     my @patrons = Koha::Patrons->find( $patron->{borrowernumber} );
60     is(scalar @patrons, 1, '->find in list context returns a value');
61     is($patrons[0]->borrowernumber, $patron->{borrowernumber}, '->find in list context returns the same value as in scalar context');
62
63     my $patrons = {
64         foo => Koha::Patrons->find('foo'),
65         bar => 'baz',
66     };
67     is ($patrons->{foo}, undef, '->find in list context returns undef when no record is found');
68
69     # Test sending undef to find; should not generate a warning
70     warning_is { $patron = Koha::Patrons->find( undef ); }
71         "", "Sending undef does not trigger a DBIx warning";
72     warning_is { $patron = Koha::Patrons->find( undef, undef ); }
73         "", "Sending two undefs does not trigger a DBIx warning too";
74 };
75
76 subtest 'update' => sub {
77     plan tests => 2;
78
79     $builder->build( { source => 'City', value => { city_country => 'UK' } } );
80     $builder->build( { source => 'City', value => { city_country => 'UK' } } );
81     $builder->build( { source => 'City', value => { city_country => 'UK' } } );
82     $builder->build( { source => 'City', value => { city_country => 'France' } } );
83     $builder->build( { source => 'City', value => { city_country => 'France' } } );
84     $builder->build( { source => 'City', value => { city_country => 'Germany' } } );
85     Koha::Cities->search( { city_country => 'UK' } )->update( { city_country => 'EU' } );
86     is( Koha::Cities->search( { city_country => 'EU' } )->count, 3, 'Koha::Objects->update should have updated the 3 rows' );
87     is( Koha::Cities->search( { city_country => 'UK' } )->count, 0, 'Koha::Objects->update should have updated the 3 rows' );
88 };
89
90 subtest 'reset' => sub {
91     plan tests => 3;
92
93     my $patrons = Koha::Patrons->search;
94     my $first_borrowernumber = $patrons->next->borrowernumber;
95     my $second_borrowernumber = $patrons->next->borrowernumber;
96     is( ref( $patrons->reset ), 'Koha::Patrons', 'Koha::Objects->reset should allow chaining' );
97     is( ref( $patrons->reset->next ), 'Koha::Patron', 'Koha::Objects->reset should allow chaining' );
98     is( $patrons->reset->next->borrowernumber, $first_borrowernumber, 'Koha::Objects->reset should work as expected');
99 };
100
101 subtest 'delete' => sub {
102     plan tests => 2;
103
104     my $patron_1 = $builder->build({source => 'Borrower'});
105     my $patron_2 = $builder->build({source => 'Borrower'});
106     is( Koha::Patrons->search({ -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}]}})->delete, 2, '');
107     is( Koha::Patrons->search({ -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}]}})->count, 0, '');
108 };
109
110 subtest 'new' => sub {
111     plan tests => 2;
112     my $a_cat_code = 'A_CAT_CODE';
113     my $patron_category = Koha::Patron::Category->new( { categorycode => $a_cat_code } )->store;
114     is( Koha::Patron::Categories->find($a_cat_code)->category_type, 'A', 'Koha::Object->new should set the default value' );
115     Koha::Patron::Categories->find($a_cat_code)->delete;
116     $patron_category = Koha::Patron::Category->new( { categorycode => $a_cat_code, category_type => undef } )->store;
117     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' );
118     Koha::Patron::Categories->find($a_cat_code)->delete;
119 };
120
121 subtest 'find' => sub {
122     plan tests => 4;
123
124     # check find on a single PK
125     my $patron = $builder->build({ source => 'Borrower' });
126     is( Koha::Patrons->find($patron->{borrowernumber})->surname,
127         $patron->{surname}, "Checking an arbitrary patron column after find"
128     );
129     # check find with unique column
130     my $obj = Koha::Patrons->find($patron->{cardnumber}, { key => 'cardnumber' });
131     is( $obj->borrowernumber, $patron->{borrowernumber},
132         'Find with unique column and key specified' );
133     # check find with an additional where clause in the attrs hash
134     # we do not expect to find something now
135     is( Koha::Patrons->find(
136         $patron->{borrowernumber},
137         { where => { surname => { '!=', $patron->{surname} }}},
138     ), undef, 'Additional where clause in find call' );
139
140     is( Koha::Patrons->find(), undef, 'Find returns undef if no params passed' );
141 };
142
143 subtest 'search_related' => sub {
144     plan tests => 6;
145     my $builder   = t::lib::TestBuilder->new;
146     my $patron_1  = $builder->build( { source => 'Borrower' } );
147     my $patron_2  = $builder->build( { source => 'Borrower' } );
148     my $libraries = Koha::Patrons->search(
149         {
150             -or => {
151                 borrowernumber =>
152                   [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ]
153             }
154         }
155     )->search_related('branchcode');
156     is( ref($libraries), 'Koha::Libraries',
157         'Koha::Objects->search_related should return an instanciated Koha::Objects-based object'
158     );
159     is( $libraries->count, 2,
160         'Koha::Objects->search_related should work as expected' );
161     ok( eq_array(
162         [ $libraries->get_column('branchcode') ],
163         [ $patron_1->{branchcode}, $patron_2->{branchcode} ] ),
164         'Koha::Objects->search_related should work as expected'
165     );
166
167     my @libraries = Koha::Patrons->search(
168         {
169             -or => {
170                 borrowernumber =>
171                   [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ]
172             }
173         }
174     )->search_related('branchcode');
175     is(
176         ref( $libraries[0] ), 'Koha::Library',
177         'Koha::Objects->search_related should return a list of Koha::Object-based objects'
178     );
179     is( scalar(@libraries), 2,
180         'Koha::Objects->search_related should work as expected' );
181     ok( eq_array(
182         [ map { $_->branchcode } @libraries ],
183         [ $patron_1->{branchcode}, $patron_2->{branchcode} ] ),
184         'Koha::Objects->search_related should work as expected'
185     );
186 };
187
188 subtest 'single' => sub {
189     plan tests => 2;
190     my $builder   = t::lib::TestBuilder->new;
191     my $patron_1  = $builder->build( { source => 'Borrower' } );
192     my $patron_2  = $builder->build( { source => 'Borrower' } );
193     my $patron = Koha::Patrons->search({}, { rows => 1 })->single;
194     is(ref($patron), 'Koha::Patron', 'Koha::Objects->single returns a single Koha::Patron object.');
195     warning_like { Koha::Patrons->search->single } qr/SQL that returns multiple rows/,
196     "Warning is presented if single is used for a result with multiple rows.";
197 };
198
199 subtest 'last' => sub {
200     plan tests => 3;
201     my $builder = t::lib::TestBuilder->new;
202     my $patron_1  = $builder->build( { source => 'Borrower' } );
203     my $patron_2  = $builder->build( { source => 'Borrower' } );
204     my $last_patron = Koha::Patrons->search->last;
205     is( $last_patron->borrowernumber, $patron_2->{borrowernumber}, '->last should return the last inserted patron' );
206     $last_patron = Koha::Patrons->search({ borrowernumber => $patron_1->{borrowernumber} })->last;
207     is( $last_patron->borrowernumber, $patron_1->{borrowernumber}, '->last should work even if there is only 1 result' );
208     $last_patron = Koha::Patrons->search({ surname => 'should_not_exist' })->last;
209     is( $last_patron, undef, '->last should return undef if search does not return any results' );
210 };
211
212 subtest 'get_column' => sub {
213     plan tests => 1;
214     my @cities = Koha::Cities->search;
215     my @city_names = map { $_->city_name } @cities;
216     is_deeply( [ Koha::Cities->search->get_column('city_name') ], \@city_names, 'Koha::Objects->get_column should be allowed' );
217 };
218
219 subtest 'Exceptions' => sub {
220     plan tests => 7;
221
222     my $patron_borrowernumber = $builder->build({ source => 'Borrower' })->{ borrowernumber };
223     my $patron = Koha::Patrons->find( $patron_borrowernumber );
224
225     # Koha::Object
226     try {
227         $patron->blah('blah');
228     } catch {
229         ok( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
230             'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
231         is( $_->message, 'The method Koha::Patron->blah is not covered by tests!', 'The message raised should contain the package and the method' );
232     };
233
234     try {
235         $patron->set({ blah => 'blah' });
236     } catch {
237         ok( $_->isa('Koha::Exceptions::Object::PropertyNotFound'),
238             'Setting a non-existent property should raise a Koha::Exceptions::Object::PropertyNotFound exception' );
239     };
240
241     # Koha::Objects
242     try {
243         Koha::Patrons->search->not_covered_yet;
244     } catch {
245         ok( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
246             'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
247         is( $_->message, 'The method Koha::Patrons->not_covered_yet is not covered by tests!', 'The message raised should contain the package and the method' );
248     };
249
250     try {
251         Koha::Patrons->not_covered_yet;
252     } catch {
253         ok( $_->isa('Koha::Exceptions::Object::MethodNotCoveredByTests'),
254             'Calling a non-covered method should raise a Koha::Exceptions::Object::MethodNotCoveredByTests exception' );
255         is( $_->message, 'The method Koha::Patrons->not_covered_yet is not covered by tests!', 'The message raised should contain the package and the method' );
256     };
257 };
258
259 $schema->storage->txn_rollback;
260
261 subtest '->is_paged and ->pager tests' => sub {
262
263     plan tests => 5;
264
265     $schema->storage->txn_begin;
266
267     # Count existing patrons
268     my $nb_patrons = Koha::Patrons->search()->count;
269     # Create 10 more patrons
270     foreach (1..10) {
271         $builder->build_object({ class => 'Koha::Patrons' });
272     }
273
274     # Non-paginated search
275     my $patrons = Koha::Patrons->search();
276     is( $patrons->count, $nb_patrons + 10, 'Search returns all patrons' );
277     ok( !$patrons->is_paged, 'Search is not paged' );
278
279     # Paginated search
280     $patrons = Koha::Patrons->search( undef, { 'page' => 1, 'rows' => 3 } );
281     is( $patrons->count, 3, 'Search returns only one page, 3 patrons' );
282     ok( $patrons->is_paged, 'Search is paged' );
283     my $pager = $patrons->pager;
284     is( ref($patrons->pager), 'DBIx::Class::ResultSet::Pager',
285        'Koha::Objects->pager returns a valid DBIx::Class object' );
286
287     $schema->storage->txn_rollback;
288 };
289
290 subtest '->search() tests' => sub {
291
292     plan tests => 12;
293
294     $schema->storage->txn_begin;
295
296     my $count = Koha::Patrons->search->count;
297
298     # Create 10 patrons
299     foreach (1..10) {
300         $builder->build_object({ class => 'Koha::Patrons' });
301     }
302
303     my $patrons = Koha::Patrons->search();
304     is( ref($patrons), 'Koha::Patrons', 'search in scalar context returns the Koha::Object-based type' );
305     my @patrons = Koha::Patrons->search();
306     is( scalar @patrons, $count + 10, 'search in list context returns a list of objects' );
307     my $i = 0;
308     foreach (1..10) {
309         is( ref($patrons[$i]), 'Koha::Patron', 'Objects in the list have the singular type' );
310         $i++;
311     }
312
313     $schema->storage->txn_rollback;
314 };
315
316 subtest "to_api() tests" => sub {
317
318     plan tests => 18;
319
320     $schema->storage->txn_begin;
321
322     my $city_1 = $builder->build_object( { class => 'Koha::Cities' } );
323     my $city_2 = $builder->build_object( { class => 'Koha::Cities' } );
324
325     my $cities = Koha::Cities->search(
326         {
327             cityid => [ $city_1->cityid, $city_2->cityid ]
328         },
329         { -orderby => { -desc => 'cityid' } }
330     );
331
332     is( $cities->count, 2, 'Count is correct' );
333     my $cities_api = $cities->to_api;
334     is( ref( $cities_api ), 'ARRAY', 'to_api returns an array' );
335     is_deeply( $cities_api->[0], $city_1->to_api, 'to_api returns the individual objects with ->to_api' );
336     is_deeply( $cities_api->[1], $city_2->to_api, 'to_api returns the individual objects with ->to_api' );
337
338     my $biblio_1 = $builder->build_sample_biblio();
339     my $item_1   = $builder->build_sample_item({ biblionumber => $biblio_1->biblionumber });
340     my $hold_1   = $builder->build_object(
341         {
342             class => 'Koha::Holds',
343             value => { itemnumber => $item_1->itemnumber }
344         }
345     );
346
347     my $biblio_2 = $builder->build_sample_biblio();
348     my $item_2   = $builder->build_sample_item({ biblionumber => $biblio_2->biblionumber });
349     my $hold_2   = $builder->build_object(
350         {
351             class => 'Koha::Holds',
352             value => { itemnumber => $item_2->itemnumber }
353         }
354     );
355
356     my $embed = { 'items' => {} };
357
358     my $i = 0;
359     my @items = ( $item_1, $item_2 );
360     my @holds = ( $hold_1, $hold_2 );
361
362     my $biblios_api = Koha::Biblios->search(
363         {
364             biblionumber => [ $biblio_1->biblionumber, $biblio_2->biblionumber ]
365         }
366     )->to_api( { embed => $embed } );
367
368     foreach my $biblio_api ( @{ $biblios_api } ) {
369         ok(exists $biblio_api->{items}, 'Items where embedded in biblio results');
370         is($biblio_api->{items}->[0]->{item_id}, $items[$i]->itemnumber, 'Item matches');
371         ok(!exists $biblio_api->{items}->[0]->{holds}, 'No holds info should be embedded yet');
372
373         $i++;
374     }
375
376     # One more level
377     $embed = {
378         'items' => {
379             children => { 'holds' => {} }
380         }
381     };
382
383     $i = 0;
384
385     $biblios_api = Koha::Biblios->search(
386         {
387             biblionumber => [ $biblio_1->biblionumber, $biblio_2->biblionumber ]
388         }
389     )->to_api( { embed => $embed } );
390
391     foreach my $biblio_api ( @{ $biblios_api } ) {
392
393         ok(exists $biblio_api->{items}, 'Items where embedded in biblio results');
394         is($biblio_api->{items}->[0]->{item_id}, $items[$i]->itemnumber, 'Item still matches');
395         ok(exists $biblio_api->{items}->[0]->{holds}, 'Holds info should be embedded');
396         is($biblio_api->{items}->[0]->{holds}->[0]->{hold_id}, $holds[$i]->reserve_id, 'Hold matches');
397
398         $i++;
399     }
400
401     $schema->storage->txn_rollback;
402 };
403
404 subtest "TO_JSON() tests" => sub {
405
406     plan tests => 4;
407
408     $schema->storage->txn_begin;
409
410     my $city_1 = $builder->build_object( { class => 'Koha::Cities' } );
411     my $city_2 = $builder->build_object( { class => 'Koha::Cities' } );
412
413     my $cities = Koha::Cities->search(
414         {
415             cityid => [ $city_1->cityid, $city_2->cityid ]
416         },
417         { -orderby => { -desc => 'cityid' } }
418     );
419
420     is( $cities->count, 2, 'Count is correct' );
421     my $cities_json = $cities->TO_JSON;
422     is( ref($cities_json), 'ARRAY', 'to_api returns an array' );
423     is_deeply( $cities_json->[0], $city_1->TO_JSON, 'TO_JSON returns the individual objects with ->TO_JSON' );
424     is_deeply( $cities_json->[1], $city_2->TO_JSON,'TO_JSON returns the individual objects with ->TO_JSON' );
425
426     $schema->storage->txn_rollback;
427 };
428
429 # Koha::Object[s] must behave the same as DBIx::Class
430 subtest 'Return same values as DBIx::Class' => sub {
431     plan tests => 2;
432
433     subtest 'Delete' => sub {
434         plan tests => 2;
435
436         $schema->storage->txn_begin;
437
438         subtest 'Simple Koha::Objects - Koha::Cities' => sub {
439             plan tests => 2;
440
441             subtest 'Koha::Object->delete' => sub {
442
443                 plan tests => 5;
444
445                 my ( $r_us, $e_us, $r_them, $e_them );
446
447                 # CASE 1 - Delete an existing object
448                 my $c = Koha::City->new( { city_name => 'city4test' } )->store;
449                 try { $r_us = $c->delete; } catch { $e_us = $_ };
450                 $c = $schema->resultset('City')->new( { city_name => 'city4test_2' } )->update_or_insert;
451                 try { $r_them = $c->delete; } catch { $e_them = $_ };
452                 ok( ref($r_us) && ref($r_them),
453                     'Successful delete should return the object ' );
454                 ok( !defined $e_us && !defined $e_them,
455                     'Successful delete should not raise an exception' );
456                 is( ref($r_us), 'Koha::City', 'Successful delete should return our Koha::Obect based object' );
457
458                 # CASE 2 - Delete an object that is not in storage
459                 try { $r_us   = $r_us->delete;   } catch { $e_us   = $_ };
460                 try { $r_them = $r_them->delete; } catch { $e_them = $_ };
461                 ok(
462                     defined $e_us && defined $e_them,
463                     'Delete an object that is not in storage should raise an exception'
464                 );
465                 is( ref($e_us), 'DBIx::Class::Exception' )
466                   ; # FIXME This needs adjustement, we want to throw a Koha::Exception
467
468             };
469
470             subtest 'Koha::Objects->delete' => sub {
471
472                 plan tests => 4;
473
474                 my ( $r_us, $e_us, $r_them, $e_them );
475
476                 # CASE 1 - Delete existing objects
477                 my $city_1 = $builder->build_object({ class => 'Koha::Cities' });
478                 my $city_2 = $builder->build_object({ class => 'Koha::Cities' });
479                 my $city_3 = $builder->build_object({ class => 'Koha::Cities' });
480                 my $cities = Koha::Cities->search(
481                     {
482                         cityid => {
483                             -in => [
484                                 $city_1->cityid,
485                                 $city_2->cityid,
486                                 $city_3->cityid,
487                             ]
488                         }
489                     }
490                 );
491
492                 try { $r_us = $cities->delete; } catch { $e_us = $_ };
493
494                 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
495                 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
496                 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
497                 $cities = $schema->resultset('City')->search(
498                     {
499                         cityid => {
500                             -in => [
501                                 $city_1->cityid,
502                                 $city_2->cityid,
503                                 $city_3->cityid,
504                             ]
505                         }
506                     }
507                 );
508
509                 try { $r_them = $cities->delete; } catch { $e_them = $_ };
510
511                 ok( $r_us == 3 && $r_them == 3 );
512                 ok (!defined($e_us) && !defined($e_them));
513
514                 # CASE 2 - One of the object is not in storage
515                 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
516                 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
517                 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
518                 $cities = Koha::Cities->search(
519                     {
520                         cityid => {
521                             -in => [
522                                 $city_1->cityid,
523                                 $city_2->cityid,
524                                 $city_3->cityid,
525                             ]
526                         }
527                     }
528                 );
529
530                 $city_2->delete; # We delete one of the object
531                 try { $r_us = $cities->delete; } catch { $e_us = $_ };
532
533                 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
534                 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
535                 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
536                 $cities = $schema->resultset('City')->search(
537                     {
538                         cityid => {
539                             -in => [
540                                 $city_1->cityid,
541                                 $city_2->cityid,
542                                 $city_3->cityid,
543                             ]
544                         }
545                     }
546                 );
547
548                 $city_2->delete; # We delete one of the object
549                 try { $r_them = $cities->delete; } catch { $e_them = $_ };
550
551                 ok( $r_us == 2 && $r_them == 2 );
552                 ok (!defined($e_us) && !defined($e_them));
553             };
554         };
555
556         subtest 'Overwritten Koha::Objects->delete - Koha::Patrons' => sub {
557
558             plan tests => 2;
559
560             subtest 'Koha::Object->delete' => sub {
561
562                 plan tests => 7;
563
564                 my ( $r_us, $e_us, $r_them, $e_them );
565
566                 # CASE 1 - Delete an existing patron
567                 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
568                 my $patron_data = $patron->unblessed;
569                 $patron->delete;
570
571                 $patron = Koha::Patron->new( $patron_data )->store;
572                 try {$r_us = $patron->delete;} catch { $e_us = $_ };
573                 $patron = $schema->resultset('Borrower')->new( $patron_data )->update_or_insert;
574                 try {$r_them = $patron->delete;} catch { $e_them = $_ };
575                 ok( ref($r_us) && ref($r_them),
576                     'Successful delete should return the patron object' );
577                 ok( !defined $e_us && !defined $e_them,
578                     'Successful delete should not raise an exception' );
579                 is( ref($r_us), 'Koha::Patron',
580                     'Successful delete should return our Koha::Obect based object' );
581
582                 # CASE 2 - Delete a patron that is not in storage
583                 try { $r_us   = $r_us->delete;   } catch { $e_us   = $_ };
584                 try { $r_them = $r_them->delete; } catch { $e_them = $_ };
585                 ok(
586                     defined $e_us && defined $e_them,
587                     'Delete a patron that is not in storage should raise an exception'
588                 );
589                 is( ref($e_us), 'DBIx::Class::Exception' )
590                   ; # FIXME This needs adjustement, we want to throw a Koha::Exception
591
592                 # CASE 3 - Delete a patron that cannot be deleted (as a checkout)
593                 $patron = Koha::Patron->new($patron_data)->store;
594                 $builder->build_object(
595                     {
596                         class => 'Koha::Checkouts',
597                         value => { borrowernumber => $patron->borrowernumber }
598                     }
599                 );
600                 try { $r_us = $r_us->delete; } catch { $e_us = $_ };
601                 $patron = $schema->resultset('Borrower')->find( $patron->borrowernumber );
602                 try { $r_them = $r_them->delete; } catch { $e_them = $_ };
603                 ok(
604                     defined $e_us && defined $e_them,
605                     'Delete a patron that cannot be deleted should raise an exception'
606                 );
607                 is( ref($e_us), 'DBIx::Class::Exception' )
608                   ; # FIXME This needs adjustement, we want to throw a Koha::Exception
609             };
610
611             subtest 'Koha::Objects->delete' => sub {
612
613                 plan tests => 7;
614
615                 my ( $r_us, $e_us, $r_them, $e_them );
616
617                 # CASE 1 - Delete existing objects
618                 my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
619                 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
620                 my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
621                 my $patrons = Koha::Patrons->search(
622                     {
623                         borrowernumber => {
624                             -in => [
625                                 $patron_1->borrowernumber,
626                                 $patron_2->borrowernumber,
627                                 $patron_3->borrowernumber
628                             ]
629                         }
630                     }
631                 );
632
633                 try { $r_us = $patrons->delete; } catch { $e_us = $_ };
634
635                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
636                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
637                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
638                 $patrons = $schema->resultset('Borrower')->search(
639                     {
640                         borrowernumber => {
641                             -in => [
642                                 $patron_1->borrowernumber,
643                                 $patron_2->borrowernumber,
644                                 $patron_3->borrowernumber
645                             ]
646                         }
647                     }
648                 );
649
650                 try { $r_them = $patrons->delete; } catch { $e_them = $_ };
651
652                 ok( $r_us == 3 && $r_them == 3, '->delete should return the number of deleted patrons' );
653                 ok (!defined($e_us) && !defined($e_them), '->delete should not raise exception if everything went well');
654
655                 # CASE 2 - One of the patrons is not in storage
656                 undef $_ for $r_us, $e_us, $r_them, $e_them;
657                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
658                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
659                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
660                 $patrons = Koha::Patrons->search(
661                     {
662                         borrowernumber => {
663                             -in => [
664                                 $patron_1->borrowernumber,
665                                 $patron_2->borrowernumber,
666                                 $patron_3->borrowernumber
667                             ]
668                         }
669                     }
670                 );
671
672                 $patron_2->delete; # We delete one of the patron
673                 try { $r_us = $patrons->delete; } catch { $e_us = $_ };
674
675                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
676                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
677                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
678                 $patrons = $schema->resultset('Borrower')->search(
679                     {
680                         borrowernumber => {
681                             -in => [
682                                 $patron_1->borrowernumber,
683                                 $patron_2->borrowernumber,
684                                 $patron_3->borrowernumber
685                             ]
686                         }
687                     }
688                 );
689
690                 $patron_2->delete; # We delete one of the patron
691                 try { $r_them = $patrons->delete; } catch { $e_them = $_ };
692
693                 ok( $r_us == 2 && $r_them == 2, 'Delete patrons with one that was not in storage should delete the patrons' );
694                 ok (!defined($e_us) && !defined($e_them), 'no exception should be raised if at least one patron was not in storage');
695
696                 # CASE 3 - Delete a set of patrons with one that that cannot be deleted (as a checkout)
697                 undef $_ for $r_us, $e_us, $r_them, $e_them;
698                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
699                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
700                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
701                 $patrons = Koha::Patrons->search(
702                     {
703                         borrowernumber => {
704                             -in => [
705                                 $patron_1->borrowernumber,
706                                 $patron_2->borrowernumber,
707                                 $patron_3->borrowernumber
708                             ]
709                         }
710                     }
711                 );
712
713                 # Adding a checkout to patron_2
714                 $builder->build_object(
715                     {
716                         class => 'Koha::Checkouts',
717                         value => { borrowernumber => $patron_2->borrowernumber }
718                     }
719                 );
720
721                 try { $r_us = $patrons->delete; } catch { $e_us = $_ };
722                 my $not_deleted_us = $patron_1->in_storage + $patron_2->in_storage + $patron_3->in_storage;
723
724                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
725                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
726                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
727                 $patrons = $schema->resultset('Borrower')->search(
728                     {
729                         borrowernumber => {
730                             -in => [
731                                 $patron_1->borrowernumber,
732                                 $patron_2->borrowernumber,
733                                 $patron_3->borrowernumber
734                             ]
735                         }
736                     }
737                 );
738
739                 # Adding a checkout to patron_2
740                 $builder->build_object(
741                     {
742                         class => 'Koha::Checkouts',
743                         value => { borrowernumber => $patron_2->borrowernumber }
744                     }
745                 );
746
747                 try { $r_them = $patrons->delete; } catch { $e_them = $_ };
748
749                 my $not_deleted_them = $patron_1->in_storage + $patron_2->in_storage + $patron_3->in_storage;
750                 ok(
751                     defined $e_us && defined $e_them,
752                     'Delete patrons with one that cannot be deleted should raise an exception'
753                 );
754                 is( ref($e_us), 'DBIx::Class::Exception' )
755                   ; # FIXME This needs adjustement, we want to throw a Koha::Exception
756
757                 ok($not_deleted_us == 3 && $not_deleted_them == 3, 'If one patron cannot be deleted, none should have been deleted');
758             };
759         };
760
761         $schema->storage->txn_rollback;
762
763     };
764
765     subtest 'Update (set/store)' => sub {
766         plan tests => 2;
767
768         $schema->storage->txn_begin;
769
770         subtest 'Simple Koha::Objects - Koha::Cities' => sub {
771             plan tests => 2;
772
773             subtest 'Koha::Object->update' => sub {
774
775                 plan tests => 5;
776
777                 my ( $r_us, $e_us, $r_them, $e_them );
778
779                 # CASE 1 - Update an existing object
780                 my $c_us = Koha::City->new( { city_name => 'city4test' } )->store;
781                 try { $r_us = $c_us->update({ city_country => 'country4test' }); } catch { $e_us = $_ };
782                 my $c_them = $schema->resultset('City')->new( { city_name => 'city4test_2' } )->update_or_insert;
783                 try { $r_them = $c_them->update({ city_country => 'country4test_2' }); } catch { $e_them = $_ };
784                 ok( ref($r_us) && ref($r_them),
785                     'Successful update should return the object ' );
786                 ok( !defined $e_us && !defined $e_them,
787                     'Successful update should not raise an exception' );
788                 is( ref($r_us), 'Koha::City', 'Successful update should return our Koha::Obect based object' );
789
790                 # CASE 2 - Update an object that is not in storage
791                 $c_us->delete;
792                 $c_them->delete;
793                 try { $r_us   = $c_us->update({ city_country => 'another_country' });   } catch { $e_us   = $_ };
794                 try { $r_them = $c_them->update({ city_country => 'another_country' }); } catch { $e_them = $_ };
795                 ok(
796                     defined $e_us && defined $e_them,
797                     'Update an object that is not in storage should raise an exception'
798                 );
799                 is( ref($e_us), 'Koha::Exceptions::Object::NotInStorage' );
800             };
801
802             subtest 'Koha::Objects->update' => sub {
803
804                 plan tests => 6;
805
806                 my ( $r_us, $e_us, $r_them, $e_them );
807
808                 # CASE 1 - update existing objects
809                 my $city_1 = $builder->build_object({ class => 'Koha::Cities' });
810                 my $city_2 = $builder->build_object({ class => 'Koha::Cities' });
811                 my $city_3 = $builder->build_object({ class => 'Koha::Cities' });
812                 my $cities = Koha::Cities->search(
813                     {
814                         cityid => {
815                             -in => [
816                                 $city_1->cityid,
817                                 $city_2->cityid,
818                                 $city_3->cityid,
819                             ]
820                         }
821                     }
822                 );
823
824                 try { $r_us = $cities->update({ city_country => 'country4test' }); } catch { $e_us = $_ };
825
826                 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
827                 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
828                 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
829                 $cities = $schema->resultset('City')->search(
830                     {
831                         cityid => {
832                             -in => [
833                                 $city_1->cityid,
834                                 $city_2->cityid,
835                                 $city_3->cityid,
836                             ]
837                         }
838                     }
839                 );
840
841                 try { $r_them = $cities->update({ city_country => 'country4test' }); } catch { $e_them = $_ };
842
843                 ok( $r_us == 3 && $r_them == 3, '->update should return the number of updated cities' );
844                 ok(!defined($e_us) && !defined($e_them));
845
846                 # CASE 2 - One of the object is not in storage
847                 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
848                 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
849                 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
850                 $cities = Koha::Cities->search(
851                     {
852                         cityid => {
853                             -in => [
854                                 $city_1->cityid,
855                                 $city_2->cityid,
856                                 $city_3->cityid,
857                             ]
858                         }
859                     }
860                 );
861
862                 $city_2->delete; # We delete one of the object
863                 try { $r_us = $cities->update({ city_country => 'country4test' }); } catch { $e_us = $_ };
864
865                 $city_1 = $builder->build_object({ class => 'Koha::Cities' });
866                 $city_2 = $builder->build_object({ class => 'Koha::Cities' });
867                 $city_3 = $builder->build_object({ class => 'Koha::Cities' });
868                 $cities = $schema->resultset('City')->search(
869                     {
870                         cityid => {
871                             -in => [
872                                 $city_1->cityid,
873                                 $city_2->cityid,
874                                 $city_3->cityid,
875                             ]
876                         }
877                     }
878                 );
879
880                 $city_2->delete; # We delete one of the object
881                 try { $r_them = $cities->update({ city_country => 'country4test' }); } catch { $e_them = $_ };
882
883                 ok( $r_us == 2 && $r_them == 2, '->update should return the number of updated cities' );
884                 ok(!defined($e_us) && !defined($e_them));
885
886                 throws_ok
887                     { Koha::Cities->update({ city_country => 'Castalia' }); }
888                     'Koha::Exceptions::Object::NotInstantiated',
889                     'Exception thrown if not instantiated class';
890
891                 is( "$@", 'Tried to access the \'update\' method, but Koha::Cities is not instantiated', 'Exception stringified correctly' );
892
893             };
894         };
895
896         subtest 'Overwritten Koha::Objects->store|update - Koha::Patrons' => sub {
897
898             plan tests => 2;
899
900             subtest 'Koha::Object->update' => sub {
901
902                 plan tests => 5;
903
904                 my ( $r_us, $e_us, $r_them, $e_them );
905
906                 # CASE 1 - Update an existing patron
907                 my $patron_us = $builder->build_object({ class => 'Koha::Patrons' });
908                 try {$r_us = $patron_us->update({city => 'a_city'});} catch { $e_us = $_ };
909
910                 my $patron_data = $builder->build_object({ class => 'Koha::Patrons' })->delete->unblessed;
911                 my $patron_them = $schema->resultset('Borrower')->new( $patron_data )->update_or_insert;
912                 try {$r_them = $patron_them->update({city => 'a_city'});} catch { $e_them = $_ };
913                 ok( ref($r_us) && ref($r_them),
914                     'Successful update should return the patron object' );
915                 ok( !defined $e_us && !defined $e_them,
916                     'Successful update should not raise an exception' );
917                 is( ref($r_us), 'Koha::Patron',
918                     'Successful update should return our Koha::Obect based object' );
919
920                 # CASE 2 - Update a patron that is not in storage
921                 $patron_us->delete;
922                 $patron_them->delete;
923                 try { $r_us   = $patron_us->update({ city => 'another_city' });   } catch { $e_us   = $_ };
924                 try { $r_them = $patron_them->update({ city => 'another_city' }); } catch { $e_them = $_ };
925                 ok(
926                     defined $e_us && defined $e_them,
927                     'Update a patron that is not in storage should raise an exception'
928                 );
929                 is( ref($e_us), 'Koha::Exceptions::Object::NotInStorage' );
930
931             };
932
933             subtest 'Koha::Objects->Update ' => sub {
934
935                 plan tests => 6;
936
937                 my ( $r_us, $e_us, $r_them, $e_them );
938
939                 # CASE 1 - Update existing objects
940                 my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
941                 my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
942                 my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
943                 my $patrons_us = Koha::Patrons->search(
944                     {
945                         borrowernumber => {
946                             -in => [
947                                 $patron_1->borrowernumber,
948                                 $patron_2->borrowernumber,
949                                 $patron_3->borrowernumber
950                             ]
951                         }
952                     }
953                 );
954
955                 try { $r_us = $patrons_us->update({ city => 'a_city' }); } catch { $e_us = $_ };
956
957                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
958                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
959                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
960                 my $patrons_them = $schema->resultset('Borrower')->search(
961                     {
962                         borrowernumber => {
963                             -in => [
964                                 $patron_1->borrowernumber,
965                                 $patron_2->borrowernumber,
966                                 $patron_3->borrowernumber
967                             ]
968                         }
969                     }
970                 );
971
972                 try { $r_them = $patrons_them->update({ city => 'a_city' }); } catch { $e_them = $_ };
973
974                 ok( $r_us == 3 && $r_them == 3, '->update should return the number of update patrons' );
975                 ok (!defined($e_us) && !defined($e_them), '->update should not raise exception if everything went well');
976
977                 # CASE 2 - One of the patrons is not in storage
978                 undef $_ for $r_us, $e_us, $r_them, $e_them;
979                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
980                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
981                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
982                 $patrons_us = Koha::Patrons->search(
983                     {
984                         borrowernumber => {
985                             -in => [
986                                 $patron_1->borrowernumber,
987                                 $patron_2->borrowernumber,
988                                 $patron_3->borrowernumber
989                             ]
990                         }
991                     }
992                 );
993
994                 $patron_2->delete; # We delete one of the patron
995                 try { $r_us = $patrons_us->update({ city => 'another_city' }); } catch { $e_us = $_ };
996
997                 $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
998                 $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
999                 $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
1000                 $patrons_them = $schema->resultset('Borrower')->search(
1001                     {
1002                         borrowernumber => {
1003                             -in => [
1004                                 $patron_1->borrowernumber,
1005                                 $patron_2->borrowernumber,
1006                                 $patron_3->borrowernumber
1007                             ]
1008                         }
1009                     }
1010                 );
1011
1012                 $patron_2->delete; # We delete one of the patron
1013                 try { $r_them = $patrons_them->update({ city => 'another_city' }); } catch { $e_them = $_ };
1014
1015                 ok( $r_us == 2 && $r_them == 2, 'Update patrons with one that was not in storage should update the patrons' );
1016                 ok (!defined($e_us) && !defined($e_them), 'no exception should be raised if at least one patron was not in storage');
1017
1018
1019                 # Testing no_triggers
1020                 t::lib::Mocks::mock_preference('uppercasesurnames', 1);
1021                 $patrons_us = Koha::Patrons->search(
1022                     {
1023                         borrowernumber => {
1024                             -in => [
1025                                 $patron_1->borrowernumber,
1026                                 $patron_2->borrowernumber,
1027                                 $patron_3->borrowernumber
1028                             ]
1029                         }
1030                     }
1031                 );
1032                 $patrons_us->update({ surname => 'foo' }); # Koha::Patron->store is supposed to uppercase the surnames
1033                 is( $patrons_us->search({ surname => 'FOO' })->count, 2, 'Koha::Patron->store is hit' );
1034
1035                 $patrons_us->update({ surname => 'foo' }, { no_triggers => 1 }); # The surnames won't be uppercase as we won't hit Koha::Patron->store
1036                 is( $patrons_us->search({ surname => 'foo' })->count, 2, 'Koha::Patron->store is not hit');
1037
1038             };
1039
1040         };
1041
1042         $schema->storage->txn_rollback;
1043
1044     };
1045
1046 };
1047
1048 subtest "attributes_from_api() tests" => sub {
1049
1050     plan tests => 1;
1051
1052     $schema->storage->txn_begin;
1053
1054     my $cities_rs = Koha::Cities->new;
1055     my $city      = Koha::City->new;
1056
1057     my $api_attributes = {
1058         name        => 'Cordoba',
1059         postal_code => 5000
1060     };
1061
1062     is_deeply(
1063         $cities_rs->attributes_from_api($api_attributes),
1064         $city->attributes_from_api($api_attributes)
1065     );
1066
1067     $schema->storage->txn_rollback;
1068
1069 };
1070
1071 subtest "filter_by_last_update" => sub {
1072
1073     $schema->storage->txn_begin;
1074
1075     my $now = dt_from_string->truncate( to => 'day' );
1076     my @borrowernumbers;
1077     # Building 6 patrons that have been created today, yesterday, ... 1 per day
1078     for my $i ( 0 .. 5 ) {
1079         push @borrowernumbers,
1080           $builder->build_object(
1081             {
1082                 class => 'Koha::Patrons',
1083                 value => { updated_on => $now->clone->subtract( days => $i ) }
1084             }
1085           )->borrowernumber;
1086     }
1087
1088     my $patrons = Koha::Patrons->search(
1089         { borrowernumber => { -in => \@borrowernumbers } } );
1090
1091     try {
1092         $patrons->filter_by_last_update( { timestamp_column_name => 'updated_on' } )
1093           ->count;
1094     }
1095     catch {
1096         ok(
1097             $_->isa('Koha::Exceptions::MissingParameter'),
1098             'Should raise an exception if no parameter given'
1099         );
1100     };
1101
1102     my $filtered_patrons = $patrons->filter_by_last_update(
1103         { timestamp_column_name => 'updated_on', days => 2 } );
1104     is( ref($filtered_patrons), 'Koha::Patrons', 'filter_by_last_update must return a Koha::Objects-based object' );
1105
1106     my $count = $patrons->filter_by_last_update(
1107         { timestamp_column_name => 'updated_on', days => 2 } )->count;
1108     is( $count, 3, '3 patrons have been updated before the last 2 days (exclusive)' );
1109
1110     $count = $patrons->filter_by_last_update(
1111         { timestamp_column_name => 'updated_on', days => 2, days_inclusive => 1 } )->count;
1112     is( $count, 4, '4 patrons have been updated before the last 2 days (inclusive)' );
1113
1114     $count = $patrons->filter_by_last_update(
1115         { timestamp_column_name => 'updated_on', days => 1 } )->count;
1116     is( $count, 4, '4 patrons have been updated before yesterday (exclusive)' );
1117
1118     $count = $patrons->filter_by_last_update(
1119         { timestamp_column_name => 'updated_on', days => 1, days_inclusive => 1 } )->count;
1120     is( $count, 5, '5 patrons have been updated before yesterday (inclusive)' );
1121
1122     $count = $patrons->filter_by_last_update(
1123         { timestamp_column_name => 'updated_on', days => 0 } )->count;
1124     is( $count, 5, '5 patrons have been updated before today (exclusive)' );
1125
1126     $count = $patrons->filter_by_last_update(
1127         { timestamp_column_name => 'updated_on', days => 0, days_inclusive => 1 } )->count;
1128     is( $count, 6, '6 patrons have been updated before today (inclusive)' );
1129
1130     $count = $patrons->filter_by_last_update(
1131         { timestamp_column_name => 'updated_on', from => $now } )->count;
1132     is( $count, 1, '1 patron has been updated "from today" (inclusive)' );
1133
1134     $count = $patrons->filter_by_last_update(
1135         { timestamp_column_name => 'updated_on', to => $now } )->count;
1136     is( $count, 6, '6 patrons have been updated "to today" (inclusive)' );
1137
1138     $count = $patrons->filter_by_last_update(
1139         {
1140             timestamp_column_name => 'updated_on',
1141             from                  => $now->clone->subtract( days => 4 ),
1142             to                    => $now->clone->subtract( days => 2 )
1143         }
1144     )->count;
1145     is( $count, 3, '3 patrons have been updated between D-4 and D-2' );
1146
1147     t::lib::Mocks::mock_preference( 'dateformat', 'metric' );
1148     try {
1149         $count = $patrons->filter_by_last_update(
1150             { timestamp_column_name => 'updated_on', from => '1970-12-31' } )
1151           ->count;
1152     }
1153     catch {
1154         ok(
1155             $_->isa(
1156                 'No exception raised, from and to parameters can take an iso formatted date'
1157             )
1158         );
1159     };
1160     try {
1161         $count = $patrons->filter_by_last_update(
1162             { timestamp_column_name => 'updated_on', from => '31/12/1970' } )
1163           ->count;
1164     }
1165     catch {
1166         ok(
1167             $_->isa(
1168                 'No exception raised, from and to parameters can take an metric formatted date (depending on dateformat syspref)'
1169             )
1170         );
1171     };
1172
1173     $schema->storage->txn_rollback;
1174 };
1175
1176 subtest "from_api_mapping() tests" => sub {
1177
1178     plan tests => 1;
1179
1180     $schema->storage->txn_begin;
1181
1182     my $cities_rs = Koha::Cities->new;
1183     my $city      = Koha::City->new;
1184
1185     is_deeply(
1186         $cities_rs->from_api_mapping,
1187         $city->from_api_mapping
1188     );
1189
1190     $schema->storage->txn_rollback;
1191 };
1192
1193 subtest 'prefetch_whitelist() tests' => sub {
1194
1195     plan tests => 3;
1196
1197     $schema->storage->txn_begin;
1198
1199     my $biblios = Koha::Biblios->new;
1200
1201     my $prefetch_whitelist = $biblios->prefetch_whitelist;
1202
1203     ok(
1204         exists $prefetch_whitelist->{orders},
1205         'Relationship matching method name is listed'
1206     );
1207     is(
1208         $prefetch_whitelist->{orders},
1209         'Koha::Acquisition::Order',
1210         'Guessed the non-standard object class correctly'
1211     );
1212
1213     is(
1214         $prefetch_whitelist->{items},
1215         'Koha::Item',
1216         'Guessed the standard object class correctly'
1217     );
1218
1219     $schema->storage->txn_rollback;
1220 };
1221
1222 subtest 'empty() tests' => sub {
1223
1224     plan tests => 6;
1225
1226     $schema->storage->txn_begin;
1227
1228     # Add a patron, we need at least 1
1229     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
1230     ok( Koha::Patrons->count > 0, 'There is at least one Koha::Patron on the resultset' );
1231
1232     my $empty = Koha::Patrons->new->empty;
1233     is( ref($empty), 'Koha::Patrons', '->empty returns a Koha::Patrons iterator' );
1234     is( $empty->count, 0, 'The empty resultset is, well, empty :-D' );
1235
1236     my $new_rs = $empty->search({ borrowernumber => $patron->borrowernumber });
1237
1238     is( $new_rs->count, 0, 'Further chaining an empty resultset, returns an empty resultset' );
1239
1240     throws_ok
1241         { Koha::Patrons->empty; }
1242         'Koha::Exceptions::Object::NotInstantiated',
1243         'Exception thrown if not instantiated class';
1244
1245     is( "$@", 'Tried to access the \'empty\' method, but Koha::Patrons is not instantiated', 'Exception stringified correctly' );
1246
1247     $schema->storage->txn_rollback;
1248 };
1249
1250 subtest 'delete() tests' => sub {
1251
1252     plan tests => 2;
1253
1254     $schema->storage->txn_begin;
1255
1256     # Make sure no cities
1257     warnings_are { Koha::Cities->delete }[],
1258       "No warnings, no Koha::City->delete called as it doesn't exist";
1259
1260     # Mock Koha::City
1261     my $mocked_city = Test::MockModule->new('Koha::City');
1262     $mocked_city->mock(
1263         'delete',
1264         sub {
1265             shift->_result->delete;
1266             warn "delete called!";
1267         }
1268     );
1269
1270     # Add two cities
1271     $builder->build_object( { class => 'Koha::Cities' } );
1272     $builder->build_object( { class => 'Koha::Cities' } );
1273
1274     my $cities = Koha::Cities->search;
1275     $cities->next;
1276     warnings_are { $cities->delete }
1277         [ "delete called!", "delete called!" ],
1278         "No warnings, no Koha::City->delete called as it doesn't exist";
1279
1280     $schema->storage->txn_rollback;
1281 };