Debian Codegraph @

t/100-set-fake-retval.t

raw symbols SCIP
1 use strictscip-perl cpan Test-MockDBI 0.70 strict/;
2 use warningsscip-perl cpan Test-MockDBI 0.70 warnings/;
3 use Test::Morescip-perl cpan Test-MockDBI 0.70 Test/More/;
4 use Test::Warnscip-perl cpan Test-MockDBI 0.70 Test/Warn/;
5
6 use_okscip-perl cpan Test-MockDBI 0.70 main/use_ok().('Test::MockDBI');
7
8 my $instance = Test::MockDBI::get_instancescip-perl cpan Test-MockDBI 0.70 Test/MockDBI/get_instance().();
9
10 my %methods = (
11 'DBI::db' => ['prepare', 'prepare_cached', 'do', 'commit', 'rollback', 'begin_work', 'ping', 'disconnect'],
12 'DBI::st' => ['bind_param', 'bind_param_inout', 'execute', 'fetchrow_arrayref', 'fetchrow_array', 'fetchrow_hashref',
13 'fetchall_arrayref', 'finish', 'rows']
14 );
15
16 my $dbh = DBI->connectscip-perl cpan Test-MockDBI 0.70 DBI/connect().('DBI:mydb:somedb', 'user1', 'password1', { AutoCommit => undef } ); #AutoCommit to silence warnings!
17 my $sth = $dbh->preparescip-perl cpan Test-MockDBI 0.70 DBI/prepare().('SELECT something FROM sometable');
18 $sth->executescip-perl cpan Test-MockDBI 0.70 main/execute().(); #Make sure its executed
19
20
21 {
22 #Testing that we can set the returnvalue to plain undef
23 #Testing the databasehandler
24 foreach my $method ( @{ $methods{'DBI::db'} } ){
25 #Setting a fake retval for the prepare method
26 $instance->set_retvalscip-perl cpan Test-MockDBI 0.70 main/set_retval().( method => $method, retval => undef );
27 my $retval = $dbh->$methodscip-perl cpan Test-MockDBI 0.70 DBI/$method().();
28 okscip-perl cpan Test-MockDBI 0.70 main/ok().(!$retval, $method . ' returned undef');
29
30 }
31
32 #Testing the statementhandler
33 foreach my $method ( @{ $methods{'DBI::st'} } ){
34 #Setting a fake retval for the prepare method
35 $instance->set_retvalscip-perl cpan Test-MockDBI 0.70 main/set_retval().( method => $method, retval => undef);
36 my $retval = $sth->$methodscip-perl cpan Test-MockDBI 0.70 main/$method().();
37 okscip-perl cpan Test-MockDBI 0.70 main/ok().(!$retval, $method . ' returned undef');
38
39 }
40 #Resetting the mock instance
41 $instance->resetscip-perl cpan Test-MockDBI 0.70 main/reset().();
42 }
43 {
44 #Testing that we can set the returnvalue and custom err and errstr
45
46
47 #Testing the databasehandler
48 foreach my $method ( @{ $methods{'DBI::db'} } ){
49 my %args = ( method => $method, retval => undef, err => 99, errstr => 'Custom DBI error' );
50 #Setting a fake retval for the prepare method
51 $instance->set_retvalscip-perl cpan Test-MockDBI 0.70 main/set_retval().( %args );
52 my $retval = $dbh->$methodscip-perl cpan Test-MockDBI 0.70 DBI/$method().();
53 okscip-perl cpan Test-MockDBI 0.70 main/ok().(!$retval, $method . ' returned undef');
54 cmp_okscip-perl cpan Test-MockDBI 0.70 main/cmp_ok().($dbh->errscip-perl cpan Test-MockDBI 0.70 DBI/err()., '==', $args{err}, '$sth->err is ' . $args{err});
55 cmp_okscip-perl cpan Test-MockDBI 0.70 main/cmp_ok().($dbh->errstrscip-perl cpan Test-MockDBI 0.70 DBI/errstr()., 'eq', $args{errstr}, '$sth->errstr is ' . $args{errstr});
56 }
57
58 #Testing the statementhandler
59 foreach my $method ( @{ $methods{'DBI::st'} } ){
60 my %args = ( method => $method, retval => undef, err => 99, errstr => 'Custom DBI error' );
61 #Setting a fake retval for the prepare method
62 $instance->set_retvalscip-perl cpan Test-MockDBI 0.70 main/set_retval().( %args );
63 my $retval = $sth->$methodscip-perl cpan Test-MockDBI 0.70 main/$method().();
64 okscip-perl cpan Test-MockDBI 0.70 main/ok().(!$retval, $method . ' returned undef');
65 cmp_okscip-perl cpan Test-MockDBI 0.70 main/cmp_ok().($sth->errscip-perl cpan Test-MockDBI 0.70 main/err()., '==', $args{err}, '$sth->err is ' . $args{err});
66 cmp_okscip-perl cpan Test-MockDBI 0.70 main/cmp_ok().($sth->errstrscip-perl cpan Test-MockDBI 0.70 main/errstr()., 'eq', $args{errstr}, '$sth->errstr is ' . $args{errstr});
67 }
68 $instance->resetscip-perl cpan Test-MockDBI 0.70 main/reset().();
69 }
70 {
71 #Setting a fake retval should fail if no method is provided
72 my %args = ( retval => undef, err => 99, errstr => 'Custom DBI error' );
73 warning_likescip-perl cpan Test-MockDBI 0.70 main/warning_like().{
74 okscip-perl cpan Test-MockDBI 0.70 main/ok().(!$instance->set_retvalscip-perl cpan Test-MockDBI 0.70 main/set_retval().( %args ), "set_retval fails without a method");
75 } qr/No method provided/, "set_retval displays warning on no method";
76 }
77 {
78 #Method must be a scalar string
79 my %args = ( method => sub{ return 'somemethod';}, retval => undef, err => 99, errstr => 'Custom DBI error' );
80 warning_likescip-perl cpan Test-MockDBI 0.70 main/warning_like().{
81 okscip-perl cpan Test-MockDBI 0.70 main/ok().(!$instance->set_retvalscip-perl cpan Test-MockDBI 0.70 main/set_retval().( %args ), "set_retval fails with an invalid method");
82 } qr/Parameter method must be a scalar string/, "set_retval displays warning on invalid method";
83 }
84
85 {
86 #If provided sql must be a scalar string
87 my %args = ( method => 'prepare', sql => ['sql'], retval => undef, err => 99, errstr => 'Custom DBI error' );
88 warning_likescip-perl cpan Test-MockDBI 0.70 main/warning_like().{
89 okscip-perl cpan Test-MockDBI 0.70 main/ok().(!$instance->set_retvalscip-perl cpan Test-MockDBI 0.70 main/set_retval().( %args ), "set_retval fails with an invalid sql");
90 } qr/Parameter SQL must be a scalar string/, "set_retval displays warning on invalid sql";
91 }
92
93 {
94 #A retval must be provided
95 my %args = ( method => 'prepare', err => 99, errstr => 'Custom DBI error' );
96 warning_likescip-perl cpan Test-MockDBI 0.70 main/warning_like().{
97 okscip-perl cpan Test-MockDBI 0.70 main/ok().(!$instance->set_retvalscip-perl cpan Test-MockDBI 0.70 main/set_retval().( %args ), "set_retval fails without a retval");
98 } qr/No retval provided/, "set_retval displays warning when called without a retval";
99 }
100 done_testingscip-perl cpan Test-MockDBI 0.70 main/done_testing().();