Bug 13690: use Koha::Schema only when it's needed
[koha.git] / t / ItemType.t
blob558bba9bf5b6827284b20939bade8080a8e94f04
1 #!/usr/bin/perl
3 use strict;
4 use warnings;
5 use DBI;
6 use Test::More tests => 26;
7 use Test::MockModule;
9 BEGIN {
10 use_ok('C4::ItemType');
13 my $module = new Test::MockModule('C4::Context');
14 $module->mock(
15 '_new_dbh',
16 sub {
17 my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
18 || die "Cannot create handle: $DBI::errstr\n";
19 return $dbh;
23 # Mock data
24 my $itemtypes = [
26 'itemtype', 'description', 'rentalcharge', 'notforloan',
27 'imageurl', 'summary', 'checkinmsg'
29 [ 'BK', 'Books', 0, 0, '', '', 'foo' ],
30 [ 'CD', 'CDRom', 0, 0, '', '', 'bar' ]
33 my $itemtypes_empty = [
35 'itemtype', 'description', 'rentalcharge', 'notforloan',
36 'imageurl', 'summary', 'checkinmsg'
40 my $dbh = C4::Context->dbh();
41 $dbh->{mock_add_resultset} = $itemtypes_empty;
43 my @itemtypes = C4::ItemType->all();
44 is( @itemtypes, 0, 'Testing all itemtypes is empty' );
46 # This should run exactly one query so we can test
47 my $history = $dbh->{mock_all_history};
48 is( scalar( @{$history} ), 1, 'Correct number of statements executed' );
50 # Now lets mock some data
51 $dbh->{mock_add_resultset} = $itemtypes;
53 @itemtypes = C4::ItemType->all();
55 $history = $dbh->{mock_all_history};
56 is( scalar( @{$history} ), 2, 'Correct number of statements executed' );
58 is( @itemtypes, 2, 'ItemType->all should return an array with 2 elements' );
60 is( $itemtypes[0]->fish, undef, 'Calling a bad descriptor gives undef' );
62 is( $itemtypes[0]->itemtype, 'BK', 'First itemtype is bk' );
64 is( $itemtypes[1]->itemtype, 'CD', 'second itemtype is cd' );
66 is( $itemtypes[0]->description, 'Books', 'First description is books' );
68 is( $itemtypes[1]->description, 'CDRom', 'second description is CDRom' );
70 is( $itemtypes[0]->rentalcharge, '0', 'first rental charge is 0' );
72 is( $itemtypes[1]->rentalcharge, '0', 'second rental charge is 0' );
74 is( $itemtypes[0]->notforloan, '0', 'first not for loan is 0' );
76 is( $itemtypes[1]->notforloan, '0', 'second not for loan is 0' );
78 is( $itemtypes[0]->imageurl, '', 'first imageurl is undef' );
80 is( $itemtypes[1]->imageurl, '', 'second imageurl is undef' );
82 is( $itemtypes[0]->checkinmsg, 'foo', 'first checkinmsg is foo' );
84 is( $itemtypes[1]->checkinmsg, 'bar', 'second checkinmsg is bar' );
86 # Mock the data again
87 $dbh->{mock_add_resultset} = $itemtypes;
89 # Test get(), which should return one itemtype
90 my $itemtype = C4::ItemType->get( 'BK' );
92 $history = $dbh->{mock_all_history};
93 is( scalar( @{$history} ), 3, 'Correct number of statements executed' );
95 is( $itemtype->fish, undef, 'Calling a bad descriptor gives undef' );
97 is( $itemtype->itemtype, 'BK', 'itemtype is bk' );
99 is( $itemtype->description, 'Books', 'description is books' );
101 is( $itemtype->rentalcharge, '0', 'rental charge is 0' );
103 is( $itemtype->notforloan, '0', 'not for loan is 0' );
105 is( $itemtype->imageurl, '', ' not for loan is undef' );
107 is( $itemtype->checkinmsg, 'foo', 'checkinmsg is foo' );