Merge remote-tracking branch 'origin/new/bug_8408'
[koha.git] / t / ItemType.t
1 #!/usr/bin/perl
2 #
3 # This Koha test module is a stub!
4 # Add more tests here!!!
5
6 use strict;
7 use warnings;
8 use DBI;
9 use Test::More tests => 15;
10 use Test::MockModule;
11
12 BEGIN {
13     use_ok('C4::ItemType');
14 }
15
16 my $module = new Test::MockModule('C4::Context');
17 $module->mock(
18     '_new_dbh',
19     sub {
20         my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
21           || die "Cannot create handle: $DBI::errstr\n";
22         return $dbh;
23     }
24 );
25
26 # Mock data
27 my $itemtypes = [
28     [
29         'itemtype', 'description', 'rentalcharge', 'notforloan',
30         'imageurl', 'summary'
31     ],
32     [ 'BK', 'Books', 0, 0, '', '' ],
33     [ 'CD', 'CDRom', 0, 0, '', '' ]
34 ];
35
36 my $dbh = C4::Context->dbh();
37
38 my @itemtypes = C4::ItemType->all();
39 is( @itemtypes, 0, 'Testing all itemtypes is empty' );
40
41 # This should run exactly one query so we can test
42 my $history = $dbh->{mock_all_history};
43 is( scalar( @{$history} ), 1, 'Correct number of statements executed' );
44
45 # Now lets mock some data
46 $dbh->{mock_add_resultset} = $itemtypes;
47
48 @itemtypes = C4::ItemType->all();
49 is( @itemtypes, 2, 'ItemType->all should return an array with 2 elements' );
50
51 is( $itemtypes[0]->fish, undef, 'Calling a bad descriptor gives undef' );
52
53 is( $itemtypes[0]->itemtype, 'BK', 'First itemtype is bk' );
54
55 is( $itemtypes[1]->itemtype, 'CD', 'second itemtype is cd' );
56
57 is( $itemtypes[0]->description, 'Books', 'First description is books' );
58
59 is( $itemtypes[1]->description, 'CDRom', 'second description is CDRom' );
60
61 is( $itemtypes[0]->rentalcharge, '0', 'first rental charge is 0' );
62
63 is( $itemtypes[1]->rentalcharge, '0', 'second rental charge is 0' );
64
65 is( $itemtypes[0]->notforloan, '0', 'first not for loan is 0' );
66
67 is( $itemtypes[1]->notforloan, '0', 'second not for loan is 0' );
68
69 is( $itemtypes[0]->imageurl, '', 'first not for loan is undef' );
70
71 is( $itemtypes[1]->imageurl, '', 'second not for loan is undef' );