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