3 # Copyright 2015 Koha Development team
5 # This file is part of Koha
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.
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.
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>.
22 use Test::More tests => 8;
25 use Koha::Authority::Types;
30 use t::lib::TestBuilder;
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
35 is( ref(Koha::Authority::Types->find('')), 'Koha::Authority::Type', 'Koha::Objects->find should work if the primary key is an empty string' );
37 my @columns = Koha::Patrons->columns;
38 my $borrowernumber_exists = grep { /^borrowernumber$/ } @columns;
39 is( $borrowernumber_exists, 1, 'Koha::Objects->columns should return the table columns' );
41 subtest 'update' => sub {
43 my $builder = t::lib::TestBuilder->new;
44 $builder->build( { source => 'City', value => { city_country => 'UK' } } );
45 $builder->build( { source => 'City', value => { city_country => 'UK' } } );
46 $builder->build( { source => 'City', value => { city_country => 'UK' } } );
47 $builder->build( { source => 'City', value => { city_country => 'France' } } );
48 $builder->build( { source => 'City', value => { city_country => 'France' } } );
49 $builder->build( { source => 'City', value => { city_country => 'Germany' } } );
50 Koha::Cities->search( { city_country => 'UK' } )->update( { city_country => 'EU' } );
51 is( Koha::Cities->search( { city_country => 'EU' } )->count, 3, 'Koha::Objects->update should have updated the 3 rows' );
52 is( Koha::Cities->search( { city_country => 'UK' } )->count, 0, 'Koha::Objects->update should have updated the 3 rows' );
55 subtest 'pager' => sub {
57 my $pager = Koha::Patrons->search( {}, { page => 1, rows => 2 } )->pager;
58 is( ref($pager), 'DBIx::Class::ResultSet::Pager', 'Koha::Objects->pager returns a valid DBIx::Class object' );
61 subtest 'reset' => sub {
63 my $builder = t::lib::TestBuilder->new;
64 my $patrons = Koha::Patrons->search;
65 my $first_borrowernumber = $patrons->next->borrowernumber;
66 my $second_borrowernumber = $patrons->next->borrowernumber;
67 is( $patrons->reset->next->borrowernumber, $first_borrowernumber, 'Koha::Objects->reset should work as expected');
70 subtest 'delete' => sub {
72 my $builder = t::lib::TestBuilder->new;
73 my $patron_1 = $builder->build({source => 'Borrower'});
74 my $patron_2 = $builder->build({source => 'Borrower'});
75 is( Koha::Patrons->search({ -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}]}})->delete, 2, '');
76 is( Koha::Patrons->search({ -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}]}})->count, 0, '');
79 subtest 'not_covered_yet' => sub {
81 warning_is { Koha::Patrons->search->not_covered_yet } { carped => 'The method not_covered_yet is not covered by tests' }, "If a method is not covered by tests, the AUTOLOAD method won't execute the method";
84 subtest 'search_related' => sub {
86 my $builder = t::lib::TestBuilder->new;
87 my $patron_1 = $builder->build( { source => 'Borrower' } );
88 my $patron_2 = $builder->build( { source => 'Borrower' } );
89 my $libraries = Koha::Patrons->search( { -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } } )->search_related('branchcode');
90 is( ref( $libraries ), 'Koha::Libraries', 'Koha::Objects->search_related should return an instanciated Koha::Objects-based object' );
91 is( $libraries->count, 2, 'Koha::Objects->search_related should work as expected' );
92 is( $libraries->next->branchcode, $patron_1->{branchcode}, 'Koha::Objects->search_related should work as expected' );
93 is( $libraries->next->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
95 my @libraries = Koha::Patrons->search( { -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } } )->search_related('branchcode');
96 is( ref( $libraries[0] ), 'Koha::Library', 'Koha::Objects->search_related should return a list of Koha::Object-based objects' );
97 is( scalar(@libraries), 2, 'Koha::Objects->search_related should work as expected' );
98 is( $libraries[0]->branchcode, $patron_1->{branchcode}, 'Koha::Objects->search_related should work as expected' );
99 is( $libraries[1]->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
102 $schema->storage->txn_rollback;