Skip to content

Commit

Permalink
uploadin
Browse files Browse the repository at this point in the history
  • Loading branch information
Stevan Little committed Jan 28, 2005
1 parent 05c3a31 commit 1dd83a0
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 41 deletions.
26 changes: 26 additions & 0 deletions Changes
@@ -1,5 +1,31 @@
Revision history for Perl extension DBD::Mock.

0.24 Fri Jan 28 2005
- added the reset() method to the DBD::Mock::Session
object so that a session can be used more than once.
- added tests for this
- added docs for this
- fixed a bug where the HASH version of 'mock_add_resultset'
would consume the result sets and they would not be
reusable. We now copy the result sets so that every time
the statement is called the same results are returned
- did not need to add docs for this, they already
documented this as the behavior (hence calling this
a bug)
- added tests for this

0.23 Tues Jan 25 2005
- removed the trace log tests from t/10_db_handle.t
since they seemed to be a source of issues on Win32.
My rationale is that it is a DBI thing, and therefore
does not need to be tested by DBD::Mock.

- added a few more tests to t/11_dr_handle.t to test the
'mock_connect_fail' feature

- added some clarification in the docs about the
'mock_connect_fail' feature as well.

0.22 Mon Jan 24 2005
- added the 'mock_connect_fail' boolean attribute
for the DBD::Mock driver handle, this will prevent
Expand Down
2 changes: 1 addition & 1 deletion META.yml
@@ -1,6 +1,6 @@
#XXXXXXX This is a prototype!!! It will change in the future!!! XXXXX#
name: DBD-Mock
version: 0.22
version: 0.24
version_from: VERSION
installdirs: site
requires:
Expand Down
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
$VERSION = '0.22';
$VERSION = '0.24';
19 changes: 15 additions & 4 deletions lib/DBD/Mock.pm
Expand Up @@ -19,7 +19,7 @@ use warnings;

require DBI;

our $VERSION = '1.25';
our $VERSION = '1.27';

our $drh = undef; # will hold driver handle
our $err = 0; # will hold any error codes
Expand Down Expand Up @@ -211,7 +211,9 @@ sub prepare {
my $rs;
if ( my $all_rs = $dbh->{mock_rs} ) {
if ( my $by_name = $all_rs->{named}{$statement} ) {
$rs = $by_name;
# we want to copy this, becauase
# it is meant to be reusable
$rs = [ @{$by_name} ];
}
else {
$rs = shift @{$all_rs->{ordered}};
Expand Down Expand Up @@ -777,7 +779,8 @@ sub new {
} => $class;
}

sub name { (shift)->{name} }
sub name { (shift)->{name} }
sub reset { (shift)->{state_index} = 0 }

sub verify_statement {
my ($self, $dbh, $statement) = @_;
Expand Down Expand Up @@ -808,7 +811,9 @@ sub verify_statement {
}
# if we are hear then things worked out well :)
# print STDERR "Adding Results: " . (join " | " => map { join ", " => @{$_} } @{$current_state->{results}}) . "\n";
$dbh->STORE('mock_add_resultset' => $current_state->{results});
# copy the result sets so that
# we can re-use the session
$dbh->STORE('mock_add_resultset' => [ @{$current_state->{results}} ]);
}

sub verify_bound_params {
Expand Down Expand Up @@ -999,6 +1004,8 @@ This is a boolean property which when set to true (C<1>) will not allow DBI to c
# this will work now ...
my $dbh = DBI->connect(...);
This feature is conceptually different from the 'mock_can_connect' attribute of the C<$dbh> in that it has a driver-wide scope, where 'mock_can_connect' is handle-wide scope. It also only prevents the initial connection, any C<$dbh> handles created prior to setting 'mock_connect_fail' to true (C<1>) will still go on working just fine.
=back
Expand Down Expand Up @@ -1452,6 +1459,10 @@ B<verify_bound_params ($dbh, $params)>
If the 'bound_params' slot is available in the current state, this will check the C<$params> against the current state's 'bound_params' value. Both number of parameters and the parameters themselves must match, or an error will be raised.
B<reset>
Calling this method will reset the state of the session object so that it can be reused.
=back
=head1 EXPERIMENTAL FUNCTIONALITY
Expand Down
37 changes: 5 additions & 32 deletions t/10_db_handle.t
Expand Up @@ -3,7 +3,7 @@
use strict;
use warnings;

use Test::More tests => 22;
use Test::More tests => 14;

BEGIN {
use_ok('DBD::Mock');
Expand Down Expand Up @@ -41,62 +41,35 @@ BEGIN {
# test setting attributes post-connect

{
my $trace_log = 'tmp_dbi_trace.log';

my $dbh = DBI->connect( 'DBI:Mock:', '', '' );
$dbh->{RaiseError} = 1;
$dbh->{PrintError} = 1;
$dbh->{AutoCommit} = 1;
$dbh->trace( 2, $trace_log );
ok(-f $trace_log, '... the trace log file has been created');

cmp_ok( $dbh->{RaiseError}, '==', 1,
'RaiseError DB attribute set after connect()' );
cmp_ok( $dbh->{PrintError}, '==', 1,
'PrintError DB attribute set after connect()' );
cmp_ok( $dbh->{AutoCommit}, '==', 1,
'AutoCommit DB attribute set after connect()' );
cmp_ok( $dbh->{TraceLevel}, '==', 2,
'TraceLevel DB attribute set after connect()' );

cmp_ok(unlink($trace_log), '==', 1, "... removing the trace log file" );
ok(!-e $trace_log, "... the trace log file is actually gone" );


$dbh->disconnect();
}

# test setting them during connect

SKIP: {
eval {
require File::Temp;
File::Temp->import( 'tempfile' );
};
skip "Cannot load File::Temp", 7 if $@;

(undef, my $trace_log) = do {
local $^W; # Disable warning about unsafe tempfile() call
tempfile( 'dbd_mock_test_XXXX', OPEN => 0 );
};

open STDERR, "> $trace_log";
ok(-f $trace_log, '... the trace log file has been created');
{
my $dbh = DBI->connect( 'DBI:Mock:', '', '',
{ RaiseError => 1,
PrintError => 1,
AutoCommit => 1,
TraceLevel => 2 } );
AutoCommit => 1 } );
cmp_ok( $dbh->{RaiseError}, '==', 1,
'RaiseError DB attribute set in connect()' );
cmp_ok( $dbh->{PrintError}, '==', 1,
'PrintError DB attribute set in connect()' );
cmp_ok( $dbh->{AutoCommit}, '==', 1,
'AutoCommit DB attribute set in connect()' );
cmp_ok( $dbh->{TraceLevel}, '==', 2,
'TraceLevel DB attribute set in connect()' );

close STDERR;
cmp_ok(unlink($trace_log), '==', 1, "... removing the trace log file" );
ok(!-e $trace_log, "... the trace log file is actually gone" );

$dbh->disconnect();
}
11 changes: 10 additions & 1 deletion t/11_dr_handle.t
Expand Up @@ -3,7 +3,7 @@
use strict;
use warnings;

use Test::More tests => 20;
use Test::More tests => 22;

BEGIN {
use_ok('DBD::Mock');
Expand Down Expand Up @@ -41,6 +41,11 @@ is($drh->data_sources(), 'DBI:Mock:', '... got the expected data sources');
{ # check the mock_connect_fail attribute
cmp_ok($drh->{mock_connect_fail}, '==', 0, '... the default is set not to fail');

# make sure the this only affects the initial connect
my $_dbh = DBI->connect('dbi:Mock:', '', '', { RaiseError => 1, PrintError => 0 });
isa_ok($_dbh, 'DBI::db');

# now no more connections
$drh->{mock_connect_fail} = 1;
cmp_ok($drh->{mock_connect_fail}, '==', 1, '... we are set to fail');

Expand All @@ -51,6 +56,10 @@ is($drh->data_sources(), 'DBI:Mock:', '... got the expected data sources');
like($@,
qr/^DBI connect\(\'\'\,\'\'\,\.\.\.\) failed\: Could not connect to mock database/, #'
'... got the error we expected too');

# make sure the handle we created before the change works
eval { $_dbh->prepare( "SELECT foo FROM bar" ) };
ok(!$@, '... we should not have an exception here');

$drh->{mock_connect_fail} = 0;
cmp_ok($drh->{'mock_connect_fail'}, '==', 0, '... we are set not to fail');
Expand Down
28 changes: 27 additions & 1 deletion t/36_mock_add_resultset_test.t
Expand Up @@ -3,7 +3,7 @@
use strict;
use warnings;

use Test::More tests => 7;
use Test::More tests => 11;

BEGIN {
use_ok('DBD::Mock');
Expand Down Expand Up @@ -39,6 +39,32 @@ $dbh->{mock_add_resultset} = {
results => [[ 'foo' ], [ 50 ]]
};

{
my $sth = $dbh->prepare('SELECT foo FROM bar');
isa_ok($sth, 'DBI::st');

$sth->execute();
my ($result) = $sth->fetchrow_array();

cmp_ok($result, '==', 50, '... got the result we expected');

$sth->finish();
}

# get it again
{
my $sth = $dbh->prepare('SELECT foo FROM bar');
isa_ok($sth, 'DBI::st');

$sth->execute();
my ($result) = $sth->fetchrow_array();

cmp_ok($result, '==', 50, '... got the result we expected');

$sth->finish();
}

# and one more time for good measure
{
my $sth = $dbh->prepare('SELECT foo FROM bar');
isa_ok($sth, 'DBI::st');
Expand Down
17 changes: 16 additions & 1 deletion t/60_DBD_Mock_Session_test.t
Expand Up @@ -3,7 +3,7 @@
use strict;
use warnings;

use Test::More tests => 53;
use Test::More tests => 56;

BEGIN {
use_ok('DBD::Mock');
Expand Down Expand Up @@ -92,6 +92,21 @@ use DBI;
$dbh->{mock_session} = $successful_login;

is(Login::Test::login($dbh, 'user', '****'), 'LOGIN SUCCESSFUL', '... logged in successfully');

# check the reusablity

# it is not reusable now
eval {
Login::Test::login($dbh, 'user', '****')
};
ok($@, '... got the exception');
like($@, qr/^Session Error\: Session states exhausted/, '... got the exception we expected');

# reset the DBD::Mock::Session object
$successful_login->reset;

# and it is re-usable now
is(Login::Test::login($dbh, 'user', '****'), 'LOGIN SUCCESSFUL', '... logged in successfully');
}

{
Expand Down

0 comments on commit 1dd83a0

Please sign in to comment.