Bug 17080 [QA Followup] - Fix number of 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 => 9;
23 use Test::Warn;
24
25 use Koha::Authority::Types;
26 use Koha::Cities;
27 use Koha::Patron::Category;
28 use Koha::Patron::Categories;
29 use Koha::Patrons;
30 use Koha::Database;
31
32 use t::lib::TestBuilder;
33
34 my $schema = Koha::Database->new->schema;
35 $schema->storage->txn_begin;
36
37 is( ref(Koha::Authority::Types->find('')), 'Koha::Authority::Type', 'Koha::Objects->find should work if the primary key is an empty string' );
38
39 my @columns = Koha::Patrons->columns;
40 my $borrowernumber_exists = grep { /^borrowernumber$/ } @columns;
41 is( $borrowernumber_exists, 1, 'Koha::Objects->columns should return the table columns' );
42
43 subtest 'update' => sub {
44     plan tests => 2;
45     my $builder = t::lib::TestBuilder->new;
46     $builder->build( { source => 'City', value => { city_country => 'UK' } } );
47     $builder->build( { source => 'City', value => { city_country => 'UK' } } );
48     $builder->build( { source => 'City', value => { city_country => 'UK' } } );
49     $builder->build( { source => 'City', value => { city_country => 'France' } } );
50     $builder->build( { source => 'City', value => { city_country => 'France' } } );
51     $builder->build( { source => 'City', value => { city_country => 'Germany' } } );
52     Koha::Cities->search( { city_country => 'UK' } )->update( { city_country => 'EU' } );
53     is( Koha::Cities->search( { city_country => 'EU' } )->count, 3, 'Koha::Objects->update should have updated the 3 rows' );
54     is( Koha::Cities->search( { city_country => 'UK' } )->count, 0, 'Koha::Objects->update should have updated the 3 rows' );
55 };
56
57 subtest 'pager' => sub {
58     plan tests => 1;
59     my $pager = Koha::Patrons->search( {}, { page => 1, rows => 2 } )->pager;
60     is( ref($pager), 'DBIx::Class::ResultSet::Pager', 'Koha::Objects->pager returns a valid DBIx::Class object' );
61 };
62
63 subtest 'reset' => sub {
64     plan tests => 1;
65     my $builder   = t::lib::TestBuilder->new;
66     my $patrons = Koha::Patrons->search;
67     my $first_borrowernumber = $patrons->next->borrowernumber;
68     my $second_borrowernumber = $patrons->next->borrowernumber;
69     is( $patrons->reset->next->borrowernumber, $first_borrowernumber, 'Koha::Objects->reset should work as expected');
70 };
71
72 subtest 'delete' => sub {
73     plan tests => 2;
74     my $builder   = t::lib::TestBuilder->new;
75     my $patron_1 = $builder->build({source => 'Borrower'});
76     my $patron_2 = $builder->build({source => 'Borrower'});
77     is( Koha::Patrons->search({ -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}]}})->delete, 2, '');
78     is( Koha::Patrons->search({ -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}]}})->count, 0, '');
79 };
80
81 subtest 'not_covered_yet' => sub {
82     plan tests => 1;
83     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 };
85 subtest 'new' => sub {
86     plan tests => 2;
87     my $a_cat_code = 'A_CAT_CODE';
88     my $patron_category = Koha::Patron::Category->new( { categorycode => $a_cat_code } )->store;
89     is( Koha::Patron::Categories->find($a_cat_code)->category_type, 'A', 'Koha::Object->new should set the default value' );
90     Koha::Patron::Categories->find($a_cat_code)->delete;
91     $patron_category = Koha::Patron::Category->new( { categorycode => $a_cat_code, category_type => undef } )->store;
92     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' );
93     Koha::Patron::Categories->find($a_cat_code)->delete;
94 };
95
96 subtest 'search_related' => sub {
97     plan tests => 8;
98     my $builder   = t::lib::TestBuilder->new;
99     my $patron_1  = $builder->build( { source => 'Borrower' } );
100     my $patron_2  = $builder->build( { source => 'Borrower' } );
101     my $libraries = Koha::Patrons->search( { -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } } )->search_related('branchcode');
102     is( ref( $libraries ), 'Koha::Libraries', 'Koha::Objects->search_related should return an instanciated Koha::Objects-based object' );
103     is( $libraries->count,            2,                       'Koha::Objects->search_related should work as expected' );
104     is( $libraries->next->branchcode, $patron_1->{branchcode}, 'Koha::Objects->search_related should work as expected' );
105     is( $libraries->next->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
106
107     my @libraries = Koha::Patrons->search( { -or => { borrowernumber => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } } )->search_related('branchcode');
108     is( ref( $libraries[0] ),      'Koha::Library',         'Koha::Objects->search_related should return a list of Koha::Object-based objects' );
109     is( scalar(@libraries),        2,                       'Koha::Objects->search_related should work as expected' );
110     is( $libraries[0]->branchcode, $patron_1->{branchcode}, 'Koha::Objects->search_related should work as expected' );
111     is( $libraries[1]->branchcode, $patron_2->{branchcode}, 'Koha::Objects->search_related should work as expected' );
112 };
113
114 $schema->storage->txn_rollback;
115 1;