Lines 1-59
Link Here
|
1 |
#!/usr/bin/perl |
1 |
#!/usr/bin/perl |
2 |
|
2 |
|
3 |
use Modern::Perl; |
3 |
use Modern::Perl; |
4 |
use DBI; |
4 |
use Test::More tests => 25; |
5 |
use Test::More tests => 27; |
5 |
use t::lib::Mocks; |
6 |
use Test::MockModule; |
6 |
|
7 |
|
7 |
use_ok('C4::ItemType'); |
8 |
BEGIN { |
8 |
|
9 |
use_ok('C4::ItemType'); |
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'; |
10 |
} |
27 |
} |
11 |
|
28 |
|
12 |
my $module = new Test::MockModule('C4::Context'); |
29 |
my $db = Test::MockModule->new('Koha::Database'); |
13 |
$module->mock( |
30 |
$db->mock( _new_schema => sub { return Schema(); } ); |
14 |
'_new_dbh', |
|
|
15 |
sub { |
16 |
my $dbh = DBI->connect( 'DBI:Mock:', '', '' ) |
17 |
|| die "Cannot create handle: $DBI::errstr\n"; |
18 |
return $dbh; |
19 |
} |
20 |
); |
21 |
|
31 |
|
22 |
# Mock data |
32 |
# Mock data |
23 |
my $itemtypes = [ |
33 |
my $itemtypes = [ |
24 |
[ |
|
|
25 |
'itemtype', 'description', 'rentalcharge', 'notforloan', |
26 |
'imageurl', 'summary', 'checkinmsg' |
27 |
], |
28 |
[ 'BK', 'Books', 0, 0, '', '', 'foo' ], |
34 |
[ 'BK', 'Books', 0, 0, '', '', 'foo' ], |
29 |
[ 'CD', 'CDRom', 0, 0, '', '', 'bar' ] |
35 |
[ 'CD', 'CDRom', 0, 0, '', '', 'bar' ] |
30 |
]; |
36 |
]; |
31 |
|
37 |
|
32 |
my $itemtypes_empty = [ |
|
|
33 |
[ |
34 |
'itemtype', 'description', 'rentalcharge', 'notforloan', |
35 |
'imageurl', 'summary', 'checkinmsg' |
36 |
], |
37 |
]; |
38 |
|
39 |
my $dbh = C4::Context->dbh(); |
40 |
$dbh->{mock_add_resultset} = $itemtypes_empty; |
41 |
|
42 |
my @itemtypes = C4::ItemType->all(); |
38 |
my @itemtypes = C4::ItemType->all(); |
43 |
is( @itemtypes, 0, 'Testing all itemtypes is empty' ); |
39 |
is( @itemtypes, 0, 'Testing all itemtypes is empty' ); |
44 |
|
40 |
|
45 |
# This should run exactly one query so we can test |
|
|
46 |
my $history = $dbh->{mock_all_history}; |
47 |
is( scalar( @{$history} ), 1, 'Correct number of statements executed' ); |
48 |
|
49 |
# Now lets mock some data |
41 |
# Now lets mock some data |
50 |
$dbh->{mock_add_resultset} = $itemtypes; |
42 |
fixtures($itemtypes); |
51 |
|
43 |
|
52 |
@itemtypes = C4::ItemType->all(); |
44 |
@itemtypes = C4::ItemType->all(); |
53 |
|
45 |
|
54 |
$history = $dbh->{mock_all_history}; |
|
|
55 |
is( scalar( @{$history} ), 2, 'Correct number of statements executed' ); |
56 |
|
57 |
is( @itemtypes, 2, 'ItemType->all should return an array with 2 elements' ); |
46 |
is( @itemtypes, 2, 'ItemType->all should return an array with 2 elements' ); |
58 |
|
47 |
|
59 |
is( $itemtypes[0]->fish, undef, 'Calling a bad descriptor gives undef' ); |
48 |
is( $itemtypes[0]->fish, undef, 'Calling a bad descriptor gives undef' ); |
Lines 82-96
is( $itemtypes[0]->checkinmsg, 'foo', 'first checkinmsg is foo' );
Link Here
|
82 |
|
71 |
|
83 |
is( $itemtypes[1]->checkinmsg, 'bar', 'second checkinmsg is bar' ); |
72 |
is( $itemtypes[1]->checkinmsg, 'bar', 'second checkinmsg is bar' ); |
84 |
|
73 |
|
85 |
# Mock the data again |
|
|
86 |
$dbh->{mock_add_resultset} = $itemtypes; |
87 |
|
88 |
# Test get(), which should return one itemtype |
74 |
# Test get(), which should return one itemtype |
89 |
my $itemtype = C4::ItemType->get( 'BK' ); |
75 |
my $itemtype = C4::ItemType->get( 'BK' ); |
90 |
|
76 |
|
91 |
$history = $dbh->{mock_all_history}; |
|
|
92 |
is( scalar( @{$history} ), 3, 'Correct number of statements executed' ); |
93 |
|
94 |
is( $itemtype->fish, undef, 'Calling a bad descriptor gives undef' ); |
77 |
is( $itemtype->fish, undef, 'Calling a bad descriptor gives undef' ); |
95 |
|
78 |
|
96 |
is( $itemtype->itemtype, 'BK', 'itemtype is bk' ); |
79 |
is( $itemtype->itemtype, 'BK', 'itemtype is bk' ); |
97 |
- |
|
|