DBD::Mock ã使ã£ããã¹ã
DBD::Mock 㯠DBI ã®ãã©ã¤ãã®ä¸ã¤ã§ãDBI ã使ã£ãããã°ã©ã ã§æå³çãªç¶æ
ãä½ãäºãåºæ¥ã¾ãã
ã¨è¨ã訳ã§ã¡ã¢ç¨åº¦ã«æ¸ãã¦è¡ãã¾ãã
ãã¼ã¿ãã¼ã¹ãã³ãã«ã®åå¾
use strict; use warnings; use Test::More; use DBI; plan tests => 3; my $dbh = DBI->connect('dbi:Mock:', '', '', +{ AutoCommit => 0, RaiseError => 1 }); ok($dbh, 'Create database handle'); isa_ok($dbh, 'DBI:db'); is($dbh->{Driver}->{Name}, 'Mock', 'Driver information');
ã§ãæ®éã« Database Handle ãåãã¾ãã
SELECT ãã¦ãç®æ
äºåã« mock_add_resultset ãå®ç¾©ãã¦ããã¨ä»»æã® resultset ãè¿ãäºãåºæ¥ã¾ãã
#!/usr/bin/perl use strict; use warnings; use Test::More; use DBI; plan tests => 2; my @records = ( [1, 'zigorou'], [2, 'kazuho'], [3, 'yappo'], [4, 'tokuhirom'], [5, 'hidek'], [6, 'typester'], ); my %user_data = ( sql => q|SELECT user_id, nickname FROM user_data WHERE user_status = ?|, results => [ [qw/user_id nickname/], @records, ], ); my $dbh = DBI->connect('dbi:Mock:', '', '', +{ AutoCommit => 0, RaiseError => 1, }); $dbh->{mock_add_resultset} = \%user_data; my $sth = $dbh->prepare($user_data{sql}); isa_ok($sth, 'DBI::st'); $sth->execute(1); is_deeply($sth->fetchall_arrayref, \@records, 'resultset');
INSERT, UPDATE, DELETE ã¨ã
$sth->rows ã§è¿ã£ã¦æ¥ã件æ°ãæå®ããã®ã mock_add_resultset ã§å®ç¾©åºæ¥ã¾ãã
#!/usr/bin/perl use strict; use warnings; use Test::More; use DBI; plan tests => 2; my %user_data = ( sql => q|INSERT INTO user_data(nickname) VALUES(?)|, results => [ [qw/rows/], [], ], ); my $dbh = DBI->connect('dbi:Mock:', '', '', +{ AutoCommit => 0, RaiseError => 1, }); $dbh->{mock_add_resultset} = \%user_data; my $sth = $dbh->prepare($user_data{sql}); isa_ok($sth, 'DBI::st'); $sth->execute('zigorou'); is_deeply($sth->rows, 1, 'affected rows'); $dbh->commit;
追è¨1 (2009-03-25T17:23:01+09:00)
DBI::connect(), DBI::st->prepare(), DBI::st->execute() ã§ããã¨å¤±æããä¾
use Test::More; use Test::Exception; use Carp; use DBI; plan tests => 9; my $drh = DBI->install_driver('Mock'); isa_ok($drh, 'DBI::dr'); dies_ok( sub { local $drh->{mock_connect_fail} = 1; my $dbh = DBI->connect('dbi:Mock:', '', '', +{ AutoCommit => 0, RaiseError => 1, }) || croak(q|Cannot connect mock database|); }, 'mock_connect_fail on' ); lives_ok( sub { local $drh->{mock_connect_fail} = 0; my $dbh = DBI->connect('dbi:Mock:', '', '', +{ AutoCommit => 0, RaiseError => 1, }) || croak(q|Cannot connect mock database|); }, 'mock_connect_fail off' ); my $dbh = DBI->connect('dbi:Mock:', '', '', +{ AutoCommit => 0, RaiseError => 1, }) || croak(q|Cannot connect mock database|); isa_ok($dbh, 'DBI::db'); lives_ok( sub { local $dbh->{mock_can_prepare} = 1; my $sth = $dbh->prepare(q|SELECT * FROM foo;|); }, 'mock_can_prepare on' ); dies_ok( sub { local $dbh->{mock_can_prepare} = 0; my $sth = $dbh->prepare(q|SELECT * FROM foo;|); }, 'mock_can_prepare off' ); my $sth = $dbh->prepare(q|SELECT * FROM foo;|); isa_ok($sth, 'DBI::st'); lives_ok( sub { local $dbh->{mock_can_execute} = 1; $sth->execute(); }, 'mock_can_execute on' ); dies_ok( sub { local $dbh->{mock_can_execute} = 0; $sth->execute(); }, 'mock_can_execute off' );
ã¾ã¨ã
ã¨ããããä¸æãäºããã¼ã¿ãã¼ã¹å¦çãå·®ãæ¿ãã¦ãã㦠DBD::Mock ã®ãã¼ã¿ãã¼ã¹ãã³ãã«ãä½ã£ã¦ãã£ã¦ãå¤å´ãã resultset ãå®ç¾©ãã¦ããããããã¨ãããæãã§ãã¹ããå®è¡åºæ¥ã¾ãã
ã¾ãä»ã«ãæ§ã ãªæ©è½ãæã£ã¦ãã¦ãæå³çã« commit 失æã¨ãããããç¶æ³ãä½ããã¿ãããªã®ã§ãç¶²ç¾ çã«ãã¹ãããäºãå¯è½ã ã¨æãã¾ãã