Bug 13799: (QA followup) make tests use random data
[koha.git] / t / ItemType.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use Test::More tests => 25;
5 use t::lib::Mocks;
6
7 use_ok('C4::ItemType');
8
9 use Test::DBIx::Class {
10     schema_class => 'Koha::Schema',
11     connect_info => ['dbi:SQLite:dbname=:memory:','',''],
12     connect_opts => { name_sep => '.', quote_char => '`', },
13     fixture_class => '::Populate',
14 }, 'Itemtype' ;
15
16 sub fixtures {
17     my ( $data ) = @_;
18     fixtures_ok [
19         Itemtype => [
20             [
21                 'itemtype', 'description', 'rentalcharge', 'notforloan',
22                 'imageurl', 'summary', 'checkinmsg'
23             ],
24             @$data,
25         ],
26     ], 'add fixtures';
27 }
28
29 my $db = Test::MockModule->new('Koha::Database');
30 $db->mock( _new_schema => sub { return Schema(); } );
31
32 # Mock data
33 my $itemtypes = [
34     [ 'BK', 'Books', 0, 0, '', '', 'foo' ],
35     [ 'CD', 'CDRom', 0, 0, '', '', 'bar' ]
36 ];
37
38 my @itemtypes = C4::ItemType->all();
39 is( @itemtypes, 0, 'Testing all itemtypes is empty' );
40
41 # Now lets mock some data
42 fixtures($itemtypes);
43
44 @itemtypes = C4::ItemType->all();
45
46 is( @itemtypes, 2, 'ItemType->all should return an array with 2 elements' );
47
48 is( $itemtypes[0]->fish, undef, 'Calling a bad descriptor gives undef' );
49
50 is( $itemtypes[0]->itemtype, 'BK', 'First itemtype is bk' );
51
52 is( $itemtypes[1]->itemtype, 'CD', 'second itemtype is cd' );
53
54 is( $itemtypes[0]->description, 'Books', 'First description is books' );
55
56 is( $itemtypes[1]->description, 'CDRom', 'second description is CDRom' );
57
58 is( $itemtypes[0]->rentalcharge, '0', 'first rental charge is 0' );
59
60 is( $itemtypes[1]->rentalcharge, '0', 'second rental charge is 0' );
61
62 is( $itemtypes[0]->notforloan, '0', 'first not for loan is 0' );
63
64 is( $itemtypes[1]->notforloan, '0', 'second not for loan is 0' );
65
66 is( $itemtypes[0]->imageurl, '', 'first imageurl is undef' );
67
68 is( $itemtypes[1]->imageurl, '', 'second imageurl is undef' );
69
70 is( $itemtypes[0]->checkinmsg, 'foo', 'first checkinmsg is foo' );
71
72 is( $itemtypes[1]->checkinmsg, 'bar', 'second checkinmsg is bar' );
73
74 # Test get(), which should return one itemtype
75 my $itemtype = C4::ItemType->get( 'BK' );
76
77 is( $itemtype->fish, undef, 'Calling a bad descriptor gives undef' );
78
79 is( $itemtype->itemtype, 'BK', 'itemtype is bk' );
80
81 is( $itemtype->description, 'Books', 'description is books' );
82
83 is( $itemtype->rentalcharge, '0', 'rental charge is 0' );
84
85 is( $itemtype->notforloan, '0', 'not for loan is 0' );
86
87 is( $itemtype->imageurl, '', ' not for loan is undef' );
88
89 is( $itemtype->checkinmsg, 'foo', 'checkinmsg is foo' );
90
91 $itemtype = C4::ItemType->get;
92 is( $itemtype, undef, 'C4::ItemType->get should return unless if no parameter is given' );